import { Fragment, ReactNode, useRef, useState } from "react"; import { observer } from "mobx-react-lite"; import { Combobox } from "@headlessui/react"; import { usePopper } from "react-popper"; import { Check, ChevronDown, Search, Triangle } from "lucide-react"; import sortBy from "lodash/sortBy"; // hooks import { useApplication, useEstimate } from "hooks/store"; import { useDropdownKeyDown } from "hooks/use-dropdown-key-down"; import useOutsideClickDetector from "hooks/use-outside-click-detector"; // helpers import { cn } from "helpers/common.helper"; // types import { TDropdownProps } from "./types"; type Props = TDropdownProps & { button?: ReactNode; dropdownArrow?: boolean; dropdownArrowClassName?: string; onChange: (val: number | null) => void; projectId: string; value: number | null; }; type ButtonProps = { className?: string; estimatePoint: string | null; dropdownArrow: boolean; dropdownArrowClassName: string; hideIcon?: boolean; hideText?: boolean; placeholder: string; }; type DropdownOptions = | { value: number | null; query: string; content: JSX.Element; }[] | undefined; const BorderButton = (props: ButtonProps) => { const { className, estimatePoint, dropdownArrow, dropdownArrowClassName, hideIcon = false, hideText = false, placeholder, } = props; return (
{!hideIcon && } {!hideText && {estimatePoint !== null ? estimatePoint : placeholder}} {dropdownArrow && (
); }; const BackgroundButton = (props: ButtonProps) => { const { className, estimatePoint, dropdownArrow, dropdownArrowClassName, hideIcon = false, hideText = false, placeholder, } = props; return (
{!hideIcon && } {!hideText && {estimatePoint !== null ? estimatePoint : placeholder}} {dropdownArrow && (
); }; const TransparentButton = (props: ButtonProps) => { const { className, estimatePoint, dropdownArrow, dropdownArrowClassName, hideIcon = false, hideText = false, placeholder, } = props; return (
{!hideIcon && } {!hideText && {estimatePoint !== null ? estimatePoint : placeholder}} {dropdownArrow && (
); }; export const EstimateDropdown: React.FC = observer((props) => { const { button, buttonClassName, buttonContainerClassName, buttonVariant, className = "", disabled = false, dropdownArrow = false, dropdownArrowClassName = "", hideIcon = false, onChange, placeholder = "Estimate", placement, projectId, value, tabIndex, } = 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 { fetchProjectEstimates, getProjectActiveEstimateDetails, getEstimatePointValue } = useEstimate(); const activeEstimate = getProjectActiveEstimateDetails(projectId); const options: DropdownOptions = sortBy(activeEstimate?.points ?? [], "key")?.map((point) => ({ value: point.key, query: `${point?.value}`, content: (
{point.value}
), })); 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 !== null ? getEstimatePointValue(value, projectId) : null; const openDropdown = () => { setIsOpen(true); if (!activeEstimate && workspaceSlug) fetchProjectEstimates(workspaceSlug, projectId); if (referenceElement) referenceElement.focus(); }; const closeDropdown = () => setIsOpen(false); const handleKeyDown = useDropdownKeyDown(openDropdown, closeDropdown, isOpen); useOutsideClickDetector(dropdownRef, closeDropdown); 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"}` } onClick={closeDropdown} > {({ selected }) => ( <> {option.content} {selected && } )} )) ) : (

No matching results

) ) : (

Loading...

)}
)}
); });