Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix: adapt page actions for document modal (#18337)

Arvin Xu4e7de3829b20
diff --git a/src/features/DocumentModal/Header.tsx b/src/features/DocumentModal/Header.tsx
index 0380657b6da..b7099117d74 100644
--- a/src/features/DocumentModal/Header.tsx
+++ b/src/features/DocumentModal/Header.tsx
@@ -1,32 +1,55 @@
'use client';
-import { ActionIcon, Avatar, Flexbox, Skeleton, Text } from '@lobehub/ui';
-import { DropdownMenu, useModalContext } from '@lobehub/ui/base-ui';
-import { cssVar } from 'antd-style';
+import { ActionIcon, Avatar, copyToClipboard, Flexbox, Skeleton, Text } from '@lobehub/ui';
+import { DropdownMenu, toast, useModalContext } from '@lobehub/ui/base-ui';
+import { createStaticStyles, cssVar } from 'antd-style';
import { MoreHorizontal, XIcon } from 'lucide-react';
-import { memo } from 'react';
+import { memo, useCallback } from 'react';
import { useTranslation } from 'react-i18next';
import ShareButton from '@/business/client/features/PageShare/ShareButton';
+import { useActiveWorkspaceSlug } from '@/business/client/hooks/useActiveWorkspaceSlug';
import { DESKTOP_HEADER_ICON_SIZE, DESKTOP_HEADER_ICON_SMALL_SIZE } from '@/const/layoutTokens';
import { AutoSaveHint } from '@/features/EditorCanvas';
import { useMenu } from '@/features/PageEditor/Header/useMenu';
import { usePageAgentPanelControl } from '@/features/PageEditor/RightPanel/OverrideContext';
import { usePageEditorStore } from '@/features/PageEditor/store';
import ToggleRightPanelButton from '@/features/RightPanel/ToggleRightPanelButton';
+import { useAppOrigin } from '@/hooks/useAppOrigin';
import { useDocumentStore } from '@/store/document';
import { editorSelectors } from '@/store/document/slices/editor';
+import { buildDocumentModalUrl } from './url';
+
const HEADER_HEIGHT = 44;
+const styles = createStaticStyles(({ css }) => ({
+ shareButton: css`
+ & button:not(:hover, :focus-visible) {
+ background: transparent;
+ }
+ `,
+}));
+
const DocumentModalHeader = memo(() => {
const { t } = useTranslation(['file', 'common']);
const { close } = useModalContext();
+ const activeWorkspaceSlug = useActiveWorkspaceSlug();
+ const appOrigin = useAppOrigin();
const [documentId, emoji, title] = usePageEditorStore((s) => [s.documentId, s.emoji, s.title]);
const isDocumentLoading = useDocumentStore(editorSelectors.isDocumentLoading(documentId));
const { expand: showPageAgentPanel, toggle: togglePageAgentPanel } = usePageAgentPanelControl();
- const { menuItems } = useMenu();
+ const handleCopyLink = useCallback(async () => {
+ if (!documentId) return;
+ await copyToClipboard(buildDocumentModalUrl(appOrigin, documentId, activeWorkspaceSlug));
+ toast.success(t('pageEditor.linkCopied'));
+ }, [activeWorkspaceSlug, appOrigin, documentId, t]);
+ const { menuItems } = useMenu({
+ onCopyLink: handleCopyLink,
+ onDeleted: close,
+ onOpenHistory: () => togglePageAgentPanel(true),
+ });
return (
<Flexbox
@@ -55,17 +78,21 @@ const DocumentModalHeader = memo(() => {
{documentId && !isDocumentLoading && (
<AutoSaveHint documentId={documentId} style={{ marginLeft: 4 }} />
)}
- </Flexbox>
- <Flexbox horizontal align={'center'} gap={4}>
- {documentId && <ShareButton documentId={documentId} />}
<DropdownMenu
iconSpaceMode={'group'}
items={menuItems}
- placement={'bottomRight'}
+ placement={'bottomLeft'}
popupProps={{ style: { minWidth: 200 } }}
>
<ActionIcon icon={MoreHorizontal} size={DESKTOP_HEADER_ICON_SMALL_SIZE} />
</DropdownMenu>
+ </Flexbox>
+ <Flexbox horizontal align={'center'} gap={4}>
+ {documentId && (
+ <span className={styles.shareButton}>
+ <ShareButton documentId={documentId} />
+ </span>
+ )}
<ToggleRightPanelButton
expand={showPageAgentPanel}
showActive={false}
diff --git a/src/features/DocumentModal/url.test.ts b/src/features/DocumentModal/url.test.ts
new file mode 100644
index 00000000000..c3ac8e663ce
--- /dev/null
+++ b/src/features/DocumentModal/url.test.ts
@@ -0,0 +1,17 @@
+import { describe, expect, it } from 'vitest';
+
+import { buildDocumentModalUrl } from './url';
+
+describe('buildDocumentModalUrl', () => {
+ it('builds a canonical personal page URL independently of the current route', () => {
+ expect(buildDocumentModalUrl('https://example.com', 'doc-1')).toBe(
+ 'https://example.com/page/doc-1',
+ );
+ });
+
+ it('includes the active workspace slug', () => {
+ expect(buildDocumentModalUrl('https://example.com', 'doc-1', 'acme')).toBe(
+ 'https://example.com/acme/page/doc-1',
+ );
+ });
+});
diff --git a/src/features/DocumentModal/url.ts b/src/features/DocumentModal/url.ts
new file mode 100644
index 00000000000..ad7cdcb7d1a
--- /dev/null
+++ b/src/features/DocumentModal/url.ts
@@ -0,0 +1,10 @@
+import { buildWorkspaceAwarePath } from '@/features/Workspace/workspaceAwarePath';
+
+export const buildDocumentModalUrl = (
+ appOrigin: string,
+ documentId: string,
+ activeWorkspaceSlug?: string | null,
+) => {
+ const path = buildWorkspaceAwarePath(`/page/${documentId}`, activeWorkspaceSlug);
+ return `${appOrigin}${path}`;
+};
diff --git a/src/features/PageEditor/Header/useMenu.test.tsx b/src/features/PageEditor/Header/useMenu.test.tsx
index 7411cc9a1ff..1165b0d39b1 100644
--- a/src/features/PageEditor/Header/useMenu.test.tsx
+++ b/src/features/PageEditor/Header/useMenu.test.tsx
@@ -16,6 +16,12 @@ const resourcePermissionMock = vi.hoisted(() => ({
workspaceId: undefined as string | undefined,
}));
const wsNavigateMock = vi.hoisted(() => vi.fn());
+const menuActionMocks = vi.hoisted(() => ({
+ handleCopyLink: vi.fn(),
+ handleDelete: vi.fn(),
+ setRightPanelMode: vi.fn(),
+ togglePageAgentPanel: vi.fn(),
+}));
vi.mock('react-i18next', () => ({
useTranslation: () => ({
@@ -141,7 +147,7 @@ vi.mock('@/store/file', () => ({
vi.mock('@/store/global', () => ({
useGlobalStore: (selector: (state: Record<string, unknown>) => unknown) =>
selector({
- togglePageAgentPanel: vi.fn(),
+ togglePageAgentPanel: menuActionMocks.togglePageAgentPanel,
toggleWideScreen: vi.fn(),
wideScreen: false,
}),
@@ -157,15 +163,15 @@ vi.mock('../store', () => ({
usePageEditorStore: (selector: (state: Record<string, unknown>) => unknown) =>
selector({
documentId: 'doc-1',
- setRightPanelMode: vi.fn(),
+ setRightPanelMode: menuActionMocks.setRightPanelMode,
}),
useStoreApi: () => ({
getState: () => ({
editor: {
getDocument: () => '# Hello',
},
- handleCopyLink: vi.fn(),
- handleDelete: vi.fn(),
+ handleCopyLink: menuActionMocks.handleCopyLink,
+ handleDelete: menuActionMocks.handleDelete,
title: 'Hello',
}),
}),
@@ -180,7 +186,7 @@ describe('PageEditor header menu', () => {
permissionMock.edit_own_content = true;
resourcePermissionMock.canManage = false;
resourcePermissionMock.workspaceId = undefined;
- wsNavigateMock.mockClear();
+ vi.clearAllMocks();
});
it('places the member-permission page entry in the overflow menu for managers', () => {
@@ -219,4 +225,24 @@ describe('PageEditor header menu', () => {
expect(getMenuItem(items, 'version-history')).not.toMatchObject({ disabled: true });
expect(getMenuItem(items, 'export')).not.toMatchObject({ disabled: true });
});
+
+ it('uses surface-specific actions when provided', async () => {
+ const onCopyLink = vi.fn();
+ const onDeleted = vi.fn();
+ const onOpenHistory = vi.fn();
+ const { result } = renderHook(() => useMenu({ onCopyLink, onDeleted, onOpenHistory }));
+
+ (getMenuItem(result.current.menuItems, 'copy-link') as { onClick: () => void }).onClick();
+ (getMenuItem(result.current.menuItems, 'version-history') as { onClick: () => void }).onClick();
+ await (
+ getMenuItem(result.current.menuItems, 'delete') as { onClick: () => Promise<void> }
+ ).onClick();
+
+ expect(onCopyLink).toHaveBeenCalledOnce();
+ expect(menuActionMocks.handleCopyLink).not.toHaveBeenCalled();
+ expect(menuActionMocks.setRightPanelMode).toHaveBeenCalledWith('history');
+ expect(onOpenHistory).toHaveBeenCalledOnce();
+ expect(menuActionMocks.togglePageAgentPanel).not.toHaveBeenCalled();
+ expect(menuActionMocks.handleDelete).toHaveBeenCalledWith(expect.any(Function), onDeleted);
+ });
});
diff --git a/src/features/PageEditor/Header/useMenu.tsx b/src/features/PageEditor/Header/useMenu.tsx
index c3996a3a8ff..157b114032a 100644
--- a/src/features/PageEditor/Header/useMenu.tsx
+++ b/src/features/PageEditor/Header/useMenu.tsx
@@ -38,7 +38,14 @@ import { usePageEditorStore, useStoreApi } from '../store';
/**
* Action menu for the page editor.
*/
-export const useMenu = (): { menuItems: any[] } => {
+interface UseMenuOptions {
+ onCopyLink?: () => void;
+ onDeleted?: () => void;
+ onOpenHistory?: () => void;
+}
+
+export const useMenu = (options: UseMenuOptions = {}): { menuItems: any[] } => {
+ const { onCopyLink, onDeleted, onOpenHistory } = options;
const { i18n, t } = useTranslation(['file', 'common', 'chat', 'setting']);
const storeApi = useStoreApi();
@@ -243,6 +250,10 @@ export const useMenu = (): { menuItems: any[] } => {
key: 'copy-link',
label: t('pageEditor.menu.copyLink'),
onClick: () => {
+ if (onCopyLink) {
+ onCopyLink();
+ return;
+ }
const state = storeApi.getState();
state.handleCopyLink(t as any);
},
@@ -253,7 +264,11 @@ export const useMenu = (): { menuItems: any[] } => {
label: t('pageEditor.history.title'),
onClick: () => {
setRightPanelMode('history');
- togglePageAgentPanel(true);
+ if (onOpenHistory) {
+ onOpenHistory();
+ } else {
+ togglePageAgentPanel(true);
+ }
},
},
{
@@ -265,7 +280,7 @@ export const useMenu = (): { menuItems: any[] } => {
onClick: async () => {
if (!canEditPage) return;
const state = storeApi.getState();
- await state.handleDelete(t as any, state.onDelete);
+ await state.handleDelete(t as any, onDeleted ?? state.onDelete);
},
},
{
@@ -343,6 +358,9 @@ export const useMenu = (): { menuItems: any[] } => {
handleExportMarkdown,
memberPermissionMenuItem,
transferMenuItems,
+ onCopyLink,
+ onDeleted,
+ onOpenHistory,
]);
return { menuItems };