From 4a9ad147aedfae6d5488a7121cacf9a18775ff21 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Tue, 26 Mar 2024 14:04:18 +0100 Subject: [PATCH] save tracker config --- resources/js/components/tracker/Toolbar.js | 6 +++--- resources/js/hooks/tracker.js | 17 ++++++++++++++++- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/resources/js/components/tracker/Toolbar.js b/resources/js/components/tracker/Toolbar.js index 169d7bc..c7545b3 100644 --- a/resources/js/components/tracker/Toolbar.js +++ b/resources/js/components/tracker/Toolbar.js @@ -13,17 +13,17 @@ const mapWild = { }; const Toolbar = () => { - const { config, setConfig } = useTracker(); + const { config, saveConfig } = useTracker(); const controller = React.useMemo(() => ({ getActive: (state, icons) => config[mapWild[icons[0]]] ? icons[0] : null, getDefault: (state, icons) => icons[0], handlePrimary: (state, setState, icons) => { const prop = mapWild[icons[0]]; - setConfig(c => ({ ...c, [prop]: !c[prop] })); + saveConfig({ [prop]: !config[prop] }); }, handleSecondary: () => null, - }), [config, setConfig]); + }), [config, saveConfig]); return diff --git a/resources/js/hooks/tracker.js b/resources/js/hooks/tracker.js index 249a34b..b77ee58 100644 --- a/resources/js/hooks/tracker.js +++ b/resources/js/hooks/tracker.js @@ -14,6 +14,21 @@ export const TrackerProvider = ({ children }) => { const [manualState, setManualState] = React.useState(makeEmptyState()); const [dungeons, setDungeons] = React.useState(DUNGEONS); + const saveConfig = React.useCallback((values) => { + setConfig(s => { + const newConfig = { ...s, ...values }; + localStorage.setItem('tracker.config', JSON.stringify(newConfig)); + return newConfig; + }); + }, []); + + React.useEffect(() => { + const savedConfig = localStorage.getItem('tracker.config'); + if (savedConfig) { + setConfig(JSON.parse(savedConfig)); + } + }, []); + React.useEffect(() => { const newDungeons = DUNGEONS.map(dungeon => { const newDungeon = JSON.parse(JSON.stringify(dungeon)); @@ -42,7 +57,7 @@ export const TrackerProvider = ({ children }) => { }, [autoState, manualState]); const value = React.useMemo(() => { - return { config, setConfig, dungeons, setAutoState, setManualState, state }; + return { config, saveConfig, dungeons, setAutoState, setManualState, state }; }, [config, dungeons, state]); return -- 2.39.2