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(resource): carry the workspace slug in copied page links (#19795)

Rdmclin220f0633e59fc
diff --git a/src/features/LibraryModal/AssignKnowledgeBase/Item/Action.tsx b/src/features/LibraryModal/AssignKnowledgeBase/Item/Action.tsx
index 75e5f03fcd1..cf955a332ff 100644
--- a/src/features/LibraryModal/AssignKnowledgeBase/Item/Action.tsx
+++ b/src/features/LibraryModal/AssignKnowledgeBase/Item/Action.tsx
@@ -4,6 +4,8 @@ import { InfoIcon, MoreVerticalIcon, Trash2 } from 'lucide-react';
import { memo, useState } from 'react';
import { useTranslation } from 'react-i18next';
+import { useActiveWorkspaceSlug } from '@/business/client/hooks/useActiveWorkspaceSlug';
+import { buildLibraryPath, buildPagePath } from '@/features/ResourceManager/utils/resourcePath';
import { useAgentStore } from '@/store/agent';
import { useServerConfigStore } from '@/store/serverConfig';
import { KnowledgeType } from '@/types/knowledgeBase';
@@ -18,6 +20,7 @@ const Actions = memo<ActionsProps>(({ id, type, enabled }) => {
const { t } = useTranslation('chat');
const mobile = useServerConfigStore((s) => s.isMobile);
+ const activeWorkspaceSlug = useActiveWorkspaceSlug();
const [
addFilesToAgent,
addKnowledgeBasesToAgent,
@@ -63,12 +66,11 @@ const Actions = memo<ActionsProps>(({ id, type, enabled }) => {
key: 'detail',
label: t('knowledgeBase.library.action.detail'),
onClick: () => {
- if (type === KnowledgeType.KnowledgeBase) {
- window.open(`/resource/library/${id}`);
- return;
- }
-
- window.open(`/resource?file=${id}`);
+ window.open(
+ type === KnowledgeType.KnowledgeBase
+ ? buildLibraryPath(id, activeWorkspaceSlug)
+ : buildPagePath(id, activeWorkspaceSlug),
+ );
},
},
{
diff --git a/src/features/LibraryModal/CreateNew/CreateForm.tsx b/src/features/LibraryModal/CreateNew/CreateForm.tsx
index f7f8885ff45..080410dcb2b 100644
--- a/src/features/LibraryModal/CreateNew/CreateForm.tsx
+++ b/src/features/LibraryModal/CreateNew/CreateForm.tsx
@@ -3,7 +3,9 @@ import { Button } from '@lobehub/ui/base-ui';
import { memo, useState } from 'react';
import { useTranslation } from 'react-i18next';
+import { useActiveWorkspaceSlug } from '@/business/client/hooks/useActiveWorkspaceSlug';
import { useResourceManagerStore } from '@/features/ResourceManager/store';
+import { buildLibraryPath } from '@/features/ResourceManager/utils/resourcePath';
import { useKnowledgeBaseStore } from '@/store/library';
interface CreateFormProps {
@@ -26,6 +28,7 @@ const CreateForm = memo<CreateFormProps>(({ id, initialValues, onClose, onSucces
// personal-mode create still resolves to 'public' — matching the pre-column
// default and giving `buildWorkspaceWhere` nothing to filter on.
const listVisibility = useResourceManagerStore((s) => s.listVisibility);
+ const activeWorkspaceSlug = useActiveWorkspaceSlug();
const isEditMode = !!id;
@@ -59,7 +62,10 @@ const CreateForm = memo<CreateFormProps>(({ id, initialValues, onClose, onSucces
onSuccess(newId);
onClose?.();
} else {
- window.location.href = `/resource/library/${newId}`;
+ // Workspace routes are mounted under `/:workspaceSlug`, so the hard
+ // navigation must carry the active slug or it lands in the personal
+ // scope where the new library does not resolve.
+ window.location.href = buildLibraryPath(newId, activeWorkspaceSlug);
}
}
} catch (e) {
diff --git a/src/features/ModelSwitchPanel/components/PanelContent.tsx b/src/features/ModelSwitchPanel/components/PanelContent.tsx
index 32120792178..9e1242400af 100644
--- a/src/features/ModelSwitchPanel/components/PanelContent.tsx
+++ b/src/features/ModelSwitchPanel/components/PanelContent.tsx
@@ -47,16 +47,16 @@ export const PanelContent: FC<PanelContentProps> = ({
const content = (
<>
<Toolbar
+ showGroupModeSwitch
groupMode={groupMode}
searchKeyword={searchKeyword}
- showGroupModeSwitch={isDevMode}
onGroupModeChange={handleGroupModeChange}
onSearchKeywordChange={setSearchKeyword}
/>
<List
ModelItemComponent={ModelItemComponent}
enabledList={enabledList}
- groupMode={isDevMode ? groupMode : 'byModel'}
+ groupMode={groupMode}
model={modelProp}
pricingMode={pricingMode}
provider={providerProp}
diff --git a/src/features/ModelSwitchPanel/hooks/usePanelState.ts b/src/features/ModelSwitchPanel/hooks/usePanelState.ts
index 96e53b9f48b..28dd8bc3334 100644
--- a/src/features/ModelSwitchPanel/hooks/usePanelState.ts
+++ b/src/features/ModelSwitchPanel/hooks/usePanelState.ts
@@ -11,7 +11,7 @@ export const usePanelState = () => {
const handleGroupModeChange = useCallback(
(mode: GroupMode) => {
- updateSystemStatus({ modelSwitchPanelGroupMode: mode });
+ updateSystemStatus({ modelSwitchPanelGroupBy: mode });
},
[updateSystemStatus],
);
diff --git a/src/features/ResourceManager/components/Explorer/ItemDropdown/useFileItemDropdown.test.tsx b/src/features/ResourceManager/components/Explorer/ItemDropdown/useFileItemDropdown.test.tsx
index 037edc23369..20ed62316f0 100644
--- a/src/features/ResourceManager/components/Explorer/ItemDropdown/useFileItemDropdown.test.tsx
+++ b/src/features/ResourceManager/components/Explorer/ItemDropdown/useFileItemDropdown.test.tsx
@@ -10,6 +10,8 @@ interface SendToMessengerParams {
const mocks = vi.hoisted(() => ({
activeWorkspaceId: null as string | null,
+ activeWorkspaceSlug: null as string | null,
+ copyToClipboard: vi.fn(async (_text: string) => undefined),
confirmModal: vi.fn(),
deleteResource: vi.fn<() => Promise<void>>(async () => {}),
dropTreeNodes: vi.fn(async () => undefined),
@@ -56,6 +58,13 @@ vi.mock('@/store/library', () => ({ useKnowledgeBaseStore: () => [vi.fn(), vi.fn
vi.mock('@/business/client/hooks/useActiveWorkspaceId', () => ({
useActiveWorkspaceId: () => mocks.activeWorkspaceId,
}));
+vi.mock('@/business/client/hooks/useActiveWorkspaceSlug', () => ({
+ useActiveWorkspaceSlug: () => mocks.activeWorkspaceSlug,
+}));
+vi.mock('@lobehub/ui', async (importOriginal) => ({
+ ...(await importOriginal<Record<string, unknown>>()),
+ copyToClipboard: mocks.copyToClipboard,
+}));
vi.mock('@/store/tree', () => ({
useTreeStore: Object.assign(() => vi.fn(), {
@@ -78,6 +87,51 @@ const pushedFile = () => mocks.useSendToMessengerMenuItem.mock.calls.at(-1)![0].
beforeEach(() => {
vi.clearAllMocks();
mocks.activeWorkspaceId = null;
+ mocks.activeWorkspaceSlug = null;
+});
+
+/** @example Sharing a workspace page copies a link other members can open. */
+describe('useFileItemDropdown — copy link', () => {
+ // ROOT CAUSE:
+ //
+ // Workspace routes live under `/:workspaceSlug`, but the copied page link was
+ // built as `${origin}/resource?file=…` regardless of scope, so recipients
+ // opened it in the personal scope where the workspace page does not resolve.
+ const copyLink = async (params: Record<string, unknown> = {}) => {
+ const { result } = renderHook(() =>
+ useFileItemDropdown({ ...baseParams, fileType: 'custom/document', ...params } as any),
+ );
+ const item = result.current.menuItems().find((entry) => entry?.key === 'copyUrl') as any;
+ await item.onClick({ domEvent: { stopPropagation: vi.fn() } });
+ return mocks.copyToClipboard.mock.calls.at(-1)![0];
+ };
+
+ it('prefixes the page link with the active workspace slug', async () => {
+ mocks.activeWorkspaceId = 'ws-1';
+ mocks.activeWorkspaceSlug = 'acme';
+ await expect(copyLink({ id: 'docs_abc' })).resolves.toBe(
+ 'https://app.example.com/acme/resource?file=docs_abc',
+ );
+ });
+
+ it('keeps the library page link inside the workspace', async () => {
+ mocks.activeWorkspaceId = 'ws-1';
+ mocks.activeWorkspaceSlug = 'acme';
+ await expect(copyLink({ id: 'docs_abc', libraryId: 'kb_1' })).resolves.toBe(
+ 'https://app.example.com/acme/resource/library/kb_1?file=docs_abc',
+ );
+ });
+
+ it('leaves the personal-scope page link unprefixed', async () => {
+ await expect(copyLink({ id: 'docs_abc' })).resolves.toBe(
+ 'https://app.example.com/resource?file=docs_abc',
+ );
+ });
+
+ it('copies the storage URL for a regular file regardless of workspace', async () => {
+ mocks.activeWorkspaceSlug = 'acme';
+ await expect(copyLink({ fileType: 'markdown' })).resolves.toBe(baseParams.url);
+ });
});
describe('useFileItemDropdown — visibility toggles', () => {
diff --git a/src/features/ResourceManager/components/Explorer/ItemDropdown/useFileItemDropdown.tsx b/src/features/ResourceManager/components/Explorer/ItemDropdown/useFileItemDropdown.tsx
index 84f42b69b97..6784ba28888 100644
--- a/src/features/ResourceManager/components/Explorer/ItemDropdown/useFileItemDropdown.tsx
+++ b/src/features/ResourceManager/components/Explorer/ItemDropdown/useFileItemDropdown.tsx
@@ -19,10 +19,12 @@ import { useTranslation } from 'react-i18next';
import { shallow } from 'zustand/shallow';
import { useActiveWorkspaceId } from '@/business/client/hooks/useActiveWorkspaceId';
+import { useActiveWorkspaceSlug } from '@/business/client/hooks/useActiveWorkspaceSlug';
import RepoIcon from '@/components/LibIcon';
import { useSendToMessengerMenuItem } from '@/features/Messenger/PushResourceModal/useSendToMessengerMenuItem';
import { useKnowledgeBaseListContext } from '@/features/ResourceManager/components/KnowledgeBaseListProvider';
import { PAGE_FILE_TYPE } from '@/features/ResourceManager/constants';
+import { buildPagePath } from '@/features/ResourceManager/utils/resourcePath';
import VisibilityConfirmContent from '@/features/VisibilityConfirmContent';
import { useAppOrigin } from '@/hooks/useAppOrigin';
import { usePermission } from '@/hooks/usePermission';
@@ -110,6 +112,7 @@ export const useFileItemDropdown = ({
const { allowed: canEditResources } = usePermission('edit_own_content');
const currentUserId = useUserStore(userProfileSelectors.userId);
const activeWorkspaceId = useActiveWorkspaceId();
+ const activeWorkspaceSlug = useActiveWorkspaceSlug();
const {
deleteResource,
@@ -362,14 +365,13 @@ export const useFileItemDropdown = ({
onClick: async ({ domEvent }) => {
domEvent.stopPropagation();
- // For pages, use the route path instead of the storage URL
+ // For pages, use the route path instead of the storage URL. Workspace
+ // routes are mounted under `/:workspaceSlug`, so the shared link must
+ // carry the active slug or recipients land in the personal scope
+ // where the page does not resolve.
let urlToCopy = url;
if (isPage) {
- if (libraryId) {
- urlToCopy = `${appOrigin}/resource/library/${libraryId}?file=${id}`;
- } else {
- urlToCopy = `${appOrigin}/resource?file=${id}`;
- }
+ urlToCopy = `${appOrigin}${buildPagePath(id, activeWorkspaceSlug, libraryId)}`;
}
await copyToClipboard(urlToCopy);
@@ -503,6 +505,7 @@ export const useFileItemDropdown = ({
addFilesToKnowledgeBase,
appOrigin,
activeWorkspaceId,
+ activeWorkspaceSlug,
canEditResources,
currentUserId,
deleteResource,
diff --git a/src/features/ResourceManager/utils/resourcePath.test.ts b/src/features/ResourceManager/utils/resourcePath.test.ts
new file mode 100644
index 00000000000..4e857595357
--- /dev/null
+++ b/src/features/ResourceManager/utils/resourcePath.test.ts
@@ -0,0 +1,40 @@
+import { describe, expect, it } from 'vitest';
+
+import { buildLibraryPath, buildPagePath } from './resourcePath';
+
+/** @example Links and detail windows built by hand land inside the workspace. */
+describe('resourcePath', () => {
+ // ROOT CAUSE:
+ //
+ // Hand-built resource URLs (copy link, library "detail", post-create landing)
+ // bypassed `useWorkspaceAwareNavigate` and were emitted as `/resource…` even
+ // inside a workspace, whose routes are mounted under `/:workspaceSlug`.
+ describe('buildLibraryPath', () => {
+ it('prefixes the library path with the active workspace slug', () => {
+ expect(buildLibraryPath('kb_1', 'acme')).toBe('/acme/resource/library/kb_1');
+ });
+
+ it('leaves the personal-scope library path unprefixed', () => {
+ expect(buildLibraryPath('kb_1', null)).toBe('/resource/library/kb_1');
+ });
+ });
+
+ describe('buildPagePath', () => {
+ it('prefixes the page path with the active workspace slug', () => {
+ expect(buildPagePath('docs_abc', 'acme')).toBe('/acme/resource?file=docs_abc');
+ });
+
+ it('keeps a library page inside the workspace', () => {
+ expect(buildPagePath('docs_abc', 'acme', 'kb_1')).toBe(
+ '/acme/resource/library/kb_1?file=docs_abc',
+ );
+ });
+
+ it('leaves the personal-scope page path unprefixed', () => {
+ expect(buildPagePath('docs_abc', null)).toBe('/resource?file=docs_abc');
+ expect(buildPagePath('docs_abc', undefined, 'kb_1')).toBe(
+ '/resource/library/kb_1?file=docs_abc',
+ );
+ });
+ });
+});
diff --git a/src/features/ResourceManager/utils/resourcePath.ts b/src/features/ResourceManager/utils/resourcePath.ts
new file mode 100644
index 00000000000..bc041b8368f
--- /dev/null
+++ b/src/features/ResourceManager/utils/resourcePath.ts
@@ -0,0 +1,31 @@
+import { buildWorkspaceAwarePath } from '@/features/Workspace/workspaceAwarePath';
+
+/**
+ * Route builders for resource surfaces that are reached outside the router's
+ * own navigation (copied links, `window.open`, `window.location`).
+ *
+ * `useWorkspaceAwareNavigate` prefixes in-app navigation automatically, but
+ * these hand-built URLs must carry the active workspace slug themselves or
+ * they land in the personal scope, where a workspace library or page does not
+ * resolve.
+ */
+
+/** Path of a library's explorer: `/resource/library/<id>`. */
+export const buildLibraryPath = (
+ libraryId: string,
+ activeWorkspaceSlug: string | null | undefined,
+): string => buildWorkspaceAwarePath(`/resource/library/${libraryId}`, activeWorkspaceSlug);
+
+/**
+ * Path that opens a page: `/resource?file=<id>`, or the library-scoped
+ * `/resource/library/<libraryId>?file=<id>` when the page lives in a library.
+ */
+export const buildPagePath = (
+ fileId: string,
+ activeWorkspaceSlug: string | null | undefined,
+ libraryId?: string | null,
+): string =>
+ buildWorkspaceAwarePath(
+ libraryId ? `/resource/library/${libraryId}?file=${fileId}` : `/resource?file=${fileId}`,
+ activeWorkspaceSlug,
+ );
diff --git a/src/store/global/initialState.ts b/src/store/global/initialState.ts
index 1958c6b9fdb..e4aa9062290 100644
--- a/src/store/global/initialState.ts
+++ b/src/store/global/initialState.ts
@@ -262,9 +262,16 @@ export interface SystemStatus {
*/
modelDetailPanelCollapsedKeys?: ModelDetailPanelExpandedKey[];
/**
- * ModelSwitchPanel grouping mode
+ * ModelSwitchPanel grouping preference. Only ever written by the user's own
+ * switch; the store must not seed a default here, because
+ * `updateSystemStatus` persists the whole merged status and a seeded value
+ * is indistinguishable from a chosen one.
+ *
+ * Replaces the legacy `modelSwitchPanelGroupMode` key, which was seeded with
+ * `'byProvider'` and therefore sits in existing users' storage without them
+ * having picked it. That key is intentionally never read again.
*/
- modelSwitchPanelGroupMode?: 'byModel' | 'byProvider';
+ modelSwitchPanelGroupBy?: 'byModel' | 'byProvider';
/**
* ModelSwitchPanel width
*/
@@ -551,7 +558,6 @@ export const INITIAL_STATUS = {
leftPanelWidth: 280,
mobileShowTopic: false,
modelDetailPanelCollapsedKeys: [],
- modelSwitchPanelGroupMode: 'byProvider',
modelSwitchPanelWidth: 460,
noWideScreen: true,
pageAgentPanelWidth: 360,
diff --git a/src/store/global/selectors/systemStatus.test.ts b/src/store/global/selectors/systemStatus.test.ts
index 2b79a212664..cc136549698 100644
--- a/src/store/global/selectors/systemStatus.test.ts
+++ b/src/store/global/selectors/systemStatus.test.ts
@@ -175,6 +175,42 @@ describe('systemStatusSelectors', () => {
});
});
+ describe('modelSwitchPanelGroupMode', () => {
+ it('should default to byModel so the everyday list looks unchanged before the user flips the switch', () => {
+ const s: GlobalState = {
+ ...initialState,
+ status: { ...initialState.status, modelSwitchPanelGroupBy: undefined },
+ };
+
+ expect(systemStatusSelectors.modelSwitchPanelGroupMode(s)).toBe('byModel');
+ });
+
+ it('should not seed the preference into the initial status', () => {
+ // `updateSystemStatus` persists the whole merged status, so a seeded
+ // value would be written to storage as if the user had chosen it.
+ expect(INITIAL_STATUS).not.toHaveProperty('modelSwitchPanelGroupBy');
+ });
+
+ it('should return the persisted byProvider preference', () => {
+ const s: GlobalState = merge(initialState, {
+ status: { modelSwitchPanelGroupBy: 'byProvider' },
+ });
+
+ expect(systemStatusSelectors.modelSwitchPanelGroupMode(s)).toBe('byProvider');
+ });
+
+ it('should ignore the legacy seeded byProvider value from a pre-upgrade persisted status', () => {
+ // Before the key was renamed, the store seeded `byProvider` and persisted
+ // it for everyone; the switch itself was hidden behind developer tools,
+ // so these users were effectively on `byModel` and must stay there.
+ const s: GlobalState = merge(initialState, {
+ status: { modelSwitchPanelGroupMode: 'byProvider' } as Record<string, unknown>,
+ });
+
+ expect(systemStatusSelectors.modelSwitchPanelGroupMode(s)).toBe('byModel');
+ });
+ });
+
describe('taskListViewMode', () => {
it('should restore the persisted task board view', () => {
const s: GlobalState = {
diff --git a/src/store/global/selectors/systemStatus.ts b/src/store/global/selectors/systemStatus.ts
index 7362c6c343e..e0afd0a3858 100644
--- a/src/store/global/selectors/systemStatus.ts
+++ b/src/store/global/selectors/systemStatus.ts
@@ -405,8 +405,9 @@ const modelDetailPanelExpandedKeys = (s: GlobalState): ModelDetailPanelExpandedK
return MODEL_DETAIL_PANEL_EXPANDABLE_KEYS.filter((key) => !collapsedKeys.includes(key));
};
-const modelSwitchPanelGroupMode = (s: GlobalState) =>
- s.status.modelSwitchPanelGroupMode || 'byProvider';
+// Reads only the user-written key; the legacy `modelSwitchPanelGroupMode`
+// was seeded into persisted status and must not influence the default.
+const modelSwitchPanelGroupMode = (s: GlobalState) => s.status.modelSwitchPanelGroupBy || 'byModel';
const modelSwitchPanelWidth = (s: GlobalState) => s.status.modelSwitchPanelWidth || 460;
const pageAgentPanelWidth = (s: GlobalState) => s.status.pageAgentPanelWidth || 360;
const workingSidebarWidth = (s: GlobalState) => s.status.workingSidebarWidth || 360;