π€― 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.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
π fix(web): gate desktop-only electron SWR hooks off-web (#19074)
Arvin Xu8f3053777540
diff --git a/src/store/electron/actions/__tests__/webSuspenseGuard.test.ts b/src/store/electron/actions/__tests__/webSuspenseGuard.test.tsnew file mode 100644index 00000000000..3d4d6eefe02--- /dev/null+++ b/src/store/electron/actions/__tests__/webSuspenseGuard.test.ts@@ -0,0 +1,61 @@+import { renderHook, waitFor } from '@testing-library/react';+import { Component, createElement, type ReactNode, Suspense } from 'react';+import { SWRConfig } from 'swr';+import { describe, expect, it } from 'vitest';++import { type ElectronStore, useElectronStore } from '@/store/electron';++/**+ * Regression: on web (non-desktop) these SWR hooks used to call the Electron+ * IPC unconditionally. The fetch always rejected with "electronAPI.invoke not+ * found", which was silent until layouts adopted `SWRConfig { suspense: true }`+ * β then the rejection was thrown during render and the route boundary+ * replaced the whole page with a load-failure screen (/settings/devices,+ * /settings/proxy). The keys must be null off-desktop so no fetch starts.+ */++class Boundary extends Component<{ children: ReactNode }, { error?: Error }> {+ state: { error?: Error } = {};++ static getDerivedStateFromError(error: Error) {+ return { error };+ }++ render() {+ return this.state.error ? `boundary: ${this.state.error.message}` : this.props.children;+ }+}++const desktopOnlySwrHooks = [+ 'useFetchGatewayDeviceInfo',+ 'useFetchGatewayStatus',+ 'useFetchDesktopHotkeys',+ 'useGetProxySettings',+ 'useDataSyncConfig',+] as const;++describe('desktop-only SWR hooks under a suspense SWRConfig on web', () => {+ it.each(desktopOnlySwrHooks)('%s neither fetches nor throws', async (hookName) => {+ const rendered: string[] = [];++ renderHook(+ () => {+ const useHook = useElectronStore((s) => s[hookName] as ElectronStore[typeof hookName]);+ useHook();+ rendered.push(hookName);+ },+ {+ wrapper: ({ children }) =>+ createElement(+ SWRConfig,+ { value: { provider: () => new Map(), suspense: true } },+ createElement(Boundary, undefined, createElement(Suspense, undefined, children)),+ ),+ },+ );++ // Before the fix the hook suspended on the doomed IPC fetch, the rejection+ // hit the boundary, and the hook body never (re)rendered to completion.+ await waitFor(() => expect(rendered.length).toBeGreaterThan(0));+ });+});diff --git a/src/store/electron/actions/gateway.ts b/src/store/electron/actions/gateway.tsindex 1d2be406ba4..3878bf3e5de 100644--- a/src/store/electron/actions/gateway.ts+++ b/src/store/electron/actions/gateway.ts@@ -1,3 +1,4 @@+import { isDesktop } from '@lobechat/const';import type { GatewayConnectionStatus } from '@lobechat/electron-client-ipc';import { type SWRResponse } from 'swr';import useSWR from 'swr';@@ -55,7 +56,10 @@ export class ElectronGatewayActionImpl {useFetchGatewayDeviceInfo = (): SWRResponse<GatewayDeviceInfo> => {return useSWR<GatewayDeviceInfo>(- electronKeys.gatewayDeviceInfo(),+ // Desktop-only IPC: on web there is no electronAPI, and under a+ // suspense-mode SWRConfig a rejected fetch throws into the route+ // boundary instead of staying in `error` β so never fetch off-desktop.+ isDesktop ? electronKeys.gatewayDeviceInfo() : null,async () => gatewayConnectionService.getDeviceInfo() as Promise<GatewayDeviceInfo>,{onSuccess: (data) => {@@ -67,7 +71,7 @@ export class ElectronGatewayActionImpl {useFetchGatewayStatus = (): SWRResponse<{ status: GatewayConnectionStatus }> => {return useSWR<{ status: GatewayConnectionStatus }>(- 'electron:getGatewayConnectionStatus',+ isDesktop ? 'electron:getGatewayConnectionStatus' : null,async () => gatewayConnectionService.getConnectionStatus(),{onSuccess: (data) => {diff --git a/src/store/electron/actions/settings.ts b/src/store/electron/actions/settings.tsindex ec6165ac1d3..54d48c10fe3 100644--- a/src/store/electron/actions/settings.ts+++ b/src/store/electron/actions/settings.ts@@ -1,3 +1,4 @@+import { isDesktop } from '@lobechat/const';import {type NetworkProxySettings,type ShortcutUpdateResult,@@ -76,7 +77,10 @@ export class ElectronSettingsActionImpl {useFetchDesktopHotkeys = (): SWRResponse => {return useSWR<Record<string, string>>(- electronKeys.desktopHotkeys(),+ // Desktop-only IPC: on web there is no electronAPI, and under a+ // suspense-mode SWRConfig a rejected fetch throws into the route+ // boundary instead of staying in `error` β so never fetch off-desktop.+ isDesktop ? electronKeys.desktopHotkeys() : null,async () => desktopSettingsService.getDesktopHotkeys(),{onSuccess: (data) => {@@ -90,7 +94,7 @@ export class ElectronSettingsActionImpl {useGetAppTrayVisible = (enabled = true): SWRResponse => {return useSWR<boolean>(- enabled ? electronKeys.appTrayVisible() : null,+ enabled && isDesktop ? electronKeys.appTrayVisible() : null,async () => desktopSettingsService.getAppTrayVisible(),{onSuccess: (data) => {@@ -104,7 +108,7 @@ export class ElectronSettingsActionImpl {useGetProxySettings = (): SWRResponse => {return useSWR<NetworkProxySettings>(- electronKeys.proxySettings(),+ isDesktop ? electronKeys.proxySettings() : null,async () => desktopSettingsService.getProxySettings(),{onSuccess: (data) => {diff --git a/src/store/electron/actions/sync.ts b/src/store/electron/actions/sync.tsindex f15329187a3..b1359a0e361 100644--- a/src/store/electron/actions/sync.ts+++ b/src/store/electron/actions/sync.ts@@ -1,3 +1,4 @@+import { isDesktop } from '@lobechat/const';import { type DataSyncConfig } from '@lobechat/electron-client-ipc';import isEqual from 'fast-deep-equal';import { type SWRResponse } from 'swr';@@ -110,7 +111,10 @@ export class ElectronRemoteServerActionImpl {useDataSyncConfig = (): SWRResponse => {return useSWR<DataSyncConfig>(- electronKeys.remoteServerConfig(),+ // Desktop-only IPC: on web there is no electronAPI, and under a+ // suspense-mode SWRConfig a rejected fetch throws into the route+ // boundary instead of staying in `error` β so never fetch off-desktop.+ isDesktop ? electronKeys.remoteServerConfig() : null,async () => {try {return await remoteServerService.getRemoteServerConfig();