import React, { useState } from "react"; import useSWR from "swr"; import { useRouter } from "next/router"; // react-popper import { usePopper } from "react-popper"; // services import { IssueLabelService } from "services/issue"; // headless ui import { Combobox } from "@headlessui/react"; // component import { CreateLabelModal } from "components/labels"; // icons import { Check, ChevronDown, PlusIcon, Search } from "lucide-react"; // types import { Tooltip } from "components/ui"; import { IUser, IIssueLabels } from "types"; import { Placement } from "@popperjs/core"; // 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; placement?: Placement; hideDropdownArrow?: boolean; disabled?: boolean; user: IUser | undefined; }; // services const issueLabelService = new IssueLabelService(); export const LabelSelect: React.FC = ({ value, projectId, onChange, labelsDetails, className = "", buttonClassName = "", optionsClassName = "", maxRender = 2, placement, hideDropdownArrow = false, disabled = false, user, }) => { const [query, setQuery] = useState(""); const [fetchStates, setFetchStates] = useState(false); const [referenceElement, setReferenceElement] = useState(null); const [popperElement, setPopperElement] = useState(null); const [labelModal, setLabelModal] = useState(false); const router = useRouter(); const { workspaceSlug } = router.query; const { styles, attributes } = usePopper(referenceElement, popperElement, { placement: placement ?? "bottom-start", }); const { data: issueLabels } = useSWR( projectId && fetchStates ? PROJECT_ISSUE_LABELS(projectId) : null, workspaceSlug && projectId && fetchStates ? () => issueLabelService.getProjectIssueLabels(workspaceSlug.toString(), 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`}
) ) : ( "" )}
); const footerOption = ( ); return ( <> {projectId && ( setLabelModal(false)} projectId={projectId} user={user} /> )} {({ open }: { open: boolean }) => { if (open) setFetchStates(true); return ( <>
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}
); }}
); };