'use client' import { useEffect, useState } from 'react' import { authFetch, clearToken, getApiBase, getToken } from '../lib/auth' export default function HeaderIdentity() { const [identity, setIdentity] = useState<{ username: string; role?: string } | null>(null) const [buildNumber, setBuildNumber] = useState(null) const [open, setOpen] = useState(false) useEffect(() => { const token = getToken() if (!token) { setIdentity(null) setBuildNumber(null) return } const load = async () => { try { const baseUrl = getApiBase() const response = await authFetch(`${baseUrl}/auth/me`) if (!response.ok) { clearToken() setIdentity(null) return } const data = await response.json() if (data?.username) { setIdentity({ username: data.username, role: data.role }) } const siteResponse = await fetch(`${baseUrl}/site/public`) if (siteResponse.ok) { const siteInfo = await siteResponse.json() if (siteInfo?.buildNumber) { setBuildNumber(siteInfo.buildNumber) } } } catch (err) { console.error(err) setIdentity(null) } } void load() }, []) if (!identity) { return null } const label = `${identity.username}${identity.role ? ` (${identity.role})` : ''}` const initial = identity.username.slice(0, 1).toUpperCase() const signOut = () => { clearToken() if (typeof window !== 'undefined') { window.location.href = '/login' } } return (
{open && (
Signed in as {label}
setOpen(false)}> My profile setOpen(false)}> Changelog
{buildNumber ?
Build {buildNumber}
: null}
)}
) }