2026-01-07 16:03:02 +01:00
|
|
|
import { create } from 'zustand';
|
|
|
|
|
import { persist } from 'zustand/middleware';
|
|
|
|
|
|
2026-01-11 15:20:48 +01:00
|
|
|
export type AlertThresholdUnit = 'hours' | 'minutes';
|
|
|
|
|
|
2026-01-07 16:03:02 +01:00
|
|
|
interface ModQueueState {
|
2026-01-11 15:20:48 +01:00
|
|
|
alertThresholdValue: number;
|
|
|
|
|
alertThresholdUnit: AlertThresholdUnit;
|
2026-01-07 16:03:02 +01:00
|
|
|
selectedBoardFilter: string | null;
|
2026-01-11 15:20:48 +01:00
|
|
|
setAlertThreshold: (value: number, unit: AlertThresholdUnit) => void;
|
2026-01-07 16:03:02 +01:00
|
|
|
setSelectedBoardFilter: (boardAddress: string | null) => void;
|
2026-01-11 15:20:48 +01:00
|
|
|
// Helper to get threshold in seconds for calculations
|
|
|
|
|
getAlertThresholdSeconds: () => number;
|
2026-01-07 16:03:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const useModQueueStore = create<ModQueueState>()(
|
|
|
|
|
persist(
|
2026-01-11 15:20:48 +01:00
|
|
|
(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 {
|
2026-01-11 15:20:48 +01:00
|
|
|
// 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;
|