import React, { useState } from "react"; import { useRouter } from "next/router"; import useSWR, { mutate } from "swr"; // headless ui import { Combobox, Dialog, Transition } from "@headlessui/react"; // icons import { RectangleStackIcon, MagnifyingGlassIcon } from "@heroicons/react/24/outline"; // services import issuesService from "services/issues.service"; // types import { ICurrentUserResponse, IIssueLabels } from "types"; // constants import { PROJECT_ISSUE_LABELS } from "constants/fetch-keys"; type Props = { isOpen: boolean; handleClose: () => void; parent: IIssueLabels | undefined; user: ICurrentUserResponse | undefined; }; export const LabelsListModal: React.FC = ({ isOpen, handleClose, parent, user }) => { const [query, setQuery] = useState(""); const router = useRouter(); const { workspaceSlug, projectId } = router.query; const { data: issueLabels, mutate } = useSWR( workspaceSlug && projectId ? PROJECT_ISSUE_LABELS(projectId as string) : null, workspaceSlug && projectId ? () => issuesService.getIssueLabels(workspaceSlug as string, projectId as string) : null ); const filteredLabels: IIssueLabels[] = query === "" ? issueLabels ?? [] : issueLabels?.filter((l) => l.name.toLowerCase().includes(query.toLowerCase())) ?? []; const handleModalClose = () => { handleClose(); setQuery(""); }; const addChildLabel = async (label: IIssueLabels) => { if (!workspaceSlug || !projectId) return; mutate( (prevData) => prevData?.map((l) => { if (l.id === label.id) return { ...l, parent: parent?.id ?? "" }; return l; }), false ); await issuesService .patchIssueLabel( workspaceSlug as string, projectId as string, label.id, { parent: parent?.id ?? "", }, user ) .then(() => mutate()); }; return ( setQuery("")} appear>
{filteredLabels.length > 0 && (
  • {query === "" && (

    Labels

    )}
      {filteredLabels.map((label) => { const children = issueLabels?.filter((l) => l.parent === label.id); if ( (label.parent === "" || label.parent === null) && // issue does not have any other parent label.id !== parent?.id && // issue is not itself children?.length === 0 // issue doesn't have any othe children ) return ( `flex w-full cursor-pointer select-none items-center gap-2 rounded-md px-3 py-2 text-custom-text-200 ${ active ? "bg-custom-background-80 text-custom-text-100" : "" }` } onClick={() => { addChildLabel(label); handleClose(); }} > {label.name} ); })}
  • )}
    {query !== "" && filteredLabels.length === 0 && (
    )}
    ); };