import React, { useCallback, useState } from "react"; import { useRouter } from "next/router"; import { mutate } from "swr"; // react-hook-form import { Controller, UseFormWatch } from "react-hook-form"; // hooks import useToast from "hooks/use-toast"; import useUserAuth from "hooks/use-user-auth"; import useUserIssueNotificationSubscription from "hooks/use-issue-notification-subscription"; import useEstimateOption from "hooks/use-estimate-option"; // services import issuesService from "services/issues.service"; import modulesService from "services/modules.service"; // contexts import { useProjectMyMembership } from "contexts/project-member.context"; // components import { LinkModal, LinksList } from "components/core"; import { DeleteIssueModal, SidebarAssigneeSelect, SidebarBlockedSelect, SidebarBlockerSelect, SidebarCycleSelect, SidebarModuleSelect, SidebarParentSelect, SidebarPrioritySelect, SidebarStateSelect, SidebarEstimateSelect, SidebarLabelSelect, SidebarDuplicateSelect, SidebarRelatesSelect, } from "components/issues"; // ui import { CustomDatePicker, Icon } from "components/ui"; // icons import { LinkIcon, CalendarDaysIcon, TrashIcon, PlusIcon, Squares2X2Icon, ChartBarIcon, UserGroupIcon, PlayIcon, UserIcon, RectangleGroupIcon, } from "@heroicons/react/24/outline"; // helpers import { copyTextToClipboard } from "helpers/string.helper"; // types import type { ICycle, IIssue, IIssueLink, linkDetails, IModule } from "types"; // fetch-keys import { ISSUE_DETAILS, PROJECT_ISSUES_ACTIVITY } from "constants/fetch-keys"; import { ContrastIcon } from "components/icons"; type Props = { control: any; submitChanges: (formData: any) => void; issueDetail: IIssue | 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; }; export const IssueDetailsSidebar: React.FC = ({ control, submitChanges, issueDetail, watch: watchIssue, fieldsToShow = ["all"], uneditable = false, }) => { const [deleteIssueModal, setDeleteIssueModal] = useState(false); const [linkModal, setLinkModal] = useState(false); const [selectedLinkToUpdate, setSelectedLinkToUpdate] = useState(null); const router = useRouter(); const { workspaceSlug, projectId, issueId } = router.query; const { user } = useUserAuth(); const { isEstimateActive } = useEstimateOption(); const { loading, handleSubscribe, handleUnsubscribe, subscribed } = useUserIssueNotificationSubscription(workspaceSlug, projectId, issueId); const { memberRole } = useProjectMyMembership(); const { setToastAlert } = useToast(); const handleCycleChange = useCallback( (cycleDetails: ICycle) => { if (!workspaceSlug || !projectId || !issueDetail) return; issuesService .addIssueToCycle( workspaceSlug as string, projectId as string, cycleDetails.id, { issues: [issueDetail.id], }, user ) .then((res) => { mutate(ISSUE_DETAILS(issueId as string)); }); }, [workspaceSlug, projectId, issueId, issueDetail, user] ); const handleModuleChange = useCallback( (moduleDetail: IModule) => { if (!workspaceSlug || !projectId || !issueDetail) return; modulesService .addIssuesToModule( workspaceSlug as string, projectId as string, moduleDetail.id, { issues: [issueDetail.id], }, user ) .then((res) => { mutate(ISSUE_DETAILS(issueId as string)); }); }, [workspaceSlug, projectId, issueId, issueDetail, user] ); const handleCreateLink = async (formData: IIssueLink) => { if (!workspaceSlug || !projectId || !issueDetail) return; const payload = { metadata: {}, ...formData }; await issuesService .createIssueLink(workspaceSlug as string, projectId as string, issueDetail.id, payload) .then(() => mutate(ISSUE_DETAILS(issueDetail.id))) .catch((err) => { if (err.status === 400) setToastAlert({ type: "error", title: "Error!", message: "This URL already exists for this issue.", }); else setToastAlert({ type: "error", title: "Error!", message: "Something went wrong. Please try again.", }); }); }; const handleUpdateLink = async (formData: IIssueLink, linkId: string) => { if (!workspaceSlug || !projectId || !issueDetail) return; const payload = { metadata: {}, ...formData }; const updatedLinks = issueDetail.issue_link.map((l) => l.id === linkId ? { ...l, title: formData.title, url: formData.url, } : l ); mutate( ISSUE_DETAILS(issueDetail.id), (prevData) => ({ ...(prevData as IIssue), issue_link: updatedLinks }), false ); await issuesService .updateIssueLink( workspaceSlug as string, projectId as string, issueDetail.id, linkId, payload ) .then((res) => { mutate(ISSUE_DETAILS(issueDetail.id)); }) .catch((err) => { console.log(err); }); }; const handleDeleteLink = async (linkId: string) => { if (!workspaceSlug || !projectId || !issueDetail) return; const updatedLinks = issueDetail.issue_link.filter((l) => l.id !== linkId); mutate( ISSUE_DETAILS(issueDetail.id), (prevData) => ({ ...(prevData as IIssue), issue_link: updatedLinks }), false ); await issuesService .deleteIssueLink(workspaceSlug as string, projectId as string, issueDetail.id, linkId) .then((res) => { mutate(ISSUE_DETAILS(issueDetail.id)); }) .catch((err) => { console.log(err); }); }; 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 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 handleEditLink = (link: linkDetails) => { setSelectedLinkToUpdate(link); setLinkModal(true); }; const isNotAllowed = memberRole.isGuest || memberRole.isViewer; return ( <> { setLinkModal(false); setSelectedLinkToUpdate(null); }} data={selectedLinkToUpdate} status={selectedLinkToUpdate ? true : false} createIssueLink={handleCreateLink} updateIssueLink={handleUpdateLink} /> setDeleteIssueModal(false)} isOpen={deleteIssueModal} data={issueDetail ?? null} user={user} />

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

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

