import { FC, useEffect, useRef, useState, Fragment } from "react"; import { observer } from "mobx-react-lite"; import { Combobox } from "@headlessui/react"; import { usePopper } from "react-popper"; import { Check, Search } from "lucide-react"; import { twMerge } from "tailwind-merge"; // hooks import { useModule } from "hooks/store"; import { useDropdownKeyDown } from "hooks/use-dropdown-key-down"; import useOutsideClickDetector from "hooks/use-outside-click-detector"; // components import { ModuleSelectButton } from "./"; // types import { TModuleSelectDropdown, TModuleSelectDropdownOption } from "./types"; import { DiceIcon } from "@plane/ui"; export const ModuleSelectDropdown: FC = observer((props) => { // props const { workspaceSlug, projectId, value = undefined, onChange, placeholder = "Module", multiple = false, disabled = false, className = "", buttonContainerClassName = "", buttonClassName = "", buttonVariant = "transparent-with-text", hideIcon = false, dropdownArrow = false, dropdownArrowClassName = "", showTooltip = false, showCount = false, placement, tabIndex, button, } = props; // states const [query, setQuery] = useState(""); const [isOpen, setIsOpen] = useState(false); // refs const dropdownRef = useRef(null); // popper-js refs const [referenceElement, setReferenceElement] = useState(null); const [popperElement, setPopperElement] = useState(null); // popper-js init const { styles, attributes } = usePopper(referenceElement, popperElement, { placement: placement ?? "bottom-start", modifiers: [ { name: "preventOverflow", options: { padding: 12, }, }, ], }); // store hooks const { getProjectModuleIds, fetchModules, getModuleById } = useModule(); const moduleIds = getProjectModuleIds(projectId); const options: TModuleSelectDropdownOption[] | undefined = moduleIds?.map((moduleId) => { const moduleDetails = getModuleById(moduleId); return { value: moduleId, query: `${moduleDetails?.name}`, content: (
{moduleDetails?.name}
), }; }); !multiple && options?.unshift({ value: undefined, query: "No module", content: (
No module
), }); const filteredOptions = query === "" ? options : options?.filter((o) => o.query.toLowerCase().includes(query.toLowerCase())); // fetch modules of the project if not already present in the store useEffect(() => { if (!workspaceSlug) return; if (!moduleIds) fetchModules(workspaceSlug, projectId); }, [moduleIds, fetchModules, projectId, workspaceSlug]); const openDropdown = () => { if (isOpen) closeDropdown(); else { setIsOpen(true); if (referenceElement) referenceElement.focus(); } }; const closeDropdown = () => setIsOpen(false); const handleKeyDown = useDropdownKeyDown(openDropdown, closeDropdown, isOpen); useOutsideClickDetector(dropdownRef, closeDropdown); const comboboxProps: any = {}; if (multiple) comboboxProps.multiple = true; return ( {button ? ( ) : ( )} {isOpen && (
setQuery(e.target.value)} placeholder="Search" displayValue={(moduleIds: any) => { const displayValueOptions: TModuleSelectDropdownOption[] | undefined = options?.filter((_module) => moduleIds.includes(_module.value) ); return displayValueOptions?.map((_option) => _option.query).join(", ") || "Select Module"; }} />
{filteredOptions ? ( filteredOptions.length > 0 ? ( filteredOptions.map((option) => ( `w-full truncate flex items-center justify-between gap-2 rounded px-1 py-1.5 cursor-pointer select-none ${ active ? "bg-custom-background-80" : "" } ${selected ? "text-custom-text-100" : "text-custom-text-200"}` } onClick={() => !multiple && closeDropdown()} > {({ selected }) => ( <> {option.content} {selected && } )} )) ) : (

No matching results

) ) : (

Loading...

)}
)}
); });