'use client'; import Link from 'next/link'; import { useEffect, useMemo, useState } from 'react'; import { motion, AnimatePresence, useReducedMotion } from 'framer-motion'; import { LogOut, Settings, Star, Paintbrush, Building2, BarChart3, Megaphone, CreditCard, Handshake, ChevronDown, TrendingUp, Trophy } from 'lucide-react'; import { useAuth } from '@/lib/auth/AuthContext'; import { useEffectiveDriverId } from '@/lib/currentDriver'; import type { DriverDTO } from '@core/racing/application/dto/DriverDTO'; import { EntityMappers } from '@core/racing/application/mappers/EntityMappers'; import DriverSummaryPill from '@/components/profile/DriverSummaryPill'; // Hook to detect sponsor mode function useSponsorMode(): boolean { const [isSponsor, setIsSponsor] = useState(false); useEffect(() => { const cookie = document.cookie .split('; ') .find(row => row.startsWith('gridpilot_demo_mode=')); if (cookie) { const value = cookie.split('=')[1]; setIsSponsor(value === 'sponsor'); } }, []); return isSponsor; } // Sponsor Pill Component - matches the style of DriverSummaryPill function SponsorSummaryPill({ onClick, companyName = 'Acme Racing Co.', activeSponsors = 7, impressions = 127, }: { onClick: () => void; companyName?: string; activeSponsors?: number; impressions?: number; }) { const shouldReduceMotion = useReducedMotion(); return ( {/* Avatar/Logo */}
{/* Active indicator */}
{/* Info */}
{companyName.split(' ')[0]}
{activeSponsors} {impressions}k
{/* Chevron */} ); } export default function UserPill() { const { session, login } = useAuth(); const [driver, setDriver] = useState(null); const [isMenuOpen, setIsMenuOpen] = useState(false); const isSponsorMode = useSponsorMode(); const shouldReduceMotion = useReducedMotion(); const user = session?.user as | { id: string; displayName?: string; primaryDriverId?: string | null; avatarUrl?: string | null; } | undefined; const primaryDriverId = useEffectiveDriverId(); useEffect(() => { let cancelled = false; async function loadDriver() { if (!primaryDriverId) { if (!cancelled) { setDriver(null); } return; } const repo = getDriverRepository(); const entity = await repo.findById(primaryDriverId); if (!cancelled) { setDriver(EntityMappers.toDriverDTO(entity)); } } loadDriver(); return () => { cancelled = true; }; }, [primaryDriverId]); const data = useMemo(() => { if (!session?.user || !primaryDriverId || !driver) { return null; } const driverStats = getDriverStats(primaryDriverId); const allRankings = getAllDriverRankings(); let rating: number | null = driverStats?.rating ?? null; let rank: number | null = null; let totalDrivers: number | null = null; if (driverStats) { totalDrivers = allRankings.length || null; if (typeof driverStats.overallRank === 'number' && driverStats.overallRank > 0) { rank = driverStats.overallRank; } else { const indexInGlobal = allRankings.findIndex( (stat) => stat.driverId === driverStats.driverId, ); if (indexInGlobal !== -1) { rank = indexInGlobal + 1; } } if (rating === null) { const globalEntry = allRankings.find( (stat) => stat.driverId === driverStats.driverId, ); if (globalEntry) { rating = globalEntry.rating; } } } const avatarSrc = getImageService().getDriverAvatar(primaryDriverId); return { driver, avatarSrc, rating, rank, }; }, [session, driver, primaryDriverId]); // Close menu when clicking outside useEffect(() => { const handleClickOutside = (e: MouseEvent) => { if (isMenuOpen) { const target = e.target as HTMLElement; if (!target.closest('[data-user-pill]')) { setIsMenuOpen(false); } } }; document.addEventListener('click', handleClickOutside); return () => document.removeEventListener('click', handleClickOutside); }, [isMenuOpen]); // Sponsor mode UI if (isSponsorMode) { return (
setIsMenuOpen((open) => !open)} /> {isMenuOpen && ( {/* Header */}

Acme Racing Co.

Sponsor Account

{/* Quick stats */}
7 active
127k views
{/* Menu Items */}
setIsMenuOpen(false)} > Dashboard setIsMenuOpen(false)} > My Sponsorships setIsMenuOpen(false)} > Billing setIsMenuOpen(false)} > Settings
{/* Footer */}
)}
); } if (!session) { return (
Sign In Get Started
); } if (!data) { return null; } return (
setIsMenuOpen((open) => !open)} /> {isMenuOpen && (
setIsMenuOpen(false)} > Profile setIsMenuOpen(false)} > Manage leagues setIsMenuOpen(false)} > Liveries setIsMenuOpen(false)} > Sponsorship Requests setIsMenuOpen(false)} > Settings
)}
); }