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(desktop): navigate after sharing a device to a workspace (#20034)

Nekob463baba9198
diff --git a/src/features/DeviceManager/ShareDeviceModal.tsx b/src/features/DeviceManager/ShareDeviceModal.tsx
index 5e9a6a283d0..d78d53c1ea9 100644
--- a/src/features/DeviceManager/ShareDeviceModal.tsx
+++ b/src/features/DeviceManager/ShareDeviceModal.tsx
@@ -17,10 +17,10 @@ import { t } from 'i18next';
import { CircleCheck, Lock, Users } from 'lucide-react';
import { memo, useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next';
-import { useNavigate } from 'react-router';
import { useWorkspaceOptionLabel } from '@/business/client/hooks/useWorkspaceOptionLabel';
import { useWorkspaces } from '@/business/client/hooks/useWorkspaces';
+import { useWorkspaceAwareNavigate } from '@/features/Workspace/useWorkspaceAwareNavigate';
import { createWorkspaceLambdaClient } from '@/libs/trpc/client';
import { refreshDeviceList } from './const';
@@ -72,7 +72,7 @@ interface CompletionState {
const ShareDeviceContent = memo<ShareDeviceContentProps>(({ device }) => {
const { t: tSetting } = useTranslation(['setting', 'common']);
const { close, setCanDismissByClickOutside } = useModalContext();
- const navigate = useNavigate();
+ const navigate = useWorkspaceAwareNavigate();
const workspaces = useWorkspaces();
const renderWorkspaceLabel = useWorkspaceOptionLabel();
@@ -205,7 +205,9 @@ const ShareDeviceContent = memo<ShareDeviceContentProps>(({ device }) => {
const goToTarget = () => {
if (!completion) return;
- navigate(`/${completion.slug}/settings/devices`);
+ // The global modal host sits outside Electron's tab routers. Route through
+ // the active tab and preserve the explicitly selected workspace destination.
+ navigate(`/${completion.slug}/settings/devices`, { escape: true });
close();
};
diff --git a/src/features/Workspace/useWorkspaceAwareNavigate.desktop.test.ts b/src/features/Workspace/useWorkspaceAwareNavigate.desktop.test.ts
index 509bcbabee6..a5bc20f49b9 100644
--- a/src/features/Workspace/useWorkspaceAwareNavigate.desktop.test.ts
+++ b/src/features/Workspace/useWorkspaceAwareNavigate.desktop.test.ts
@@ -1,6 +1,15 @@
-import { renderHook } from '@testing-library/react';
+import { ModalHost } from '@lobehub/ui/base-ui';
+import {
+ act,
+ cleanup,
+ fireEvent,
+ render,
+ renderHook,
+ screen,
+ waitFor,
+} from '@testing-library/react';
import { createElement, type ReactNode } from 'react';
-import { createMemoryRouter } from 'react-router';
+import { createMemoryRouter, RouterProvider } from 'react-router';
import { afterEach, describe, expect, it, vi } from 'vitest';
import * as slugModule from '@/business/client/hooks/useActiveWorkspaceSlug';
@@ -11,9 +20,31 @@ import {
} from '@/features/Electron/TabHost';
const mocks = vi.hoisted(() => ({
+ shareDevice: vi.fn().mockResolvedValue({ success: true }),
storeState: { activeTabId: 'tA' as string | null, addNewTab: vi.fn() },
}));
+vi.mock('@/business/client/hooks/useWorkspaces', () => ({
+ useWorkspaces: () => [{ id: 'workspace-target', name: 'Target', role: 'member', slug: 'target' }],
+}));
+
+vi.mock('@/libs/trpc/client', () => ({
+ createWorkspaceLambdaClient: () => ({
+ device: { shareDeviceToWorkspace: { mutate: mocks.shareDevice } },
+ }),
+}));
+
+vi.mock('@/features/DeviceManager/const', () => ({ refreshDeviceList: vi.fn() }));
+
+// NOTICE:
+// Exercise the same platform module replacement as the Electron renderer.
+// Vitest otherwise resolves the Web hook used by the shared modal.
+// Source: useWorkspaceAwareNavigate.desktop.ts and the Vite platform resolver.
+// Remove when this test project resolves desktop twins automatically.
+vi.mock('@/features/Workspace/useWorkspaceAwareNavigate', () =>
+ vi.importActual('@/features/Workspace/useWorkspaceAwareNavigate.desktop'),
+);
+
vi.mock('@/store/electron', () => ({
useElectronStore: Object.assign(
(selector: (s: unknown) => unknown) => selector(mocks.storeState),
@@ -46,6 +77,7 @@ const renderNavigate = (tabId?: string) =>
}).result.current;
afterEach(() => {
+ cleanup();
resetTabRouterManager();
vi.restoreAllMocks();
mocks.storeState.activeTabId = 'tA';
@@ -53,6 +85,49 @@ afterEach(() => {
});
describe('useWorkspaceAwareNavigate (desktop)', () => {
+ /** @example Sharing from personal settings opens the destination in the visible tab. */
+ it('navigates the active tab from the globally hosted device-share completion', async () => {
+ // ROOT CAUSE:
+ // The share modal is rendered by the global ModalHost outside the tab router.
+ // Its raw useNavigate updated the root router, leaving the visible tab unchanged.
+ // The workspace-aware desktop hook instead resolves the active tab router.
+ const { openShareDeviceModal } = await import('@/features/DeviceManager/ShareDeviceModal');
+ vi.spyOn(slugModule, 'getActiveWorkspaceSlug').mockReturnValue('source');
+ const tabRouter = getOrCreateTabRouter('tA', '/settings/devices', makeRouter);
+ const rootRouter = createMemoryRouter([{ element: createElement(ModalHost), path: '*' }], {
+ initialEntries: ['/'],
+ });
+ render(createElement(RouterProvider, { router: rootRouter }));
+
+ act(() => {
+ openShareDeviceModal({
+ channels: [],
+ defaultCwd: null,
+ deviceId: 'local-device',
+ enroller: null,
+ friendlyName: null,
+ hostname: 'Local Mac',
+ identitySource: 'machine-id',
+ lastSeen: new Date(0).toISOString(),
+ online: true,
+ platform: 'darwin',
+ registered: true,
+ scope: 'personal',
+ visibility: null,
+ workingDirs: [],
+ });
+ });
+ fireEvent.click(await screen.findByRole('button', { name: 'devices.share.confirm' }));
+ fireEvent.click(await screen.findByRole('button', { name: 'devices.share.goToTarget' }));
+
+ /** @example The tab reaches /target/settings/devices while the root stays frozen. */
+ await waitFor(() => expect(tabRouter.state.location.pathname).toBe('/target/settings/devices'));
+ expect(rootRouter.state.location.pathname).toBe('/');
+ /** @example The success modal closes after navigation. */
+ await waitFor(() => expect(screen.queryByRole('dialog')).toBeNull());
+ rootRouter.dispose();
+ });
+
it('shell (no tab context): navigates the active tab with the workspace-resolved url', () => {
vi.spyOn(slugModule, 'getActiveWorkspaceSlug').mockReturnValue('acme');
const navigateA = seedRouter('tA');