2023-06-16 13:27:17 +00:00
|
|
|
import { useEffect, useState } from "react";
|
|
|
|
|
|
|
|
import { useRouter } from "next/router";
|
|
|
|
|
2023-06-23 07:49:26 +00:00
|
|
|
import { mutate } from "swr";
|
|
|
|
|
2023-06-16 13:27:17 +00:00
|
|
|
// react-datepicker
|
|
|
|
import DatePicker from "react-datepicker";
|
|
|
|
// headless ui
|
|
|
|
import { Popover } from "@headlessui/react";
|
|
|
|
// contexts
|
|
|
|
import { useProjectMyMembership } from "contexts/project-member.context";
|
2023-06-23 07:49:26 +00:00
|
|
|
// services
|
|
|
|
import inboxServices from "services/inbox.service";
|
2023-06-16 13:27:17 +00:00
|
|
|
// hooks
|
|
|
|
import useInboxView from "hooks/use-inbox-view";
|
|
|
|
import useUserAuth from "hooks/use-user-auth";
|
2023-06-23 07:49:26 +00:00
|
|
|
import useToast from "hooks/use-toast";
|
2023-06-16 13:27:17 +00:00
|
|
|
// components
|
2023-06-23 07:49:26 +00:00
|
|
|
import {
|
2023-06-23 14:00:53 +00:00
|
|
|
AcceptIssueModal,
|
2023-06-23 07:49:26 +00:00
|
|
|
DeclineIssueModal,
|
|
|
|
DeleteIssueModal,
|
|
|
|
FiltersDropdown,
|
|
|
|
SelectDuplicateInboxIssueModal,
|
|
|
|
} from "components/inbox";
|
2023-06-16 13:27:17 +00:00
|
|
|
// 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
|
2023-06-23 07:49:26 +00:00
|
|
|
import type { IInboxIssueDetail, TInboxStatus } from "types";
|
|
|
|
// fetch-keys
|
|
|
|
import { INBOX_ISSUE_DETAILS } from "constants/fetch-keys";
|
2023-06-16 13:27:17 +00:00
|
|
|
|
2023-06-23 07:49:26 +00:00
|
|
|
export const InboxActionHeader = () => {
|
2023-06-16 13:27:17 +00:00
|
|
|
const [date, setDate] = useState(new Date());
|
2023-06-23 07:49:26 +00:00
|
|
|
const [selectDuplicateIssue, setSelectDuplicateIssue] = useState(false);
|
2023-06-23 14:00:53 +00:00
|
|
|
const [acceptIssueModal, setAcceptIssueModal] = useState(false);
|
2023-06-23 07:49:26 +00:00
|
|
|
const [declineIssueModal, setDeclineIssueModal] = useState(false);
|
|
|
|
const [deleteIssueModal, setDeleteIssueModal] = useState(false);
|
2023-06-16 13:27:17 +00:00
|
|
|
|
|
|
|
const router = useRouter();
|
2023-06-23 07:49:26 +00:00
|
|
|
const { workspaceSlug, projectId, inboxId, inboxIssueId } = router.query;
|
2023-06-16 13:27:17 +00:00
|
|
|
|
|
|
|
const { user } = useUserAuth();
|
2023-06-23 07:49:26 +00:00
|
|
|
const { memberRole } = useProjectMyMembership();
|
|
|
|
const { issues: inboxIssues, mutate: mutateInboxIssues } = useInboxView();
|
|
|
|
const { setToastAlert } = useToast();
|
|
|
|
|
|
|
|
const markInboxStatus = async (data: TInboxStatus) => {
|
|
|
|
if (!workspaceSlug || !projectId || !inboxId || !inboxIssueId) return;
|
|
|
|
|
|
|
|
mutate<IInboxIssueDetail>(
|
|
|
|
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) =>
|
|
|
|
(prevData ?? []).map((i) =>
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
};
|
2023-06-16 13:27:17 +00:00
|
|
|
|
2023-06-23 07:49:26 +00:00
|
|
|
const issue = inboxIssues?.find((issue) => issue.bridge_id === inboxIssueId);
|
|
|
|
const currentIssueIndex =
|
|
|
|
inboxIssues?.findIndex((issue) => issue.bridge_id === inboxIssueId) ?? 0;
|
|
|
|
|
2023-06-16 13:27:17 +00:00
|
|
|
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;
|
|
|
|
|
2023-06-21 11:40:52 +00:00
|
|
|
const today = new Date();
|
|
|
|
const tomorrow = new Date(today);
|
|
|
|
|
|
|
|
tomorrow.setDate(today.getDate() + 1);
|
|
|
|
|
2023-06-16 13:27:17 +00:00
|
|
|
return (
|
2023-06-23 07:49:26 +00:00
|
|
|
<>
|
|
|
|
<SelectDuplicateInboxIssueModal
|
|
|
|
isOpen={selectDuplicateIssue}
|
|
|
|
onClose={() => 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));
|
|
|
|
}}
|
|
|
|
/>
|
2023-06-23 14:00:53 +00:00
|
|
|
<AcceptIssueModal
|
|
|
|
isOpen={acceptIssueModal}
|
|
|
|
handleClose={() => setAcceptIssueModal(false)}
|
|
|
|
data={inboxIssues?.find((i) => i.bridge_id === inboxIssueId)}
|
|
|
|
onSubmit={async () => {
|
|
|
|
await markInboxStatus({
|
|
|
|
status: 1,
|
|
|
|
}).finally(() => setAcceptIssueModal(false));
|
|
|
|
}}
|
|
|
|
/>
|
2023-06-23 07:49:26 +00:00
|
|
|
<DeclineIssueModal
|
|
|
|
isOpen={declineIssueModal}
|
|
|
|
handleClose={() => setDeclineIssueModal(false)}
|
|
|
|
data={inboxIssues?.find((i) => i.bridge_id === inboxIssueId)}
|
|
|
|
onSubmit={async () => {
|
|
|
|
await markInboxStatus({
|
|
|
|
status: -1,
|
|
|
|
}).finally(() => setDeclineIssueModal(false));
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<DeleteIssueModal
|
|
|
|
isOpen={deleteIssueModal}
|
|
|
|
handleClose={() => setDeleteIssueModal(false)}
|
|
|
|
data={inboxIssues?.find((i) => i.bridge_id === inboxIssueId)}
|
|
|
|
/>
|
2023-07-17 07:30:44 +00:00
|
|
|
<div className="grid grid-cols-4 border-b border-custom-border-300 divide-x divide-custom-border-300">
|
2023-06-23 07:49:26 +00:00
|
|
|
<div className="col-span-1 flex justify-between p-4">
|
|
|
|
<div className="flex items-center gap-2">
|
2023-07-10 07:17:00 +00:00
|
|
|
<InboxIcon className="h-4 w-4 text-custom-text-200" />
|
2023-06-23 07:49:26 +00:00
|
|
|
<h3 className="font-medium">Inbox</h3>
|
2023-06-16 13:27:17 +00:00
|
|
|
</div>
|
2023-06-23 07:49:26 +00:00
|
|
|
<FiltersDropdown />
|
|
|
|
</div>
|
|
|
|
{inboxIssueId && (
|
|
|
|
<div className="flex justify-between items-center gap-4 px-4 col-span-3">
|
|
|
|
<div className="flex items-center gap-x-2">
|
|
|
|
<button
|
|
|
|
type="button"
|
2023-07-17 07:30:44 +00:00
|
|
|
className="rounded border border-custom-border-300 bg-custom-background-90 p-1.5 hover:bg-custom-background-80"
|
2023-06-23 07:49:26 +00:00
|
|
|
onClick={() => {
|
|
|
|
const e = new KeyboardEvent("keydown", { key: "ArrowUp" });
|
|
|
|
document.dispatchEvent(e);
|
|
|
|
}}
|
2023-06-21 11:40:52 +00:00
|
|
|
>
|
2023-06-23 07:49:26 +00:00
|
|
|
<ChevronUpIcon className="h-3.5 w-3.5" />
|
|
|
|
</button>
|
|
|
|
<button
|
|
|
|
type="button"
|
2023-07-17 07:30:44 +00:00
|
|
|
className="rounded border border-custom-border-300 bg-custom-background-90 p-1.5 hover:bg-custom-background-80"
|
2023-06-23 07:49:26 +00:00
|
|
|
onClick={() => {
|
|
|
|
const e = new KeyboardEvent("keydown", { key: "ArrowDown" });
|
|
|
|
document.dispatchEvent(e);
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<ChevronDownIcon className="h-3.5 w-3.5" />
|
|
|
|
</button>
|
|
|
|
<div className="text-sm">
|
|
|
|
{currentIssueIndex + 1}/{inboxIssues?.length ?? 0}
|
2023-06-16 13:27:17 +00:00
|
|
|
</div>
|
2023-06-23 07:49:26 +00:00
|
|
|
</div>
|
|
|
|
<div className="flex items-center gap-3 flex-wrap">
|
|
|
|
{isAllowed && (issueStatus === 0 || issueStatus === -2) && (
|
|
|
|
<div className="flex-shrink-0">
|
|
|
|
<Popover className="relative">
|
|
|
|
<Popover.Button as="button" type="button">
|
|
|
|
<SecondaryButton className="flex gap-x-1 items-center" size="sm">
|
2023-07-10 07:17:00 +00:00
|
|
|
<ClockIcon className="h-4 w-4 text-custom-text-200" />
|
2023-06-23 07:49:26 +00:00
|
|
|
<span>Snooze</span>
|
|
|
|
</SecondaryButton>
|
|
|
|
</Popover.Button>
|
2023-07-17 07:30:44 +00:00
|
|
|
<Popover.Panel className="w-80 p-2 absolute right-0 z-10 mt-2 rounded-md border border-custom-border-300 bg-custom-background-80 shadow-lg">
|
2023-06-23 07:49:26 +00:00
|
|
|
{({ close }) => (
|
|
|
|
<div className="w-full h-full flex flex-col gap-y-1">
|
|
|
|
<DatePicker
|
|
|
|
selected={date ? new Date(date) : null}
|
|
|
|
onChange={(val) => {
|
|
|
|
if (!val) return;
|
|
|
|
setDate(val);
|
|
|
|
}}
|
|
|
|
dateFormat="dd-MM-yyyy"
|
|
|
|
minDate={tomorrow}
|
|
|
|
inline
|
|
|
|
/>
|
|
|
|
<PrimaryButton
|
|
|
|
className="ml-auto"
|
|
|
|
onClick={() => {
|
|
|
|
close();
|
|
|
|
markInboxStatus({
|
|
|
|
status: 0,
|
|
|
|
snoozed_till: new Date(date),
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
Snooze
|
|
|
|
</PrimaryButton>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</Popover.Panel>
|
|
|
|
</Popover>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
{isAllowed && issueStatus === -2 && (
|
|
|
|
<div className="flex-shrink-0">
|
|
|
|
<SecondaryButton
|
|
|
|
size="sm"
|
|
|
|
className="flex gap-2 items-center"
|
|
|
|
onClick={() => setSelectDuplicateIssue(true)}
|
|
|
|
>
|
2023-07-10 07:17:00 +00:00
|
|
|
<StackedLayersHorizontalIcon className="h-4 w-4 text-custom-text-200" />
|
2023-06-23 07:49:26 +00:00
|
|
|
<span>Mark as duplicate</span>
|
|
|
|
</SecondaryButton>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
{isAllowed && (issueStatus === 0 || issueStatus === -2) && (
|
|
|
|
<div className="flex-shrink-0">
|
|
|
|
<SecondaryButton
|
|
|
|
size="sm"
|
|
|
|
className="flex gap-2 items-center"
|
2023-06-23 14:00:53 +00:00
|
|
|
onClick={() => setAcceptIssueModal(true)}
|
2023-06-23 07:49:26 +00:00
|
|
|
>
|
|
|
|
<CheckCircleIcon className="h-4 w-4 text-green-500" />
|
2023-06-23 14:00:53 +00:00
|
|
|
<span>Accept</span>
|
2023-06-23 07:49:26 +00:00
|
|
|
</SecondaryButton>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
{isAllowed && issueStatus === -2 && (
|
|
|
|
<div className="flex-shrink-0">
|
|
|
|
<SecondaryButton
|
|
|
|
size="sm"
|
|
|
|
className="flex gap-2 items-center"
|
|
|
|
onClick={() => setDeclineIssueModal(true)}
|
|
|
|
>
|
|
|
|
<XCircleIcon className="h-4 w-4 text-red-500" />
|
|
|
|
<span>Decline</span>
|
|
|
|
</SecondaryButton>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
{(isAllowed || user?.id === issue?.created_by) && (
|
|
|
|
<div className="flex-shrink-0">
|
|
|
|
<SecondaryButton
|
|
|
|
size="sm"
|
|
|
|
className="flex gap-2 items-center"
|
|
|
|
onClick={() => setDeleteIssueModal(true)}
|
|
|
|
>
|
|
|
|
<TrashIcon className="h-4 w-4 text-red-500" />
|
|
|
|
<span>Delete</span>
|
|
|
|
</SecondaryButton>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
2023-06-16 13:27:17 +00:00
|
|
|
</div>
|
2023-06-23 07:49:26 +00:00
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</>
|
2023-06-16 13:27:17 +00:00
|
|
|
);
|
|
|
|
};
|