import React, { useState, useRef } from "react"; import { useRouter } from "next/router"; import useSWR from "swr"; // store import { observer } from "mobx-react-lite"; import { useMobxStore } from "lib/mobx/store-provider"; // components import { CreateUpdateLabelInline, DeleteLabelModal, LabelsListModal, ProjectSettingLabelItem, ProjectSettingLabelGroup, } from "components/labels"; // ui import { Button, Loader } from "@plane/ui"; import { EmptyState } from "components/common"; // images import emptyLabel from "public/empty-state/label.svg"; // types import { IIssueLabels } from "types"; export const ProjectSettingsLabelList: React.FC = observer(() => { // router const router = useRouter(); const { workspaceSlug, projectId } = router.query; // store const { project: projectStore } = useMobxStore(); // states const [labelForm, setLabelForm] = useState(false); const [isUpdating, setIsUpdating] = useState(false); const [labelsListModal, setLabelsListModal] = useState(false); const [labelToUpdate, setLabelToUpdate] = useState(null); const [parentLabel, setParentLabel] = useState(undefined); const [selectDeleteLabel, setSelectDeleteLabel] = useState(null); // ref const scrollToRef = useRef(null); // api call to fetch project details useSWR( workspaceSlug && projectId ? "PROJECT_LABELS" : null, workspaceSlug && projectId ? () => projectStore.fetchProjectLabels(workspaceSlug.toString(), projectId.toString()) : null ); // derived values const issueLabels = projectStore.labels?.[projectId?.toString()!] ?? null; const newLabel = () => { setIsUpdating(false); setLabelForm(true); }; const addLabelToGroup = (parentLabel: IIssueLabels) => { setLabelsListModal(true); setParentLabel(parentLabel); }; const editLabel = (label: IIssueLabels) => { setLabelForm(true); setIsUpdating(true); setLabelToUpdate(label); }; return ( <> setLabelsListModal(false)} /> setSelectDeleteLabel(null)} />

Labels

{labelForm && ( { setLabelForm(false); setIsUpdating(false); setLabelToUpdate(null); }} /> )} {/* labels */} {issueLabels && issueLabels.map((label) => { const children = issueLabels?.filter((l) => l.parent === label.id); if (children && children.length === 0) { if (!label.parent) return ( addLabelToGroup(label)} editLabel={(label) => { editLabel(label); scrollToRef.current?.scrollIntoView({ behavior: "smooth", }); }} handleLabelDelete={() => setSelectDeleteLabel(label)} /> ); } else { return ( { editLabel(label); scrollToRef.current?.scrollIntoView({ behavior: "smooth", }); }} handleLabelDelete={() => setSelectDeleteLabel(label)} /> ); } })} {/* loading state */} {!issueLabels && ( )} {/* empty state */} {issueLabels && issueLabels.length === 0 && ( newLabel(), }} /> )}
); });