mirror of
https://github.com/langgenius/dify.git
synced 2026-05-04 17:38:04 +08:00
refactor: migrate localStorage calls to storage utility module
Replace direct localStorage.getItem/setItem/removeItem with the centralized storage module which provides versioned keys, automatic JSON serialization, SSR safety, and error handling.
This commit is contained in:
@ -17,6 +17,7 @@ import useTheme from '@/hooks/use-theme'
|
||||
import { useGenerateStructuredOutputRules } from '@/service/use-common'
|
||||
import { ModelModeType, Theme } from '@/types/app'
|
||||
import { cn } from '@/utils/classnames'
|
||||
import { storage } from '@/utils/storage'
|
||||
import { useMittContext } from '../visual-editor/context'
|
||||
import { useVisualEditorStore } from '../visual-editor/store'
|
||||
import { SchemaGeneratorDark, SchemaGeneratorLight } from './assets'
|
||||
@ -37,9 +38,7 @@ const JsonSchemaGenerator: FC<JsonSchemaGeneratorProps> = ({
|
||||
onApply,
|
||||
crossAxisOffset,
|
||||
}) => {
|
||||
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 localModel = storage.get<Model>(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL)
|
||||
const [open, setOpen] = useState(false)
|
||||
const [view, setView] = useState(GeneratorView.promptEditor)
|
||||
const [model, setModel] = useState<Model>(localModel || {
|
||||
@ -61,9 +60,7 @@ const JsonSchemaGenerator: FC<JsonSchemaGeneratorProps> = ({
|
||||
|
||||
useEffect(() => {
|
||||
if (defaultModel) {
|
||||
const localModel = localStorage.getItem(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL)
|
||||
? JSON.parse(localStorage.getItem(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL) || '')
|
||||
: null
|
||||
const localModel = storage.get<Model>(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL)
|
||||
if (localModel) {
|
||||
setModel(localModel)
|
||||
}
|
||||
@ -96,7 +93,7 @@ const JsonSchemaGenerator: FC<JsonSchemaGeneratorProps> = ({
|
||||
mode: newValue.mode as ModelModeType,
|
||||
}
|
||||
setModel(newModel)
|
||||
localStorage.setItem(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL, JSON.stringify(newModel))
|
||||
storage.set(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL, newModel)
|
||||
}, [model, setModel])
|
||||
|
||||
const handleCompletionParamsChange = useCallback((newParams: FormValue) => {
|
||||
@ -105,7 +102,7 @@ const JsonSchemaGenerator: FC<JsonSchemaGeneratorProps> = ({
|
||||
completion_params: newParams as CompletionParams,
|
||||
}
|
||||
setModel(newModel)
|
||||
localStorage.setItem(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL, JSON.stringify(newModel))
|
||||
storage.set(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL, newModel)
|
||||
}, [model, setModel])
|
||||
|
||||
const { mutateAsync: generateStructuredOutputRules, isPending: isGenerating } = useGenerateStructuredOutputRules()
|
||||
|
||||
@ -27,6 +27,7 @@ import { useGetLanguage } from '@/context/i18n'
|
||||
import { languages } from '@/i18n-config/language'
|
||||
import { fetchContextGenerateSuggestedQuestions, generateContext } from '@/service/debug'
|
||||
import { AppModeEnum } from '@/types/app'
|
||||
import { storage } from '@/utils/storage'
|
||||
import { CONTEXT_GEN_STORAGE_SUFFIX, getContextGenStorageKey } from '../utils/storage'
|
||||
import useContextGenData from './use-context-gen-data'
|
||||
|
||||
@ -272,10 +273,9 @@ 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.LOCAL.GENERATOR.AUTO_GEN_MODEL)
|
||||
if (!stored)
|
||||
const parsed = storage.get<Model>(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL)
|
||||
if (!parsed)
|
||||
return null
|
||||
const parsed = JSON.parse(stored) as Model
|
||||
return {
|
||||
...parsed,
|
||||
completion_params: (parsed.completion_params ?? {}) as CompletionParams,
|
||||
@ -328,7 +328,7 @@ const useContextGenerate = ({
|
||||
mode: newValue.mode as ModelModeType,
|
||||
}
|
||||
setModelOverride(newModel)
|
||||
localStorage.setItem(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL, JSON.stringify(newModel))
|
||||
storage.set(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL, newModel)
|
||||
}, [model])
|
||||
|
||||
const handleCompletionParamsChange = useCallback((newParams: FormValue) => {
|
||||
@ -337,7 +337,7 @@ const useContextGenerate = ({
|
||||
completion_params: newParams as CompletionParams,
|
||||
}
|
||||
setModelOverride(newModel)
|
||||
localStorage.setItem(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL, JSON.stringify(newModel))
|
||||
storage.set(STORAGE_KEYS.LOCAL.GENERATOR.AUTO_GEN_MODEL, newModel)
|
||||
}, [model])
|
||||
|
||||
const promptMessageCount = promptMessages?.length ?? 0
|
||||
|
||||
Reference in New Issue
Block a user