Files
5chan/src/stores/use-mod-queue-store.ts
T

62 lines
2.2 KiB
TypeScript
Raw Normal View History

2026-01-07 16:03:02 +01:00
import { create } from 'zustand';
import { persist } from 'zustand/middleware';
export type AlertThresholdUnit = 'hours' | 'minutes';
2026-01-07 16:03:02 +01:00
interface ModQueueState {
alertThresholdValue: number;
alertThresholdUnit: AlertThresholdUnit;
2026-01-07 16:03:02 +01:00
selectedBoardFilter: string | null;
setAlertThreshold: (value: number, unit: AlertThresholdUnit) => void;
2026-01-07 16:03:02 +01:00
setSelectedBoardFilter: (boardAddress: string | null) => void;
// Helper to get threshold in seconds for calculations
getAlertThresholdSeconds: () => number;
2026-01-07 16:03:02 +01:00
}
const useModQueueStore = create<ModQueueState>()(
persist(
(set, get) => {
// Migration: Check for old format in localStorage
let initialState = { alertThresholdValue: 6, alertThresholdUnit: 'hours' as AlertThresholdUnit };
if (typeof window !== 'undefined') {
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',
};
}
}
2026-01-11 15:40:24 +01:00
} catch {
// Ignore parse errors, use defaults
}
}
return {
...initialState,
selectedBoardFilter: null,
setAlertThreshold: (value, unit) => set({ alertThresholdValue: value, alertThresholdUnit: unit }),
setSelectedBoardFilter: (boardAddress) => set({ selectedBoardFilter: boardAddress }),
getAlertThresholdSeconds: () => {
const { alertThresholdValue, alertThresholdUnit } = get();
return alertThresholdUnit === 'hours' ? alertThresholdValue * 3600 : alertThresholdValue * 60;
},
};
},
2026-01-07 16:03:02 +01:00
{
name: 'mod-queue-storage',
},
),
);
export default useModQueueStore;