🐛(frontend) preserve notification preferences while merging new types

When loading notification settings from localStorage,
keep user preferences for existing notification types while adding
new notification types with default values.

If a notification type is removed, make sure to get rid of it.
My initial implementation wasn't future proof.
This commit is contained in:
lebaudantoine
2025-02-12 10:30:34 +01:00
committed by aleb_the_flash
parent 09a0496d25
commit a979f05549

View File

@@ -24,7 +24,21 @@ function getNotificationsState(): State {
const stored = localStorage.getItem(STORAGE_KEYS.NOTIFICATIONS)
if (!stored) return DEFAULT_STATE
const parsed = JSON.parse(stored, deserializeToProxyMap)
return parsed || DEFAULT_STATE
// Ensure all default notification types exist in the recovered state
return {
...DEFAULT_STATE,
...parsed,
soundNotifications: proxyMap(
new Map(
Array.from(DEFAULT_STATE.soundNotifications.keys()).map((key) => [
key,
parsed.soundNotifications.has(key)
? parsed.soundNotifications.get(key)
: DEFAULT_STATE.soundNotifications.get(key),
])
)
),
}
} catch (error: unknown) {
console.error(
'[NotificationsStore] Failed to parse stored settings:',