112 lines
2.9 KiB
TypeScript
112 lines
2.9 KiB
TypeScript
import { createSlice, PayloadAction } from '@reduxjs/toolkit';
|
|
|
|
export type Locale = 'zh' | 'en';
|
|
export type UnitSystem = 'metric' | 'imperial';
|
|
export type DateFormat = 'YYYY-MM-DD HH:mm:ss' | 'YYYY-MM-DD HH:mm' | 'MM/DD/YYYY HH:mm:ss';
|
|
|
|
export interface PreferencesState {
|
|
locale: Locale;
|
|
timezone: string;
|
|
unitSystem: UnitSystem;
|
|
dateFormat: DateFormat;
|
|
}
|
|
|
|
const STORAGE_KEY = 'preferences';
|
|
|
|
const detectBrowserTimezone = (): string => {
|
|
try {
|
|
return Intl.DateTimeFormat().resolvedOptions().timeZone || 'Asia/Shanghai';
|
|
} catch {
|
|
return 'Asia/Shanghai';
|
|
}
|
|
};
|
|
|
|
const detectLocale = (): Locale => {
|
|
const stored = localStorage.getItem('i18nextLng');
|
|
if (stored?.startsWith('en')) return 'en';
|
|
return 'zh';
|
|
};
|
|
|
|
const loadFromStorage = (): PreferencesState => {
|
|
try {
|
|
const raw = localStorage.getItem(STORAGE_KEY);
|
|
if (raw) {
|
|
const parsed = JSON.parse(raw) as Partial<PreferencesState>;
|
|
return {
|
|
locale: parsed.locale === 'en' ? 'en' : 'zh',
|
|
timezone: parsed.timezone || detectBrowserTimezone(),
|
|
unitSystem: parsed.unitSystem === 'imperial' ? 'imperial' : 'metric',
|
|
dateFormat: (parsed.dateFormat as DateFormat) || 'YYYY-MM-DD HH:mm:ss',
|
|
};
|
|
}
|
|
} catch {
|
|
// ignore
|
|
}
|
|
return {
|
|
locale: detectLocale(),
|
|
timezone: detectBrowserTimezone(),
|
|
unitSystem: 'metric',
|
|
dateFormat: 'YYYY-MM-DD HH:mm:ss',
|
|
};
|
|
};
|
|
|
|
const persist = (state: PreferencesState) => {
|
|
try {
|
|
localStorage.setItem(STORAGE_KEY, JSON.stringify(state));
|
|
} catch {
|
|
// ignore
|
|
}
|
|
};
|
|
|
|
const initialState: PreferencesState = loadFromStorage();
|
|
persist(initialState);
|
|
|
|
const preferencesSlice = createSlice({
|
|
name: 'preferences',
|
|
initialState,
|
|
reducers: {
|
|
setLocale: (state, action: PayloadAction<Locale>) => {
|
|
state.locale = action.payload;
|
|
persist(state);
|
|
},
|
|
setTimezone: (state, action: PayloadAction<string>) => {
|
|
state.timezone = action.payload;
|
|
persist(state);
|
|
},
|
|
setUnitSystem: (state, action: PayloadAction<UnitSystem>) => {
|
|
state.unitSystem = action.payload;
|
|
persist(state);
|
|
},
|
|
setDateFormat: (state, action: PayloadAction<DateFormat>) => {
|
|
state.dateFormat = action.payload;
|
|
persist(state);
|
|
},
|
|
setPreferences: (state, action: PayloadAction<Partial<PreferencesState>>) => {
|
|
const { locale, timezone, unitSystem, dateFormat } = action.payload;
|
|
if (locale) state.locale = locale;
|
|
if (timezone) state.timezone = timezone;
|
|
if (unitSystem) state.unitSystem = unitSystem;
|
|
if (dateFormat) state.dateFormat = dateFormat;
|
|
persist(state);
|
|
},
|
|
resetPreferences: (state) => {
|
|
state.locale = detectLocale();
|
|
state.timezone = detectBrowserTimezone();
|
|
state.unitSystem = 'metric';
|
|
state.dateFormat = 'YYYY-MM-DD HH:mm:ss';
|
|
persist(state);
|
|
},
|
|
},
|
|
});
|
|
|
|
export const {
|
|
setLocale,
|
|
setTimezone,
|
|
setUnitSystem,
|
|
setDateFormat,
|
|
setPreferences,
|
|
resetPreferences,
|
|
} = preferencesSlice.actions;
|
|
|
|
export default preferencesSlice.reducer;
|