import { useEffect, useState } from 'react'; import { getCurrentUser, type CurrentUser } from '../api/endpoints'; /** * Returns the currently-logged-in dashboard user, or null if not logged in / * not yet loaded. Useful for conditionally showing admin-only UI bits. * * Fetches `/me` once on mount. Cheap — the endpoint just decodes the * session cookie and returns {username, is_admin}. */ export function useCurrentUser(): { user: CurrentUser | null; loading: boolean } { const [user, setUser] = useState(null); const [loading, setLoading] = useState(true); useEffect(() => { let cancelled = false; getCurrentUser() .then(u => { if (!cancelled) setUser(u); }) .catch(() => { if (!cancelled) setUser(null); }) .finally(() => { if (!cancelled) setLoading(false); }); return () => { cancelled = true; }; }, []); return { user, loading }; }