Skip to content

Commit

Permalink
fix: user state persisting
Browse files Browse the repository at this point in the history
  • Loading branch information
TurtIeSocks committed Sep 14, 2023
1 parent a95818c commit d332474
Showing 1 changed file with 10 additions and 31 deletions.
41 changes: 10 additions & 31 deletions src/components/Config.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,6 @@ export default function Config({ children }) {
localStorage.getItem('local-state') || '{ "state": {} }',
)

const updateObjState = (defaults, category) => {
if (localState?.state?.[category]) {
return deepMerge({}, defaults, localState.state[category])
}
return defaults
}

/**
* @template T
* @param {T} defaults
Expand All @@ -67,25 +60,6 @@ export default function Config({ children }) {
return defaults
}

if (localState?.state?.settings) {
const validNav = data.navigation.map((x) => x.name)
localState.state.settings.navigation = validNav.includes(
localState.state.settings.navigation,
)
? localState.state.settings.navigation
: validNav[0]

const validTileLayer = data.tileServers.map((x) => x.name)
localState.state.settings.tileServers = validTileLayer.includes(
localState.state.settings.tileServers,
)
? localState.state.settings.tileServers
: validTileLayer[0]

localState.state.settings.navigationControls =
data.map.misc.navigationControls
}

const defaultLocation = /** @type {const} */ ([
data.map.general.startLat,
data.map.general.startLon,
Expand Down Expand Up @@ -147,17 +121,22 @@ export default function Config({ children }) {
tutorialExcludeList: data.authentication.excludeFromTutorial || [],
})

useStore.setState({
useStore.setState((prev) => ({
tutorial:
!localState?.state?.tutorial || data.user.tutorial === undefined
? !!localState?.state?.tutorial
: !data.user.tutorial,
menus: updateObjState(data.menus, 'menus'),
userSettings: updateObjState(data.userSettings, 'userSettings'),
settings: updateObjState(settings, 'settings'),
menus: deepMerge({}, data.menus, prev.menus),
userSettings: deepMerge({}, data.userSettings, prev.userSettings),
settings: {
...Object.fromEntries(
Object.entries(settings).map(([k, v]) => [k, Object.keys(v)[0]]),
),
...prev.settings,
},
zoom: safeZoom,
location,
})
}))
setServerSettings({ ...serverSettings, fetched: true })
} else {
setServerSettings({ error: true, status: 500, fetched: true })
Expand Down

0 comments on commit d332474

Please sign in to comment.