diff --git a/app/actions/definitions/documents.tsx b/app/actions/definitions/documents.tsx
index 87ed254482..1739ea3098 100644
--- a/app/actions/definitions/documents.tsx
+++ b/app/actions/definitions/documents.tsx
@@ -447,14 +447,24 @@ export const createNewDocument = createActionWithChildren({
children: [createDocumentBefore, createDocumentAfter, createNestedDocument],
});
-export const createNewDocumentInAlphabeticalCollection =
- createInternalLinkAction({
+/**
+ * Creates the action that opens a new document nested inside the active
+ * document.
+ *
+ * @param visible - additional visibility check for the action.
+ * @returns the action.
+ */
+function newNestedDocumentAction(
+ visible: (context: ActionContext) => boolean = () => true
+) {
+ return createInternalLinkAction({
name: ({ t }) => t("New document"),
analyticsName: "New document",
section: ActiveDocumentSection,
icon: ,
keywords: "create",
- visible: ({ currentTeamId, activeDocumentId, stores }) => {
+ visible: (context) => {
+ const { currentTeamId, activeDocumentId, stores } = context;
if (!activeDocumentId || !currentTeamId) {
return false;
}
@@ -464,7 +474,7 @@ export const createNewDocumentInAlphabeticalCollection =
if (!stores.policies.abilities(activeDocumentId).createChildDocument) {
return false;
}
- return isAlphabeticallySorted(stores, activeDocumentId);
+ return visible(context);
},
to: ({ activeDocumentId, sidebarContext }) => {
const [pathname, search] =
@@ -477,6 +487,16 @@ export const createNewDocumentInAlphabeticalCollection =
};
},
});
+}
+
+export const createNewDocumentInAlphabeticalCollection =
+ newNestedDocumentAction(
+ ({ activeDocumentId, stores }) =>
+ !!activeDocumentId && isAlphabeticallySorted(stores, activeDocumentId)
+ );
+
+/** A single action to create a nested document, for menus outside the sidebar. */
+export const createNewNestedDocument = newNestedDocumentAction();
export const starDocument = createAction({
name: ({ t }) => t("Star"),
diff --git a/app/components/Sidebar/components/CollectionLink.tsx b/app/components/Sidebar/components/CollectionLink.tsx
index 9068c41532..524381a502 100644
--- a/app/components/Sidebar/components/CollectionLink.tsx
+++ b/app/components/Sidebar/components/CollectionLink.tsx
@@ -108,13 +108,13 @@ const CollectionLink: React.FC = ({
const contextMenuAction = useCollectionMenuAction({
collectionId: collection.id,
onRename: handleRename,
- showLinkActions: true,
+ isSidebar: true,
});
const menu = !isDraggingAnyCollection ? (
diff --git a/app/components/Sidebar/components/StarredLink.tsx b/app/components/Sidebar/components/StarredLink.tsx
index d582ba9e2b..e7c057f7df 100644
--- a/app/components/Sidebar/components/StarredLink.tsx
+++ b/app/components/Sidebar/components/StarredLink.tsx
@@ -186,6 +186,7 @@ const StarredDocumentLink = observer(function StarredDocumentLink({
const contextMenuAction = useDocumentMenuAction({
documentId: document.id,
onRename: handleRename,
+ isSidebar: true,
});
const isActive = React.useCallback(
@@ -206,6 +207,7 @@ const StarredDocumentLink = observer(function StarredDocumentLink({
const menu = (
void;
- /** Whether to include the action that adds a link, used in the sidebar */
- showLinkActions?: boolean;
+ /** Whether the menu is shown in the sidebar, where links can be added */
+ isSidebar?: boolean;
};
export function useCollectionMenuAction({
collectionId,
onRename,
- showLinkActions = false,
+ isSidebar = false,
}: Props) {
const actions = useMemo(
() => [
// Actions that add to the tree of the collection, not change it.
createDocument,
- ...(showLinkActions ? [addLinkToCollection] : []),
+ ...(isSidebar ? [addLinkToCollection] : []),
importDocument,
ActionSeparator,
restoreCollection,
@@ -68,7 +68,7 @@ export function useCollectionMenuAction({
ActionSeparator,
deleteCollection,
],
- [collectionId, onRename, showLinkActions]
+ [collectionId, onRename, isSidebar]
);
return useMenuAction(actions);
diff --git a/app/hooks/useDocumentMenuAction.tsx b/app/hooks/useDocumentMenuAction.tsx
index e62113fd3d..3dd7bb7a82 100644
--- a/app/hooks/useDocumentMenuAction.tsx
+++ b/app/hooks/useDocumentMenuAction.tsx
@@ -13,6 +13,7 @@ import {
shareDocument,
createNewDocument,
createNewDocumentInAlphabeticalCollection,
+ createNewNestedDocument,
importDocument,
createTemplateFromDocument,
duplicateDocument,
@@ -54,8 +55,8 @@ type Props = {
onRename?: () => void;
/** Callback when a template is selected to apply its content to the document */
onSelectTemplate?: (template: Template) => void;
- /** Whether to include the actions that add links around the document, used in the sidebar */
- showLinkActions?: boolean;
+ /** Whether the menu is shown in the sidebar, where items can be added around the document */
+ isSidebar?: boolean;
};
export function useDocumentMenuAction({
@@ -64,7 +65,7 @@ export function useDocumentMenuAction({
onFindAndReplace,
onRename,
onSelectTemplate,
- showLinkActions = false,
+ isSidebar = false,
}: Props) {
const { t } = useTranslation();
const isMobile = useMobile();
@@ -78,9 +79,13 @@ export function useDocumentMenuAction({
() =>
createRootMenuAction([
// Actions that add to the tree around the document, not change it.
- createNewDocument,
- createNewDocumentInAlphabeticalCollection,
- ...(showLinkActions ? [addLinkToDocument] : []),
+ ...(isSidebar
+ ? [
+ createNewDocument,
+ createNewDocumentInAlphabeticalCollection,
+ addLinkToDocument,
+ ]
+ : [createNewNestedDocument]),
importDocument,
ActionSeparator,
restoreDocument,
@@ -132,7 +137,7 @@ export function useDocumentMenuAction({
t,
isMobile,
isViewing,
- showLinkActions,
+ isSidebar,
templateMenuActions,
documentId,
onFindAndReplace,
diff --git a/app/menus/CollectionMenu.tsx b/app/menus/CollectionMenu.tsx
index 1134267e0b..cf86a08f96 100644
--- a/app/menus/CollectionMenu.tsx
+++ b/app/menus/CollectionMenu.tsx
@@ -17,8 +17,8 @@ type Props = {
onRename?: () => void;
onOpen?: () => void;
onClose?: () => void;
- /** Whether to include the action that adds a link, used in the sidebar */
- showLinkActions?: boolean;
+ /** Whether the menu is shown in the sidebar, where links can be added */
+ isSidebar?: boolean;
};
function CollectionMenu({
@@ -28,7 +28,7 @@ function CollectionMenu({
onRename,
onOpen,
onClose,
- showLinkActions,
+ isSidebar,
}: Props) {
const { subscriptions } = useStores();
const { t } = useTranslation();
@@ -53,7 +53,7 @@ function CollectionMenu({
const rootAction = useCollectionMenuAction({
collectionId: collection.id,
onRename,
- showLinkActions,
+ isSidebar,
});
return (
diff --git a/app/menus/DocumentMenu.tsx b/app/menus/DocumentMenu.tsx
index eacd6288ca..8f6bc827a8 100644
--- a/app/menus/DocumentMenu.tsx
+++ b/app/menus/DocumentMenu.tsx
@@ -47,8 +47,8 @@ type Props = {
onOpen?: () => void;
/** Invoked when menu is closed */
onClose?: () => void;
- /** Whether to include the actions that add links around the document, used in the sidebar */
- showLinkActions?: boolean;
+ /** Whether the menu is shown in the sidebar, where items can be added around the document */
+ isSidebar?: boolean;
};
function DocumentMenu({
@@ -62,7 +62,7 @@ function DocumentMenu({
onOpen,
onClose,
onFindAndReplace,
- showLinkActions,
+ isSidebar,
}: Props) {
const { t } = useTranslation();
const user = useCurrentUser();
@@ -133,7 +133,7 @@ function DocumentMenu({
const rootAction = useDocumentMenuAction({
documentId: document.id,
isViewing: showDisplayOptions,
- showLinkActions,
+ isSidebar,
onFindAndReplace,
onRename,
onSelectTemplate,