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.

✨ feat(docs): add favorite toggle to the docs list row menu (#22147)

Rdmclin273f9e3c07420
diff --git a/src/features/Pages/PageLayout/Body/List/Item/useDropdownMenu.test.tsx b/src/features/Pages/PageLayout/Body/List/Item/useDropdownMenu.test.tsx
index ba0d65ceab0..98e8a1266d2 100644
--- a/src/features/Pages/PageLayout/Body/List/Item/useDropdownMenu.test.tsx
+++ b/src/features/Pages/PageLayout/Body/List/Item/useDropdownMenu.test.tsx
@@ -15,10 +15,28 @@ const CURRENT_USER_ID = vi.hoisted(() => 'user-1');
const useDocumentTransferMenuItemMock = vi.hoisted(() => vi.fn(() => []));
+const favoriteMenuItemFor = vi.hoisted(
+ () => (target: unknown, _options?: unknown) =>
+ target ? { key: 'toggleFavorite', label: 'favorite.add', onClick: () => {} } : null,
+);
+
+const useFavoriteMenuItemMock = vi.hoisted(() => vi.fn(favoriteMenuItemFor));
+
const storeMock = vi.hoisted(() => ({
activeWorkspaceId: undefined as string | undefined,
document: undefined as
- { id: string; userId?: string; visibility?: 'private' | 'public' | null } | undefined,
+ | {
+ id: string;
+ metadata?: { emoji?: string };
+ title?: string;
+ userId?: string;
+ visibility?: 'private' | 'public' | null;
+ }
+ | undefined,
+}));
+
+vi.mock('@/features/Favorites', () => ({
+ useFavoriteMenuItem: useFavoriteMenuItemMock,
}));
vi.mock('antd', async (importOriginal) => ({
@@ -111,6 +129,7 @@ describe('Page list item dropdown menu', () => {
permissionMock.edit_own_content = true;
storeMock.activeWorkspaceId = undefined;
storeMock.document = undefined;
+ useFavoriteMenuItemMock.mockImplementation(favoriteMenuItemFor);
});
it('labels the page transfer action as Move', () => {
@@ -243,4 +262,75 @@ describe('Page list item dropdown menu', () => {
expect(getMenuItem(items, 'publishToWorkspace')).toBeUndefined();
});
+
+ it('offers the same page favorite as the open page menu', () => {
+ storeMock.document = { id: 'page-1', metadata: { emoji: 'πŸ“„' }, title: 'Roadmap' };
+
+ const { result } = renderHook(() =>
+ useDropdownMenu({ pageId: 'page-1', toggleEditing: vi.fn() }),
+ );
+
+ expect(useFavoriteMenuItemMock).toHaveBeenLastCalledWith(
+ {
+ icon: 'πŸ“„',
+ path: '/page/page-1',
+ targetId: 'page-1',
+ title: 'Roadmap',
+ type: 'page',
+ },
+ { showHotkey: false },
+ );
+ expect(getMenuItem(result.current(), 'toggleFavorite')).toBeTruthy();
+ });
+
+ it('falls back to the untitled placeholder for a page without a title', () => {
+ storeMock.document = { id: 'page-1' };
+
+ renderHook(() => useDropdownMenu({ pageId: 'page-1', toggleEditing: vi.fn() }));
+
+ expect(useFavoriteMenuItemMock).toHaveBeenLastCalledWith(
+ expect.objectContaining({ icon: null, title: 'pageEditor.titlePlaceholder' }),
+ { showHotkey: false },
+ );
+ });
+
+ it('keeps the row click from firing when toggling the favorite', () => {
+ storeMock.document = { id: 'page-1', title: 'Roadmap' };
+ const toggle = vi.fn();
+ useFavoriteMenuItemMock.mockImplementation((target) =>
+ target ? { key: 'toggleFavorite', label: 'favorite.add', onClick: toggle } : null,
+ );
+
+ const { result } = renderHook(() =>
+ useDropdownMenu({ pageId: 'page-1', toggleEditing: vi.fn() }),
+ );
+ const favorite = getMenuItem(result.current(), 'toggleFavorite');
+ const domEvent = { stopPropagation: vi.fn() };
+
+ (favorite?.onClick as unknown as (info: { domEvent: typeof domEvent }) => void)({ domEvent });
+
+ expect(domEvent.stopPropagation).toHaveBeenCalledTimes(1);
+ expect(toggle).toHaveBeenCalledTimes(1);
+ });
+
+ it('has no favorite entry until the page is loaded', () => {
+ const { result } = renderHook(() =>
+ useDropdownMenu({ pageId: 'page-1', toggleEditing: vi.fn() }),
+ );
+
+ expect(getMenuItem(result.current(), 'toggleFavorite')).toBeUndefined();
+ });
+
+ it('has no favorite entry for a page that is still being created', () => {
+ // An optimistic row's client-minted id is swapped for the server id once
+ // creation lands; a favorite saved against it would point at no page.
+ storeMock.document = { id: 'temp-page-1', title: 'Draft' };
+
+ const { result } = renderHook(() =>
+ useDropdownMenu({ pageId: 'temp-page-1', toggleEditing: vi.fn() }),
+ );
+
+ expect(useFavoriteMenuItemMock).toHaveBeenLastCalledWith(undefined, { showHotkey: false });
+ expect(getMenuItem(result.current(), 'toggleFavorite')).toBeUndefined();
+ });
});
diff --git a/src/features/Pages/PageLayout/Body/List/Item/useDropdownMenu.tsx b/src/features/Pages/PageLayout/Body/List/Item/useDropdownMenu.tsx
index 66bcc8a00a8..f7f9841d9ae 100644
--- a/src/features/Pages/PageLayout/Body/List/Item/useDropdownMenu.tsx
+++ b/src/features/Pages/PageLayout/Body/List/Item/useDropdownMenu.tsx
@@ -8,6 +8,7 @@ import { useActiveWorkspaceId } from '@/business/client/hooks/useActiveWorkspace
import { useActiveWorkspaceSlug } from '@/business/client/hooks/useActiveWorkspaceSlug';
import { useDocumentTransferMenuItem } from '@/business/client/hooks/useDocumentTransferMenuItem';
import { isDesktop } from '@/const/version';
+import { useFavoriteMenuItem } from '@/features/Favorites';
import { docsPagePath } from '@/features/ResourceHome/paths';
import VisibilityConfirmContent from '@/features/VisibilityConfirmContent';
import { useWorkspaceAwareNavigate } from '@/features/Workspace/useWorkspaceAwareNavigate';
@@ -15,6 +16,7 @@ import { buildWorkspaceAwarePath } from '@/features/Workspace/workspaceAwarePath
import { usePermission } from '@/hooks/usePermission';
import { useElectronStore } from '@/store/electron';
import { pageActions, pageSelectors, usePageStore } from '@/store/page';
+import { isTempPageId } from '@/store/page/projection';
import { useUserStore } from '@/store/user';
import { userProfileSelectors } from '@/store/user/selectors';
@@ -70,6 +72,22 @@ export const useDropdownMenu = ({ pageId, toggleEditing }: ActionProps): (() =>
transferLabel: t('pageEditor.menu.move', { ns: 'file' }),
});
const currentUserId = useUserStore(userProfileSelectors.userId);
+ // Same target as the open page's β‹― menu (PageEditor/Header/useMenu), so both
+ // entries read and write one favorite. No hotkey: it toggles the open page,
+ // not a row in this list. A row still being created carries a client-minted
+ // id that the server row replaces, so it cannot be favorited until then.
+ const favoriteMenuItem = useFavoriteMenuItem(
+ document && !isTempPageId(pageId)
+ ? {
+ icon: document.metadata?.emoji ?? null,
+ path: `/page/${pageId}`,
+ targetId: pageId,
+ title: document.title || t('pageEditor.titlePlaceholder', { ns: 'file' }),
+ type: 'page',
+ }
+ : undefined,
+ { showHotkey: false },
+ );
const isPrivate = document?.visibility === 'private';
const isPublic = document?.visibility === 'public';
@@ -172,6 +190,7 @@ export const useDropdownMenu = ({ pageId, toggleEditing }: ActionProps): (() =>
{ type: 'divider' as const },
]
: []),
+ ...(favoriteMenuItem ? wrapMenuItems([favoriteMenuItem]) : []),
{
disabled: !canEditPage,
icon: <Icon icon={Pencil} />,
@@ -240,6 +259,7 @@ export const useDropdownMenu = ({ pageId, toggleEditing }: ActionProps): (() =>
addTab,
navigate,
transferMenuItems,
+ favoriteMenuItem,
],
);
};