import type { User } from './types'; import { getMe } from './client'; class SessionStore { me = $state(null); status = $state<'loading' | 'anon' | 'authed'>('loading'); async refresh(): Promise { try { const { user } = await getMe(); if (!user || typeof user.id !== 'string' || user.id.length === 0) { this.clear(); return null; } this.me = user; this.status = 'authed'; return user; } catch (e) { const status = (e as { status?: number }).status ?? 0; if (status === 0) { // network/protocol error: keep previous state so UI can show retry instead if (this.status === 'loading') this.clear(); return null; } this.clear(); return null; } } clear() { this.me = null; this.status = 'anon'; } set(u: User | null) { if (u && typeof u.id === 'string' && u.id.length > 0) { this.me = u; this.status = 'authed'; } else { this.clear(); } } } export const session: SessionStore = new SessionStore();