import React, { Fragment, useState } from "react"; import { useRouter } from "next/router"; import useSWR from "swr"; import { Combobox, Transition } from "@headlessui/react"; import { usePopper } from "react-popper"; // services import { IssueLabelService } from "services/issue"; // ui import { IssueLabelsList } from "components/ui"; // icons import { Check, Component, Plus, Search, Tag } from "lucide-react"; // types import type { IIssueLabels } from "types"; // fetch-keys import { PROJECT_ISSUE_LABELS } from "constants/fetch-keys"; type Props = { setIsOpen: React.Dispatch>; value: string[]; onChange: (value: string[]) => void; projectId: string; label?: JSX.Element; }; const issueLabelService = new IssueLabelService(); export const IssueLabelSelect: React.FC = ({ setIsOpen, value, onChange, projectId, label }) => { // states const [query, setQuery] = useState(""); const router = useRouter(); const { workspaceSlug } = router.query; const [referenceElement, setReferenceElement] = useState(null); const [popperElement, setPopperElement] = useState(null); const { styles, attributes } = usePopper(referenceElement, popperElement, { placement: "bottom-start", }); const { data: issueLabels } = useSWR( projectId ? PROJECT_ISSUE_LABELS(projectId) : null, workspaceSlug && projectId ? () => issueLabelService.getProjectIssueLabels(workspaceSlug as string, projectId) : null ); const filteredOptions = query === "" ? issueLabels : issueLabels?.filter((l) => l.name.toLowerCase().includes(query.toLowerCase())); return ( onChange(val)} className="relative flex-shrink-0" multiple> {({ open }: any) => ( <>
{label ? ( label ) : value && value.length > 0 ? ( issueLabels?.find((l) => l.id === v)) ?? []} length={3} showLength /> ) : (
Label
)}
setQuery(event.target.value)} placeholder="Search for label..." displayValue={(assigned: any) => assigned?.name} />
{issueLabels && filteredOptions ? ( filteredOptions.length > 0 ? ( filteredOptions.map((label) => { const children = issueLabels?.filter((l) => l.parent === label.id); if (children.length === 0) { if (!label.parent) return ( `${ active ? "bg-custom-background-80" : "" } group flex min-w-[14rem] cursor-pointer select-none items-center gap-2 truncate rounded px-1 py-1.5 text-custom-text-200` } value={label.id} > {({ selected }) => (
{label.name}
)}
); } else return (
{label.name}
{children.map((child) => ( `${ active ? "bg-custom-background-80" : "" } group flex min-w-[14rem] cursor-pointer select-none items-center gap-2 truncate rounded px-1 py-1.5 text-custom-text-200` } value={child.id} > {({ selected }) => (
{child.name}
)}
))}
); }) ) : (

No labels found

) ) : (

Loading...

)}
)}
); };