import React, { useCallback, useState } from "react"; import { useRouter } from "next/router"; import { observer } from "mobx-react-lite"; import { mutate } from "swr"; import { Controller, UseFormWatch } from "react-hook-form"; import { Bell, CalendarDays, LinkIcon, Signal, Tag, Trash2, Triangle, LayoutPanelTop } from "lucide-react"; // hooks import { useEstimate, useIssues, useProject, useProjectState, useUser } from "hooks/store"; import useToast from "hooks/use-toast"; import useUserIssueNotificationSubscription from "hooks/use-issue-notification-subscription"; // services import { IssueService } from "services/issue"; import { ModuleService } from "services/module.service"; // components import { DeleteIssueModal, SidebarIssueRelationSelect, SidebarCycleSelect, SidebarModuleSelect, SidebarParentSelect, SidebarLabelSelect, IssueLinkRoot, } from "components/issues"; import { EstimateDropdown, PriorityDropdown, ProjectMemberDropdown, StateDropdown } from "components/dropdowns"; // ui import { CustomDatePicker } from "components/ui"; // icons import { Button, ContrastIcon, DiceIcon, DoubleCircleIcon, StateGroupIcon, UserGroupIcon } from "@plane/ui"; // helpers import { copyTextToClipboard } from "helpers/string.helper"; // types import type { TIssue } from "@plane/types"; // fetch-keys import { ISSUE_DETAILS } from "constants/fetch-keys"; import { EUserProjectRoles } from "constants/project"; import { EIssuesStoreType } from "constants/issue"; type Props = { control: any; submitChanges: (formData: any) => void; issueDetail: TIssue | undefined; watch: UseFormWatch; fieldsToShow?: ( | "state" | "assignee" | "priority" | "estimate" | "parent" | "blocker" | "blocked" | "startDate" | "dueDate" | "cycle" | "module" | "label" | "link" | "delete" | "all" | "subscribe" | "duplicate" | "relates_to" )[]; uneditable?: boolean; }; const issueService = new IssueService(); const moduleService = new ModuleService(); export const IssueDetailsSidebar: React.FC = observer((props) => { const { control, submitChanges, issueDetail, watch: watchIssue, fieldsToShow = ["all"], uneditable = false } = props; // states const [deleteIssueModal, setDeleteIssueModal] = useState(false); // store hooks const { getProjectById } = useProject(); const { issues: { removeIssue }, } = useIssues(EIssuesStoreType.PROJECT); const { currentUser, membership: { currentProjectRole }, } = useUser(); const { projectStates } = useProjectState(); const { areEstimatesEnabledForCurrentProject } = useEstimate(); // router const router = useRouter(); const { workspaceSlug, projectId, issueId, inboxIssueId } = router.query; const { loading, handleSubscribe, handleUnsubscribe, subscribed } = useUserIssueNotificationSubscription( currentUser, workspaceSlug, projectId, issueId ); const { setToastAlert } = useToast(); const handleCycleChange = useCallback( (cycleId: string) => { if (!workspaceSlug || !projectId || !issueDetail || !currentUser) return; issueService .addIssueToCycle(workspaceSlug as string, projectId as string, cycleId, { issues: [issueDetail.id], }) .then(() => { mutate(ISSUE_DETAILS(issueId as string)); }); }, [workspaceSlug, projectId, issueId, issueDetail, currentUser] ); const handleModuleChange = useCallback( (moduleId: string) => { if (!workspaceSlug || !projectId || !issueDetail || !currentUser) return; moduleService .addIssuesToModule(workspaceSlug as string, projectId as string, moduleId, { issues: [issueDetail.id], }) .then(() => { mutate(ISSUE_DETAILS(issueId as string)); }); }, [workspaceSlug, projectId, issueId, issueDetail, currentUser] ); const handleCopyText = () => { const originURL = typeof window !== "undefined" && window.location.origin ? window.location.origin : ""; copyTextToClipboard(`${originURL}/${workspaceSlug}/projects/${projectId}/issues/${issueDetail?.id}`).then(() => { setToastAlert({ type: "success", title: "Link Copied!", message: "Issue link copied to clipboard.", }); }); }; const projectDetails = issueDetail ? getProjectById(issueDetail?.project_id) : null; const showFirstSection = fieldsToShow.includes("all") || fieldsToShow.includes("state") || fieldsToShow.includes("assignee") || fieldsToShow.includes("priority") || fieldsToShow.includes("estimate"); const showSecondSection = fieldsToShow.includes("all") || fieldsToShow.includes("parent") || fieldsToShow.includes("blocker") || fieldsToShow.includes("blocked") || fieldsToShow.includes("dueDate"); const showThirdSection = fieldsToShow.includes("all") || fieldsToShow.includes("cycle") || fieldsToShow.includes("module"); const startDate = watchIssue("start_date"); const targetDate = watchIssue("target_date"); const minDate = startDate ? new Date(startDate) : null; minDate?.setDate(minDate.getDate()); const maxDate = targetDate ? new Date(targetDate) : null; maxDate?.setDate(maxDate.getDate()); const isAllowed = !!currentProjectRole && currentProjectRole >= EUserProjectRoles.MEMBER; const currentIssueState = projectStates?.find((s) => s.id === issueDetail?.state_id); return ( <> {workspaceSlug && projectId && issueDetail && ( setDeleteIssueModal(false)} isOpen={deleteIssueModal} data={issueDetail} onSubmit={async () => { await removeIssue(workspaceSlug.toString(), projectId.toString(), issueDetail.id); router.push(`/${workspaceSlug}/projects/${projectId}/issues`); }} /> )}
{currentIssueState ? ( ) : inboxIssueId ? ( ) : null}

