mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-31 23:58:50 +01:00
32 lines
909 B
TypeScript
32 lines
909 B
TypeScript
|
|
import { atom } from 'recoil';
|
||
|
|
|
||
|
|
// Improved helper function to create atoms with localStorage
|
||
|
|
export function atomWithLocalStorage<T>(key: string, defaultValue: T) {
|
||
|
|
return atom<T>({
|
||
|
|
key,
|
||
|
|
default: defaultValue,
|
||
|
|
effects_UNSTABLE: [
|
||
|
|
({ setSelf, onSet }) => {
|
||
|
|
const savedValue = localStorage.getItem(key);
|
||
|
|
if (savedValue !== null) {
|
||
|
|
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);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
onSet((newValue: T) => {
|
||
|
|
localStorage.setItem(key, JSON.stringify(newValue));
|
||
|
|
});
|
||
|
|
},
|
||
|
|
],
|
||
|
|
});
|
||
|
|
}
|