import React from "react"; // next imports import { useRouter } from "next/router"; // swr import useSWR, { mutate } from "swr"; // lucide icons import { Plus, ChevronRight, ChevronDown } from "lucide-react"; // 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 "components/ui"; // hooks import { useProjectMyMembership } from "contexts/project-member.context"; // helpers import { copyTextToClipboard } from "helpers/string.helper"; // types import { ICurrentUserResponse, IIssue, ISearchIssueResponse } from "types"; // services import issuesService from "services/issues.service"; // fetch keys import { SUB_ISSUES } from "constants/fetch-keys"; export interface ISubIssuesRoot { parentIssue: IIssue; user: ICurrentUserResponse | undefined; editable: boolean; } export const SubIssuesRoot: React.FC = ({ parentIssue, user, editable }) => { const router = useRouter(); const { workspaceSlug, projectId } = router.query as { workspaceSlug: string; projectId: string }; const { memberRole } = useProjectMyMembership(); const { data: issues } = useSWR( workspaceSlug && projectId && parentIssue && parentIssue?.id ? SUB_ISSUES(parentIssue?.id) : null, workspaceSlug && projectId && parentIssue && parentIssue?.id ? () => issuesService.subIssues(workspaceSlug, projectId, parentIssue.id) : null ); const [issuesVisibility, setIssuesVisibility] = React.useState([parentIssue?.id]); const handleIssuesVisibility = (issueId: string) => { if (issuesVisibility.includes(issueId)) { setIssuesVisibility(issuesVisibility.filter((i: string) => i !== issueId)); } else { setIssuesVisibility([...issuesVisibility, 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 || !parentIssue || issueCrudOperation?.existing?.issueId === null) return; const issueId = issueCrudOperation?.existing?.issueId; const payload = { sub_issue_ids: data.map((i) => i.id), }; await issuesService.addSubIssues(workspaceSlug, projectId, issueId, payload).finally(() => { if (issueId) mutate(SUB_ISSUES(issueId)); }); }; const removeIssueFromSubIssues = async (parentIssueId: string, issue: IIssue) => { if (!workspaceSlug || !parentIssue || !issue?.id) return; issuesService .patchIssue(workspaceSlug, projectId, issue.id, { parent: null }, user) .finally(() => { if (parentIssueId) mutate(SUB_ISSUES(parentIssueId)); }); }; 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 isEditable = memberRole?.isGuest || memberRole?.isViewer ? false : true; const mutateSubIssues = (parentIssueId: string | null) => { if (parentIssueId) mutate(SUB_ISSUES(parentIssueId)); }; return (
{parentIssue && parentIssue?.sub_issues_count > 0 ? ( <> {/* header */}
handleIssuesVisibility(parentIssue?.id)} >
{issuesVisibility.includes(parentIssue?.id) ? ( ) : ( )}
Sub-issues
({parentIssue?.sub_issues_count})
{isEditable && issuesVisibility.includes(parentIssue?.id) && (
handleIssueCrudOperation("create", parentIssue?.id)} > Add sub-issue
handleIssueCrudOperation("existing", parentIssue?.id)} > Add an existing issue
)}
{/* issues */} {issuesVisibility.includes(parentIssue?.id) && (
)} ) : ( isEditable && (
No sub issues are available
<> Add sub-issue } buttonClassName="whitespace-nowrap" position="left" noBorder noChevron > handleIssueCrudOperation("create", parentIssue?.id)} > Create new handleIssueCrudOperation("existing", parentIssue?.id)} > Add an existing issue
) )} {isEditable && issueCrudOperation?.create?.toggle && ( 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 && ( { mutateSubIssues(issueCrudOperation?.delete?.issueId); handleIssueCrudOperation("delete", null, null); }} data={issueCrudOperation?.delete?.issue} user={user} redirection={false} /> )}
); };