import { ReactNode, useRef, useState } from "react"; import { observer } from "mobx-react"; import { useParams } from "next/navigation"; import { usePopper } from "react-popper"; import { Check, ChevronDown, Search, Triangle } from "lucide-react"; import { Combobox } from "@headlessui/react"; // plane imports import { useTranslation } from "@plane/i18n"; import { EEstimateSystem } from "@plane/types"; import { ComboDropDown } from "@plane/ui"; import { convertMinutesToHoursMinutesString, cn } from "@plane/utils"; // hooks import { useProjectEstimates } from "@/hooks/store/estimates"; import { useEstimate } from "@/hooks/store/estimates/use-estimate"; 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 | undefined; value: string | undefined | null; renderByDefault?: boolean; }; type DropdownOptions = | { value: string | null; query: string; content: React.ReactNode; }[] | 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, renderByDefault = true, } = props; // i18n const { t } = useTranslation(); // 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, }, }, ], }); // router const { workspaceSlug } = useParams(); // store hooks const { currentActiveEstimateIdByProjectId, getProjectEstimates, getEstimateById } = useProjectEstimates(); const { estimatePointIds, estimatePointById } = useEstimate( projectId ? currentActiveEstimateIdByProjectId(projectId) : undefined ); const currentActiveEstimateId = projectId ? currentActiveEstimateIdByProjectId(projectId) : undefined; const currentActiveEstimate = currentActiveEstimateId ? getEstimateById(currentActiveEstimateId) : undefined; const options: DropdownOptions = (estimatePointIds ?? []) ?.map((estimatePoint) => { const currentEstimatePoint = estimatePointById(estimatePoint); if (currentEstimatePoint) return { value: currentEstimatePoint.id, query: `${currentEstimatePoint?.value}`, content: (
{currentActiveEstimate?.type === EEstimateSystem.TIME ? convertMinutesToHoursMinutesString(Number(currentEstimatePoint.value)) : currentEstimatePoint.value}
), }; else undefined; }) .filter((estimatePointDropdownOption) => estimatePointDropdownOption != undefined) as DropdownOptions; options?.unshift({ value: null, query: t("project_settings.estimates.no_estimate"), content: (
{t("project_settings.estimates.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 && projectId) await getProjectEstimates(workspaceSlug.toString(), projectId); }; const { handleClose, handleKeyDown, handleOnClick, searchInputKeyDown } = useDropdown({ dropdownRef, inputRef, isOpen, onClose, onOpen, query, setIsOpen, setQuery, }); const dropdownOnChange = (val: string | undefined) => { onChange(val); handleClose(); }; const comboButton = ( <> {button ? ( ) : ( )} ); return ( {isOpen && (
setQuery(e.target.value)} placeholder={t("common.search.placeholder")} displayValue={(assigned: any) => assigned?.name} onKeyDown={searchInputKeyDown} />
{currentActiveEstimateId === undefined ? (
{/* NOTE: This condition renders when estimates are not enabled for the project */}
{t("project_settings.estimates.no_estimate")}
) : ( <> {filteredOptions ? ( filteredOptions.length > 0 ? ( filteredOptions.map((option) => ( {({ active, selected }) => (
{option.content} {selected && }
)}
)) ) : (

{t("common.search.no_matching_results")}

) ) : (

{t("common.loading")}

)} )}
)}
); });