fix(mod queue): alert threshold returns NaN when migrating from old localStorage format

This commit is contained in:
plebeius
2026-01-11 16:30:35 +01:00
parent a381353290
commit 4a9f4685cb
+26 -30
View File
@@ -13,36 +13,19 @@ interface ModQueueState {
getAlertThresholdSeconds: () => number; getAlertThresholdSeconds: () => number;
} }
const useModQueueStore = create<ModQueueState>()( // Type for old persisted state format (before migration)
persist( interface OldPersistedState {
(set, get) => { alertThresholdHours?: number;
// Migration: Check for old format in localStorage alertThresholdValue?: number;
let initialState = { alertThresholdValue: 6, alertThresholdUnit: 'hours' as AlertThresholdUnit }; alertThresholdUnit?: AlertThresholdUnit;
if (typeof window !== 'undefined') { selectedBoardFilter?: string | null;
try {
const stored = localStorage.getItem('mod-queue-storage');
if (stored) {
const parsed = JSON.parse(stored);
// If old format exists, migrate it
if (parsed.state?.alertThresholdHours !== undefined) {
initialState = {
alertThresholdValue: parsed.state.alertThresholdHours,
alertThresholdUnit: 'hours',
};
} else if (parsed.state?.alertThresholdValue !== undefined) {
initialState = {
alertThresholdValue: parsed.state.alertThresholdValue,
alertThresholdUnit: parsed.state.alertThresholdUnit || 'hours',
};
}
}
} catch {
// Ignore parse errors, use defaults
}
} }
return { const useModQueueStore = create<ModQueueState>()(
...initialState, persist(
(set, get) => ({
alertThresholdValue: 6,
alertThresholdUnit: 'hours' as AlertThresholdUnit,
selectedBoardFilter: null, selectedBoardFilter: null,
setAlertThreshold: (value, unit) => set({ alertThresholdValue: value, alertThresholdUnit: unit }), setAlertThreshold: (value, unit) => set({ alertThresholdValue: value, alertThresholdUnit: unit }),
setSelectedBoardFilter: (boardAddress) => set({ selectedBoardFilter: boardAddress }), setSelectedBoardFilter: (boardAddress) => set({ selectedBoardFilter: boardAddress }),
@@ -50,10 +33,23 @@ const useModQueueStore = create<ModQueueState>()(
const { alertThresholdValue, alertThresholdUnit } = get(); const { alertThresholdValue, alertThresholdUnit } = get();
return alertThresholdUnit === 'hours' ? alertThresholdValue * 3600 : alertThresholdValue * 60; return alertThresholdUnit === 'hours' ? alertThresholdValue * 3600 : alertThresholdValue * 60;
}, },
}; }),
},
{ {
name: 'mod-queue-storage', name: 'mod-queue-storage',
version: 1,
// Migrate old alertThresholdHours format to new alertThresholdValue/alertThresholdUnit format
migrate: (persistedState, version) => {
const state = persistedState as OldPersistedState;
if (version === 0 && state.alertThresholdHours !== undefined) {
return {
...state,
alertThresholdValue: state.alertThresholdHours,
alertThresholdUnit: 'hours' as AlertThresholdUnit,
alertThresholdHours: undefined, // Remove old field
};
}
return state;
},
}, },
), ),
); );