52 lines
1.4 KiB
TypeScript
52 lines
1.4 KiB
TypeScript
|
|
import { create } from 'zustand';
|
||
|
|
|
||
|
|
export type Theme = 'light' | 'dark' | 'system';
|
||
|
|
export type Locale = 'de' | 'en';
|
||
|
|
|
||
|
|
export interface Toast {
|
||
|
|
id: string;
|
||
|
|
type: 'success' | 'error' | 'warning' | 'info';
|
||
|
|
message: string;
|
||
|
|
duration?: number;
|
||
|
|
}
|
||
|
|
|
||
|
|
export interface UIState {
|
||
|
|
theme: Theme;
|
||
|
|
locale: Locale;
|
||
|
|
sidebarOpen: boolean;
|
||
|
|
toasts: Toast[];
|
||
|
|
setTheme: (theme: Theme) => void;
|
||
|
|
setLocale: (locale: Locale) => void;
|
||
|
|
toggleSidebar: () => void;
|
||
|
|
setSidebarOpen: (open: boolean) => void;
|
||
|
|
addToast: (toast: Omit<Toast, 'id'>) => void;
|
||
|
|
removeToast: (id: string) => void;
|
||
|
|
clearToasts: () => void;
|
||
|
|
}
|
||
|
|
|
||
|
|
let toastIdCounter = 0;
|
||
|
|
|
||
|
|
export const useUIStore = create<UIState>((set) => ({
|
||
|
|
theme: (localStorage.getItem('leocrm_theme') as Theme) || 'light',
|
||
|
|
locale: (localStorage.getItem('leocrm_lang') as Locale) || 'de',
|
||
|
|
sidebarOpen: true,
|
||
|
|
toasts: [],
|
||
|
|
setTheme: (theme) => {
|
||
|
|
localStorage.setItem('leocrm_theme', theme);
|
||
|
|
set({ theme });
|
||
|
|
},
|
||
|
|
setLocale: (locale) => {
|
||
|
|
localStorage.setItem('leocrm_lang', locale);
|
||
|
|
set({ locale });
|
||
|
|
},
|
||
|
|
toggleSidebar: () => set((s) => ({ sidebarOpen: !s.sidebarOpen })),
|
||
|
|
setSidebarOpen: (open) => set({ sidebarOpen: open }),
|
||
|
|
addToast: (toast) => {
|
||
|
|
const id = `toast-${++toastIdCounter}`;
|
||
|
|
set((s) => ({ toasts: [...s.toasts, { ...toast, id }] }));
|
||
|
|
},
|
||
|
|
removeToast: (id) =>
|
||
|
|
set((s) => ({ toasts: s.toasts.filter((t) => t.id !== id) })),
|
||
|
|
clearToasts: () => set({ toasts: [] }),
|
||
|
|
}));
|