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