import { useCallback, useEffect, useState } from "react"; import Router, { useRouter } from "next/router"; import { observer } from "mobx-react-lite"; import useSWR from "swr"; import { useForm } from "react-hook-form"; import { AlertTriangle, CheckCircle2, Clock, Copy, ExternalLink, Inbox, XCircle } from "lucide-react"; // mobx store import { useMobxStore } from "lib/mobx/store-provider"; // components import { IssueDescriptionForm, IssueDetailsSidebar, IssueReaction, IssueUpdateStatus } from "components/issues"; import { InboxIssueActivity } from "components/inbox"; // ui import { Loader, StateGroupIcon } from "@plane/ui"; // helpers import { renderFormattedDate } from "helpers/date-time.helper"; // types import { IInboxIssue, IIssue } from "types"; import { EUserWorkspaceRoles } from "constants/workspace"; const defaultValues: Partial = { name: "", description_html: "", assignees: [], priority: "low", target_date: new Date().toString(), labels: [], }; export const InboxMainContent: React.FC = observer(() => { const router = useRouter(); const { workspaceSlug, projectId, inboxId, inboxIssueId } = router.query; // states const [isSubmitting, setIsSubmitting] = useState<"submitting" | "submitted" | "saved">("saved"); const { inboxIssues: inboxIssuesStore, inboxIssueDetails: inboxIssueDetailsStore, user: { currentUser, currentProjectRole }, projectState: { states }, } = useMobxStore(); const { reset, control, watch } = useForm({ defaultValues, }); useSWR( workspaceSlug && projectId && inboxId && inboxIssueId ? `INBOX_ISSUE_DETAILS_${inboxIssueId.toString()}` : null, workspaceSlug && projectId && inboxId && inboxIssueId ? () => inboxIssueDetailsStore.fetchIssueDetails( workspaceSlug.toString(), projectId.toString(), inboxId.toString(), inboxIssueId.toString() ) : null ); const issuesList = inboxId ? inboxIssuesStore.inboxIssues[inboxId.toString()] : undefined; const issueDetails = inboxIssueId ? inboxIssueDetailsStore.issueDetails[inboxIssueId.toString()] : undefined; const currentIssueState = projectId ? states[projectId.toString()]?.find((s) => s.id === issueDetails?.state) : undefined; const submitChanges = useCallback( async (formData: Partial) => { if (!workspaceSlug || !projectId || !inboxIssueId || !inboxId || !issueDetails) return; await inboxIssueDetailsStore.updateIssue( workspaceSlug.toString(), projectId.toString(), inboxId.toString(), issueDetails.issue_inbox[0].id, formData ); }, [workspaceSlug, inboxIssueId, projectId, inboxId, issueDetails, inboxIssueDetailsStore] ); const onKeyDown = useCallback( (e: KeyboardEvent) => { if (!issuesList || !inboxIssueId) return; const currentIssueIndex = issuesList.findIndex((issue) => issue.issue_inbox[0].id === inboxIssueId); switch (e.key) { case "ArrowUp": Router.push({ pathname: `/${workspaceSlug}/projects/${projectId}/inbox/${inboxId}`, query: { inboxIssueId: currentIssueIndex === 0 ? issuesList[issuesList.length - 1].issue_inbox[0].id : issuesList[currentIssueIndex - 1].issue_inbox[0].id, }, }); break; case "ArrowDown": Router.push({ pathname: `/${workspaceSlug}/projects/${projectId}/inbox/${inboxId}`, query: { inboxIssueId: currentIssueIndex === issuesList.length - 1 ? issuesList[0].issue_inbox[0].id : issuesList[currentIssueIndex + 1].issue_inbox[0].id, }, }); break; default: break; } }, [workspaceSlug, projectId, inboxIssueId, inboxId, issuesList] ); useEffect(() => { document.addEventListener("keydown", onKeyDown); return () => { document.removeEventListener("keydown", onKeyDown); }; }, [onKeyDown]); useEffect(() => { if (!issueDetails || !inboxIssueId) return; reset({ ...issueDetails, assignees: issueDetails.assignees ?? (issueDetails.assignee_details ?? []).map((user) => user.id), labels: issueDetails.labels ?? issueDetails.labels, }); }, [issueDetails, reset, inboxIssueId]); const issueStatus = issueDetails?.issue_inbox[0].status; if (!inboxIssueId) return (
{issuesList && issuesList.length > 0 ? ( {issuesList?.length} issues found. Select an issue from the sidebar to view its details. ) : ( No issues found )}
); const isAllowed = !!currentProjectRole && currentProjectRole >= EUserWorkspaceRoles.MEMBER; return ( <> {issueDetails ? (
{issueStatus === -2 ? ( <>

This issue is still pending.

) : issueStatus === -1 ? ( <>

This issue has been declined.

) : issueStatus === 0 ? ( <> {new Date(issueDetails.issue_inbox[0].snoozed_till ?? "") < new Date() ? (

This issue was snoozed till{" "} {renderFormattedDate(issueDetails.issue_inbox[0].snoozed_till ?? "")}.

) : (

This issue has been snoozed till{" "} {renderFormattedDate(issueDetails.issue_inbox[0].snoozed_till ?? "")}.

)} ) : issueStatus === 1 ? ( <>

This issue has been accepted.

) : issueStatus === 2 ? ( <>

This issue has been marked as a duplicate of this issue .

) : null}
{currentIssueState && ( )}
setIsSubmitting(value)} isSubmitting={isSubmitting} workspaceSlug={workspaceSlug as string} issue={{ name: issueDetails.name, description_html: issueDetails.description_html, id: issueDetails.id, }} handleFormSubmit={submitChanges} isAllowed={isAllowed || currentUser?.id === issueDetails.created_by} />
{workspaceSlug && projectId && ( )}
) : (
)} ); });