mirror of
https://github.com/bitsocialnet/5chan.git
synced 2026-08-03 07:41:04 +02:00
fix(mod queue): alert threshold returns NaN when migrating from old localStorage format
This commit is contained in:
@@ -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;
|
||||||
|
},
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user