2024-04-11 15:58:59 +00:00
|
|
|
import { observer } from "mobx-react";
|
2024-04-17 09:23:01 +00:00
|
|
|
import { ArchiveRestoreIcon, Clipboard, Copy, Link, Lock, LockOpen } from "lucide-react";
|
2024-04-11 15:58:59 +00:00
|
|
|
// document editor
|
|
|
|
import { EditorReadOnlyRefApi, EditorRefApi } from "@plane/document-editor";
|
|
|
|
// ui
|
|
|
|
import { ArchiveIcon, CustomMenu, TOAST_TYPE, ToggleSwitch, setToast } from "@plane/ui";
|
2024-04-28 19:20:15 +00:00
|
|
|
// constants
|
|
|
|
import { EUserProjectRoles } from "@/constants/project";
|
2024-04-11 15:58:59 +00:00
|
|
|
// helpers
|
2024-04-28 19:20:15 +00:00
|
|
|
import { cn } from "@/helpers/common.helper";
|
2024-04-11 15:58:59 +00:00
|
|
|
import { copyTextToClipboard, copyUrlToClipboard } from "@/helpers/string.helper";
|
|
|
|
// hooks
|
2024-04-29 06:42:33 +00:00
|
|
|
import { useAppRouter , useUser} from "@/hooks/store";
|
2024-04-11 15:58:59 +00:00
|
|
|
// store
|
|
|
|
import { IPageStore } from "@/store/pages/page.store";
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
editorRef: EditorRefApi | EditorReadOnlyRefApi | null;
|
|
|
|
handleDuplicatePage: () => void;
|
|
|
|
pageStore: IPageStore;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const PageOptionsDropdown: React.FC<Props> = observer((props) => {
|
|
|
|
const { editorRef, handleDuplicatePage, pageStore } = props;
|
|
|
|
// store values
|
|
|
|
const {
|
2024-04-17 09:23:01 +00:00
|
|
|
archived_at,
|
|
|
|
is_locked,
|
|
|
|
id,
|
2024-04-11 15:58:59 +00:00
|
|
|
archive,
|
|
|
|
lock,
|
|
|
|
unlock,
|
|
|
|
canCurrentUserArchivePage,
|
|
|
|
canCurrentUserDuplicatePage,
|
|
|
|
canCurrentUserLockPage,
|
|
|
|
restore,
|
|
|
|
view_props,
|
|
|
|
updateViewProps,
|
|
|
|
} = pageStore;
|
|
|
|
// store hooks
|
2024-04-29 06:42:33 +00:00
|
|
|
const { workspaceSlug, projectId } = useAppRouter();
|
2024-04-28 19:20:15 +00:00
|
|
|
const {
|
|
|
|
membership: { currentProjectRole },
|
|
|
|
} = useUser();
|
|
|
|
// auth
|
|
|
|
const isEditingAllowed = !!currentProjectRole && currentProjectRole >= EUserProjectRoles.MEMBER;
|
2024-04-11 15:58:59 +00:00
|
|
|
|
|
|
|
const handleArchivePage = async () =>
|
|
|
|
await archive().catch(() =>
|
|
|
|
setToast({
|
|
|
|
type: TOAST_TYPE.ERROR,
|
|
|
|
title: "Error!",
|
|
|
|
message: "Page could not be archived. Please try again later.",
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
const handleRestorePage = async () =>
|
|
|
|
await restore().catch(() =>
|
|
|
|
setToast({
|
|
|
|
type: TOAST_TYPE.ERROR,
|
|
|
|
title: "Error!",
|
|
|
|
message: "Page could not be restored. Please try again later.",
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
const handleLockPage = async () =>
|
|
|
|
await lock().catch(() =>
|
|
|
|
setToast({
|
|
|
|
type: TOAST_TYPE.ERROR,
|
|
|
|
title: "Error!",
|
|
|
|
message: "Page could not be locked. Please try again later.",
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
const handleUnlockPage = async () =>
|
|
|
|
await unlock().catch(() =>
|
|
|
|
setToast({
|
|
|
|
type: TOAST_TYPE.ERROR,
|
|
|
|
title: "Error!",
|
|
|
|
message: "Page could not be unlocked. Please try again later.",
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
// menu items list
|
|
|
|
const MENU_ITEMS: {
|
|
|
|
key: string;
|
|
|
|
action: () => void;
|
|
|
|
label: string;
|
|
|
|
icon: React.FC<any>;
|
|
|
|
shouldRender: boolean;
|
|
|
|
}[] = [
|
|
|
|
{
|
|
|
|
key: "copy-markdown",
|
|
|
|
action: () => {
|
|
|
|
if (!editorRef) return;
|
|
|
|
copyTextToClipboard(editorRef.getMarkDown()).then(() =>
|
|
|
|
setToast({
|
|
|
|
type: TOAST_TYPE.SUCCESS,
|
|
|
|
title: "Successful!",
|
|
|
|
message: "Markdown copied to clipboard.",
|
|
|
|
})
|
|
|
|
);
|
|
|
|
},
|
|
|
|
label: "Copy markdown",
|
|
|
|
icon: Clipboard,
|
|
|
|
shouldRender: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "copy-page-link",
|
|
|
|
action: () => {
|
2024-04-17 09:23:01 +00:00
|
|
|
copyUrlToClipboard(`${workspaceSlug}/projects/${projectId}/pages/${id}`).then(() =>
|
2024-04-11 15:58:59 +00:00
|
|
|
setToast({
|
|
|
|
type: TOAST_TYPE.SUCCESS,
|
|
|
|
title: "Successful!",
|
|
|
|
message: "Page link copied to clipboard.",
|
|
|
|
})
|
|
|
|
);
|
|
|
|
},
|
|
|
|
label: "Copy page link",
|
|
|
|
icon: Link,
|
|
|
|
shouldRender: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "make-a-copy",
|
|
|
|
action: handleDuplicatePage,
|
|
|
|
label: "Make a copy",
|
|
|
|
icon: Copy,
|
|
|
|
shouldRender: canCurrentUserDuplicatePage,
|
|
|
|
},
|
|
|
|
{
|
2024-04-17 09:23:01 +00:00
|
|
|
key: "lock-unlock-page",
|
|
|
|
action: is_locked ? handleUnlockPage : handleLockPage,
|
|
|
|
label: is_locked ? "Unlock page" : "Lock page",
|
|
|
|
icon: is_locked ? LockOpen : Lock,
|
|
|
|
shouldRender: canCurrentUserLockPage,
|
2024-04-11 15:58:59 +00:00
|
|
|
},
|
|
|
|
{
|
2024-04-17 09:23:01 +00:00
|
|
|
key: "archive-restore-page",
|
|
|
|
action: archived_at ? handleRestorePage : handleArchivePage,
|
|
|
|
label: archived_at ? "Restore page" : "Archive page",
|
|
|
|
icon: archived_at ? ArchiveRestoreIcon : ArchiveIcon,
|
|
|
|
shouldRender: canCurrentUserArchivePage,
|
2024-04-11 15:58:59 +00:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
return (
|
|
|
|
<CustomMenu maxHeight="md" placement="bottom-start" verticalEllipsis closeOnSelect>
|
|
|
|
<CustomMenu.MenuItem
|
2024-04-28 19:22:43 +00:00
|
|
|
className="hidden md:flex w-full items-center justify-between gap-2"
|
2024-04-11 15:58:59 +00:00
|
|
|
onClick={() =>
|
|
|
|
updateViewProps({
|
|
|
|
full_width: !view_props?.full_width,
|
|
|
|
})
|
|
|
|
}
|
2024-04-28 19:20:15 +00:00
|
|
|
disabled={!isEditingAllowed}
|
2024-04-11 15:58:59 +00:00
|
|
|
>
|
|
|
|
Full width
|
2024-04-28 19:20:15 +00:00
|
|
|
<ToggleSwitch
|
|
|
|
value={!!view_props?.full_width}
|
|
|
|
onChange={() => {}}
|
|
|
|
className={cn({
|
|
|
|
"opacity-40": !isEditingAllowed,
|
|
|
|
})}
|
|
|
|
disabled={!isEditingAllowed}
|
|
|
|
/>
|
2024-04-11 15:58:59 +00:00
|
|
|
</CustomMenu.MenuItem>
|
|
|
|
{MENU_ITEMS.map((item) => {
|
|
|
|
if (!item.shouldRender) return null;
|
|
|
|
return (
|
|
|
|
<CustomMenu.MenuItem key={item.key} onClick={item.action} className="flex items-center gap-2">
|
|
|
|
<item.icon className="h-3 w-3" />
|
2024-04-17 09:23:01 +00:00
|
|
|
{item.label}
|
2024-04-11 15:58:59 +00:00
|
|
|
</CustomMenu.MenuItem>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</CustomMenu>
|
|
|
|
);
|
|
|
|
});
|