import { ReactNode, useRef, useState } from "react"; import { observer } from "mobx-react"; import { usePopper } from "react-popper"; import { Briefcase, Check, ChevronDown, Search } from "lucide-react"; import { Combobox } from "@headlessui/react"; // ui import { useTranslation } from "@plane/i18n"; import { ComboDropDown } from "@plane/ui"; // components import { Logo } from "@/components/common"; // helpers import { cn } from "@/helpers/common.helper"; // hooks import { useProject } from "@/hooks/store"; import { useDropdown } from "@/hooks/use-dropdown"; // plane web types import { TProject } from "@/plane-web/types"; // 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; onClose?: () => void; renderCondition?: (project: TProject) => boolean; renderByDefault?: boolean; } & ( | { multiple: false; onChange: (val: string) => void; value: string | null; } | { multiple: true; onChange: (val: string[]) => void; value: string[]; } ); export const ProjectDropdown: React.FC = observer((props) => { const { button, buttonClassName, buttonContainerClassName, buttonVariant, className = "", disabled = false, dropdownArrow = false, dropdownArrowClassName = "", hideIcon = false, multiple, onChange, onClose, placeholder = "Project", placement, renderCondition, showTooltip = false, tabIndex, value, renderByDefault = true, } = 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 { joinedProjectIds, getProjectById } = useProject(); const { t } = useTranslation(); const options = joinedProjectIds?.map((projectId) => { const projectDetails = getProjectById(projectId); if (renderCondition && projectDetails && !renderCondition(projectDetails)) return; return { value: projectId, query: `${projectDetails?.name}`, content: (
{projectDetails && ( )} {projectDetails?.name}
), }; }); const filteredOptions = query === "" ? options : options?.filter((o) => o?.query.toLowerCase().includes(query.toLowerCase())); const { handleClose, handleKeyDown, handleOnClick, searchInputKeyDown } = useDropdown({ dropdownRef, inputRef, isOpen, onClose, query, setIsOpen, setQuery, }); const dropdownOnChange = (val: string & string[]) => { onChange(val); if (!multiple) handleClose(); }; const getDisplayName = (value: string | string[] | null, placeholder: string = "") => { if (Array.isArray(value)) { const firstProject = getProjectById(value[0]); return value.length ? (value.length === 1 ? firstProject?.name : `${value.length} projects`) : placeholder; } else { return value ? (getProjectById(value)?.name ?? placeholder) : placeholder; } }; const getProjectIcon = (value: string | string[] | null) => { const renderIcon = (projectDetails: TProject) => ( ); if (Array.isArray(value)) { return (
{value.length > 0 ? ( value.map((projectId) => { const projectDetails = getProjectById(projectId); return projectDetails ? renderIcon(projectDetails) : null; }) ) : ( )}
); } else { const projectDetails = getProjectById(value); return projectDetails ? renderIcon(projectDetails) : null; } }; const comboButton = ( <> {button ? ( ) : ( )} ); return ( {isOpen && (
setQuery(e.target.value)} placeholder={t("search")} displayValue={(assigned: any) => assigned?.name} onKeyDown={searchInputKeyDown} />
{filteredOptions ? ( filteredOptions.length > 0 ? ( filteredOptions.map((option) => { if (!option) return; return ( `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"}` } > {({ selected }) => ( <> {option.content} {selected && } )} ); }) ) : (

{t("no_matching_results")}

) ) : (

{t("loading")}

)}
)}
); });