Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

⚛️ fix(atomWithLocalStorage): Handle Parsing Error (#2883)

Danny Avilac704a23749d7
diff --git a/client/src/store/settings.ts b/client/src/store/settings.ts
index bc589056f1..0c7f41dc0a 100644
--- a/client/src/store/settings.ts
+++ b/client/src/store/settings.ts
@@ -6,16 +6,24 @@ import type { TOptionSettings } from '~/common';
function atomWithLocalStorage<T>(key: string, defaultValue: T) {
return atom<T>({
key,
- default: defaultValue, // Set the default value directly
+ default: defaultValue,
effects_UNSTABLE: [
({ setSelf, onSet }) => {
- // Load the initial value from localStorage if it exists
const savedValue = localStorage.getItem(key);
if (savedValue !== null) {
- setSelf(JSON.parse(savedValue));
+ try {
+ const parsedValue = JSON.parse(savedValue);
+ setSelf(parsedValue);
+ } catch (e) {
+ console.error(
+ `Error parsing localStorage key "${key}", \`savedValue\`: defaultValue, error:`,
+ e,
+ );
+ localStorage.setItem(key, JSON.stringify(defaultValue));
+ setSelf(defaultValue);
+ }
}
- // Update localStorage whenever the atom's value changes
onSet((newValue: T) => {
localStorage.setItem(key, JSON.stringify(newValue));
});