From 4822261187d0bb95df9797115212e596c7b27d3d Mon Sep 17 00:00:00 2001
From: Hemachandar <132386067+hmacr@users.noreply.github.com>
Date: Sat, 9 Aug 2025 18:11:20 +0530
Subject: [PATCH] chore: Move `DocumentMenu` to Radix (#9879)
* works without custom items in menu
* tooltip support for dropdown button
* fix: menu goes out of bounds
* custom node, lazy render menu for perf
* bug fixes
* fix: useTemplateActions re-computed templates on every render
* fix: hide subscribe/unsubscribe for inactive documents & collections
* cleanup `MenuContext` & `useTemplateMenuItems`
* rename prop, increase dropdown max-height
* jsdoc
---
app/actions/definitions/collections.tsx | 2 +
app/actions/definitions/documents.tsx | 376 +++++++++++---
app/actions/index.ts | 1 +
app/components/DocumentListItem.tsx | 4 -
app/components/Menu/DropdownMenu.tsx | 82 +--
app/components/Menu/transformer.tsx | 1 +
app/components/PaginatedDocumentList.tsx | 1 -
app/components/primitives/DropdownMenu.tsx | 41 +-
app/hooks/useTemplateMenuActions.tsx | 53 +-
app/hooks/useTemplateMenuItems.tsx | 88 ----
app/menus/DocumentMenu.tsx | 547 ++++++---------------
app/menus/MenuContext.tsx | 19 -
app/scenes/Document/components/Header.tsx | 11 +-
app/stores/DocumentsStore.ts | 1 +
app/types.ts | 3 +
shared/i18n/locales/en_US/translation.json | 11 +-
16 files changed, 597 insertions(+), 644 deletions(-)
delete mode 100644 app/hooks/useTemplateMenuItems.tsx
delete mode 100644 app/menus/MenuContext.tsx
diff --git a/app/actions/definitions/collections.tsx b/app/actions/definitions/collections.tsx
index f0bea3ed9b..fdd2252dae 100644
--- a/app/actions/definitions/collections.tsx
+++ b/app/actions/definitions/collections.tsx
@@ -235,6 +235,7 @@ export const subscribeCollection = createActionV2({
const collection = stores.collections.get(activeCollectionId);
return (
+ !!collection?.isActive &&
!collection?.isSubscribed &&
stores.policies.abilities(activeCollectionId).subscribe
);
@@ -265,6 +266,7 @@ export const unsubscribeCollection = createActionV2({
const collection = stores.collections.get(activeCollectionId);
return (
+ !!collection?.isActive &&
!!collection?.isSubscribed &&
stores.policies.abilities(activeCollectionId).unsubscribe
);
diff --git a/app/actions/definitions/documents.tsx b/app/actions/definitions/documents.tsx
index 9ecf55388a..a3df8920eb 100644
--- a/app/actions/definitions/documents.tsx
+++ b/app/actions/definitions/documents.tsx
@@ -31,6 +31,8 @@ import {
GlobeIcon,
LogoutIcon,
CaseSensitiveIcon,
+ RestoreIcon,
+ EditIcon,
} from "outline-icons";
import { toast } from "sonner";
import Icon from "@shared/components/Icon";
@@ -52,7 +54,13 @@ import MarkdownIcon from "~/components/Icons/MarkdownIcon";
import SharePopover from "~/components/Sharing/Document";
import { getHeaderExpandedKey } from "~/components/Sidebar/components/Header";
import DocumentTemplatizeDialog from "~/components/TemplatizeDialog";
-import { createAction, createActionV2 } from "~/actions";
+import {
+ createAction,
+ createActionV2,
+ createActionV2Group,
+ createActionV2WithChildren,
+ createInternalLinkActionV2,
+} from "~/actions";
import {
ActiveDocumentSection,
DocumentSection,
@@ -71,7 +79,11 @@ import {
documentPath,
urlify,
trashPath,
+ documentEditPath,
} from "~/utils/routeHelpers";
+import capitalize from "lodash/capitalize";
+import CollectionIcon from "~/components/Icons/CollectionIcon";
+import { ActionV2, ActionV2Group, ActionV2Separator } from "~/types";
export const openDocument = createAction({
name: ({ t }) => t("Open document"),
@@ -103,6 +115,38 @@ export const openDocument = createAction({
},
});
+export const editDocument = createInternalLinkActionV2({
+ name: ({ t }) => t("Edit"),
+ analyticsName: "Edit document",
+ section: ActiveDocumentSection,
+ keywords: "edit",
+ icon: ,
+ visible: ({ activeDocumentId, stores }) => {
+ const { auth, documents, policies } = stores;
+
+ const document = activeDocumentId
+ ? documents.get(activeDocumentId)
+ : undefined;
+ const can = activeDocumentId
+ ? policies.abilities(activeDocumentId)
+ : undefined;
+
+ return (
+ !!can?.update && !!auth.user?.separateEditMode && !document?.template
+ );
+ },
+ to: ({ activeDocumentId, stores }) => {
+ const document = activeDocumentId
+ ? stores.documents.get(activeDocumentId)
+ : undefined;
+ if (!document) {
+ return "";
+ }
+
+ return documentEditPath(document);
+ },
+});
+
export const createDocument = createAction({
name: ({ t }) => t("New document"),
analyticsName: "New document",
@@ -141,7 +185,7 @@ export const createDraftDocument = createAction({
}),
});
-export const createDocumentFromTemplate = createAction({
+export const createDocumentFromTemplate = createInternalLinkActionV2({
name: ({ t }) => t("New from template"),
analyticsName: "New document",
section: DocumentSection,
@@ -171,16 +215,24 @@ export const createDocumentFromTemplate = createAction({
}
return stores.policies.abilities(currentTeamId).createDocument;
},
- perform: ({ activeCollectionId, activeDocumentId, sidebarContext }) =>
- history.push(
- newDocumentPath(activeCollectionId, { templateId: activeDocumentId }),
- {
- sidebarContext,
- }
- ),
+ to: ({ activeDocumentId, activeCollectionId, sidebarContext }) => {
+ if (!activeDocumentId || !activeCollectionId) {
+ return "";
+ }
+
+ const [pathname, search] = newDocumentPath(activeCollectionId, {
+ templateId: activeDocumentId,
+ }).split("?");
+
+ return {
+ pathname,
+ search,
+ state: { sidebarContext },
+ };
+ },
});
-export const createNestedDocument = createAction({
+export const createNestedDocument = createInternalLinkActionV2({
name: ({ t }) => t("New nested document"),
analyticsName: "New document",
section: ActiveDocumentSection,
@@ -191,13 +243,19 @@ export const createNestedDocument = createAction({
!!activeDocumentId &&
stores.policies.abilities(currentTeamId).createDocument &&
stores.policies.abilities(activeDocumentId).createChildDocument,
- perform: ({ activeDocumentId, sidebarContext }) =>
- history.push(newNestedDocumentPath(activeDocumentId), {
- sidebarContext,
- }),
+ to: ({ activeDocumentId, sidebarContext }) => {
+ const [pathname, search] =
+ newNestedDocumentPath(activeDocumentId).split("?");
+
+ return {
+ pathname,
+ search,
+ state: { sidebarContext },
+ };
+ },
});
-export const starDocument = createAction({
+export const starDocument = createActionV2({
name: ({ t }) => t("Star"),
analyticsName: "Star document",
section: ActiveDocumentSection,
@@ -223,7 +281,7 @@ export const starDocument = createAction({
},
});
-export const unstarDocument = createAction({
+export const unstarDocument = createActionV2({
name: ({ t }) => t("Unstar"),
analyticsName: "Unstar document",
section: ActiveDocumentSection,
@@ -249,7 +307,7 @@ export const unstarDocument = createAction({
},
});
-export const publishDocument = createAction({
+export const publishDocument = createActionV2({
name: ({ t }) => t("Publish"),
analyticsName: "Publish document",
section: ActiveDocumentSection,
@@ -291,7 +349,7 @@ export const publishDocument = createAction({
},
});
-export const unpublishDocument = createAction({
+export const unpublishDocument = createActionV2({
name: ({ t }) => t("Unpublish"),
analyticsName: "Unpublish document",
section: ActiveDocumentSection,
@@ -322,11 +380,27 @@ export const unpublishDocument = createAction({
},
});
-export const subscribeDocument = createAction({
+export const subscribeDocument = createActionV2({
name: ({ t }) => t("Subscribe"),
analyticsName: "Subscribe to document",
section: ActiveDocumentSection,
icon: ,
+ tooltip: ({ activeCollectionId, isContextMenu, stores, t }) => {
+ if (!isContextMenu || !activeCollectionId) {
+ return undefined;
+ }
+
+ return stores.collections.get(activeCollectionId)?.isSubscribed
+ ? t("Subscription inherited from collection")
+ : undefined;
+ },
+ disabled: ({ activeCollectionId, isContextMenu, stores }) => {
+ if (!isContextMenu || !activeCollectionId) {
+ return false;
+ }
+
+ return !!stores.collections.get(activeCollectionId)?.isSubscribed;
+ },
visible: ({ activeDocumentId, stores }) => {
if (!activeDocumentId) {
return false;
@@ -335,6 +409,7 @@ export const subscribeDocument = createAction({
const document = stores.documents.get(activeDocumentId);
return (
+ !!document?.isActive &&
!document?.collection?.isSubscribed &&
!document?.isSubscribed &&
stores.policies.abilities(activeDocumentId).subscribe
@@ -351,11 +426,27 @@ export const subscribeDocument = createAction({
},
});
-export const unsubscribeDocument = createAction({
+export const unsubscribeDocument = createActionV2({
name: ({ t }) => t("Unsubscribe"),
analyticsName: "Unsubscribe from document",
section: ActiveDocumentSection,
icon: ,
+ tooltip: ({ activeCollectionId, isContextMenu, stores, t }) => {
+ if (!isContextMenu || !activeCollectionId) {
+ return undefined;
+ }
+
+ return stores.collections.get(activeCollectionId)?.isSubscribed
+ ? t("Subscription inherited from collection")
+ : undefined;
+ },
+ disabled: ({ activeCollectionId, isContextMenu, stores }) => {
+ if (!isContextMenu || !activeCollectionId) {
+ return false;
+ }
+
+ return !!stores.collections.get(activeCollectionId)?.isSubscribed;
+ },
visible: ({ activeDocumentId, stores }) => {
if (!activeDocumentId) {
return false;
@@ -364,9 +455,10 @@ export const unsubscribeDocument = createAction({
const document = stores.documents.get(activeDocumentId);
return (
- !!document?.collection?.isSubscribed ||
- (!!document?.isSubscribed &&
- stores.policies.abilities(activeDocumentId).unsubscribe)
+ !!document?.isActive &&
+ (!!document?.collection?.isSubscribed ||
+ (!!document?.isSubscribed &&
+ stores.policies.abilities(activeDocumentId).unsubscribe))
);
},
perform: async ({ activeDocumentId, stores, currentUserId, t }) => {
@@ -382,7 +474,7 @@ export const unsubscribeDocument = createAction({
},
});
-export const shareDocument = createAction({
+export const shareDocument = createActionV2({
name: ({ t }) => `${t("Permissions")}…`,
analyticsName: "Share document",
section: ActiveDocumentSection,
@@ -415,7 +507,7 @@ export const shareDocument = createAction({
},
});
-export const downloadDocumentAsHTML = createAction({
+export const downloadDocumentAsHTML = createActionV2({
name: ({ t }) => t("HTML"),
analyticsName: "Download document as HTML",
section: ActiveDocumentSection,
@@ -434,7 +526,7 @@ export const downloadDocumentAsHTML = createAction({
},
});
-export const downloadDocumentAsPDF = createAction({
+export const downloadDocumentAsPDF = createActionV2({
name: ({ t }) => t("PDF"),
analyticsName: "Download document as PDF",
section: ActiveDocumentSection,
@@ -442,9 +534,11 @@ export const downloadDocumentAsPDF = createAction({
icon: ,
iconInContextMenu: false,
visible: ({ activeDocumentId, stores }) =>
- !!activeDocumentId &&
- stores.policies.abilities(activeDocumentId).download &&
- env.PDF_EXPORT_ENABLED,
+ !!(
+ activeDocumentId &&
+ stores.policies.abilities(activeDocumentId).download &&
+ env.PDF_EXPORT_ENABLED
+ ),
perform: ({ activeDocumentId, t, stores }) => {
if (!activeDocumentId) {
return;
@@ -458,7 +552,7 @@ export const downloadDocumentAsPDF = createAction({
},
});
-export const downloadDocumentAsMarkdown = createAction({
+export const downloadDocumentAsMarkdown = createActionV2({
name: ({ t }) => t("Markdown"),
analyticsName: "Download document as Markdown",
section: ActiveDocumentSection,
@@ -477,7 +571,7 @@ export const downloadDocumentAsMarkdown = createAction({
},
});
-export const downloadDocument = createAction({
+export const downloadDocument = createActionV2WithChildren({
name: ({ t, isContextMenu }) =>
isContextMenu ? t("Download") : t("Download document"),
analyticsName: "Download document",
@@ -493,7 +587,7 @@ export const downloadDocument = createAction({
],
});
-export const copyDocumentAsMarkdown = createAction({
+export const copyDocumentAsMarkdown = createActionV2({
name: ({ t }) => t("Copy as Markdown"),
section: ActiveDocumentSection,
keywords: "clipboard",
@@ -512,7 +606,7 @@ export const copyDocumentAsMarkdown = createAction({
},
});
-export const copyDocumentAsPlainText = createAction({
+export const copyDocumentAsPlainText = createActionV2({
name: ({ t }) => t("Copy as text"),
section: ActiveDocumentSection,
keywords: "clipboard",
@@ -531,7 +625,7 @@ export const copyDocumentAsPlainText = createAction({
},
});
-export const copyDocumentShareLink = createAction({
+export const copyDocumentShareLink = createActionV2({
name: ({ t }) => t("Copy public link"),
section: ActiveDocumentSection,
keywords: "clipboard share",
@@ -552,7 +646,7 @@ export const copyDocumentShareLink = createAction({
},
});
-export const copyDocumentLink = createAction({
+export const copyDocumentLink = createActionV2({
name: ({ t }) => t("Copy link"),
section: ActiveDocumentSection,
keywords: "clipboard",
@@ -570,7 +664,7 @@ export const copyDocumentLink = createAction({
},
});
-export const copyDocument = createAction({
+export const copyDocument = createActionV2WithChildren({
name: ({ t }) => t("Copy"),
analyticsName: "Copy document",
section: ActiveDocumentSection,
@@ -584,7 +678,7 @@ export const copyDocument = createAction({
],
});
-export const duplicateDocument = createAction({
+export const duplicateDocument = createActionV2({
name: ({ t, isContextMenu }) =>
isContextMenu ? t("Duplicate") : t("Duplicate document"),
analyticsName: "Duplicate document",
@@ -620,7 +714,7 @@ export const duplicateDocument = createAction({
* Pin a document to a collection. Pinned documents will be displayed at the top
* of the collection for all collection members to see.
*/
-export const pinDocumentToCollection = createAction({
+export const pinDocumentToCollection = createActionV2({
name: ({ activeDocumentId = "", t, stores }) => {
const selectedDocument = stores.documents.get(activeDocumentId);
const collectionName = selectedDocument
@@ -665,7 +759,7 @@ export const pinDocumentToCollection = createAction({
* Pin a document to team home. Pinned documents will be displayed at the top
* of the home screen for all team members to see.
*/
-export const pinDocumentToHome = createAction({
+export const pinDocumentToHome = createActionV2({
name: ({ t }) => t("Pin to home"),
analyticsName: "Pin document to home",
section: ActiveDocumentSection,
@@ -697,7 +791,7 @@ export const pinDocumentToHome = createAction({
},
});
-export const pinDocument = createAction({
+export const pinDocument = createActionV2WithChildren({
name: ({ t }) => t("Pin"),
analyticsName: "Pin document",
section: ActiveDocumentSection,
@@ -705,7 +799,7 @@ export const pinDocument = createAction({
children: [pinDocumentToCollection, pinDocumentToHome],
});
-export const searchInDocument = createAction({
+export const searchInDocument = createInternalLinkActionV2({
name: ({ t }) => t("Search in document"),
analyticsName: "Search document",
section: ActiveDocumentSection,
@@ -718,12 +812,24 @@ export const searchInDocument = createAction({
const document = stores.documents.get(activeDocumentId);
return !!document?.isActive;
},
- perform: ({ activeDocumentId }) => {
- history.push(searchPath({ documentId: activeDocumentId }));
+ to: ({ activeDocumentId, sidebarContext }) => {
+ if (!activeDocumentId) {
+ return "";
+ }
+
+ const [pathname, search] = searchPath({
+ documentId: activeDocumentId,
+ }).split("?");
+
+ return {
+ pathname,
+ search,
+ state: { sidebarContext },
+ };
},
});
-export const printDocument = createAction({
+export const printDocument = createActionV2({
name: ({ t, isContextMenu }) =>
isContextMenu ? t("Print") : t("Print document"),
analyticsName: "Print document",
@@ -735,7 +841,7 @@ export const printDocument = createAction({
},
});
-export const importDocument = createAction({
+export const importDocument = createActionV2({
name: ({ t }) => t("Import document"),
analyticsName: "Import document",
section: DocumentSection,
@@ -782,7 +888,7 @@ export const importDocument = createAction({
},
});
-export const createTemplateFromDocument = createAction({
+export const createTemplateFromDocument = createActionV2({
name: ({ t }) => t("Templatize"),
analyticsName: "Templatize document",
section: ActiveDocumentSection,
@@ -844,7 +950,7 @@ export const searchDocumentsForQuery = (query: string) =>
visible: ({ location }) => location.pathname !== searchPath(),
});
-export const moveTemplateToWorkspace = createAction({
+export const moveTemplateToWorkspace = createActionV2({
name: ({ t }) => t("Move to workspace"),
analyticsName: "Move template to workspace",
section: DocumentSection,
@@ -874,7 +980,7 @@ export const moveTemplateToWorkspace = createAction({
},
});
-export const moveDocumentToCollection = createAction({
+export const moveDocumentToCollection = createActionV2({
name: ({ activeDocumentId, stores, t }) => {
if (!activeDocumentId) {
return t("Move");
@@ -911,7 +1017,7 @@ export const moveDocumentToCollection = createAction({
},
});
-export const moveDocument = createAction({
+export const moveDocument = createActionV2({
name: ({ t }) => t("Move"),
analyticsName: "Move document",
section: ActiveDocumentSection,
@@ -930,7 +1036,7 @@ export const moveDocument = createAction({
perform: moveDocumentToCollection.perform,
});
-export const moveTemplate = createAction({
+export const moveTemplate = createActionV2WithChildren({
name: ({ t }) => t("Move"),
analyticsName: "Move document",
section: ActiveDocumentSection,
@@ -949,7 +1055,7 @@ export const moveTemplate = createAction({
children: [moveTemplateToWorkspace, moveDocumentToCollection],
});
-export const archiveDocument = createAction({
+export const archiveDocument = createActionV2({
name: ({ t }) => `${t("Archive")}…`,
analyticsName: "Archive document",
section: ActiveDocumentSection,
@@ -989,7 +1095,102 @@ export const archiveDocument = createAction({
},
});
-export const deleteDocument = createAction({
+export const restoreDocument = createActionV2({
+ name: ({ t }) => `${t("Restore")}`,
+ analyticsName: "Restore document",
+ section: ActiveDocumentSection,
+ icon: ,
+ visible: ({ activeDocumentId, stores }) => {
+ const document = activeDocumentId
+ ? stores.documents.get(activeDocumentId)
+ : undefined;
+ if (!document) {
+ return false;
+ }
+
+ const collection = document.collectionId
+ ? stores.collections.get(document.collectionId)
+ : undefined;
+ const can = stores.policies.abilities(document.id);
+
+ return (
+ !!(document.isWorkspaceTemplate || collection?.isActive) &&
+ !!(can.restore || can.unarchive)
+ );
+ },
+ perform: async ({ t, stores, activeDocumentId }) => {
+ const document = activeDocumentId
+ ? stores.documents.get(activeDocumentId)
+ : undefined;
+ if (!document) {
+ return;
+ }
+
+ await document.restore();
+ toast.success(
+ t("{{ documentName }} restored", {
+ documentName: capitalize(document.noun),
+ })
+ );
+ },
+});
+
+export const restoreDocumentToCollection = createActionV2WithChildren({
+ name: ({ t }) => `${t("Restore")}…`,
+ analyticsName: "Restore document",
+ section: ActiveDocumentSection,
+ icon: ,
+ visible: ({ stores, activeDocumentId }) => {
+ const document = activeDocumentId
+ ? stores.documents.get(activeDocumentId)
+ : undefined;
+ if (!document) {
+ return false;
+ }
+
+ const can = stores.policies.abilities(document.id);
+ const collection = document.collectionId
+ ? stores.collections.get(document.collectionId)
+ : undefined;
+
+ return (
+ !(document.isWorkspaceTemplate || collection?.isActive) &&
+ !!(can.restore || can.unarchive)
+ );
+ },
+ children: ({ t, activeDocumentId, stores }) => {
+ const { collections, documents, policies } = stores;
+
+ const document = activeDocumentId
+ ? documents.get(activeDocumentId)
+ : undefined;
+ if (!document) {
+ return [];
+ }
+
+ const actions = collections.orderedData.map((collection) => {
+ const can = policies.abilities(collection.id);
+ return createActionV2({
+ name: collection.name,
+ section: ActiveDocumentSection,
+ icon: ,
+ visible: can.createDocument,
+ perform: async () => {
+ await document.restore({ collectionId: collection.id });
+ toast.success(
+ t("{{ documentName }} restored", {
+ documentName: capitalize(document.noun),
+ })
+ );
+ },
+ });
+ });
+
+ return [createActionV2Group({ name: t("Choose a collection"), actions })];
+ },
+});
+
+export const deleteDocument = createActionV2({
name: ({ t }) => `${t("Delete")}…`,
analyticsName: "Delete document",
section: ActiveDocumentSection,
@@ -1023,7 +1224,7 @@ export const deleteDocument = createAction({
},
});
-export const permanentlyDeleteDocument = createAction({
+export const permanentlyDeleteDocument = createActionV2({
name: ({ t }) => t("Permanently delete"),
analyticsName: "Permanently delete document",
section: ActiveDocumentSection,
@@ -1078,7 +1279,7 @@ export const permanentlyDeleteDocumentsInTrash = createAction({
},
});
-export const openDocumentComments = createAction({
+export const openDocumentComments = createActionV2({
name: ({ t }) => t("Comments"),
analyticsName: "Open comments",
section: ActiveDocumentSection,
@@ -1101,7 +1302,7 @@ export const openDocumentComments = createAction({
},
});
-export const openDocumentHistory = createAction({
+export const openDocumentHistory = createInternalLinkActionV2({
name: ({ t }) => t("History"),
analyticsName: "Open document history",
section: ActiveDocumentSection,
@@ -1110,19 +1311,25 @@ export const openDocumentHistory = createAction({
const can = stores.policies.abilities(activeDocumentId ?? "");
return !!activeDocumentId && can.listRevisions;
},
- perform: ({ activeDocumentId, stores }) => {
- if (!activeDocumentId) {
- return;
- }
- const document = stores.documents.get(activeDocumentId);
+ to: ({ activeDocumentId, stores, sidebarContext }) => {
+ const document = activeDocumentId
+ ? stores.documents.get(activeDocumentId)
+ : undefined;
if (!document) {
- return;
+ return "";
}
- history.push(documentHistoryPath(document));
+
+ const [pathname, search] = documentHistoryPath(document).split("?");
+
+ return {
+ pathname,
+ search,
+ state: { sidebarContext },
+ };
},
});
-export const openDocumentInsights = createAction({
+export const openDocumentInsights = createInternalLinkActionV2({
name: ({ t }) => t("Insights"),
analyticsName: "Open document insights",
section: ActiveDocumentSection,
@@ -1140,15 +1347,21 @@ export const openDocumentInsights = createAction({
!document?.isDeleted
);
},
- perform: ({ activeDocumentId, stores }) => {
- if (!activeDocumentId) {
- return;
- }
- const document = stores.documents.get(activeDocumentId);
+ to: ({ activeDocumentId, stores, sidebarContext }) => {
+ const document = activeDocumentId
+ ? stores.documents.get(activeDocumentId)
+ : undefined;
if (!document) {
- return;
+ return "";
}
- history.push(documentInsightsPath(document));
+
+ const [pathname, search] = documentInsightsPath(document).split("?");
+
+ return {
+ pathname,
+ search,
+ state: { sidebarContext },
+ };
},
});
@@ -1183,7 +1396,7 @@ export const toggleViewerInsights = createActionV2({
},
});
-export const leaveDocument = createAction({
+export const leaveDocument = createActionV2({
name: ({ t }) => t("Leave document"),
analyticsName: "Leave document",
section: ActiveDocumentSection,
@@ -1219,6 +1432,27 @@ export const leaveDocument = createAction({
},
});
+export const applyTemplateFactory = ({
+ actions,
+}: {
+ actions: (ActionV2 | ActionV2Group | ActionV2Separator)[];
+}) =>
+ createActionV2WithChildren({
+ name: ({ t }) => t("Apply template"),
+ analyticsName: "Apply template",
+ section: ActiveDocumentSection,
+ icon: ,
+ visible: ({ activeDocumentId, stores }) => {
+ const { policies } = stores;
+ const can = activeDocumentId
+ ? policies.abilities(activeDocumentId)
+ : undefined;
+
+ return !!can?.update;
+ },
+ children: actions,
+ });
+
export const rootDocumentActions = [
openDocument,
archiveDocument,
diff --git a/app/actions/index.ts b/app/actions/index.ts
index 2c83d80e8f..590d70eba8 100644
--- a/app/actions/index.ts
+++ b/app/actions/index.ts
@@ -283,6 +283,7 @@ export function actionV2ToMenuItem(
icon,
visible,
disabled,
+ tooltip: resolve(action.tooltip, context),
selected: resolve(action.selected, context),
dangerous: action.dangerous,
onClick: () => performActionV2(action, context),
diff --git a/app/components/DocumentListItem.tsx b/app/components/DocumentListItem.tsx
index b890bd8714..56ec8d0358 100644
--- a/app/components/DocumentListItem.tsx
+++ b/app/components/DocumentListItem.tsx
@@ -33,7 +33,6 @@ type Props = {
showParentDocuments?: boolean;
showCollection?: boolean;
showPublished?: boolean;
- showPin?: boolean;
showDraft?: boolean;
showTemplate?: boolean;
};
@@ -68,7 +67,6 @@ function DocumentListItem(
showParentDocuments,
showCollection,
showPublished,
- showPin,
showDraft = true,
showTemplate,
highlight,
@@ -156,10 +154,8 @@ function DocumentListItem(
diff --git a/app/components/Menu/DropdownMenu.tsx b/app/components/Menu/DropdownMenu.tsx
index 12b65a380d..a6168ba62b 100644
--- a/app/components/Menu/DropdownMenu.tsx
+++ b/app/components/Menu/DropdownMenu.tsx
@@ -24,14 +24,24 @@ import {
} from "~/types";
import { toDropdownMenuItems, toMobileMenuItems } from "./transformer";
import { observer } from "mobx-react";
+import { useComputed } from "~/hooks/useComputed";
type Props = {
+ /** Root action with children representing the menu items */
action: ActionV2WithChildren;
+ /** Action context to use - new context will be created if not provided */
context?: ActionContext;
+ /** Trigger for the menu */
children: React.ReactNode;
+ /** Alignment w.r.t trigger - defaults to start */
align?: "start" | "end";
+ /** ARIA label for the menu */
ariaLabel: string;
+ /** Additional component to display at the bottom of the top-level menu */
+ append?: React.ReactNode;
+ /** Callback when menu is opened */
onOpen?: () => void;
+ /** Callback when menu is closed */
onClose?: () => void;
};
@@ -43,19 +53,28 @@ export const DropdownMenu = observer(function DropdownMenu({
ariaLabel,
onOpen,
onClose,
+ append,
}: Props) {
const [open, setOpen] = React.useState(false);
const isMobile = useMobile();
const contentRef =
React.useRef>(null);
+
const actionContext =
context ??
useActionContext({
isContextMenu: true,
});
- const menuItems = (action.children as ActionV2Variant[]).map((childAction) =>
- actionV2ToMenuItem(childAction, actionContext)
- );
+
+ const menuItems = useComputed(() => {
+ if (!open) {
+ return [];
+ }
+
+ return (action.children as ActionV2Variant[]).map((childAction) =>
+ actionV2ToMenuItem(childAction, actionContext)
+ );
+ }, [open, action.children, actionContext]);
const handleOpenChange = React.useCallback(
(open: boolean) => {
@@ -81,22 +100,26 @@ export const DropdownMenu = observer(function DropdownMenu({
}
}, []);
+ const handleCloseAutoFocus = React.useCallback(
+ (e: Event) => e.preventDefault(),
+ []
+ );
+
if (isMobile) {
return (
);
}
const content = toDropdownMenuItems(menuItems);
- if (!content) {
- return null;
- }
-
return (
@@ -107,41 +130,33 @@ export const DropdownMenu = observer(function DropdownMenu({
aria-label={ariaLabel}
onAnimationStart={disablePointerEvents}
onAnimationEnd={enablePointerEvents}
+ onCloseAutoFocus={handleCloseAutoFocus}
>
{content}
+ {append}
);
});
type MobileDropdownProps = {
+ open: boolean;
+ onOpenChange: (open: boolean) => void;
items: MenuItem[];
trigger: React.ReactNode;
-} & Pick;
+} & Pick;
function MobileDropdown({
+ open,
+ onOpenChange,
items,
trigger,
ariaLabel,
- onOpen,
- onClose,
+ append,
}: MobileDropdownProps) {
- const [open, setOpen] = React.useState(false);
const [submenuName, setSubmenuName] = React.useState();
const contentRef = React.useRef>(null);
- const handleOpenChange = React.useCallback(
- (open: boolean) => {
- setOpen(open);
- if (open) {
- onOpen?.();
- } else {
- onClose?.();
- }
- },
- [onOpen, onClose]
- );
-
const enablePointerEvents = React.useCallback(() => {
if (contentRef.current) {
contentRef.current.style.pointerEvents = "auto";
@@ -155,9 +170,9 @@ function MobileDropdown({
}, []);
const closeDrawer = React.useCallback(() => {
- handleOpenChange(false);
+ onOpenChange(false);
setTimeout(() => setSubmenuName(undefined), 500); // needed for a Vaul bug where 'onAnimationEnd' is not called for controlled state.
- }, [handleOpenChange]);
+ }, [onOpenChange]);
const resetSubmenu = React.useCallback((isOpen: boolean) => {
if (!isOpen) {
@@ -166,7 +181,7 @@ function MobileDropdown({
}, []);
const menuItems = React.useMemo(() => {
- if (!submenuName) {
+ if (!items.length || !submenuName) {
return items;
}
@@ -180,12 +195,12 @@ function MobileDropdown({
const content = toMobileMenuItems(menuItems, closeDrawer, setSubmenuName);
- if (!content) {
- return null;
- }
-
return (
-
+
{trigger}
@@ -197,7 +212,10 @@ function MobileDropdown({
onAnimationEnd={enablePointerEvents}
>
{ariaLabel}
- {content}
+
+ {content}
+ {!submenuName ? append : null}
+
);
diff --git a/app/components/Menu/transformer.tsx b/app/components/Menu/transformer.tsx
index 599ded4074..a10f4aa1e9 100644
--- a/app/components/Menu/transformer.tsx
+++ b/app/components/Menu/transformer.tsx
@@ -52,6 +52,7 @@ export function toDropdownMenuItems(items: MenuItem[]) {
label={item.title as string}
icon={icon}
disabled={item.disabled}
+ tooltip={item.tooltip}
selected={item.selected}
dangerous={item.dangerous}
onClick={item.onClick}
diff --git a/app/components/PaginatedDocumentList.tsx b/app/components/PaginatedDocumentList.tsx
index 915de45019..4a7d3f0e2c 100644
--- a/app/components/PaginatedDocumentList.tsx
+++ b/app/components/PaginatedDocumentList.tsx
@@ -46,7 +46,6 @@ const PaginatedDocumentList = React.memo(function PaginatedDocumentList({
-
+
{children}
@@ -81,7 +88,12 @@ const DropdownSubMenuContent = React.forwardRef<
return (
-
+
{children}
@@ -121,6 +133,7 @@ type BaseDropdownItemProps = {
type DropdownMenuButtonProps = BaseDropdownItemProps & {
onClick: (event: React.MouseEvent) => void;
+ tooltip?: React.ReactChild;
selected?: boolean;
dangerous?: boolean;
} & Omit<
@@ -132,10 +145,18 @@ const DropdownMenuButton = React.forwardRef<
React.ElementRef,
DropdownMenuButtonProps
>((props, ref) => {
- const { label, icon, disabled, selected, dangerous, onClick, ...rest } =
- props;
+ const {
+ label,
+ icon,
+ tooltip,
+ disabled,
+ selected,
+ dangerous,
+ onClick,
+ ...rest
+ } = props;
- return (
+ const button = (
{icon}
@@ -148,6 +169,14 @@ const DropdownMenuButton = React.forwardRef<
);
+
+ return tooltip ? (
+
+ {button}
+
+ ) : (
+ <>{button}>
+ );
});
DropdownMenuButton.displayName = "DropdownMenuButton";
@@ -226,7 +255,7 @@ const StyledScrollable = styled(Scrollable)`
min-width: 180px;
max-width: 276px;
min-height: 44px;
- max-height: 75vh;
+ max-height: min(85vh, var(--radix-dropdown-menu-content-available-height));
font-weight: normal;
background: ${s("menuBackground")};
diff --git a/app/hooks/useTemplateMenuActions.tsx b/app/hooks/useTemplateMenuActions.tsx
index a854769caa..381647c2c3 100644
--- a/app/hooks/useTemplateMenuActions.tsx
+++ b/app/hooks/useTemplateMenuActions.tsx
@@ -13,6 +13,7 @@ import { DocumentsSection } from "~/actions/sections";
import useCurrentUser from "~/hooks/useCurrentUser";
import useStores from "~/hooks/useStores";
import { ActionV2 } from "~/types";
+import { useComputed } from "./useComputed";
type Props = {
/** The document to which the templates will be applied */
@@ -22,7 +23,7 @@ type Props = {
};
/**
- * This hook provides a memoized list of menu items for both collection-specific
+ * This hook provides a memoized list of actions for both collection-specific
* templates and workspace-wide templates. It filters templates based on whether
* they are published and organizes them into appropriate sections.
*
@@ -56,32 +57,34 @@ export function useTemplateMenuActions({ document, onSelectTemplate }: Props) {
[user, onSelectTemplate]
);
- const templates = documents.templates.filter(
- (template) => template.publishedAt
- );
+ return useComputed(() => {
+ if (!onSelectTemplate) {
+ return [];
+ }
- const collectionTemplatesActions = templates
- .filter(
- (template) =>
- !template.isWorkspaceTemplate &&
- template.collectionId === document.collectionId
- )
- .map(templateToAction);
+ const templates = documents.templates.filter(
+ (template) => template.publishedAt
+ );
- const workspaceTemplatesActions = templates
- .filter((tmpl) => tmpl.isWorkspaceTemplate)
- .map(templateToAction);
+ const collectionTemplatesActions = templates
+ .filter(
+ (template) =>
+ !template.isWorkspaceTemplate &&
+ template.collectionId === document.collectionId
+ )
+ .map(templateToAction);
- if (!onSelectTemplate) {
- return [];
- }
+ const workspaceTemplatesActions = templates
+ .filter((tmpl) => tmpl.isWorkspaceTemplate)
+ .map(templateToAction);
- return [
- ...collectionTemplatesActions,
- ActionV2Separator,
- createActionV2Group({
- name: t("Workspace"),
- actions: workspaceTemplatesActions,
- }),
- ];
+ return [
+ ...collectionTemplatesActions,
+ ActionV2Separator,
+ createActionV2Group({
+ name: t("Workspace"),
+ actions: workspaceTemplatesActions,
+ }),
+ ];
+ }, []);
}
diff --git a/app/hooks/useTemplateMenuItems.tsx b/app/hooks/useTemplateMenuItems.tsx
deleted file mode 100644
index 6653f75fb9..0000000000
--- a/app/hooks/useTemplateMenuItems.tsx
+++ /dev/null
@@ -1,88 +0,0 @@
-import { DocumentIcon } from "outline-icons";
-import { useCallback, useMemo } from "react";
-import { useTranslation } from "react-i18next";
-import Icon from "@shared/components/Icon";
-import { TextHelper } from "@shared/utils/TextHelper";
-import Document from "~/models/Document";
-import useCurrentUser from "~/hooks/useCurrentUser";
-import useStores from "~/hooks/useStores";
-import { MenuItem } from "~/types";
-
-type Props = {
- /** The document to which the templates will be applied */
- document: Document;
- /** Callback to handle when a template is selected */
- onSelectTemplate?: (template: Document) => void;
-};
-
-/**
- * This hook provides a memoized list of menu items for both collection-specific
- * templates and workspace-wide templates. It filters templates based on whether
- * they are published and organizes them into appropriate sections.
- *
- * Collection-specific templates are displayed first, followed by workspace templates
- * with a separator in between (if both types exist).
- *
- * @returns An array of MenuItem objects representing templates that can be applied
- * to the current document. Returns an empty array if no callback is provided.
- */
-export function useTemplateMenuItems({ document, onSelectTemplate }: Props) {
- const user = useCurrentUser();
- const { documents } = useStores();
- const { t } = useTranslation();
-
- const templateToMenuItem = useCallback(
- (template: Document): MenuItem => ({
- type: "button",
- title: TextHelper.replaceTemplateVariables(
- template.titleWithDefault,
- user
- ),
- icon: template.icon ? (
-
- ) : (
-
- ),
- onClick: () => onSelectTemplate?.(template),
- }),
- [user, onSelectTemplate]
- );
-
- const templates = documents.templates.filter(
- (template) => template.publishedAt
- );
-
- const collectionItems = templates
- .filter(
- (template) =>
- !template.isWorkspaceTemplate &&
- template.collectionId === document.collectionId
- )
- .map(templateToMenuItem);
-
- const workspaceTemplates = templates
- .filter((tmpl) => tmpl.isWorkspaceTemplate)
- .map(templateToMenuItem);
-
- const workspaceItems: MenuItem[] = useMemo(
- () =>
- workspaceTemplates.length
- ? [{ type: "heading", title: t("Workspace") }, ...workspaceTemplates]
- : [],
- [t, workspaceTemplates]
- );
-
- if (!onSelectTemplate) {
- return [];
- }
-
- return collectionItems
- ? workspaceItems.length
- ? [
- ...collectionItems,
- { type: "separator" } as MenuItem,
- ...workspaceItems,
- ]
- : collectionItems
- : workspaceItems;
-}
diff --git a/app/menus/DocumentMenu.tsx b/app/menus/DocumentMenu.tsx
index 602abeb92c..a7b5e7d2e2 100644
--- a/app/menus/DocumentMenu.tsx
+++ b/app/menus/DocumentMenu.tsx
@@ -1,33 +1,17 @@
-import * as VisuallyHidden from "@radix-ui/react-visually-hidden";
-import capitalize from "lodash/capitalize";
-import isEmpty from "lodash/isEmpty";
import noop from "lodash/noop";
import { observer } from "mobx-react";
-import {
- EditIcon,
- InputIcon,
- RestoreIcon,
- SearchIcon,
- ShapesIcon,
-} from "outline-icons";
+import { InputIcon, SearchIcon } from "outline-icons";
import * as React from "react";
import { useTranslation } from "react-i18next";
-import { useHistory } from "react-router-dom";
-import { MenuButton, MenuButtonHTMLProps } from "reakit/Menu";
-import { toast } from "sonner";
import styled from "styled-components";
import breakpoint from "styled-components-breakpoint";
import { s } from "@shared/styles";
import { SubscriptionType, UserPreference } from "@shared/types";
-import { getEventFiles } from "@shared/utils/files";
import Document from "~/models/Document";
-import ContextMenu from "~/components/ContextMenu";
-import OverflowMenuButton from "~/components/ContextMenu/OverflowMenuButton";
-import Separator from "~/components/ContextMenu/Separator";
-import Template from "~/components/ContextMenu/Template";
-import CollectionIcon from "~/components/Icons/CollectionIcon";
+import { DropdownMenu } from "~/components/Menu/DropdownMenu";
+import { OverflowMenuButton } from "~/components/Menu/OverflowMenuButton";
import Switch from "~/components/Switch";
-import { actionToMenuItem } from "~/actions";
+import { ActionV2Separator, createActionV2 } from "~/actions";
import {
pinDocument,
createTemplateFromDocument,
@@ -55,35 +39,36 @@ import {
searchInDocument,
leaveDocument,
moveTemplate,
+ restoreDocument,
+ restoreDocumentToCollection,
+ editDocument,
+ applyTemplateFactory,
} from "~/actions/definitions/documents";
import useActionContext from "~/hooks/useActionContext";
-import useBoolean from "~/hooks/useBoolean";
import useCurrentUser from "~/hooks/useCurrentUser";
-import { useMenuState } from "~/hooks/useMenuState";
import useMobile from "~/hooks/useMobile";
import usePolicy from "~/hooks/usePolicy";
import useRequest from "~/hooks/useRequest";
import useStores from "~/hooks/useStores";
-import { useTemplateMenuItems } from "~/hooks/useTemplateMenuItems";
-import { MenuItem, MenuItemButton } from "~/types";
-import { documentEditPath } from "~/utils/routeHelpers";
-import { MenuContext, useMenuContext } from "./MenuContext";
+import { ActiveDocumentSection } from "~/actions/sections";
+import { useTemplateMenuActions } from "~/hooks/useTemplateMenuActions";
+import { useMenuAction } from "~/hooks/useMenuAction";
+import { MenuSeparator } from "~/components/primitives/components/Menu";
type Props = {
/** Document for which the menu is to be shown */
document: Document;
- isRevision?: boolean;
+ /** Alignment w.r.t trigger - defaults to start */
+ align?: "start" | "end";
+ /** Trigger's variant - renders nude variant if unset */
+ neutral?: boolean;
/** Pass true if the document is currently being displayed */
showDisplayOptions?: boolean;
- /** Whether to display menu as a modal */
- modal?: boolean;
/** Whether to include the option of toggling embeds as menu item */
showToggleEmbeds?: boolean;
- showPin?: boolean;
- /** Label for menu button */
- label?: (props: MenuButtonHTMLProps) => React.ReactNode;
/** Invoked when the "Find and replace" menu item is clicked */
onFindAndReplace?: () => void;
+ /** Callback when a template is selected to apply its content to the document */
onSelectTemplate?: (template: Document) => void;
/** Invoked when the "Rename" menu item is clicked */
onRename?: () => void;
@@ -93,16 +78,24 @@ type Props = {
onClose?: () => void;
};
-type MenuTriggerProps = {
- label?: (props: MenuButtonHTMLProps) => React.ReactNode;
- onTrigger: () => void;
-};
-
-const MenuTrigger: React.FC = ({ label, onTrigger }) => {
+function DocumentMenu({
+ document,
+ align,
+ neutral,
+ showToggleEmbeds,
+ showDisplayOptions,
+ onSelectTemplate,
+ onRename,
+ onOpen,
+ onClose,
+ onFindAndReplace,
+}: Props) {
const { t } = useTranslation();
+ const user = useCurrentUser();
+ const isMobile = useMobile();
+ const can = usePolicy(document);
const { subscriptions, pins } = useStores();
- const { model: document, menuState } = useMenuContext();
const {
loading: auxDataLoading,
@@ -134,106 +127,6 @@ const MenuTrigger: React.FC = ({ label, onTrigger }) => {
}
}, [auxDataLoading, auxDataLoaded, auxDataRequest, document]);
- return label ? (
-
- {label}
-
- ) : (
-
- );
-};
-
-type MenuContentProps = {
- onOpen?: () => void;
- onClose?: () => void;
- onFindAndReplace?: () => void;
- onSelectTemplate?: (template: Document) => void;
- onRename?: () => void;
- showDisplayOptions?: boolean;
- showToggleEmbeds?: boolean;
-};
-
-const MenuContent: React.FC = observer(function MenuContent_({
- onOpen,
- onClose,
- onFindAndReplace,
- onSelectTemplate,
- onRename,
- showDisplayOptions,
- showToggleEmbeds,
-}) {
- const user = useCurrentUser();
- const { model: document, menuState } = useMenuContext();
- const can = usePolicy(document);
- const { t } = useTranslation();
- const { policies, collections } = useStores();
-
- const collection = document.collectionId
- ? collections.get(document.collectionId)
- : undefined;
-
- const context = useActionContext({
- isContextMenu: true,
- activeDocumentId: document.id,
- activeCollectionId: document.collectionId ?? undefined,
- });
-
- const isMobile = useMobile();
-
- const handleRestore = React.useCallback(
- async (
- ev: React.SyntheticEvent,
- options?: {
- collectionId: string;
- }
- ) => {
- await document.restore(options);
- toast.success(
- t("{{ documentName }} restored", {
- documentName: capitalize(document.noun),
- })
- );
- },
- [t, document]
- );
-
- const restoreItems = React.useMemo(
- () => [
- ...collections.orderedData.reduce