import React, { useRef, useState } from "react"; import useSWR from "swr"; import { useRouter } from "next/router"; // services import issuesService from "services/issues.service"; // hooks import useDynamicDropdownPosition from "hooks/use-dynamic-dropdown"; // headless ui import { Combobox } from "@headlessui/react"; // component import { CreateLabelModal } from "components/labels"; // icons import { ChevronDownIcon } from "@heroicons/react/20/solid"; import { CheckIcon, MagnifyingGlassIcon } from "@heroicons/react/24/outline"; import { PlusIcon } from "lucide-react"; // types import { Tooltip } from "components/ui"; import { ICurrentUserResponse, IIssueLabels } from "types"; // constants import { PROJECT_ISSUE_LABELS } from "constants/fetch-keys"; type Props = { value: string[]; projectId: string; onChange: (data: any) => void; labelsDetails: any[]; className?: string; buttonClassName?: string; optionsClassName?: string; maxRender?: number; hideDropdownArrow?: boolean; disabled?: boolean; user: ICurrentUserResponse | undefined; }; export const LabelSelect: React.FC = ({ value, projectId, onChange, labelsDetails, className = "", buttonClassName = "", optionsClassName = "", maxRender = 2, hideDropdownArrow = false, disabled = false, user, }) => { const [query, setQuery] = useState(""); const [isOpen, setIsOpen] = useState(false); const [fetchStates, setFetchStates] = useState(false); const [labelModal, setLabelModal] = useState(false); const router = useRouter(); const { workspaceSlug } = router.query; const dropdownBtn = useRef(null); const dropdownOptions = useRef(null); const { data: issueLabels } = useSWR( projectId && fetchStates ? PROJECT_ISSUE_LABELS(projectId) : null, workspaceSlug && projectId && fetchStates ? () => issuesService.getIssueLabels(workspaceSlug as string, projectId) : null ); const options = issueLabels?.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 = (
{labelsDetails.length > 0 ? ( labelsDetails.length <= maxRender ? ( <> {labelsDetails.map((label) => (
{label.name}
))} ) : (
l.name).join(", ")} >
{`${value.length} Labels`}
) ) : ( "" )}
); useDynamicDropdownPosition(isOpen, () => setIsOpen(false), dropdownBtn, dropdownOptions); const footerOption = ( ); return ( <> {projectId && ( setLabelModal(false)} projectId={projectId} user={user} /> )} {({ open }: { open: boolean }) => { if (open) { if (!isOpen) setIsOpen(true); setFetchStates(true); } else if (isOpen) setIsOpen(false); return ( <> {label} {!hideDropdownArrow && !disabled && (
setQuery(e.target.value)} placeholder="Search" displayValue={(assigned: any) => assigned?.name} />
{filteredOptions ? ( filteredOptions.length > 0 ? ( filteredOptions.map((option) => ( `flex items-center justify-between gap-2 cursor-pointer select-none truncate rounded px-1 py-1.5 ${ active && !selected ? "bg-custom-background-80" : "" } ${selected ? "text-custom-text-100" : "text-custom-text-200"}` } > {({ selected }) => ( <> {option.content} {selected && } )} )) ) : (

No matching results

) ) : (

Loading...

)}
{footerOption}
); }}
); };