"use client"; import { ReactNode, useRef, useState } from "react"; import { observer } from "mobx-react"; import { useParams } from "next/navigation"; import { usePopper } from "react-popper"; import { ChevronDown, Search } from "lucide-react"; import { Combobox } from "@headlessui/react"; import { useTranslation } from "@plane/i18n"; // ui import { ComboDropDown, Spinner, StateGroupIcon } from "@plane/ui"; // helpers import { cn } from "@/helpers/common.helper"; // hooks import { useProjectState } from "@/hooks/store"; import { useDropdown } from "@/hooks/use-dropdown"; // Plane-web import { StateOption } from "@/plane-web/components/workflow"; // components import { DropdownButton } from "./buttons"; // constants import { BUTTON_VARIANTS_WITH_TEXT } from "./constants"; // types import { TDropdownProps } from "./types"; type Props = TDropdownProps & { button?: ReactNode; dropdownArrow?: boolean; dropdownArrowClassName?: string; onChange: (val: string) => void; onClose?: () => void; projectId: string | undefined; showDefaultState?: boolean; value: string | undefined | null; renderByDefault?: boolean; stateIds?: string[]; filterAvailableStateIds?: boolean; }; export const StateDropdown: React.FC = observer((props) => { const { button, buttonClassName, buttonContainerClassName, buttonVariant, className = "", disabled = false, dropdownArrow = false, dropdownArrowClassName = "", hideIcon = false, onChange, onClose, placement, projectId, showDefaultState = true, showTooltip = false, tabIndex, value, renderByDefault = true, stateIds, filterAvailableStateIds = true, } = props; // states const [query, setQuery] = useState(""); const [isOpen, setIsOpen] = useState(false); const [stateLoader, setStateLoader] = useState(false); // refs const dropdownRef = useRef(null); const inputRef = 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 { t } = useTranslation(); const { workspaceSlug } = useParams(); const { fetchProjectStates, getProjectStates, getStateById } = useProjectState(); const statesList = stateIds ? stateIds.map((stateId) => getStateById(stateId)).filter((state) => !!state) : getProjectStates(projectId); const defaultState = statesList?.find((state) => state?.default); const stateValue = !!value ? value : showDefaultState ? defaultState?.id : undefined; const options = statesList?.map((state) => ({ value: state?.id, query: `${state?.name}`, content: (
{state?.name}
), })); const filteredOptions = query === "" ? options : options?.filter((o) => o.query.toLowerCase().includes(query.toLowerCase())); const selectedState = stateValue ? getStateById(stateValue) : undefined; const onOpen = async () => { if (!statesList && workspaceSlug && projectId) { setStateLoader(true); await fetchProjectStates(workspaceSlug.toString(), projectId); setStateLoader(false); } }; const { handleClose, handleKeyDown, handleOnClick, searchInputKeyDown } = useDropdown({ dropdownRef, inputRef, isOpen, onClose, onOpen, query, setIsOpen, setQuery, }); const dropdownOnChange = (val: string) => { onChange(val); handleClose(); }; const comboButton = ( <> {button ? ( ) : ( )} ); return ( {isOpen && (
setQuery(e.target.value)} placeholder={t("common.search.label")} displayValue={(assigned: any) => assigned?.name} onKeyDown={searchInputKeyDown} />
{filteredOptions ? ( filteredOptions.length > 0 ? ( filteredOptions.map((option) => ( )) ) : (

{t("no_matching_results")}

) ) : (

{t("loading")}

)}
)}
); });