mirror of
https://github.com/Shadowfita/docmost.git
synced 2025-11-22 12:41:02 +10:00
switch to nx monorepo
This commit is contained in:
17
apps/client/src/lib/jotai-helper.ts
Normal file
17
apps/client/src/lib/jotai-helper.ts
Normal file
@ -0,0 +1,17 @@
|
||||
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());
|
||||
},
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user