2020-08-07 07:15:16 +00:00
|
|
|
import get from "lodash.get";
|
|
|
|
import set from "lodash.set";
|
|
|
|
|
2021-02-06 02:32:38 +00:00
|
|
|
import { useSettings } from "../contexts/SettingsContext";
|
2020-08-07 07:15:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper to get and set nested settings that are saved in local storage
|
2021-07-12 22:59:28 +00:00
|
|
|
* @param {string} path The path to the setting within the Settings object provided by the SettingsContext
|
2020-08-07 07:15:16 +00:00
|
|
|
*/
|
2021-07-12 22:59:28 +00:00
|
|
|
function useSetting<Type>(path: string): [Type, (value: Type) => void] {
|
2021-02-06 02:32:38 +00:00
|
|
|
const { settings, setSettings } = useSettings();
|
2020-08-07 07:15:16 +00:00
|
|
|
|
2021-07-12 22:59:28 +00:00
|
|
|
const setting = get(settings, path) as Type;
|
2020-08-07 07:15:16 +00:00
|
|
|
|
2021-07-12 22:59:28 +00:00
|
|
|
const setSetting = (value: Type) =>
|
2020-08-07 07:15:16 +00:00
|
|
|
setSettings((prev) => {
|
|
|
|
const updated = set({ ...prev }, path, value);
|
|
|
|
return updated;
|
|
|
|
});
|
|
|
|
|
|
|
|
return [setting, setSetting];
|
|
|
|
}
|
|
|
|
|
|
|
|
export default useSetting;
|