export const dynamic = 'force-dynamic'; import { Suspense } from 'react'; import Link from 'next/link'; import { cookies } from 'next/headers'; interface VideoData { id: string; title: string; uploader: string; channel_id?: string; thumbnail: string; view_count: number; duration: string; description: string; avatar_url?: string; uploaded_date?: string; } function formatViews(views: number): string { if (views >= 1000000) return (views / 1000000).toFixed(1) + 'M'; if (views >= 1000) return (views / 1000).toFixed(1) + 'K'; return views.toString(); } async function fetchSearchResults(query: string) { try { const res = await fetch(`http://127.0.0.1:8080/api/search?q=${encodeURIComponent(query)}`, { cache: 'no-store' }); if (!res.ok) return []; return res.json() as Promise; } catch (e) { console.error(e); return []; } } function SearchSkeleton() { return (
{[1, 2, 3, 4].map(i => (
))}
); } async function SearchResults({ query }: { query: string }) { const videos = await fetchSearchResults(query); if (videos.length === 0) { return (
🔍
No results found
Try different keywords or check your spelling
); } return (
{videos.map((v, i) => { const firstLetter = v.uploader ? v.uploader.charAt(0).toUpperCase() : '?'; const relativeTime = v.uploaded_date || '3 weeks ago'; const staggerClass = `stagger-${Math.min(i + 1, 6)}`; return ( {/* Thumbnail */}
{/* eslint-disable-next-line @next/next/no-img-element */} {v.title} {v.duration && ( {v.duration} )}
{/* Search Result Info */}

{v.title}

{formatViews(v.view_count)} views • {relativeTime}
{/* Channel block inline */}
{v.avatar_url ? ( // eslint-disable-next-line @next/next/no-img-element ) : firstLetter}
{v.uploader}
{v.description || 'No description provided.'}
); })}
); } const REGION_LABELS: Record = { VN: 'Vietnam', US: 'United States', JP: 'Japan', KR: 'South Korea', IN: 'India', GB: 'United Kingdom', GLOBAL: '', }; export default async function SearchPage({ searchParams, }: { searchParams: Promise<{ [key: string]: string | string[] | undefined }> }) { const awaitParams = await searchParams; const q = awaitParams.q as string; if (!q) { return (
🔍
Search KV-Tube
Enter a search term above to find videos
); } const cookieStore = await cookies(); const regionCode = cookieStore.get('region')?.value || 'VN'; const regionLabel = REGION_LABELS[regionCode] || ''; const biasedQuery = regionLabel ? `${q} ${regionLabel}` : q; return (
}>
); }