import { FC, useEffect, useRef, useState } from "react"; import { Placement } from "@popperjs/core"; import { observer } from "mobx-react"; import { usePopper } from "react-popper"; // components import { Combobox } from "@headlessui/react"; // icon import { Check, Search } from "lucide-react"; // ui import { ContrastIcon, CycleGroupIcon } from "@plane/ui"; // store hooks import { useApplication, useCycle } from "hooks/store"; // types import { TCycleGroups } from "@plane/types"; type DropdownOptions = | { value: string | null; query: string; content: JSX.Element; }[] | undefined; type CycleOptionsProps = { projectId: string; referenceElement: HTMLButtonElement | null; placement: Placement | undefined; isOpen: boolean; }; export const CycleOptions: FC = observer((props) => { const { projectId, isOpen, referenceElement, placement } = props; //state hooks const [query, setQuery] = useState(""); const [popperElement, setPopperElement] = useState(null); const inputRef = useRef(null); // store hooks const { router: { workspaceSlug }, } = useApplication(); const { getProjectCycleIds, fetchAllCycles, getCycleById } = useCycle(); 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 cycleIds = (getProjectCycleIds(projectId) ?? [])?.filter((cycleId) => { const cycleDetails = getCycleById(cycleId); return cycleDetails?.status ? (cycleDetails?.status.toLowerCase() != "completed" ? true : false) : true; }); const onOpen = () => { if (workspaceSlug && !cycleIds) fetchAllCycles(workspaceSlug, projectId); }; const searchInputKeyDown = (e: React.KeyboardEvent) => { if (query !== "" && e.key === "Escape") { e.stopPropagation(); setQuery(""); } }; const options: DropdownOptions = cycleIds?.map((cycleId) => { const cycleDetails = getCycleById(cycleId); const cycleStatus = cycleDetails?.status ? (cycleDetails.status.toLocaleLowerCase() as TCycleGroups) : "draft"; return { value: cycleId, query: `${cycleDetails?.name}`, content: (
{cycleDetails?.name}
), }; }); options?.unshift({ value: null, query: "No cycle", content: (
No cycle
), }); 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) => ( `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"}` } > {({ selected }) => ( <> {option.content} {selected && } )} )) ) : (

No matches found

) ) : (

Loading...

)}
); });