Skip to content

Commit

Permalink
Smarter store restore
Browse files Browse the repository at this point in the history
  • Loading branch information
wkramer committed Jan 25, 2024
1 parent b3cff5e commit bfebd94
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion src/stores/userSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,42 @@ export const useUserSettingsStore = defineStore({
}
},
},
persist: true,
persist: [
{
key: 'weboc-user-settings-v1.0.0',
storage: window.localStorage,
paths: ['items'],
serializer: {
serialize: (context) => {
return JSON.stringify(context)
},
deserialize: (context) => {
const parsedState = JSON.parse(context)
const newState = [...defaultUserSettings]
for (const prop of newState) {
const storedProp = parsedState.items.find(
(item: UserSettingsItem) => item.id === prop.id,
)
if (storedProp) {
if (
prop.type === 'oneOfMultiple' &&
!prop.items
?.map((i: UserSettingsWithIcon) => i.value)
.includes(storedProp.value)
) {
const index = storedProp.items?.findIndex(
(i: UserSettingsWithIcon) => i.value === storedProp.value,
)
prop.value = prop.items ? prop.items[index ?? 0].value : ''
} else {
prop.value = storedProp.value
}
prop.favorite = storedProp.favorite
}
}
return newState
},
},
},
],
})

0 comments on commit bfebd94

Please sign in to comment.