import { Fragment, ReactNode, useRef, useState } from "react"; import { observer } from "mobx-react"; import { usePopper } from "react-popper"; import { Check, ChevronDown, Search, Triangle } from "lucide-react"; import { Combobox } from "@headlessui/react"; // helpers import { cn } from "@/helpers/common.helper"; // hooks import { useAppRouter, useEstimate, useProjectEstimates, // useEstimate } from "@/hooks/store"; import { useDropdown } from "@/hooks/use-dropdown"; // components import { DropdownButton } from "./buttons"; import { BUTTON_VARIANTS_WITH_TEXT } from "./constants"; // types import { TDropdownProps } from "./types"; type Props = TDropdownProps & { button?: ReactNode; dropdownArrow?: boolean; dropdownArrowClassName?: string; onChange: (val: string | undefined) => void; onClose?: () => void; projectId: string; value: string | undefined; }; type DropdownOptions = | { value: string | null; query: string; content: JSX.Element; }[] | undefined; export const EstimateDropdown: React.FC = observer((props) => { const { button, buttonClassName, buttonContainerClassName, buttonVariant, className = "", disabled = false, dropdownArrow = false, dropdownArrowClassName = "", hideIcon = false, onChange, onClose, placeholder = "", placement, projectId, showTooltip = false, tabIndex, value, } = props; // states const [query, setQuery] = useState(""); const [isOpen, setIsOpen] = 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 { workspaceSlug } = useAppRouter(); const { currentActiveEstimateId, getProjectEstimates } = useProjectEstimates(); const { estimatePointIds, estimatePointById } = useEstimate( currentActiveEstimateId ? currentActiveEstimateId : undefined ); const options: DropdownOptions = (estimatePointIds ?? []) ?.map((estimatePoint) => { const currentEstimatePoint = estimatePointById(estimatePoint); if (currentEstimatePoint) return { value: currentEstimatePoint.id, query: `${currentEstimatePoint?.value}`, content: (
{currentEstimatePoint.value}
), }; else undefined; }) .filter((estimatePointDropdownOption) => estimatePointDropdownOption != undefined) as DropdownOptions; options?.unshift({ value: null, query: "No estimate", content: (
No estimate
), }); const filteredOptions = query === "" ? options : options?.filter((o) => o.query.toLowerCase().includes(query.toLowerCase())); const selectedEstimate = value && estimatePointById ? estimatePointById(value) : undefined; const onOpen = async () => { if (!currentActiveEstimateId && workspaceSlug) await getProjectEstimates(workspaceSlug, projectId); }; const { handleClose, handleKeyDown, handleOnClick, searchInputKeyDown } = useDropdown({ dropdownRef, inputRef, isOpen, onClose, onOpen, query, setIsOpen, setQuery, }); const dropdownOnChange = (val: string | undefined) => { onChange(val); handleClose(); }; return ( {button ? ( ) : ( )} {isOpen && (
setQuery(e.target.value)} placeholder="Search" displayValue={(assigned: any) => assigned?.name} onKeyDown={searchInputKeyDown} />
{filteredOptions ? ( filteredOptions.length > 0 ? ( filteredOptions.map((option) => ( {({ active, selected }) => (
{option.content} {selected && }
)}
)) ) : (

No matching results

) ) : (

Loading...

)}
)}
); });