'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(() => { document.title = q.trim() ? `"${q.trim()}" · World Cup` : 'Search · World Cup' }, [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 (