refactor: Refactor storage keys into hierarchical structure

This commit is contained in:
zhsama
2026-01-30 23:23:21 +08:00
parent a052c414ac
commit 03ec2f64cd
9 changed files with 52 additions and 35 deletions

View File

@@ -84,8 +84,8 @@ const GetAutomaticRes: FC<IGetAutomaticResProps> = ({
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<Model>(localModel || {
name: '',
@@ -135,7 +135,9 @@ const GetAutomaticRes: FC<IGetAutomaticResProps> = ({
},
] as const
const [instructionFromSessionStorage, setInstruction] = useSessionStorageState<string>(`improve-instruction-${flowId}${isBasicMode ? '' : `-${nodeId}${editorId ? `-${editorId}` : ''}`}`)
const [instructionFromSessionStorage, setInstruction] = useSessionStorageState<string>(
`${STORAGE_KEYS.SESSION.GENERATOR.INSTRUCTION_PREFIX}${flowId}${isBasicMode ? '' : `-${nodeId}${editorId ? `-${editorId}` : ''}`}`,
)
const instruction = instructionFromSessionStorage || ''
const [ideaOutput, setIdeaOutput] = useState<string>('')
@@ -179,8 +181,8 @@ const GetAutomaticRes: FC<IGetAutomaticResProps> = ({
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<IGetAutomaticResProps> = ({
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<IGetAutomaticResProps> = ({
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 () => {

View File

@@ -54,8 +54,8 @@ export const GetCodeGeneratorResModal: FC<IGetCodeGeneratorResProps> = (
},
) => {
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<Model>(localModel || {
name: '',
@@ -66,7 +66,9 @@ export const GetCodeGeneratorResModal: FC<IGetCodeGeneratorResProps> = (
const {
defaultModel,
} = useModelListAndDefaultModelAndCurrentProviderAndModel(ModelTypeEnum.textGeneration)
const [instructionFromSessionStorage, setInstruction] = useSessionStorageState<string>(`improve-instruction-${flowId}-${nodeId}`)
const [instructionFromSessionStorage, setInstruction] = useSessionStorageState<string>(
`${STORAGE_KEYS.SESSION.GENERATOR.INSTRUCTION_PREFIX}${flowId}-${nodeId}`,
)
const instruction = instructionFromSessionStorage || ''
const [ideaOutput, setIdeaOutput] = useState<string>('')
@@ -107,7 +109,7 @@ export const GetCodeGeneratorResModal: FC<IGetCodeGeneratorResProps> = (
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<IGetCodeGeneratorResProps> = (
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<IGetCodeGeneratorResProps> = (
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({

View File

@@ -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', {

View File

@@ -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)

View File

@@ -37,8 +37,8 @@ const JsonSchemaGenerator: FC<JsonSchemaGeneratorProps> = ({
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<JsonSchemaGeneratorProps> = ({
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<JsonSchemaGeneratorProps> = ({
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<JsonSchemaGeneratorProps> = ({
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()

View File

@@ -272,7 +272,7 @@ const useContextGenerate = ({
const [inputValue, setInputValue] = useState('')
const [isGenerating, { setTrue: setGeneratingTrue, setFalse: setGeneratingFalse }] = useBoolean(false)
const [modelOverride, setModelOverride] = useState<Model | null>(() => {
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

View File

@@ -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 => {

View File

@@ -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 (
<aside
ref={containerRef}
style={{ width: storage.getNumber(STORAGE_KEYS.SKILL.SIDEBAR_WIDTH, SIDEBAR_DEFAULT_WIDTH) }}
style={{ width: storage.getNumber(STORAGE_KEYS.LOCAL.SKILL.SIDEBAR_WIDTH, SIDEBAR_DEFAULT_WIDTH) }}
className="relative flex h-full shrink-0 flex-col gap-px overflow-hidden rounded-[10px] border border-components-panel-border-subtle bg-components-panel-bg"
>
{children}

View File

@@ -1,8 +1,21 @@
export const STORAGE_KEYS = {
SKILL: {
SIDEBAR_WIDTH: 'skill-sidebar-width',
LOCAL: {
SKILL: {
SIDEBAR_WIDTH: 'skill-sidebar-width',
},
GENERATOR: {
AUTO_GEN_MODEL: 'auto-gen-model',
},
WORKFLOW: {
SANDBOX_RUNTIME_PREFIX: 'workflow:sandbox-runtime:',
},
},
GENERATOR: {
AUTO_GEN_MODEL: 'auto-gen-model',
SESSION: {
GENERATOR: {
INSTRUCTION_PREFIX: 'improve-instruction-',
},
CONTEXT_GENERATE: {
PREFIX: 'context-gen-',
},
},
} as const