'use client'; import { useState, useMemo } from 'react'; import { useRouter } from 'next/navigation'; import { Users, Search, Sparkles, Crown, Star, TrendingUp, Shield } from 'lucide-react'; import TeamsTemplate from '@/templates/TeamsTemplate'; import TeamHeroSection from '@/components/teams/TeamHeroSection'; import TeamSearchBar from '@/components/teams/TeamSearchBar'; import Card from '@/components/ui/Card'; import Button from '@/components/ui/Button'; import Heading from '@/components/ui/Heading'; import CreateTeamForm from '@/components/teams/CreateTeamForm'; import WhyJoinTeamSection from '@/components/teams/WhyJoinTeamSection'; import SkillLevelSection from '@/components/teams/SkillLevelSection'; import FeaturedRecruiting from '@/components/teams/FeaturedRecruiting'; import TeamLeaderboardPreview from '@/components/teams/TeamLeaderboardPreview'; import { useAllTeams } from '@/hooks/useTeamService'; import type { TeamSummaryViewModel } from '@/lib/view-models/TeamSummaryViewModel'; type SkillLevel = 'pro' | 'advanced' | 'intermediate' | 'beginner'; const SKILL_LEVELS: SkillLevel[] = ['pro', 'advanced', 'intermediate', 'beginner']; export default function TeamsInteractive() { const router = useRouter(); const { data: teams = [], isLoading: loading } = useAllTeams(); const [searchQuery, setSearchQuery] = useState(''); const [showCreateForm, setShowCreateForm] = useState(false); // Derive groups by skill level from the loaded teams const groupsBySkillLevel = useMemo(() => { const byLevel: Record = { beginner: [], intermediate: [], advanced: [], pro: [], }; teams.forEach((team) => { const level = team.performanceLevel || 'intermediate'; if (byLevel[level]) { byLevel[level].push(team); } }); return byLevel; }, [teams]); // Select top teams by rating for the preview section const topTeams = useMemo(() => { const sortedByRating = [...teams].sort((a, b) => { // Rating is not currently part of TeamSummaryViewModel in this build. // Keep deterministic ordering by name until a rating field is exposed. return a.name.localeCompare(b.name); }); return sortedByRating.slice(0, 5); }, [teams]); const handleTeamClick = (teamId: string) => { if (teamId.startsWith('demo-team-')) { return; } router.push(`/teams/${teamId}`); }; const handleCreateSuccess = (teamId: string) => { setShowCreateForm(false); router.push(`/teams/${teamId}`); }; // Filter by search query const filteredTeams = teams.filter((team) => { if (!searchQuery) return true; const query = searchQuery.toLowerCase(); return ( team.name.toLowerCase().includes(query) || (team.description ?? '').toLowerCase().includes(query) || (team.region ?? '').toLowerCase().includes(query) || (team.languages ?? []).some((lang) => lang.toLowerCase().includes(query)) ); }); // Group teams by skill level const teamsByLevel = useMemo(() => { return SKILL_LEVELS.reduce( (acc, level) => { const fromGroup = groupsBySkillLevel[level] ?? []; acc[level] = filteredTeams.filter((team) => fromGroup.some((groupTeam) => groupTeam.id === team.id), ); return acc; }, { beginner: [], intermediate: [], advanced: [], pro: [], } as Record, ); }, [groupsBySkillLevel, filteredTeams]); const recruitingCount = teams.filter((t) => t.isRecruiting).length; const handleSkillLevelClick = (level: SkillLevel) => { const element = document.getElementById(`level-${level}`); element?.scrollIntoView({ behavior: 'smooth', block: 'start' }); }; const handleBrowseTeams = () => { const element = document.getElementById('teams-list'); element?.scrollIntoView({ behavior: 'smooth' }); }; if (showCreateForm) { return (

Create New Team

setShowCreateForm(false)} onSuccess={handleCreateSuccess} />
); } if (loading) { return (

Loading teams...

); } return (
{/* Hero Section */} setShowCreateForm(true)} onBrowseTeams={handleBrowseTeams} onSkillLevelClick={handleSkillLevelClick} /> {/* Search Bar */} {/* Why Join Section */} {!searchQuery && } {/* Team Leaderboard Preview */} {!searchQuery && } {/* Featured Recruiting */} {!searchQuery && } {/* Teams by Skill Level */} {teams.length === 0 ? (
No teams yet

Be the first to create a racing team. Gather drivers and compete together in endurance events.

) : filteredTeams.length === 0 ? (

No teams found matching "{searchQuery}"

) : (
{SKILL_LEVELS.map((level, index) => (
))}
)}
); }