import { useEffect, useRef, useState } from "react"; import { Placement } from "@popperjs/core"; import { observer } from "mobx-react"; import { usePopper } from "react-popper"; import { Check, Search } from "lucide-react"; import { Combobox } from "@headlessui/react"; //components import { DiceIcon } from "@plane/ui"; //store import { cn } from "@/helpers/common.helper"; import { useAppRouter, useModule } from "@/hooks/store"; //hooks //icon //types 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; // states const [query, setQuery] = useState(""); const [popperElement, setPopperElement] = useState(null); // refs const inputRef = useRef(null); // store hooks const { workspaceSlug } = useAppRouter(); const { getProjectModuleIds, fetchModules, getModuleById } = useModule(); useEffect(() => { if (isOpen) { onOpen(); inputRef.current && inputRef.current.focus(); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [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( "flex w-full cursor-pointer select-none items-center justify-between gap-2 truncate rounded px-1 py-1.5", { "bg-custom-background-80": active, "text-custom-text-100": selected, "text-custom-text-200": !selected, } ) } > {({ selected }) => ( <> {option.content} {selected && } )} )) ) : (

No matching results

) ) : (

Loading...

)}
); });