From 03ec2f64cd0a27d555fda8d8d5503adfdfe81e46 Mon Sep 17 00:00:00 2001 From: zhsama Date: Fri, 30 Jan 2026 23:23:21 +0800 Subject: [PATCH] refactor: Refactor storage keys into hierarchical structure --- .../config/automatic/get-automatic-res.tsx | 16 +++++++------- .../code-generator/get-code-generator-res.tsx | 16 +++++++------- .../components/app/create-app-modal/index.tsx | 5 ++--- .../workflow-app/hooks/use-workflow-init.ts | 3 ++- .../json-schema-generator/index.tsx | 12 +++++------ .../hooks/use-context-generate.ts | 6 +++--- .../context-generate-modal/utils/storage.ts | 4 ++-- web/app/components/workflow/skill/sidebar.tsx | 4 ++-- web/config/storage-keys.ts | 21 +++++++++++++++---- 9 files changed, 52 insertions(+), 35 deletions(-) diff --git a/web/app/components/app/configuration/config/automatic/get-automatic-res.tsx b/web/app/components/app/configuration/config/automatic/get-automatic-res.tsx index 28e8299710..6478e06fec 100644 --- a/web/app/components/app/configuration/config/automatic/get-automatic-res.tsx +++ b/web/app/components/app/configuration/config/automatic/get-automatic-res.tsx @@ -84,8 +84,8 @@ const GetAutomaticRes: FC = ({ onFinished, }) => { const { t } = useTranslation() - const localModel = localStorage.getItem(STORAGE_KEYS.GENERATOR.AUTO_GEN_MODEL) - ? JSON.parse(localStorage.getItem(STORAGE_KEYS.GENERATOR.AUTO_GEN_MODEL) as string) as Model + const localModel = localStorage.getItem(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL) + ? JSON.parse(localStorage.getItem(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL) as string) as Model : null const [model, setModel] = React.useState(localModel || { name: '', @@ -135,7 +135,9 @@ const GetAutomaticRes: FC = ({ }, ] as const - const [instructionFromSessionStorage, setInstruction] = useSessionStorageState(`improve-instruction-${flowId}${isBasicMode ? '' : `-${nodeId}${editorId ? `-${editorId}` : ''}`}`) + const [instructionFromSessionStorage, setInstruction] = useSessionStorageState( + `${STORAGE_KEYS.SESSION.GENERATOR.INSTRUCTION_PREFIX}${flowId}${isBasicMode ? '' : `-${nodeId}${editorId ? `-${editorId}` : ''}`}`, + ) const instruction = instructionFromSessionStorage || '' const [ideaOutput, setIdeaOutput] = useState('') @@ -179,8 +181,8 @@ const GetAutomaticRes: FC = ({ useEffect(() => { if (defaultModel) { - const localModel = localStorage.getItem(STORAGE_KEYS.GENERATOR.AUTO_GEN_MODEL) - ? JSON.parse(localStorage.getItem(STORAGE_KEYS.GENERATOR.AUTO_GEN_MODEL) || '') + const localModel = localStorage.getItem(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL) + ? JSON.parse(localStorage.getItem(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL) || '') : null if (localModel) { setModel(localModel) @@ -210,7 +212,7 @@ const GetAutomaticRes: FC = ({ mode: newValue.mode as ModelModeType, } setModel(newModel) - localStorage.setItem(STORAGE_KEYS.GENERATOR.AUTO_GEN_MODEL, JSON.stringify(newModel)) + localStorage.setItem(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL, JSON.stringify(newModel)) }, [model, setModel]) const handleCompletionParamsChange = useCallback((newParams: FormValue) => { @@ -219,7 +221,7 @@ const GetAutomaticRes: FC = ({ completion_params: newParams as CompletionParams, } setModel(newModel) - localStorage.setItem(STORAGE_KEYS.GENERATOR.AUTO_GEN_MODEL, JSON.stringify(newModel)) + localStorage.setItem(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL, JSON.stringify(newModel)) }, [model, setModel]) const onGenerate = async () => { diff --git a/web/app/components/app/configuration/config/code-generator/get-code-generator-res.tsx b/web/app/components/app/configuration/config/code-generator/get-code-generator-res.tsx index 35bf48732f..333a6c7c0d 100644 --- a/web/app/components/app/configuration/config/code-generator/get-code-generator-res.tsx +++ b/web/app/components/app/configuration/config/code-generator/get-code-generator-res.tsx @@ -54,8 +54,8 @@ export const GetCodeGeneratorResModal: FC = ( }, ) => { const { t } = useTranslation() - const localModel = localStorage.getItem(STORAGE_KEYS.GENERATOR.AUTO_GEN_MODEL) - ? JSON.parse(localStorage.getItem(STORAGE_KEYS.GENERATOR.AUTO_GEN_MODEL) as string) as Model + const localModel = localStorage.getItem(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL) + ? JSON.parse(localStorage.getItem(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL) as string) as Model : null const [model, setModel] = React.useState(localModel || { name: '', @@ -66,7 +66,9 @@ export const GetCodeGeneratorResModal: FC = ( const { defaultModel, } = useModelListAndDefaultModelAndCurrentProviderAndModel(ModelTypeEnum.textGeneration) - const [instructionFromSessionStorage, setInstruction] = useSessionStorageState(`improve-instruction-${flowId}-${nodeId}`) + const [instructionFromSessionStorage, setInstruction] = useSessionStorageState( + `${STORAGE_KEYS.SESSION.GENERATOR.INSTRUCTION_PREFIX}${flowId}-${nodeId}`, + ) const instruction = instructionFromSessionStorage || '' const [ideaOutput, setIdeaOutput] = useState('') @@ -107,7 +109,7 @@ export const GetCodeGeneratorResModal: FC = ( mode: newValue.mode as ModelModeType, } setModel(newModel) - localStorage.setItem(STORAGE_KEYS.GENERATOR.AUTO_GEN_MODEL, JSON.stringify(newModel)) + localStorage.setItem(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL, JSON.stringify(newModel)) }, [model, setModel]) const handleCompletionParamsChange = useCallback((newParams: FormValue) => { @@ -116,7 +118,7 @@ export const GetCodeGeneratorResModal: FC = ( completion_params: newParams as CompletionParams, } setModel(newModel) - localStorage.setItem(STORAGE_KEYS.GENERATOR.AUTO_GEN_MODEL, JSON.stringify(newModel)) + localStorage.setItem(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL, JSON.stringify(newModel)) }, [model, setModel]) const onGenerate = async () => { @@ -167,8 +169,8 @@ export const GetCodeGeneratorResModal: FC = ( useEffect(() => { if (defaultModel) { - const localModel = localStorage.getItem(STORAGE_KEYS.GENERATOR.AUTO_GEN_MODEL) - ? JSON.parse(localStorage.getItem(STORAGE_KEYS.GENERATOR.AUTO_GEN_MODEL) || '') + const localModel = localStorage.getItem(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL) + ? JSON.parse(localStorage.getItem(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL) || '') : null if (localModel) { setModel({ diff --git a/web/app/components/app/create-app-modal/index.tsx b/web/app/components/app/create-app-modal/index.tsx index 47008c9f74..5325450c5d 100644 --- a/web/app/components/app/create-app-modal/index.tsx +++ b/web/app/components/app/create-app-modal/index.tsx @@ -22,6 +22,7 @@ import Textarea from '@/app/components/base/textarea' import { ToastContext } from '@/app/components/base/toast' import AppsFull from '@/app/components/billing/apps-full-in-dialog' import { NEED_REFRESH_APP_LIST_KEY } from '@/config' +import { STORAGE_KEYS } from '@/config/storage-keys' import { useAppContext } from '@/context/app-context' import { useProviderContext } from '@/context/provider-context' import useTheme from '@/hooks/use-theme' @@ -47,8 +48,6 @@ type RuntimeOption = { recommended?: boolean } -const WORKFLOW_RUNTIME_STORAGE_KEY_PREFIX = 'workflow:sandbox-runtime:' - function CreateApp({ onClose, onSuccess, onCreateFromTemplate, defaultAppMode }: CreateAppProps) { const { t } = useTranslation() const { push } = useRouter() @@ -99,7 +98,7 @@ function CreateApp({ onClose, onSuccess, onCreateFromTemplate, defaultAppMode }: }) if (runtimeMode === 'sandboxed' && (appMode === AppModeEnum.WORKFLOW || appMode === AppModeEnum.ADVANCED_CHAT)) - localStorage.setItem(`${WORKFLOW_RUNTIME_STORAGE_KEY_PREFIX}${app.id}`, '1') + localStorage.setItem(`${STORAGE_KEYS.LOCAL.WORKFLOW.SANDBOX_RUNTIME_PREFIX}${app.id}`, '1') // Track app creation success trackEvent('create_app', { diff --git a/web/app/components/workflow-app/hooks/use-workflow-init.ts b/web/app/components/workflow-app/hooks/use-workflow-init.ts index 817e4a566f..93600151b6 100644 --- a/web/app/components/workflow-app/hooks/use-workflow-init.ts +++ b/web/app/components/workflow-app/hooks/use-workflow-init.ts @@ -12,6 +12,7 @@ import { useWorkflowStore, } from '@/app/components/workflow/store' import { BlockEnum } from '@/app/components/workflow/types' +import { STORAGE_KEYS } from '@/config/storage-keys' import { useWorkflowConfig } from '@/service/use-workflow' import { fetchNodesDefaultConfigs, @@ -85,7 +86,7 @@ export const useWorkflowInit = () => { const nodesData = isAdvancedChat ? nodesTemplate : [] const edgesData = isAdvancedChat ? edgesTemplate : [] - const runtimeStorageKey = `workflow:sandbox-runtime:${appDetail.id}` + const runtimeStorageKey = `${STORAGE_KEYS.LOCAL.WORKFLOW.SANDBOX_RUNTIME_PREFIX}${appDetail.id}` const enableSandboxRuntime = localStorage.getItem(runtimeStorageKey) === '1' if (enableSandboxRuntime) localStorage.removeItem(runtimeStorageKey) diff --git a/web/app/components/workflow/nodes/llm/components/json-schema-config-modal/json-schema-generator/index.tsx b/web/app/components/workflow/nodes/llm/components/json-schema-config-modal/json-schema-generator/index.tsx index ef91e98676..5ec68d9ce6 100644 --- a/web/app/components/workflow/nodes/llm/components/json-schema-config-modal/json-schema-generator/index.tsx +++ b/web/app/components/workflow/nodes/llm/components/json-schema-config-modal/json-schema-generator/index.tsx @@ -37,8 +37,8 @@ const JsonSchemaGenerator: FC = ({ onApply, crossAxisOffset, }) => { - const localModel = localStorage.getItem(STORAGE_KEYS.GENERATOR.AUTO_GEN_MODEL) - ? JSON.parse(localStorage.getItem(STORAGE_KEYS.GENERATOR.AUTO_GEN_MODEL) as string) as Model + const localModel = localStorage.getItem(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL) + ? JSON.parse(localStorage.getItem(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL) as string) as Model : null const [open, setOpen] = useState(false) const [view, setView] = useState(GeneratorView.promptEditor) @@ -61,8 +61,8 @@ const JsonSchemaGenerator: FC = ({ useEffect(() => { if (defaultModel) { - const localModel = localStorage.getItem(STORAGE_KEYS.GENERATOR.AUTO_GEN_MODEL) - ? JSON.parse(localStorage.getItem(STORAGE_KEYS.GENERATOR.AUTO_GEN_MODEL) || '') + const localModel = localStorage.getItem(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL) + ? JSON.parse(localStorage.getItem(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL) || '') : null if (localModel) { setModel(localModel) @@ -96,7 +96,7 @@ const JsonSchemaGenerator: FC = ({ mode: newValue.mode as ModelModeType, } setModel(newModel) - localStorage.setItem(STORAGE_KEYS.GENERATOR.AUTO_GEN_MODEL, JSON.stringify(newModel)) + localStorage.setItem(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL, JSON.stringify(newModel)) }, [model, setModel]) const handleCompletionParamsChange = useCallback((newParams: FormValue) => { @@ -105,7 +105,7 @@ const JsonSchemaGenerator: FC = ({ completion_params: newParams as CompletionParams, } setModel(newModel) - localStorage.setItem(STORAGE_KEYS.GENERATOR.AUTO_GEN_MODEL, JSON.stringify(newModel)) + localStorage.setItem(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL, JSON.stringify(newModel)) }, [model, setModel]) const { mutateAsync: generateStructuredOutputRules, isPending: isGenerating } = useGenerateStructuredOutputRules() diff --git a/web/app/components/workflow/nodes/tool/components/context-generate-modal/hooks/use-context-generate.ts b/web/app/components/workflow/nodes/tool/components/context-generate-modal/hooks/use-context-generate.ts index 045e2944fe..50dcb07846 100644 --- a/web/app/components/workflow/nodes/tool/components/context-generate-modal/hooks/use-context-generate.ts +++ b/web/app/components/workflow/nodes/tool/components/context-generate-modal/hooks/use-context-generate.ts @@ -272,7 +272,7 @@ const useContextGenerate = ({ const [inputValue, setInputValue] = useState('') const [isGenerating, { setTrue: setGeneratingTrue, setFalse: setGeneratingFalse }] = useBoolean(false) const [modelOverride, setModelOverride] = useState(() => { - const stored = localStorage.getItem(STORAGE_KEYS.GENERATOR.AUTO_GEN_MODEL) + const stored = localStorage.getItem(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL) if (!stored) return null const parsed = JSON.parse(stored) as Model @@ -328,7 +328,7 @@ const useContextGenerate = ({ mode: newValue.mode as ModelModeType, } setModelOverride(newModel) - localStorage.setItem(STORAGE_KEYS.GENERATOR.AUTO_GEN_MODEL, JSON.stringify(newModel)) + localStorage.setItem(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL, JSON.stringify(newModel)) }, [model]) const handleCompletionParamsChange = useCallback((newParams: FormValue) => { @@ -337,7 +337,7 @@ const useContextGenerate = ({ completion_params: newParams as CompletionParams, } setModelOverride(newModel) - localStorage.setItem(STORAGE_KEYS.GENERATOR.AUTO_GEN_MODEL, JSON.stringify(newModel)) + localStorage.setItem(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL, JSON.stringify(newModel)) }, [model]) const promptMessageCount = promptMessages?.length ?? 0 diff --git a/web/app/components/workflow/nodes/tool/components/context-generate-modal/utils/storage.ts b/web/app/components/workflow/nodes/tool/components/context-generate-modal/utils/storage.ts index 4dd2d6ca18..89b7e7b84e 100644 --- a/web/app/components/workflow/nodes/tool/components/context-generate-modal/utils/storage.ts +++ b/web/app/components/workflow/nodes/tool/components/context-generate-modal/utils/storage.ts @@ -1,5 +1,5 @@ // Storage key prefix used by useContextGenData -const CONTEXT_GEN_PREFIX = 'context-gen-' +import { STORAGE_KEYS } from '@/config/storage-keys' export const CONTEXT_GEN_STORAGE_SUFFIX = { versions: 'versions', @@ -24,7 +24,7 @@ export const buildContextGenStorageKey = ( } const buildContextGenStorageKeyWithPrefix = (storageKey: string, suffix: ContextGenStorageSuffix): string => { - return `${CONTEXT_GEN_PREFIX}${storageKey}-${suffix}` + return `${STORAGE_KEYS.SESSION.CONTEXT_GENERATE.PREFIX}${storageKey}-${suffix}` } export const getContextGenStorageKey = (storageKey: string, suffix: ContextGenStorageSuffix): string => { diff --git a/web/app/components/workflow/skill/sidebar.tsx b/web/app/components/workflow/skill/sidebar.tsx index 91b6548a8c..50235bedc7 100644 --- a/web/app/components/workflow/skill/sidebar.tsx +++ b/web/app/components/workflow/skill/sidebar.tsx @@ -13,7 +13,7 @@ type SidebarProps = PropsWithChildren const Sidebar = ({ children }: SidebarProps) => { const { run: persistWidth } = useDebounceFn( - (width: number) => storage.set(STORAGE_KEYS.SKILL.SIDEBAR_WIDTH, width), + (width: number) => storage.set(STORAGE_KEYS.LOCAL.SKILL.SIDEBAR_WIDTH, width), { wait: 200 }, ) @@ -32,7 +32,7 @@ const Sidebar = ({ children }: SidebarProps) => { return (