import { useEffect, useState } from "react"; import { useRouter } from "next/router"; import { mutate } from "swr"; // react-datepicker import DatePicker from "react-datepicker"; // headless ui import { Popover } from "@headlessui/react"; // contexts import { useProjectMyMembership } from "contexts/project-member.context"; // services import inboxServices from "services/inbox.service"; // hooks import useInboxView from "hooks/use-inbox-view"; import useUserAuth from "hooks/use-user-auth"; import useToast from "hooks/use-toast"; // components import { AcceptIssueModal, DeclineIssueModal, DeleteIssueModal, FiltersDropdown, SelectDuplicateInboxIssueModal, } from "components/inbox"; // ui import { PrimaryButton, SecondaryButton } from "components/ui"; // icons import { InboxIcon, StackedLayersHorizontalIcon } from "components/icons"; import { ChevronDownIcon, ChevronUpIcon, CheckCircleIcon, ClockIcon, XCircleIcon, TrashIcon, } from "@heroicons/react/24/outline"; // types import type { IInboxIssueDetail, TInboxStatus } from "types"; // fetch-keys import { INBOX_ISSUE_DETAILS } from "constants/fetch-keys"; export const InboxActionHeader = () => { const [date, setDate] = useState(new Date()); const [selectDuplicateIssue, setSelectDuplicateIssue] = useState(false); const [acceptIssueModal, setAcceptIssueModal] = useState(false); const [declineIssueModal, setDeclineIssueModal] = useState(false); const [deleteIssueModal, setDeleteIssueModal] = useState(false); const router = useRouter(); const { workspaceSlug, projectId, inboxId, inboxIssueId } = router.query; const { user } = useUserAuth(); const { memberRole } = useProjectMyMembership(); const { issues: inboxIssues, mutate: mutateInboxIssues } = useInboxView(); const { setToastAlert } = useToast(); const markInboxStatus = async (data: TInboxStatus) => { if (!workspaceSlug || !projectId || !inboxId || !inboxIssueId) return; mutate( INBOX_ISSUE_DETAILS(inboxId as string, inboxIssueId as string), (prevData) => { if (!prevData) return prevData; return { ...prevData, issue_inbox: [{ ...prevData.issue_inbox[0], ...data }], }; }, false ); mutateInboxIssues( (prevData: any) => (prevData ?? []).map((i: any) => i.bridge_id === inboxIssueId ? { ...i, issue_inbox: [{ ...i.issue_inbox[0], ...data }] } : i ), false ); await inboxServices .markInboxStatus( workspaceSlug.toString(), projectId.toString(), inboxId.toString(), inboxIssues?.find((inboxIssue) => inboxIssue.bridge_id === inboxIssueId)?.bridge_id!, data, user ) .catch(() => setToastAlert({ type: "error", title: "Error!", message: "Something went wrong while updating inbox status. Please try again.", }) ) .finally(() => { mutate(INBOX_ISSUE_DETAILS(inboxId as string, inboxIssueId as string)); mutateInboxIssues(); }); }; const issue = inboxIssues?.find((issue) => issue.bridge_id === inboxIssueId); const currentIssueIndex = inboxIssues?.findIndex((issue) => issue.bridge_id === inboxIssueId) ?? 0; useEffect(() => { if (!issue?.issue_inbox[0].snoozed_till) return; setDate(new Date(issue.issue_inbox[0].snoozed_till)); }, [issue]); const issueStatus = issue?.issue_inbox[0].status; const isAllowed = memberRole.isMember || memberRole.isOwner; const today = new Date(); const tomorrow = new Date(today); tomorrow.setDate(today.getDate() + 1); return ( <> setSelectDuplicateIssue(false)} value={ inboxIssues?.find((inboxIssue) => inboxIssue.bridge_id === inboxIssueId)?.issue_inbox[0] .duplicate_to } onSubmit={(dupIssueId: string) => { markInboxStatus({ status: 2, duplicate_to: dupIssueId, }).finally(() => setSelectDuplicateIssue(false)); }} /> setAcceptIssueModal(false)} data={inboxIssues?.find((i) => i.bridge_id === inboxIssueId)} onSubmit={async () => { await markInboxStatus({ status: 1, }).finally(() => setAcceptIssueModal(false)); }} /> setDeclineIssueModal(false)} data={inboxIssues?.find((i) => i.bridge_id === inboxIssueId)} onSubmit={async () => { await markInboxStatus({ status: -1, }).finally(() => setDeclineIssueModal(false)); }} /> setDeleteIssueModal(false)} data={inboxIssues?.find((i) => i.bridge_id === inboxIssueId)} />

Inbox

{inboxIssueId && (
{currentIssueIndex + 1}/{inboxIssues?.length ?? 0}
{isAllowed && (issueStatus === 0 || issueStatus === -2) && (
Snooze {({ close }) => (
{ if (!val) return; setDate(val); }} dateFormat="dd-MM-yyyy" minDate={tomorrow} inline /> { close(); markInboxStatus({ status: 0, snoozed_till: new Date(date), }); }} > Snooze
)}
)} {isAllowed && issueStatus === -2 && (
setSelectDuplicateIssue(true)} > Mark as duplicate
)} {isAllowed && (issueStatus === 0 || issueStatus === -2) && (
setAcceptIssueModal(true)} > Accept
)} {isAllowed && issueStatus === -2 && (
setDeclineIssueModal(true)} > Decline
)} {(isAllowed || user?.id === issue?.created_by) && (
setDeleteIssueModal(true)} > Delete
)}
)}
); };