import {useEffect, useState} from 'react'; import {apiFetch} from '../utils/api.js'; import {sessionStamp} from '../utils/auth.js'; /** * The user's Steam name and avatar from Core, or null while loading, without a Steam ID or on any error. Only * decoration, so a failed call shows the default icon and never ends the session. Loads again after a new login * or a settings save (see sessionStamp) on the next render, e.g. the next navigation. */ export default function useSteamProfile(enabled) { const [profile, setProfile] = useState(null); const stamp = sessionStamp(); useEffect(() => { if (!enabled || !stamp) { setProfile(null); return undefined; } let cancelled = false; (async () => { try { const response = await apiFetch(`${import.meta.env.VITE_CORE_API_URL}/api/library/steam-profile`); const data = response.ok ? await response.json() : null; if (!cancelled) setProfile(data); } catch { if (!cancelled) setProfile(null); } })(); return () => { cancelled = true; }; }, [enabled, stamp]); return profile; }