import { Fragment, useState } from "react"; import { observer } from "mobx-react-lite"; import { useMobxStore } from "lib/mobx/store-provider"; // hooks import { usePopper } from "react-popper"; // ui import { Combobox } from "@headlessui/react"; import { StateGroupIcon, Tooltip } from "@plane/ui"; import { Check, ChevronDown, Search } from "lucide-react"; // types import { IState } from "types"; import { Placement } from "@popperjs/core"; import { RootStore } from "store/root"; export interface IIssuePropertyState { projectId: string | null; value: any | string | null; defaultOptions?: any; onChange: (state: IState) => void; disabled?: boolean; hideDropdownArrow?: boolean; className?: string; buttonClassName?: string; optionsClassName?: string; placement?: Placement; } export const IssuePropertyState: React.FC = observer((props) => { const { projectId, value, defaultOptions = [], onChange, disabled, hideDropdownArrow = false, className = "", buttonClassName = "", optionsClassName = "", placement, } = props; const { workspace: workspaceStore, projectState: projectStateStore }: RootStore = useMobxStore(); const workspaceSlug = workspaceStore?.workspaceSlug; const [query, setQuery] = useState(""); const [referenceElement, setReferenceElement] = useState(null); const [popperElement, setPopperElement] = useState(null); const [isLoading, setIsLoading] = useState(false); let projectStates: IState[] = defaultOptions; const storeStates = projectId ? projectStateStore.states[projectId] : []; if (storeStates && storeStates.length > 0) projectStates = storeStates; const fetchProjectStates = () => { setIsLoading(true); if (workspaceSlug && projectId) workspaceSlug && projectId && projectStateStore.fetchProjectStates(workspaceSlug, projectId).then(() => setIsLoading(false)); }; const selectedOption: IState | undefined = (projectStates && value && projectStates?.find((state) => state.id === value)) || undefined; const dropdownOptions = projectStates?.map((state) => ({ value: state.id, query: state.name, content: (
{state.name}
), })); const { styles, attributes } = usePopper(referenceElement, popperElement, { placement: placement ?? "bottom-start", modifiers: [ { name: "preventOverflow", options: { padding: 12, }, }, ], }); const filteredOptions = query === "" ? dropdownOptions : dropdownOptions?.filter((option) => option.query.toLowerCase().includes(query.toLowerCase())); const label = (
{selectedOption && } {selectedOption?.name ?? "State"}
); return ( <> {workspaceSlug && projectId && ( { const selectedState = projectStates?.find((state) => state.id === data); if (selectedState) onChange(selectedState); }} disabled={disabled} >
setQuery(e.target.value)} placeholder="Search" displayValue={(assigned: any) => assigned?.name} />
{isLoading ? (

Loading...

) : filteredOptions.length > 0 ? ( filteredOptions.map((option) => ( `flex 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"}` } onClick={(e) => e.stopPropagation()} > {({ selected }) => ( <> {option.content} {selected && (
)} )}
)) ) : (

No matching results

)}
)} ); });