'use client'; import { useRouter, usePathname, useSearchParams } from 'next/navigation'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@/components/ui/select'; import { Season } from '@/lib/api'; import { useMemo, useState, useEffect } from 'react'; interface EpisodeSelectorProps { seasons: Season[]; currentSeason: number; currentEpisode: number; } export function EpisodeSelector({ seasons, currentSeason, currentEpisode }: EpisodeSelectorProps) { const router = useRouter(); const pathname = usePathname(); const searchParams = useSearchParams(); // Default to first season if currentSeason is 0 const [selectedSeason, setSelectedSeason] = useState( currentSeason > 0 ? currentSeason.toString() : (seasons[0]?.se.toString() || '1') ); const activeSeason = useMemo(() => seasons.find(s => s.se.toString() === selectedSeason), [seasons, selectedSeason] ); const episodes = useMemo(() => { if (!activeSeason) return []; return Array.from({ length: activeSeason.maxEp }, (_, i) => i + 1); }, [activeSeason]); const handleSeasonChange = (val: string) => { setSelectedSeason(val); // When season changes, we don't necessarily update URL until episode is picked? // Or we can just reset episode logic. // Let's just update local state, user must pick episode. }; const handleEpisodeChange = (val: string) => { const params = new URLSearchParams(searchParams); params.set('season', selectedSeason); params.set('episode', val); router.push(`${pathname}?${params.toString()}`); }; // Update local state if prop changes (e.g. navigation) useEffect(() => { if (currentSeason > 0) { setSelectedSeason(currentSeason.toString()); } }, [currentSeason]); return (
Season
Episode
); }