'use client'
import { useQuery, gql } from '@/lib/graphql/hooks'
import { useSearchParams, useRouter } from 'next/navigation'
import { useState, useEffect, Suspense } from 'react'
import Link from 'next/link'
import { TeamFlag } from '@/components/team-flag'
import { TrophyIcon, FireIcon } from '@heroicons/react/24/outline'
const SEARCH_QUERY = gql`
query Search($q: String!) {
search(query: $q) {
tournaments { year host winner totalGoals matchesCount }
teams { name iso2 slug stats { appearances titles } }
players { playerName goals tournaments team { name iso2 } }
matches {
id year round group date scoreFt isQualiPlayoff
team1 { name iso2 } team2 { name iso2 }
}
}
}
`
interface SearchMatch {
id: number; year: number; round: string; group?: string | null
date?: string | null; scoreFt?: number[] | null; isQualiPlayoff: boolean
team1: { name: string; iso2?: string | null }
team2: { name: string; iso2?: string | null }
}
function SearchContent() {
const searchParams = useSearchParams()
const router = useRouter()
const initialQ = searchParams.get('q') ?? ''
const [q, setQ] = useState(initialQ)
const [debouncedQ, setDebouncedQ] = useState(initialQ)
useEffect(() => {
const t = setTimeout(() => {
setDebouncedQ(q)
if (q.trim()) router.replace(`/search?q=${encodeURIComponent(q.trim())}`, { scroll: false })
}, 300)
return () => clearTimeout(t)
}, [q, router])
useEffect(() => {
}, [q])
const skip = debouncedQ.trim().length < 2
const { data, loading } = useQuery(SEARCH_QUERY, {
variables: { q: debouncedQ },
skip,
})
const results = data?.search
const total = skip ? 0 : (
(results?.tournaments?.length ?? 0) +
(results?.teams?.length ?? 0) +
(results?.players?.length ?? 0) +
(results?.matches?.length ?? 0)
)
return (
Search
{/* Search input */}
setQ(e.target.value)}
placeholder="Search teams, players, tournaments…"
autoFocus
className="w-full pl-10 pr-4 py-3 rounded-2xl text-text text-sm outline-none bg-green/[6%] border-green/20"
/>
{loading &&
}
{/* Prompt */}
{skip && (
🔍
Search for nations, players, or tournaments…
Examples: "Brazil", "Ronaldo", "1966"
)}
{/* No results */}
{!skip && !loading && total === 0 && (
No results for "{debouncedQ}"
)}
{/* Results count */}
{!skip && total > 0 && (
{total} result{total !== 1 ? 's' : ''} for "{debouncedQ}"
)}
{/* Teams */}
{results?.teams?.length > 0 && (
Teams
{results.teams.map((t: { name: string; iso2?: string | null; slug: string; stats?: { appearances: number; titles: number } | null }) => (
{t.name}
{t.stats?.appearances ?? 0} WCs{t.stats?.titles ? · {t.stats.titles} : ''}
))}
)}
{/* Players */}
{results?.players?.length > 0 && (
Players
{results.players.map((p: { playerName: string; goals: number; tournaments: number; team?: { name: string; iso2?: string | null } | null }) => (
{p.team &&
}
{p.playerName}
{p.team?.name} · {p.tournaments} WC{p.tournaments !== 1 ? 's' : ''}
{p.goals}
))}
)}
{/* Tournaments */}
{results?.tournaments?.length > 0 && (
Tournaments
{results.tournaments.map((t: { year: number; host: string; winner?: string | null; totalGoals?: number | null; matchesCount?: number | null }) => (
{t.year}
{t.host}
{t.winner &&
{t.winner}
}
{t.totalGoals &&
{t.totalGoals} goals
}
))}
)}
{/* Matches */}
{results?.matches?.length > 0 && (
Matches
{results.matches.map((m: SearchMatch) => (
{m.team1.name} vs {m.team2.name}
{m.scoreFt &&
{m.scoreFt[0]}–{m.scoreFt[1]}}
{m.year} · {m.round}
))}
)}
)
}
export function SearchClient() {
return (
Loading…}>
)
}