AcadeDoc/client/src/lib/jotai-helper.ts
Philipinho 9b682c8af5 vite
* replace next with vite
* disable strictmode (it interferes with collaboration in dev mode)
2023-10-20 17:12:08 +01:00

17 lines
576 B
TypeScript

import { atom } from "jotai";
export function atomWithWebStorage<Value>(key: string, initialValue: Value, storage = localStorage) {
const storedValue = localStorage.getItem(key);
const isString = typeof initialValue === "string";
const storageValue = storedValue ? isString ? storedValue : storedValue === "true" : undefined;
const baseAtom = atom(storageValue ?? initialValue);
return atom(
get => get(baseAtom) as Value,
(_get, set, nextValue: Value) => {
set(baseAtom, nextValue);
storage.setItem(key, nextValue!.toString());
},
);
}