"use client"; import { FC, useEffect, useRef, useState } from "react"; import { Placement } from "@popperjs/core"; import { observer } from "mobx-react"; import { usePopper } from "react-popper"; // components import { Check, Search } from "lucide-react"; import { Combobox } from "@headlessui/react"; // icon import { TCycleGroups } from "@plane/types"; // ui import { ContrastIcon, CycleGroupIcon } from "@plane/ui"; // store hooks import { useAppRouter, useCycle } from "@/hooks/store"; // 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 { workspaceSlug } = useAppRouter(); 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) => ( `flex w-full cursor-pointer select-none items-center justify-between gap-2 truncate rounded px-1 py-1.5 ${ active ? "bg-custom-background-80" : "" } ${selected ? "text-custom-text-100" : "text-custom-text-200"}` } > {({ selected }) => ( <> {option.content} {selected && } )} )) ) : (

No matches found

) ) : (

Loading...

)}
); });