import { Fragment, useEffect, useRef, useState } from "react"; import { Placement } from "@popperjs/core"; import { observer } from "mobx-react-lite"; import { usePopper } from "react-popper"; import { Check, ChevronDown, Search, Tags } from "lucide-react"; import { Combobox } from "@headlessui/react"; import { IIssueLabel } from "@plane/types"; // hooks import { Tooltip } from "@plane/ui"; import { useApplication, useLabel } from "@/hooks/store"; import { useDropdownKeyDown } from "@/hooks/use-dropdown-key-down"; import useOutsideClickDetector from "@/hooks/use-outside-click-detector"; import { usePlatformOS } from "@/hooks/use-platform-os"; // components // types export interface IIssuePropertyLabels { projectId: string | null; value: string[]; defaultOptions?: any; onChange: (data: string[]) => void; disabled?: boolean; hideDropdownArrow?: boolean; className?: string; buttonClassName?: string; optionsClassName?: string; placement?: Placement; maxRender?: number; noLabelBorder?: boolean; placeholderText?: string; onClose?: () => void; } export const IssuePropertyLabels: React.FC = observer((props) => { const { projectId, value, defaultOptions = [], onChange, onClose, disabled, hideDropdownArrow = false, className, buttonClassName = "", optionsClassName = "", placement, maxRender = 2, noLabelBorder = false, placeholderText, } = 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); const [isLoading, setIsLoading] = useState(false); // store hooks const { router: { workspaceSlug }, } = useApplication(); const { fetchProjectLabels, getProjectLabels } = useLabel(); const { isMobile } = usePlatformOS(); const storeLabels = getProjectLabels(projectId); const onOpen = () => { if (!storeLabels && workspaceSlug && projectId) fetchProjectLabels(workspaceSlug, projectId).then(() => setIsLoading(false)); }; const handleClose = () => { if (!isOpen) return; setIsOpen(false); onClose && onClose(); }; const toggleDropdown = () => { if (!isOpen) onOpen(); setIsOpen((prevIsOpen) => !prevIsOpen); if (isOpen) onClose && onClose(); }; const handleKeyDown = useDropdownKeyDown(toggleDropdown, handleClose); const handleOnClick = (e: React.MouseEvent) => { e.stopPropagation(); e.preventDefault(); toggleDropdown(); }; useOutsideClickDetector(dropdownRef, handleClose); const searchInputKeyDown = (e: React.KeyboardEvent) => { if (query !== "" && e.key === "Escape") { e.stopPropagation(); setQuery(""); } }; useEffect(() => { if (isOpen && inputRef.current) { inputRef.current.focus(); } }, [isOpen]); const { styles, attributes } = usePopper(referenceElement, popperElement, { placement: placement ?? "bottom-start", modifiers: [ { name: "preventOverflow", options: { padding: 12, }, }, ], }); if (!value) return null; let projectLabels: IIssueLabel[] = defaultOptions; if (storeLabels && storeLabels.length > 0) projectLabels = storeLabels; const options = projectLabels.map((label) => ({ value: label?.id, query: label?.name, content: (
{label?.name}
), })); const filteredOptions = query === "" ? options : options?.filter((option) => option.query.toLowerCase().includes(query.toLowerCase())); const label = (
{value.length > 0 ? ( value.length <= maxRender ? ( <> {projectLabels ?.filter((l) => value.includes(l?.id)) .map((label) => (
{label?.name}
))} ) : (
value.includes(l?.id)) .map((l) => l?.name) .join(", ")} >
{`${value.length} Labels`}
) ) : (
{placeholderText}
)}
); return ( {isOpen && (
setQuery(e.target.value)} placeholder="Search" displayValue={(assigned: any) => assigned?.name || ""} onKeyDown={searchInputKeyDown} />
{isLoading ? (

Loading...

) : filteredOptions.length > 0 ? ( filteredOptions.map((option) => ( { if (e.key === "Enter") { e.preventDefault(); e.stopPropagation(); } }} className={({ active, selected }) => `flex cursor-pointer select-none items-center justify-between gap-2 truncate rounded px-1 py-1.5 hover:bg-custom-background-80 ${ active ? "bg-custom-background-80" : "" } ${selected ? "text-custom-text-100" : "text-custom-text-200"}` } > {({ selected }) => ( <> {option.content} {selected && (
)} )}
)) ) : (

No matching results

)}
)}
); });