State

( submitChanges({ state: val })} disabled={memberRole.isGuest || memberRole.isViewer || uneditable} /> )} />
)} {(fieldsToShow.includes("all") || fieldsToShow.includes("assignee")) && (

Assignees

( submitChanges({ assignees_list: val })} disabled={memberRole.isGuest || memberRole.isViewer || uneditable} /> )} />
)} {(fieldsToShow.includes("all") || fieldsToShow.includes("priority")) && (

Priority

( submitChanges({ priority: val })} disabled={memberRole.isGuest || memberRole.isViewer || uneditable} /> )} />
)} {(fieldsToShow.includes("all") || fieldsToShow.includes("estimate")) && isEstimateActive && (

Estimate

( submitChanges({ estimate_point: val }) } disabled={memberRole.isGuest || memberRole.isViewer || uneditable} /> )} />
)}
)} {showSecondSection && (
{(fieldsToShow.includes("all") || fieldsToShow.includes("parent")) && (

Parent

( { submitChanges({ parent: val }); onChange(val); }} issueDetails={issueDetail} disabled={memberRole.isGuest || memberRole.isViewer || uneditable} /> )} />
)} {(fieldsToShow.includes("all") || fieldsToShow.includes("blocker")) && ( { mutate( ISSUE_DETAILS(issueId as string), (prevData) => { if (!prevData) return prevData; return { ...prevData, ...data, }; }, false ); mutate(PROJECT_ISSUES_ACTIVITY(issueId as string)); }} watch={watchIssue} disabled={memberRole.isGuest || memberRole.isViewer || uneditable} /> )} {(fieldsToShow.includes("all") || fieldsToShow.includes("blocked")) && ( { mutate( ISSUE_DETAILS(issueId as string), (prevData) => { if (!prevData) return prevData; return { ...prevData, ...data, }; }, false ); mutate(PROJECT_ISSUES_ACTIVITY(issueId as string)); }} watch={watchIssue} disabled={memberRole.isGuest || memberRole.isViewer || uneditable} /> )} {(fieldsToShow.includes("all") || fieldsToShow.includes("duplicate")) && ( { if (!data) return mutate(ISSUE_DETAILS(issueId as string)); mutate(ISSUE_DETAILS(issueId as string), (prevData) => { if (!prevData) return prevData; return { ...prevData, ...data, }; }); mutate(PROJECT_ISSUES_ACTIVITY(issueId as string)); }} watch={watchIssue} disabled={memberRole.isGuest || memberRole.isViewer || uneditable} /> )} {(fieldsToShow.includes("all") || fieldsToShow.includes("relates_to")) && ( { if (!data) return mutate(ISSUE_DETAILS(issueId as string)); mutate(ISSUE_DETAILS(issueId as string), (prevData) => { if (!prevData) return prevData; return { ...prevData, ...data, }; }); mutate(PROJECT_ISSUES_ACTIVITY(issueId as string)); }} watch={watchIssue} disabled={memberRole.isGuest || memberRole.isViewer || uneditable} /> )} {(fieldsToShow.includes("all") || fieldsToShow.includes("startDate")) && (

Start date

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

Due date

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

Cycle

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

Module

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

Links

{!isNotAllowed && ( )}
{issueDetail?.issue_link && issueDetail.issue_link.length > 0 ? ( ) : null}
)}
); };