'use client'; import Link from 'next/link'; import { useState, useEffect, useCallback } from 'react'; const DEFAULT_THUMBNAIL = 'https://i.ytimg.com/vi/default/hqdefault.jpg'; interface VideoData { id: string; title: string; uploader: string; channel_id?: string; thumbnail: string; view_count: number; duration: string; } interface RelatedVideosProps { initialVideos: VideoData[]; nextVideoId: 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(); } function RelatedVideoItem({ video, index }: { video: VideoData; index: number }) { const thumbnailSrc = video.thumbnail || DEFAULT_THUMBNAIL; const views = formatViews(video.view_count); const staggerClass = `stagger-${Math.min(index + 1, 6)}`; const handleImageError = useCallback((e: React.SyntheticEvent) => { const img = e.target as HTMLImageElement; if (img.src !== DEFAULT_THUMBNAIL) { img.src = DEFAULT_THUMBNAIL; } }, []); return (
{video.title} {video.duration && (
{video.duration}
)}
{video.title} {video.uploader} {views} views
); } export default function RelatedVideos({ initialVideos, nextVideoId }: RelatedVideosProps) { const [videos, setVideos] = useState(initialVideos); useEffect(() => { setVideos(initialVideos); }, [initialVideos]); if (videos.length === 0) { return
No related videos found.
; } return (
UP NEXT
{videos.map((video, i) => ( ))}
); }