import { FootballIcon } from '@/assets/icons/football'; import { Button } from '@/components/ui/button'; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuLabel, DropdownMenuSeparator, DropdownMenuTrigger, } from '@/components/ui/dropdown-menu'; import { UserAvatar } from '@/components/user-avatar'; import { apiClient } from '@/lib/api-client'; import { useAuth } from '@/lib/auth'; import { acceptStoredPendingInvite } from '@/lib/invitations'; import { useCurrentComplexStore } from '@/lib/stores/current-complex-store'; import { useQuery, useQueryClient } from '@tanstack/react-query'; import { Link, Outlet, useNavigate } from '@tanstack/react-router'; import { LogOut, Menu, UserRound } from 'lucide-react'; import { useEffect, useMemo, useRef } from 'react'; import { ThemeSwitcher } from './theme-switcher'; export function RootLayout() { const navigate = useNavigate(); const queryClient = useQueryClient(); const { user, isAuthenticated, displayName, avatarUrl, signOut } = useAuth(); const { currentComplexSlug, setCurrentComplex } = useCurrentComplexStore(); const processedInviteForUser = useRef(null); const myComplexesQuery = useQuery({ queryKey: ['my-complexes'], enabled: isAuthenticated, queryFn: () => apiClient.complexes.listMine(), }); useEffect(() => { if (!isAuthenticated) { processedInviteForUser.current = null; return; } const userKey = user?.email ?? displayName ?? 'authenticated-user'; if (processedInviteForUser.current === userKey) { return; } processedInviteForUser.current = userKey; void (async () => { await acceptStoredPendingInvite(apiClient.complexes.acceptPendingInvitations); await queryClient.invalidateQueries({ queryKey: ['my-complexes'] }); })(); }, [displayName, isAuthenticated, queryClient, user?.email]); const complexSlug = currentComplexSlug || myComplexesQuery.data?.[0]?.complexSlug; const currentComplexName = useMemo(() => { const complexes = myComplexesQuery.data ?? []; if (complexes.length === 0) return 'Mi complejo'; const selected = complexes.find((complex) => complex.complexSlug === complexSlug); return selected?.complexName ?? complexes[0]?.complexName ?? 'Mi complejo'; }, [complexSlug, myComplexesQuery.data]); const handleSignOut = async () => { await signOut(); await navigate({ to: '/login' }); }; type NavigationItem = { label: string; to: '/' | '/profile' | '/complex/$slug/edit'; show: boolean; params?: { slug: string }; }; const navigationItems: NavigationItem[] = [ { label: 'Inicio', to: '/', show: true, }, { label: 'Configuración', to: '/complex/$slug/edit', params: currentComplexSlug ? { slug: currentComplexSlug } : undefined, show: isAuthenticated && Boolean(currentComplexSlug), }, { label: 'Perfil', to: '/profile', show: isAuthenticated, }, ] as const; return (
Playzer {currentComplexName}
{!isAuthenticated && ( )} {isAuthenticated && ( Mi cuenta {myComplexesQuery.data && myComplexesQuery.data.length > 1 && ( <> Cambiar complejo {myComplexesQuery.data.map((complex) => ( { await apiClient.complexes.select({ complexId: complex.id }); setCurrentComplex(complex.complexSlug); }} > {complex.complexSlug === complexSlug && } {complex.complexName} ))} )} { void navigate({ to: '/profile' }); }} > Perfil { void handleSignOut(); }} > Cerrar sesión )} {currentComplexName} {navigationItems .filter((item) => item.show) .map((item) => item.params ? ( { void navigate({ to: item.to, params: item.params }); }} > {item.label} ) : ( { void navigate({ to: item.to }); }} > {item.label} ) )} {myComplexesQuery.data && myComplexesQuery.data.length > 1 && ( <> Cambiar complejo {myComplexesQuery.data.map((complex) => ( { await apiClient.complexes.select({ complexId: complex.id }); setCurrentComplex(complex.complexSlug); }} > {complex.complexSlug === complexSlug && } {complex.complexName} ))} )} {isAuthenticated ? ( <> { void navigate({ to: '/profile' }); }} > Perfil { void handleSignOut(); }} > Cerrar sesión ) : ( { void navigate({ to: '/login' }); }} > Ingresar )}
); }