Skip to content

Commit 5c9d742

Browse files
remove interface local storage (#6947)
1 parent 5285f25 commit 5c9d742

File tree

3 files changed

+9
-131
lines changed

3 files changed

+9
-131
lines changed

web-app/src/constants/localStorage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export const localStorageKey = {
55
theme: 'theme',
66
modelProvider: 'model-provider',
77
modelSources: 'model-sources',
8-
settingInterface: 'setting-interface',
8+
settingInterface: 'setting-appearance',
99
settingGeneral: 'setting-general',
1010
settingCodeBlock: 'setting-code-block',
1111
settingLocalApiServer: 'setting-local-api-server',

web-app/src/hooks/__tests__/useInterfaceSettings.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { THREAD_SCROLL_BEHAVIOR } from '@/constants/threadScroll'
66
// Mock constants
77
vi.mock('@/constants/localStorage', () => ({
88
localStorageKey: {
9-
settingInterface: 'setting-interface',
9+
settingInterface: 'setting-appearance',
1010
},
1111
}))
1212

web-app/src/hooks/useInterfaceSettings.ts

Lines changed: 7 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@ interface InterfaceSettingsState {
4141
resetInterface: () => void
4242
}
4343

44-
const LEGACY_INTERFACE_STORAGE_KEY = 'setting-appearance' as const
45-
const GENERAL_SETTINGS_STORAGE_KEY = localStorageKey.settingGeneral
46-
4744
type InterfaceSettingsPersistedSlice = Omit<
4845
InterfaceSettingsState,
4946
| 'resetInterface'
@@ -213,132 +210,9 @@ const createDefaultInterfaceValues = (): InterfaceSettingsPersistedSlice => {
213210
}
214211
}
215212

216-
const buildDefaultPersistedSnapshot = () =>
217-
JSON.stringify({ state: createDefaultInterfaceValues(), version: 0 })
218-
219-
const validatePersistedSnapshot = (value: string): string | null => {
220-
try {
221-
const parsed = JSON.parse(value) as {
222-
state?: Record<string, unknown>
223-
version?: unknown
224-
}
225-
226-
if (parsed && typeof parsed === 'object' && parsed.state) {
227-
const state = parsed.state
228-
229-
if (
230-
!isThreadScrollBehavior(
231-
state.threadScrollBehavior as ThreadScrollBehavior
232-
)
233-
) {
234-
const draft = {
235-
...parsed,
236-
state: {
237-
...state,
238-
threadScrollBehavior: DEFAULT_THREAD_SCROLL_BEHAVIOR,
239-
},
240-
}
241-
242-
return JSON.stringify(draft)
243-
}
244-
return value
245-
}
246-
} catch {
247-
// ignore parse failures
248-
}
249-
250-
return null
251-
}
252-
253-
const migrateLegacySnapshot = (): string | null => {
254-
const legacy = localStorage.getItem(LEGACY_INTERFACE_STORAGE_KEY)
255-
if (!legacy) return null
256-
257-
const migrated =
258-
validatePersistedSnapshot(legacy) ?? buildDefaultPersistedSnapshot()
259-
260-
localStorage.setItem(localStorageKey.settingInterface, migrated)
261-
localStorage.removeItem(LEGACY_INTERFACE_STORAGE_KEY)
262-
263-
return migrated
264-
}
265-
266-
const migrateFromGeneralSettings = (): string | null => {
267-
const general = localStorage.getItem(GENERAL_SETTINGS_STORAGE_KEY)
268-
if (!general) return null
269-
270-
try {
271-
const parsed = JSON.parse(general) as {
272-
state?: Record<string, unknown>
273-
version?: unknown
274-
}
275-
276-
const legacyBehavior = parsed?.state?.threadScrollBehavior
277-
278-
if (!isThreadScrollBehavior(legacyBehavior)) {
279-
return null
280-
}
281-
282-
const nextInterfaceState = {
283-
...createDefaultInterfaceValues(),
284-
threadScrollBehavior: legacyBehavior,
285-
}
286-
287-
const migrated = JSON.stringify({
288-
state: nextInterfaceState,
289-
version: 0,
290-
})
291-
292-
localStorage.setItem(localStorageKey.settingInterface, migrated)
293-
294-
if (parsed?.state) {
295-
const rest = { ...parsed.state }
296-
delete rest.threadScrollBehavior
297-
298-
const cleaned = JSON.stringify({
299-
...parsed,
300-
state: rest,
301-
})
302-
localStorage.setItem(GENERAL_SETTINGS_STORAGE_KEY, cleaned)
303-
}
304-
305-
return migrated
306-
} catch {
307-
return null
308-
}
309-
}
310-
311-
const interfaceStorage = createJSONStorage<InterfaceSettingsState>(() => ({
312-
getItem: (name: string) => {
313-
const existing = localStorage.getItem(name)
314-
if (existing !== null) {
315-
const valid = validatePersistedSnapshot(existing)
316-
if (valid) {
317-
if (valid !== existing) {
318-
localStorage.setItem(name, valid)
319-
}
320-
return valid
321-
}
322-
323-
const fallback = buildDefaultPersistedSnapshot()
324-
localStorage.setItem(name, fallback)
325-
return fallback
326-
}
327-
328-
if (name !== localStorageKey.settingInterface) {
329-
return null
330-
}
331-
332-
return migrateLegacySnapshot() ?? migrateFromGeneralSettings()
333-
},
334-
setItem: (name: string, value: string) => {
335-
const valid = validatePersistedSnapshot(value)
336-
localStorage.setItem(name, valid ?? buildDefaultPersistedSnapshot())
337-
},
338-
removeItem: (name: string) => {
339-
localStorage.removeItem(name)
340-
},
341-
}))
213+
const interfaceStorage = createJSONStorage<InterfaceSettingsState>(() =>
214+
localStorage
215+
)
342216

343217
// Hook to check if alpha slider should be shown
344218
export const useBlurSupport = () => {
@@ -826,6 +700,10 @@ export const useInterfaceSettings = create<InterfaceSettingsState>()(
826700
// Apply settings when hydrating from storage
827701
onRehydrateStorage: () => (state) => {
828702
if (state) {
703+
if (!isThreadScrollBehavior(state.threadScrollBehavior)) {
704+
state.threadScrollBehavior = DEFAULT_THREAD_SCROLL_BEHAVIOR
705+
}
706+
829707
// Apply font size from storage
830708
document.documentElement.style.setProperty(
831709
'--font-size-base',

0 commit comments

Comments
 (0)