import { create } from 'zustand'; import { api, setToken } from '@/lib/api'; import type { User } from '@/lib/types'; interface AuthState { user: User | null; loading: boolean; load: () => Promise; setUser: (u: User | null) => void; logout: () => Promise; updatePrefs: (p: Partial & { display_name?: string }) => Promise; } export const useAuth = create((set, get) => ({ user: null, loading: true, load: async () => { try { const u = await api('/me'); set({ user: u, loading: false }); } catch { set({ user: null, loading: false }); } }, setUser: (u) => set({ user: u, loading: false }), logout: async () => { try { await api('/auth/logout', { method: 'POST' }); } finally { setToken(null); set({ user: null }); } }, updatePrefs: async (p) => { const u = await api('/me/preferences', { method: 'PATCH', body: JSON.stringify(p) }); set({ user: u }); const fs = u.preferences.font_scale ?? 1; document.documentElement.style.setProperty('--font-scale', String(fs)); void get; }, })); window.addEventListener('uqo:unauthorized', () => useAuth.setState({ user: null, loading: false }));