mirror of
https://github.com/docmost/docmost.git
synced 2026-05-17 06:44:05 +08:00
b85b34d6b1
* feat: resizable sidebar * only expand space sidebar
18 lines
622 B
TypeScript
18 lines
622 B
TypeScript
import { atom } from "jotai";
|
|
|
|
export function atomWithWebStorage<Value>(key: string, initialValue: Value, storage = localStorage) {
|
|
const storedValue = localStorage.getItem(key);
|
|
const isStringOrInt = typeof initialValue === "string" || typeof initialValue === "number";
|
|
|
|
const storageValue = storedValue ? isStringOrInt ? 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());
|
|
},
|
|
);
|
|
}
|