'use client'; import Link from 'next/link'; import { useEffect, useState } from 'react'; import { useI18n } from './I18nProvider'; type SessionUser = { id: string; email: string; role: string; status: string }; export default function NavBar() { const { t, locale, setLocale } = useI18n(); const [user, setUser] = useState(null); const [pendingCount, setPendingCount] = useState(0); async function loadUser() { try { const res = await fetch('/api/auth/me', { cache: 'no-store' }); const data = await res.json(); if (data.user) setUser(data.user); else setUser(null); } catch (e) { setUser(null); } } useEffect(() => { loadUser(); }, []); useEffect(() => { const role = user?.role; const canSeeApprovals = role === 'ADMIN' || role === 'LISTING_MODERATOR' || role === 'USER_MODERATOR'; if (!canSeeApprovals) { setPendingCount(0); return; } fetch('/api/admin/pending/count', { cache: 'no-store' }) .then((res) => res.json()) .then((data) => { if (data && typeof data.total === 'number') setPendingCount(data.total); }) .catch(() => setPendingCount(0)); }, [user]); async function logout() { try { await fetch('/api/auth/logout', { method: 'POST' }); } catch (e) { // ignore } setUser(null); } const isAdmin = user?.role === 'ADMIN'; const isListingMod = user?.role === 'LISTING_MODERATOR'; const isUserMod = user?.role === 'USER_MODERATOR'; const showApprovals = Boolean(user && (isAdmin || isListingMod || isUserMod)); return (
{t('brand')} {t('navBrowse')}
); }