-
Michael Clark authored
* restart server silently on add self signed cert and add some logging * release notes * fix name * updating names to be more specific * removing setloading * feedback
Michael Clark authored* restart server silently on add self signed cert and add some logging * release notes * fix name * updating names to be more specific * removing setloading * feedback
useGlobalPref.ts 1014 B
import { useCallback } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { saveGlobalPrefs } from 'loot-core/src/client/actions';
import { type State } from 'loot-core/src/client/state-types';
import { type GlobalPrefs } from 'loot-core/src/types/prefs';
type SetGlobalPrefAction<K extends keyof GlobalPrefs> = (
value: GlobalPrefs[K],
) => void;
export function useGlobalPref<K extends keyof GlobalPrefs>(
prefName: K,
onSaveGlobalPrefs?: () => void,
): [GlobalPrefs[K], SetGlobalPrefAction<K>] {
const dispatch = useDispatch();
const setGlobalPref = useCallback<SetGlobalPrefAction<K>>(
value => {
dispatch(
saveGlobalPrefs(
{
[prefName]: value,
} as GlobalPrefs,
onSaveGlobalPrefs,
),
);
},
[prefName, dispatch, onSaveGlobalPrefs],
);
const globalPref = useSelector(
(state: State) => state.prefs.global?.[prefName] as GlobalPrefs[K],
);
return [globalPref, setGlobalPref];
}