2024-06-10 09:43:10 +00:00
|
|
|
"use client";
|
|
|
|
|
2024-04-11 15:58:59 +00:00
|
|
|
import { useState } from "react";
|
|
|
|
import { observer } from "mobx-react";
|
|
|
|
import { ArchiveRestoreIcon, ExternalLink, Link, Lock, Trash2, UsersRound } from "lucide-react";
|
2024-04-30 13:29:07 +00:00
|
|
|
import { ArchiveIcon, ContextMenu, CustomMenu, TContextMenuItem, TOAST_TYPE, setToast } from "@plane/ui";
|
2024-04-11 15:58:59 +00:00
|
|
|
// components
|
|
|
|
import { DeletePageModal } from "@/components/pages";
|
|
|
|
// helpers
|
|
|
|
import { copyUrlToClipboard } from "@/helpers/string.helper";
|
2024-06-13 07:15:00 +00:00
|
|
|
// store
|
|
|
|
import { IPageStore } from "@/store/pages/page.store";
|
2024-04-11 15:58:59 +00:00
|
|
|
|
|
|
|
type Props = {
|
2024-06-13 07:15:00 +00:00
|
|
|
page: IPageStore;
|
|
|
|
pageLink: string;
|
2024-04-30 13:29:07 +00:00
|
|
|
parentRef: React.RefObject<HTMLElement>;
|
2024-04-11 15:58:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const PageQuickActions: React.FC<Props> = observer((props) => {
|
2024-06-13 07:15:00 +00:00
|
|
|
const { page, pageLink, parentRef } = props;
|
2024-04-11 15:58:59 +00:00
|
|
|
// states
|
|
|
|
const [deletePageModal, setDeletePageModal] = useState(false);
|
|
|
|
// store hooks
|
2024-04-17 09:23:01 +00:00
|
|
|
const {
|
|
|
|
access,
|
|
|
|
archive,
|
|
|
|
archived_at,
|
|
|
|
makePublic,
|
|
|
|
makePrivate,
|
|
|
|
restore,
|
|
|
|
canCurrentUserArchivePage,
|
|
|
|
canCurrentUserChangeAccess,
|
|
|
|
canCurrentUserDeletePage,
|
2024-06-13 07:15:00 +00:00
|
|
|
} = page;
|
2024-04-11 15:58:59 +00:00
|
|
|
|
|
|
|
const handleCopyText = () =>
|
|
|
|
copyUrlToClipboard(pageLink).then(() => {
|
|
|
|
setToast({
|
|
|
|
type: TOAST_TYPE.SUCCESS,
|
|
|
|
title: "Link Copied!",
|
|
|
|
message: "Page link copied to clipboard.",
|
|
|
|
});
|
|
|
|
});
|
2024-04-17 09:23:01 +00:00
|
|
|
|
2024-04-11 15:58:59 +00:00
|
|
|
const handleOpenInNewTab = () => window.open(`/${pageLink}`, "_blank");
|
|
|
|
|
2024-04-30 13:29:07 +00:00
|
|
|
const MENU_ITEMS: TContextMenuItem[] = [
|
2024-04-17 09:23:01 +00:00
|
|
|
{
|
2024-04-30 13:29:07 +00:00
|
|
|
key: "make-public-private",
|
|
|
|
action: access === 0 ? makePrivate : makePublic,
|
|
|
|
title: access === 0 ? "Make private" : "Make public",
|
|
|
|
icon: access === 0 ? Lock : UsersRound,
|
|
|
|
shouldRender: canCurrentUserChangeAccess && !archived_at,
|
2024-04-17 09:23:01 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "open-new-tab",
|
|
|
|
action: handleOpenInNewTab,
|
2024-04-30 13:29:07 +00:00
|
|
|
title: "Open in new tab",
|
2024-04-17 09:23:01 +00:00
|
|
|
icon: ExternalLink,
|
|
|
|
shouldRender: true,
|
|
|
|
},
|
2024-04-30 13:29:07 +00:00
|
|
|
{
|
|
|
|
key: "copy-link",
|
|
|
|
action: handleCopyText,
|
|
|
|
title: "Copy link",
|
|
|
|
icon: Link,
|
|
|
|
shouldRender: true,
|
|
|
|
},
|
2024-04-17 09:23:01 +00:00
|
|
|
{
|
|
|
|
key: "archive-restore",
|
|
|
|
action: archived_at ? restore : archive,
|
2024-04-30 13:29:07 +00:00
|
|
|
title: archived_at ? "Restore" : "Archive",
|
2024-04-17 09:23:01 +00:00
|
|
|
icon: archived_at ? ArchiveRestoreIcon : ArchiveIcon,
|
|
|
|
shouldRender: canCurrentUserArchivePage,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "delete",
|
|
|
|
action: () => setDeletePageModal(true),
|
2024-04-30 13:29:07 +00:00
|
|
|
title: "Delete",
|
2024-04-17 09:23:01 +00:00
|
|
|
icon: Trash2,
|
|
|
|
shouldRender: canCurrentUserDeletePage && !!archived_at,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2024-04-11 15:58:59 +00:00
|
|
|
return (
|
|
|
|
<>
|
2024-06-13 07:15:00 +00:00
|
|
|
<DeletePageModal isOpen={deletePageModal} onClose={() => setDeletePageModal(false)} pageId={page.id ?? ""} />
|
2024-04-30 13:29:07 +00:00
|
|
|
<ContextMenu parentRef={parentRef} items={MENU_ITEMS} />
|
2024-04-11 15:58:59 +00:00
|
|
|
<CustomMenu placement="bottom-end" ellipsis closeOnSelect>
|
2024-04-17 09:23:01 +00:00
|
|
|
{MENU_ITEMS.map((item) => {
|
|
|
|
if (!item.shouldRender) return null;
|
|
|
|
return (
|
|
|
|
<CustomMenu.MenuItem
|
|
|
|
key={item.key}
|
|
|
|
onClick={(e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
item.action();
|
|
|
|
}}
|
|
|
|
className="flex items-center gap-2"
|
2024-05-02 10:43:58 +00:00
|
|
|
disabled={item.disabled}
|
2024-04-17 09:23:01 +00:00
|
|
|
>
|
2024-04-30 13:29:07 +00:00
|
|
|
{item.icon && <item.icon className="h-3 w-3" />}
|
|
|
|
{item.title}
|
2024-04-17 09:23:01 +00:00
|
|
|
</CustomMenu.MenuItem>
|
|
|
|
);
|
|
|
|
})}
|
2024-04-11 15:58:59 +00:00
|
|
|
</CustomMenu>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
});
|