{projectDetails?.identifier}-{issueDetail?.sequence_id}

{issueDetail?.created_by !== currentUser?.id && !issueDetail?.assignee_ids.includes(currentUser?.id ?? "") && !router.pathname.includes("[archivedIssueId]") && (fieldsToShow.includes("all") || fieldsToShow.includes("subscribe")) && ( )} {(fieldsToShow.includes("all") || fieldsToShow.includes("link")) && ( )} {isAllowed && (fieldsToShow.includes("all") || fieldsToShow.includes("delete")) && ( )}
{showFirstSection && (
{(fieldsToShow.includes("all") || fieldsToShow.includes("state")) && (

State

(
submitChanges({ state: val })} projectId={projectId?.toString() ?? ""} disabled={!isAllowed || uneditable} buttonVariant="background-with-text" />
)} />
)} {(fieldsToShow.includes("all") || fieldsToShow.includes("assignee")) && (

Assignees

(
submitChanges({ assignees: val })} disabled={!isAllowed || uneditable} projectId={projectId?.toString() ?? ""} placeholder="Assignees" multiple buttonVariant={value?.length > 0 ? "transparent-without-text" : "background-with-text"} buttonClassName={value?.length > 0 ? "hover:bg-transparent px-0" : ""} />
)} />
)} {(fieldsToShow.includes("all") || fieldsToShow.includes("priority")) && (

Priority

(
submitChanges({ priority: val })} disabled={!isAllowed || uneditable} buttonVariant="background-with-text" />
)} />
)} {(fieldsToShow.includes("all") || fieldsToShow.includes("estimate")) && areEstimatesEnabledForCurrentProject && (

Estimate

(
submitChanges({ estimate_point: val })} projectId={projectId?.toString() ?? ""} disabled={!isAllowed || uneditable} buttonVariant="background-with-text" />
)} />
)}
)} {showSecondSection && (
{(fieldsToShow.includes("all") || fieldsToShow.includes("parent")) && (

Parent

( { submitChanges({ parent: val }); onChange(val); }} issueDetails={issueDetail} disabled={!isAllowed || uneditable} /> )} />
)} {(fieldsToShow.includes("all") || fieldsToShow.includes("blocker")) && ( )} {(fieldsToShow.includes("all") || fieldsToShow.includes("blocked")) && ( )} {(fieldsToShow.includes("all") || fieldsToShow.includes("duplicate")) && ( )} {(fieldsToShow.includes("all") || fieldsToShow.includes("relates_to")) && ( )} {(fieldsToShow.includes("all") || fieldsToShow.includes("startDate")) && (

Start date

( submitChanges({ start_date: val, }) } className="border-none bg-custom-background-80" maxDate={maxDate ?? undefined} disabled={!isAllowed || uneditable} /> )} />
)} {(fieldsToShow.includes("all") || fieldsToShow.includes("dueDate")) && (

Due date

( submitChanges({ target_date: val, }) } className="border-none bg-custom-background-80" minDate={minDate ?? undefined} disabled={!isAllowed || uneditable} /> )} />
)}
)} {showThirdSection && (
{(fieldsToShow.includes("all") || fieldsToShow.includes("cycle")) && projectDetails?.cycle_view && (

Cycle

)} {(fieldsToShow.includes("all") || fieldsToShow.includes("module")) && projectDetails?.module_view && (

Module

)}
)}
{(fieldsToShow.includes("all") || fieldsToShow.includes("label")) && (

Label

)} {(fieldsToShow.includes("all") || fieldsToShow.includes("link")) && ( )}
); });