import { Fragment, ReactNode, useEffect, useRef, useState } from "react"; import { observer } from "mobx-react-lite"; import { Combobox } from "@headlessui/react"; import { usePopper } from "react-popper"; import { Check, ChevronDown, Search } from "lucide-react"; // hooks import { useApplication, useCycle } from "hooks/store"; import { useDropdownKeyDown } from "hooks/use-dropdown-key-down"; import useOutsideClickDetector from "hooks/use-outside-click-detector"; // components import { DropdownButton } from "./buttons"; // icons import { ContrastIcon } from "@plane/ui"; // helpers import { cn } from "helpers/common.helper"; // types import { TDropdownProps } from "./types"; // constants import { BUTTON_VARIANTS_WITH_TEXT } from "./constants"; type Props = TDropdownProps & { button?: ReactNode; dropdownArrow?: boolean; dropdownArrowClassName?: string; onChange: (val: string | null) => void; onClose?: () => void; projectId: string; value: string | null; }; type DropdownOptions = | { value: string | null; query: string; content: JSX.Element; }[] | undefined; export const CycleDropdown: React.FC = observer((props) => { const { button, buttonClassName, buttonContainerClassName, buttonVariant, className = "", disabled = false, dropdownArrow = false, dropdownArrowClassName = "", hideIcon = false, onChange, onClose, placeholder = "Cycle", placement, projectId, showTooltip = false, tabIndex, value, } = 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 { router: { workspaceSlug }, } = useApplication(); const { getProjectCycleIds, fetchAllCycles, getCycleById } = useCycle(); const cycleIds = getProjectCycleIds(projectId); const options: DropdownOptions = cycleIds?.map((cycleId) => { const cycleDetails = getCycleById(cycleId); 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())); // fetch cycles of the project if not already present in the store useEffect(() => { if (!workspaceSlug) return; if (!cycleIds) fetchAllCycles(workspaceSlug, projectId); }, [cycleIds, fetchAllCycles, projectId, workspaceSlug]); const selectedCycle = value ? getCycleById(value) : null; const onOpen = () => { if (referenceElement) referenceElement.focus(); }; const handleClose = () => { if (!isOpen) return; setIsOpen(false); if (referenceElement) referenceElement.blur(); onClose && onClose(); }; const toggleDropdown = () => { if (!isOpen) onOpen(); setIsOpen((prevIsOpen) => !prevIsOpen); }; const dropdownOnChange = (val: string | null) => { onChange(val); handleClose(); }; const handleKeyDown = useDropdownKeyDown(toggleDropdown, handleClose); const handleOnClick = (e: React.MouseEvent) => { e.stopPropagation(); e.preventDefault(); toggleDropdown(); }; useOutsideClickDetector(dropdownRef, handleClose); return ( {button ? ( ) : ( )} {isOpen && (
setQuery(e.target.value)} placeholder="Search" displayValue={(assigned: any) => assigned?.name} />
{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...

)}
)}
); });