import React, { useCallback } from "react"; import { useRouter } from "next/router"; import { observer } from "mobx-react-lite"; import useSWR, { mutate } from "swr"; import { Plus, ChevronRight, ChevronDown } from "lucide-react"; // mobx store import { useMobxStore } from "lib/mobx/store-provider"; // components import { ExistingIssuesListModal } from "components/core"; import { CreateUpdateIssueModal, DeleteIssueModal } from "components/issues"; import { SubIssuesRootList } from "./issues-list"; import { ProgressBar } from "./progressbar"; // ui import { CustomMenu } from "@plane/ui"; // hooks import useToast from "hooks/use-toast"; // helpers import { copyTextToClipboard } from "helpers/string.helper"; // types import { IUser, IIssue, ISearchIssueResponse } from "types"; // services import { IssueService } from "services/issue"; // fetch keys import { SUB_ISSUES } from "constants/fetch-keys"; export interface ISubIssuesRoot { parentIssue: IIssue; user: IUser | undefined; } export interface ISubIssuesRootLoaders { visibility: string[]; delete: string[]; sub_issues: string[]; } export interface ISubIssuesRootLoadersHandler { key: "visibility" | "delete" | "sub_issues"; issueId: string; } const issueService = new IssueService(); export const SubIssuesRoot: React.FC = observer((props) => { const { parentIssue, user } = props; const { user: userStore, issue: issueStore, issueDetail: issueDetailStore } = useMobxStore(); const userRole = userStore.currentProjectRole; const router = useRouter(); const { workspaceSlug, projectId } = router.query; const { setToastAlert } = useToast(); const { data: issues, isLoading } = useSWR( workspaceSlug && projectId && parentIssue && parentIssue?.id ? SUB_ISSUES(parentIssue?.id) : null, workspaceSlug && projectId && parentIssue && parentIssue?.id ? () => issueService.subIssues(workspaceSlug.toString(), projectId.toString(), parentIssue.id) : null ); const [issuesLoader, setIssuesLoader] = React.useState({ visibility: [parentIssue?.id], delete: [], sub_issues: [], }); const handleIssuesLoader = ({ key, issueId }: ISubIssuesRootLoadersHandler) => { setIssuesLoader((previousData: ISubIssuesRootLoaders) => ({ ...previousData, [key]: previousData[key].includes(issueId) ? previousData[key].filter((i: string) => i !== issueId) : [...previousData[key], issueId], })); }; const [issueCrudOperation, setIssueCrudOperation] = React.useState<{ create: { toggle: boolean; issueId: string | null }; existing: { toggle: boolean; issueId: string | null }; edit: { toggle: boolean; issueId: string | null; issue: IIssue | null }; delete: { toggle: boolean; issueId: string | null; issue: IIssue | null }; }>({ create: { toggle: false, issueId: null, }, existing: { toggle: false, issueId: null, }, edit: { toggle: false, issueId: null, // parent issue id for mutation issue: null, }, delete: { toggle: false, issueId: null, // parent issue id for mutation issue: null, }, }); const handleIssueCrudOperation = ( key: "create" | "existing" | "edit" | "delete", issueId: string | null, issue: IIssue | null = null ) => { setIssueCrudOperation({ ...issueCrudOperation, [key]: { toggle: !issueCrudOperation[key].toggle, issueId: issueId, issue: issue, }, }); }; const addAsSubIssueFromExistingIssues = async (data: ISearchIssueResponse[]) => { if (!workspaceSlug || !projectId || !parentIssue || issueCrudOperation?.existing?.issueId === null) return; const issueId = issueCrudOperation?.existing?.issueId; const payload = { sub_issue_ids: data.map((i) => i.id), }; await issueService.addSubIssues(workspaceSlug.toString(), projectId.toString(), issueId, payload).finally(() => { if (issueId) mutate(SUB_ISSUES(issueId)); }); }; const removeIssueFromSubIssues = async (parentIssueId: string, issue: IIssue) => { if (!workspaceSlug || !projectId || !parentIssue || !issue?.id) return; issueService .patchIssue(workspaceSlug.toString(), projectId.toString(), issue.id, { parent: null }) .then(async () => { if (parentIssueId) await mutate(SUB_ISSUES(parentIssueId)); handleIssuesLoader({ key: "delete", issueId: issue?.id }); setToastAlert({ type: "success", title: `Issue removed!`, message: `Issue removed successfully.`, }); }) .catch(() => { handleIssuesLoader({ key: "delete", issueId: issue?.id }); setToastAlert({ type: "warning", title: `Error!`, message: `Error, Please try again later.`, }); }); }; const copyText = (text: string) => { const originURL = typeof window !== "undefined" && window.location.origin ? window.location.origin : ""; copyTextToClipboard(`${originURL}/${text}`).then(() => { setToastAlert({ type: "success", title: "Link Copied!", message: "Issue link copied to clipboard.", }); }); }; const handleUpdateIssue = useCallback( (issue: IIssue, data: Partial) => { if (!workspaceSlug || !projectId || !user) return; const payload = { ...issue, ...data, }; issueStore.updateIssueStructure(null, null, payload); issueDetailStore.updateIssue(workspaceSlug.toString(), projectId.toString(), issue.id, data); }, [issueStore, issueDetailStore, projectId, user, workspaceSlug] ); const isEditable = userRole === 5 || userRole === 10 ? false : true; const mutateSubIssues = (parentIssueId: string | null) => { if (parentIssueId) mutate(SUB_ISSUES(parentIssueId)); }; return (
{!issues && isLoading ? (
Loading...
) : ( <> {issues && issues?.sub_issues && issues?.sub_issues?.length > 0 ? ( <> {/* header */}
handleIssuesLoader({ key: "visibility", issueId: parentIssue?.id })} >
{issuesLoader.visibility.includes(parentIssue?.id) ? ( ) : ( )}
Sub-issues
({issues?.sub_issues?.length || 0})
{isEditable && issuesLoader.visibility.includes(parentIssue?.id) && (
handleIssueCrudOperation("create", parentIssue?.id)} > Add sub-issue
handleIssueCrudOperation("existing", parentIssue?.id)} > Add an existing issue
)}
{/* issues */} {issuesLoader.visibility.includes(parentIssue?.id) && workspaceSlug && projectId && (
)}
Add sub-issue } buttonClassName="whitespace-nowrap" // position="left" noBorder noChevron > { mutateSubIssues(parentIssue?.id); handleIssueCrudOperation("create", parentIssue?.id); }} > Create new { mutateSubIssues(parentIssue?.id); handleIssueCrudOperation("existing", parentIssue?.id); }} > Add an existing issue
) : ( isEditable && (
No Sub-Issues yet
Add sub-issue } buttonClassName="whitespace-nowrap" // position="left" noBorder noChevron > { mutateSubIssues(parentIssue?.id); handleIssueCrudOperation("create", parentIssue?.id); }} > Create new { mutateSubIssues(parentIssue?.id); handleIssueCrudOperation("existing", parentIssue?.id); }} > Add an existing issue
) )} {isEditable && issueCrudOperation?.create?.toggle && ( { mutateSubIssues(issueCrudOperation?.create?.issueId); handleIssueCrudOperation("create", null); }} /> )} {isEditable && issueCrudOperation?.existing?.toggle && issueCrudOperation?.existing?.issueId && ( handleIssueCrudOperation("existing", null)} searchParams={{ sub_issue: true, issue_id: issueCrudOperation?.existing?.issueId }} handleOnSubmit={addAsSubIssueFromExistingIssues} workspaceLevelToggle /> )} {isEditable && issueCrudOperation?.edit?.toggle && issueCrudOperation?.edit?.issueId && ( <> { mutateSubIssues(issueCrudOperation?.edit?.issueId); handleIssueCrudOperation("edit", null, null); }} data={issueCrudOperation?.edit?.issue} /> )} {isEditable && issueCrudOperation?.delete?.toggle && issueCrudOperation?.delete?.issueId && issueCrudOperation?.delete?.issue && ( { mutateSubIssues(issueCrudOperation?.delete?.issueId); handleIssueCrudOperation("delete", null, null); }} data={issueCrudOperation?.delete?.issue} /> )} )}
); });