"use client" import Link from "next/link" import { useSession } from "./SessionProvider" import { authClient } from "@/lib/auth-client" import { useEffect, useState } from "react" import { useRoleSwitcher } from "./RoleSwitcher" export default function Navigation() { const { session, loading } = useSession() const [userRole, setUserRole] = useState(null) const { viewAsRole, setViewAsRole, effectiveRole } = useRoleSwitcher() useEffect(() => { const fetchUserRole = async () => { const userId = (session?.user as { id?: string })?.id if (userId) { try { const response = await fetch(`/api/users/${userId}/role`) if (response.ok) { const data = await response.json() setUserRole(data.role) } else { setUserRole(null) } } catch { setUserRole(null) } } else { setUserRole(null) } } fetchUserRole() }, [session]) const handleLogout = async () => { setViewAsRole(null) await authClient.signOut() window.location.href = '/auth/login' } const displayRole = effectiveRole || userRole const isSiteAdmin = userRole === "site_admin" const wordmarkHref = session ? (displayRole === "club_admin" || displayRole === "site_admin") ? "/admin" : "/rankings" : "/" const roleLabels: Record = { player: "Player", tournament_admin: "Tournament Admin", club_admin: "Club Admin", site_admin: "Site Admin", } return ( <> {viewAsRole && (

Viewing as {roleLabels[viewAsRole]} {" "}— you are seeing what a {roleLabels[viewAsRole]?.toLowerCase()} would see.

)} ) }