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"; import { observer } from "mobx-react-lite"; // store import { useMobxStore } from "lib/mobx/store-provider"; // ui import { IssueLabelsList } from "components/ui"; // icons import { Check, Component, Plus, Search, Tag } from "lucide-react"; type Props = { setIsOpen: React.Dispatch>; value: string[]; onChange: (value: string[]) => void; projectId: string; label?: JSX.Element; }; export const IssueLabelSelect: React.FC = observer((props) => { const { setIsOpen, value, onChange, projectId, label } = props; // states const [query, setQuery] = useState(""); const router = useRouter(); const { workspaceSlug } = router.query; const { project: { labels, fetchProjectLabels }, } = useMobxStore(); const [referenceElement, setReferenceElement] = useState(null); const [popperElement, setPopperElement] = useState(null); const { styles, attributes } = usePopper(referenceElement, popperElement, { placement: "bottom-start", }); const issueLabels = labels?.[projectId] || []; useSWR( workspaceSlug && projectId ? `PROJECT_ISSUE_LABELS_${projectId.toUpperCase()}` : null, workspaceSlug && projectId ? () => fetchProjectLabels(workspaceSlug.toString(), 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...

)}
)}
); });