mirror of
https://github.com/langgenius/dify.git
synced 2026-03-19 13:47:37 +08:00
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
This commit is contained in:
@ -32,9 +32,9 @@ export const createLayoutSlice: StateCreator<LayoutSliceShape> = 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<LayoutSliceShape> = 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 })),
|
||||
})
|
||||
|
||||
@ -1,8 +0,0 @@
|
||||
import type { StateStorage } from 'zustand/middleware'
|
||||
import { storage } from '@/utils/storage'
|
||||
|
||||
export const createZustandStorage = (): StateStorage => ({
|
||||
getItem: (name: string) => storage.get<string>(name),
|
||||
setItem: storage.set,
|
||||
removeItem: storage.remove,
|
||||
})
|
||||
@ -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<string | number>(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<string | boolean>(key)
|
||||
if (value === null)
|
||||
|
||||
Reference in New Issue
Block a user