2024-01-25 08:11:02 +00:00
|
|
|
import { FC, useCallback, useEffect, useMemo, useState } from "react";
|
2024-01-24 15:03:54 +00:00
|
|
|
import { observer } from "mobx-react-lite";
|
2024-03-06 13:09:14 +00:00
|
|
|
import { useRouter } from "next/router";
|
2024-02-21 14:25:18 +00:00
|
|
|
import { DayPicker } from "react-day-picker";
|
2024-01-24 15:03:54 +00:00
|
|
|
import { Popover } from "@headlessui/react";
|
2024-03-06 13:09:14 +00:00
|
|
|
// icons
|
|
|
|
import { CheckCircle2, ChevronDown, ChevronUp, Clock, FileStack, Trash2, XCircle } from "lucide-react";
|
|
|
|
// ui
|
|
|
|
import { Button, TOAST_TYPE, setToast } from "@plane/ui";
|
2024-01-24 15:03:54 +00:00
|
|
|
// components
|
|
|
|
import {
|
|
|
|
AcceptIssueModal,
|
|
|
|
DeclineIssueModal,
|
|
|
|
DeleteInboxIssueModal,
|
|
|
|
SelectDuplicateInboxIssueModal,
|
|
|
|
} from "components/inbox";
|
2024-03-06 13:09:14 +00:00
|
|
|
import { ISSUE_DELETED } from "constants/event-tracker";
|
|
|
|
import { EUserProjectRoles } from "constants/project";
|
|
|
|
// hooks
|
|
|
|
import { useUser, useInboxIssues, useIssueDetail, useWorkspace, useEventTracker } from "hooks/store";
|
2024-01-24 15:03:54 +00:00
|
|
|
// types
|
2024-03-06 08:54:36 +00:00
|
|
|
import type { TInboxDetailedStatus } from "@plane/types";
|
2024-03-15 10:37:20 +00:00
|
|
|
//helpers
|
|
|
|
import { getDate } from "helpers/date-time.helper";
|
2024-01-24 15:03:54 +00:00
|
|
|
|
|
|
|
type TInboxIssueActionsHeader = {
|
|
|
|
workspaceSlug: string;
|
|
|
|
projectId: string;
|
|
|
|
inboxId: string;
|
|
|
|
inboxIssueId: string | undefined;
|
|
|
|
};
|
|
|
|
|
|
|
|
type TInboxIssueOperations = {
|
2024-03-06 08:54:36 +00:00
|
|
|
updateInboxIssueStatus: (data: TInboxDetailedStatus) => Promise<void>;
|
2024-01-24 15:03:54 +00:00
|
|
|
removeInboxIssue: () => Promise<void>;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const InboxIssueActionsHeader: FC<TInboxIssueActionsHeader> = observer((props) => {
|
|
|
|
const { workspaceSlug, projectId, inboxId, inboxIssueId } = props;
|
|
|
|
// router
|
|
|
|
const router = useRouter();
|
|
|
|
// hooks
|
2024-02-05 07:49:07 +00:00
|
|
|
const { captureIssueEvent } = useEventTracker();
|
2024-01-24 15:03:54 +00:00
|
|
|
const { currentWorkspace } = useWorkspace();
|
|
|
|
const {
|
|
|
|
issues: { getInboxIssuesByInboxId, getInboxIssueByIssueId, updateInboxIssueStatus, removeInboxIssue },
|
|
|
|
} = useInboxIssues();
|
|
|
|
const {
|
|
|
|
issue: { getIssueById },
|
|
|
|
} = useIssueDetail();
|
|
|
|
const {
|
|
|
|
currentUser,
|
|
|
|
membership: { currentProjectRole },
|
|
|
|
} = useUser();
|
|
|
|
|
|
|
|
// states
|
|
|
|
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);
|
|
|
|
|
|
|
|
// derived values
|
|
|
|
const inboxIssues = getInboxIssuesByInboxId(inboxId);
|
|
|
|
const issueStatus = (inboxIssueId && inboxId && getInboxIssueByIssueId(inboxId, inboxIssueId)) || undefined;
|
|
|
|
const issue = (inboxIssueId && getIssueById(inboxIssueId)) || undefined;
|
|
|
|
|
|
|
|
const currentIssueIndex = inboxIssues?.findIndex((issue) => issue === inboxIssueId) ?? 0;
|
|
|
|
|
|
|
|
const inboxIssueOperations: TInboxIssueOperations = useMemo(
|
|
|
|
() => ({
|
|
|
|
updateInboxIssueStatus: async (data: TInboxDetailedStatus) => {
|
|
|
|
try {
|
|
|
|
if (!workspaceSlug || !projectId || !inboxId || !inboxIssueId) throw new Error("Missing required parameters");
|
|
|
|
await updateInboxIssueStatus(workspaceSlug, projectId, inboxId, inboxIssueId, data);
|
|
|
|
} catch (error) {
|
2024-03-06 08:48:41 +00:00
|
|
|
setToast({
|
|
|
|
type: TOAST_TYPE.ERROR,
|
2024-01-24 15:03:54 +00:00
|
|
|
title: "Error!",
|
|
|
|
message: "Something went wrong while updating inbox status. Please try again.",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
removeInboxIssue: async () => {
|
|
|
|
try {
|
|
|
|
if (!workspaceSlug || !projectId || !inboxId || !inboxIssueId || !currentWorkspace)
|
|
|
|
throw new Error("Missing required parameters");
|
|
|
|
await removeInboxIssue(workspaceSlug, projectId, inboxId, inboxIssueId);
|
2024-02-05 07:49:07 +00:00
|
|
|
captureIssueEvent({
|
2024-02-09 10:52:08 +00:00
|
|
|
eventName: ISSUE_DELETED,
|
2024-02-05 07:49:07 +00:00
|
|
|
payload: {
|
|
|
|
id: inboxIssueId,
|
2024-01-24 15:03:54 +00:00
|
|
|
state: "SUCCESS",
|
2024-02-05 07:49:07 +00:00
|
|
|
element: "Inbox page",
|
2024-02-27 11:28:46 +00:00
|
|
|
},
|
2024-02-05 07:49:07 +00:00
|
|
|
});
|
2024-01-24 15:03:54 +00:00
|
|
|
router.push({
|
|
|
|
pathname: `/${workspaceSlug}/projects/${projectId}/inbox/${inboxId}`,
|
|
|
|
});
|
|
|
|
} catch (error) {
|
2024-03-06 08:48:41 +00:00
|
|
|
setToast({
|
|
|
|
type: TOAST_TYPE.ERROR,
|
2024-01-24 15:03:54 +00:00
|
|
|
title: "Error!",
|
|
|
|
message: "Something went wrong while deleting inbox issue. Please try again.",
|
|
|
|
});
|
2024-02-05 07:49:07 +00:00
|
|
|
captureIssueEvent({
|
2024-02-09 10:52:08 +00:00
|
|
|
eventName: ISSUE_DELETED,
|
2024-02-05 07:49:07 +00:00
|
|
|
payload: {
|
|
|
|
id: inboxIssueId,
|
2024-01-24 15:03:54 +00:00
|
|
|
state: "FAILED",
|
2024-02-05 07:49:07 +00:00
|
|
|
element: "Inbox page",
|
2024-01-24 15:03:54 +00:00
|
|
|
},
|
2024-02-05 07:49:07 +00:00
|
|
|
});
|
2024-01-24 15:03:54 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
[
|
|
|
|
currentWorkspace,
|
|
|
|
workspaceSlug,
|
|
|
|
projectId,
|
|
|
|
inboxId,
|
|
|
|
inboxIssueId,
|
|
|
|
updateInboxIssueStatus,
|
|
|
|
removeInboxIssue,
|
2024-02-05 07:49:07 +00:00
|
|
|
captureIssueEvent,
|
2024-01-24 15:03:54 +00:00
|
|
|
router,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2024-01-25 08:11:02 +00:00
|
|
|
const handleInboxIssueNavigation = useCallback(
|
|
|
|
(direction: "next" | "prev") => {
|
|
|
|
if (!inboxIssues || !inboxIssueId) return;
|
2024-03-01 11:59:30 +00:00
|
|
|
const activeElement = document.activeElement as HTMLElement;
|
|
|
|
if (activeElement && (activeElement.classList.contains("tiptap") || activeElement.id === "title-input")) return;
|
2024-01-25 08:11:02 +00:00
|
|
|
const nextIssueIndex =
|
|
|
|
direction === "next"
|
|
|
|
? (currentIssueIndex + 1) % inboxIssues.length
|
|
|
|
: (currentIssueIndex - 1 + inboxIssues.length) % inboxIssues.length;
|
|
|
|
const nextIssueId = inboxIssues[nextIssueIndex];
|
|
|
|
if (!nextIssueId) return;
|
|
|
|
router.push({
|
|
|
|
pathname: `/${workspaceSlug}/projects/${projectId}/inbox/${inboxId}`,
|
|
|
|
query: {
|
|
|
|
inboxIssueId: nextIssueId,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
|
|
|
[workspaceSlug, projectId, inboxId, inboxIssues, inboxIssueId, currentIssueIndex, router]
|
|
|
|
);
|
|
|
|
|
|
|
|
const onKeyDown = useCallback(
|
|
|
|
(e: KeyboardEvent) => {
|
|
|
|
if (e.key === "ArrowUp") {
|
|
|
|
handleInboxIssueNavigation("prev");
|
|
|
|
} else if (e.key === "ArrowDown") {
|
|
|
|
handleInboxIssueNavigation("next");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[handleInboxIssueNavigation]
|
|
|
|
);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
document.addEventListener("keydown", onKeyDown);
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
document.removeEventListener("keydown", onKeyDown);
|
|
|
|
};
|
|
|
|
}, [onKeyDown]);
|
|
|
|
|
2024-01-24 15:03:54 +00:00
|
|
|
const isAllowed = !!currentProjectRole && currentProjectRole >= EUserProjectRoles.MEMBER;
|
|
|
|
|
|
|
|
const today = new Date();
|
2024-03-15 10:37:20 +00:00
|
|
|
const tomorrow = getDate(today);
|
|
|
|
tomorrow?.setDate(today.getDate() + 1);
|
2024-01-24 15:03:54 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (!issueStatus || !issueStatus.snoozed_till) return;
|
2024-03-15 10:37:20 +00:00
|
|
|
setDate(issueStatus.snoozed_till);
|
2024-01-24 15:03:54 +00:00
|
|
|
}, [issueStatus]);
|
|
|
|
|
|
|
|
if (!issueStatus || !issue || !inboxIssues) return <></>;
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{issue && (
|
|
|
|
<>
|
|
|
|
<SelectDuplicateInboxIssueModal
|
|
|
|
isOpen={selectDuplicateIssue}
|
|
|
|
onClose={() => setSelectDuplicateIssue(false)}
|
|
|
|
value={issueStatus.duplicate_to}
|
|
|
|
onSubmit={(dupIssueId) => {
|
|
|
|
inboxIssueOperations
|
|
|
|
.updateInboxIssueStatus({
|
|
|
|
status: 2,
|
|
|
|
duplicate_to: dupIssueId,
|
|
|
|
})
|
|
|
|
.finally(() => setSelectDuplicateIssue(false));
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
|
|
|
|
<AcceptIssueModal
|
|
|
|
data={issue}
|
|
|
|
isOpen={acceptIssueModal}
|
|
|
|
onClose={() => setAcceptIssueModal(false)}
|
|
|
|
onSubmit={async () => {
|
|
|
|
await inboxIssueOperations
|
|
|
|
.updateInboxIssueStatus({
|
|
|
|
status: 1,
|
|
|
|
})
|
|
|
|
.finally(() => setAcceptIssueModal(false));
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
|
|
|
|
<DeclineIssueModal
|
|
|
|
data={issue}
|
|
|
|
isOpen={declineIssueModal}
|
|
|
|
onClose={() => setDeclineIssueModal(false)}
|
|
|
|
onSubmit={async () => {
|
|
|
|
await inboxIssueOperations
|
|
|
|
.updateInboxIssueStatus({
|
|
|
|
status: -1,
|
|
|
|
})
|
|
|
|
.finally(() => setDeclineIssueModal(false));
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
|
|
|
|
<DeleteInboxIssueModal
|
|
|
|
data={issue}
|
|
|
|
isOpen={deleteIssueModal}
|
|
|
|
onClose={() => setDeleteIssueModal(false)}
|
|
|
|
onSubmit={async () => {
|
|
|
|
await inboxIssueOperations.removeInboxIssue().finally(() => setDeclineIssueModal(false));
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
|
|
|
|
{inboxIssueId && (
|
2024-03-06 13:09:14 +00:00
|
|
|
<div className="relative flex h-full w-full items-center justify-between gap-2 px-4">
|
2024-01-24 15:03:54 +00:00
|
|
|
<div className="flex items-center gap-x-2">
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
className="rounded border border-custom-border-200 bg-custom-background-90 p-1.5 hover:bg-custom-background-80"
|
2024-01-25 08:11:02 +00:00
|
|
|
onClick={() => handleInboxIssueNavigation("prev")}
|
2024-01-24 15:03:54 +00:00
|
|
|
>
|
|
|
|
<ChevronUp size={14} strokeWidth={2} />
|
|
|
|
</button>
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
className="rounded border border-custom-border-200 bg-custom-background-90 p-1.5 hover:bg-custom-background-80"
|
2024-01-25 08:11:02 +00:00
|
|
|
onClick={() => handleInboxIssueNavigation("next")}
|
2024-01-24 15:03:54 +00:00
|
|
|
>
|
|
|
|
<ChevronDown size={14} strokeWidth={2} />
|
|
|
|
</button>
|
|
|
|
<div className="text-sm">
|
|
|
|
{currentIssueIndex + 1}/{inboxIssues?.length ?? 0}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="flex flex-wrap items-center gap-3">
|
|
|
|
{isAllowed && (issueStatus.status === 0 || issueStatus.status === -2) && (
|
|
|
|
<div className="flex-shrink-0">
|
|
|
|
<Popover className="relative">
|
|
|
|
<Popover.Button as="button" type="button">
|
|
|
|
<Button variant="neutral-primary" prependIcon={<Clock size={14} strokeWidth={2} />} size="sm">
|
|
|
|
Snooze
|
|
|
|
</Button>
|
|
|
|
</Popover.Button>
|
|
|
|
<Popover.Panel className="absolute right-0 z-10 mt-2 w-80 rounded-md bg-custom-background-100 p-2 shadow-lg">
|
|
|
|
{({ close }) => (
|
|
|
|
<div className="flex h-full w-full flex-col gap-y-1">
|
2024-02-21 14:25:18 +00:00
|
|
|
<DayPicker
|
2024-03-15 10:37:20 +00:00
|
|
|
selected={date ? getDate(date) : undefined}
|
|
|
|
defaultMonth={date ? getDate(date) : undefined}
|
2024-02-27 11:28:46 +00:00
|
|
|
onSelect={(date) => {
|
|
|
|
if (!date) return;
|
|
|
|
setDate(date);
|
|
|
|
}}
|
2024-02-21 14:25:18 +00:00
|
|
|
mode="single"
|
2024-03-06 13:09:14 +00:00
|
|
|
className="rounded-md border border-custom-border-200 p-3"
|
2024-03-15 10:37:20 +00:00
|
|
|
disabled={
|
|
|
|
tomorrow
|
|
|
|
? [
|
|
|
|
{
|
|
|
|
before: tomorrow,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
: undefined
|
|
|
|
}
|
2024-01-24 15:03:54 +00:00
|
|
|
/>
|
|
|
|
<Button
|
|
|
|
variant="primary"
|
|
|
|
onClick={() => {
|
|
|
|
close();
|
|
|
|
inboxIssueOperations.updateInboxIssueStatus({
|
|
|
|
status: 0,
|
2024-03-15 10:37:20 +00:00
|
|
|
snoozed_till: date,
|
2024-01-24 15:03:54 +00:00
|
|
|
});
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
Snooze
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</Popover.Panel>
|
|
|
|
</Popover>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
|
|
|
|
{isAllowed && issueStatus.status === -2 && (
|
|
|
|
<div className="flex-shrink-0">
|
|
|
|
<Button
|
|
|
|
variant="neutral-primary"
|
|
|
|
size="sm"
|
|
|
|
prependIcon={<FileStack size={14} strokeWidth={2} />}
|
|
|
|
onClick={() => setSelectDuplicateIssue(true)}
|
|
|
|
>
|
|
|
|
Mark as duplicate
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
|
|
|
|
{isAllowed && (issueStatus.status === 0 || issueStatus.status === -2) && (
|
|
|
|
<div className="flex-shrink-0">
|
|
|
|
<Button
|
|
|
|
variant="neutral-primary"
|
|
|
|
size="sm"
|
|
|
|
prependIcon={<CheckCircle2 className="text-green-500" size={14} strokeWidth={2} />}
|
|
|
|
onClick={() => setAcceptIssueModal(true)}
|
|
|
|
>
|
|
|
|
Accept
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
|
|
|
|
{isAllowed && issueStatus.status === -2 && (
|
|
|
|
<div className="flex-shrink-0">
|
|
|
|
<Button
|
|
|
|
variant="neutral-primary"
|
|
|
|
size="sm"
|
|
|
|
prependIcon={<XCircle className="text-red-500" size={14} strokeWidth={2} />}
|
|
|
|
onClick={() => setDeclineIssueModal(true)}
|
|
|
|
>
|
|
|
|
Decline
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
|
|
|
|
{(isAllowed || currentUser?.id === issue?.created_by) && (
|
|
|
|
<div className="flex-shrink-0">
|
|
|
|
<Button
|
|
|
|
variant="neutral-primary"
|
|
|
|
size="sm"
|
|
|
|
prependIcon={<Trash2 className="text-red-500" size={14} strokeWidth={2} />}
|
|
|
|
onClick={() => setDeleteIssueModal(true)}
|
|
|
|
>
|
|
|
|
Delete
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
});
|