Add settings hook
This commit is contained in:
parent
85e5bc887f
commit
bc1e9abf3e
1 changed files with 26 additions and 0 deletions
26
src/app/state/hooks/settings.ts
Normal file
26
src/app/state/hooks/settings.ts
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
import { atom, useAtomValue, useSetAtom, WritableAtom } from 'jotai';
|
||||||
|
import { selectAtom } from 'jotai/utils';
|
||||||
|
import { useMemo } from 'react';
|
||||||
|
import { Settings } from '../settings';
|
||||||
|
|
||||||
|
export const useSetting = <K extends keyof Settings>(
|
||||||
|
settingsAtom: WritableAtom<Settings, Settings>,
|
||||||
|
key: K
|
||||||
|
): [Settings[K], (value: Settings[K]) => void] => {
|
||||||
|
const selector = useMemo(() => (s: Settings) => s[key], [key]);
|
||||||
|
const setting = useAtomValue(selectAtom(settingsAtom, selector));
|
||||||
|
|
||||||
|
const setterAtom = useMemo(
|
||||||
|
() =>
|
||||||
|
atom<null, Settings[K]>(null, (get, set, value) => {
|
||||||
|
const s = { ...get(settingsAtom) };
|
||||||
|
s[key] = value;
|
||||||
|
set(settingsAtom, s);
|
||||||
|
}),
|
||||||
|
[settingsAtom, key]
|
||||||
|
);
|
||||||
|
|
||||||
|
const setter = useSetAtom(setterAtom);
|
||||||
|
|
||||||
|
return [setting, setter];
|
||||||
|
};
|
Loading…
Reference in a new issue