From 70bea856244348639b44dea7ae2c5ab3cb95c1cd Mon Sep 17 00:00:00 2001 From: yyh Date: Sun, 18 Jan 2026 16:10:04 +0800 Subject: [PATCH] refactor(web): improve storage utility type safety with function overloads Add function overloads to getNumber and getBoolean so they return non-nullable types when a defaultValue is provided. This eliminates the need for non-null assertions at call sites. - Remove unused persist-config.ts (zustand adapter not needed) - Remove ! assertions from layout-slice.ts --- .../components/workflow/store/workflow/layout-slice.ts | 8 ++++---- .../components/workflow/store/workflow/persist-config.ts | 8 -------- web/utils/storage.ts | 4 ++++ 3 files changed, 8 insertions(+), 12 deletions(-) delete mode 100644 web/app/components/workflow/store/workflow/persist-config.ts diff --git a/web/app/components/workflow/store/workflow/layout-slice.ts b/web/app/components/workflow/store/workflow/layout-slice.ts index 3380c6cf27..a55c93dec1 100644 --- a/web/app/components/workflow/store/workflow/layout-slice.ts +++ b/web/app/components/workflow/store/workflow/layout-slice.ts @@ -32,9 +32,9 @@ export const createLayoutSlice: StateCreator = set => ({ setWorkflowCanvasHeight: height => set(() => ({ workflowCanvasHeight: height })), rightPanelWidth: undefined, setRightPanelWidth: width => set(() => ({ rightPanelWidth: width })), - nodePanelWidth: storage.getNumber(STORAGE_KEYS.WORKFLOW.NODE_PANEL_WIDTH, 400)!, + nodePanelWidth: storage.getNumber(STORAGE_KEYS.WORKFLOW.NODE_PANEL_WIDTH, 400), setNodePanelWidth: width => set(() => ({ nodePanelWidth: width })), - previewPanelWidth: storage.getNumber(STORAGE_KEYS.WORKFLOW.PREVIEW_PANEL_WIDTH, 400)!, + previewPanelWidth: storage.getNumber(STORAGE_KEYS.WORKFLOW.PREVIEW_PANEL_WIDTH, 400), setPreviewPanelWidth: width => set(() => ({ previewPanelWidth: width })), otherPanelWidth: 400, setOtherPanelWidth: width => set(() => ({ otherPanelWidth: width })), @@ -42,8 +42,8 @@ export const createLayoutSlice: StateCreator = set => ({ setBottomPanelWidth: width => set(() => ({ bottomPanelWidth: width })), bottomPanelHeight: 324, setBottomPanelHeight: height => set(() => ({ bottomPanelHeight: height })), - variableInspectPanelHeight: storage.getNumber(STORAGE_KEYS.WORKFLOW.VARIABLE_INSPECT_PANEL_HEIGHT, 320)!, + variableInspectPanelHeight: storage.getNumber(STORAGE_KEYS.WORKFLOW.VARIABLE_INSPECT_PANEL_HEIGHT, 320), setVariableInspectPanelHeight: height => set(() => ({ variableInspectPanelHeight: height })), - maximizeCanvas: storage.getBoolean(STORAGE_KEYS.WORKFLOW.CANVAS_MAXIMIZE, false)!, + maximizeCanvas: storage.getBoolean(STORAGE_KEYS.WORKFLOW.CANVAS_MAXIMIZE, false), setMaximizeCanvas: maximize => set(() => ({ maximizeCanvas: maximize })), }) diff --git a/web/app/components/workflow/store/workflow/persist-config.ts b/web/app/components/workflow/store/workflow/persist-config.ts deleted file mode 100644 index b560f2b832..0000000000 --- a/web/app/components/workflow/store/workflow/persist-config.ts +++ /dev/null @@ -1,8 +0,0 @@ -import type { StateStorage } from 'zustand/middleware' -import { storage } from '@/utils/storage' - -export const createZustandStorage = (): StateStorage => ({ - getItem: (name: string) => storage.get(name), - setItem: storage.set, - removeItem: storage.remove, -}) diff --git a/web/utils/storage.ts b/web/utils/storage.ts index aef1ce69aa..640861fd4b 100644 --- a/web/utils/storage.ts +++ b/web/utils/storage.ts @@ -73,6 +73,8 @@ function remove(key: string): void { } } +function getNumber(key: string): number | null +function getNumber(key: string, defaultValue: number): number function getNumber(key: string, defaultValue?: number): number | null { const value = get(key) if (value === null) @@ -82,6 +84,8 @@ function getNumber(key: string, defaultValue?: number): number | null { return Number.isNaN(parsed) ? (defaultValue ?? null) : parsed } +function getBoolean(key: string): boolean | null +function getBoolean(key: string, defaultValue: boolean): boolean function getBoolean(key: string, defaultValue?: boolean): boolean | null { const value = get(key) if (value === null)