import { useEffect, useRef, useState } from "react"; import { Combobox } from "@headlessui/react"; import { observer } from "mobx-react"; //components import { DiceIcon } from "@plane/ui"; //store import { useApplication, useModule } from "hooks/store"; //hooks import { usePopper } from "react-popper"; import { cn } from "helpers/common.helper"; //icon import { Check, Search } from "lucide-react"; //types import { Placement } from "@popperjs/core"; type DropdownOptions = | { value: string | null; query: string; content: JSX.Element; }[] | undefined; interface Props { projectId: string; referenceElement: HTMLButtonElement | null; placement: Placement | undefined; isOpen: boolean; multiple: boolean; } export const ModuleOptions = observer((props: Props) => { const { projectId, isOpen, referenceElement, placement, multiple } = props; const [query, setQuery] = useState(""); const [popperElement, setPopperElement] = useState(null); const inputRef = useRef(null); // store hooks const { router: { workspaceSlug }, } = useApplication(); const { getProjectModuleIds, fetchModules, getModuleById } = useModule(); useEffect(() => { if (isOpen) { onOpen(); inputRef.current && inputRef.current.focus(); } }, [isOpen]); // popper-js init const { styles, attributes } = usePopper(referenceElement, popperElement, { placement: placement ?? "bottom-start", modifiers: [ { name: "preventOverflow", options: { padding: 12, }, }, ], }); const moduleIds = getProjectModuleIds(projectId); const onOpen = () => { if (workspaceSlug && !moduleIds) fetchModules(workspaceSlug, projectId); }; const searchInputKeyDown = (e: React.KeyboardEvent) => { if (query !== "" && e.key === "Escape") { e.stopPropagation(); setQuery(""); } }; const options: DropdownOptions = moduleIds?.map((moduleId) => { const moduleDetails = getModuleById(moduleId); return { value: moduleId, query: `${moduleDetails?.name}`, content: (
{moduleDetails?.name}
), }; }); if (!multiple) options?.unshift({ value: null, query: "No module", content: (
No module
), }); const filteredOptions = query === "" ? options : options?.filter((o) => o.query.toLowerCase().includes(query.toLowerCase())); return (
setQuery(e.target.value)} placeholder="Search" displayValue={(assigned: any) => assigned?.name} onKeyDown={searchInputKeyDown} />
{filteredOptions ? ( filteredOptions.length > 0 ? ( filteredOptions.map((option) => ( cn( "w-full truncate flex items-center justify-between gap-2 rounded px-1 py-1.5 cursor-pointer select-none", { "bg-custom-background-80": active, "text-custom-text-100": selected, "text-custom-text-200": !selected, } ) } > {({ selected }) => ( <> {option.content} {selected && } )} )) ) : (

No matching results

) ) : (

Loading...

)}
); });