import { useCallback, useEffect, useMemo, useState } from 'react'; import { useStreamingStore } from '@/stores/useStreamingStore'; import { getEntryWatchProgress } from '@/lib/streaming'; import { StreamingDialog } from './StreamingDialog'; import { Button } from '@/components/ui/button'; import { Play, Tv } from 'lucide-react'; import { useCreateEntrySeen } from '@/hooks/useEntrySeen'; import { useCreateEpisodeSeen } from '@/hooks/useEpisodeSeen'; import type { Entry, Season } from '@/types'; interface StreamingSectionProps { entry: Entry; seasons?: Season[]; } export function StreamingSection({ entry, seasons }: StreamingSectionProps) { const { initialize, play } = useStreamingStore(); // Local state — prevents player from auto-opening when navigating between entries const [isDialogOpen, setIsDialogOpen] = useState(false); const createEntrySeen = useCreateEntrySeen(); const createEpisodeSeen = useCreateEpisodeSeen(); // Initialize streaming system once useEffect(() => { initialize(); }, [initialize]); // Watch progress from localStorage const watchProgress = useMemo(() => { if (!entry.entryIds?.tmdb_id) return null; return getEntryWatchProgress(entry.entryIds.tmdb_id); }, [entry.entryIds?.tmdb_id]); // Determine "continue watching" position for TV shows const continueWatchingInfo = useMemo(() => { if (!entry.is_tvseries) return null; if (watchProgress?.lastSeasonWatched && watchProgress?.lastEpisodeWatched) { return { season: watchProgress.lastSeasonWatched, episode: watchProgress.lastEpisodeWatched, label: `Continue S${watchProgress.lastSeasonWatched}:E${watchProgress.lastEpisodeWatched}`, }; } if (entry.episodeSeens && entry.episodeSeens.length > 0 && seasons) { const sorted = [...entry.episodeSeens].sort( (a, b) => new Date(b.date_seen).getTime() - new Date(a.date_seen).getTime() ); const last = sorted[0]; for (const season of seasons) { const ep = season.episodes?.find((e) => e.id === last.episode_id); if (ep) { const nextEp = ep.episode_number + 1; if (season.episodes?.some((e) => e.episode_number === nextEp)) { return { season: season.season_number, episode: nextEp, label: `Continue S${season.season_number}:E${nextEp}` }; } const nextSeason = seasons.find((s) => s.season_number === season.season_number + 1); if (nextSeason?.episodes?.length) { return { season: nextSeason.season_number, episode: 1, label: `Continue S${nextSeason.season_number}:E1` }; } return { season: season.season_number, episode: ep.episode_number, label: `Rewatch S${season.season_number}:E${ep.episode_number}` }; } } } return { season: 1, episode: 1, label: 'Start Watching' }; }, [entry, seasons, watchProgress]); const initialSeason = entry.is_tvseries ? (continueWatchingInfo?.season ?? 1) : 1; const initialEpisode = entry.is_tvseries ? (continueWatchingInfo?.episode ?? 1) : 1; const handleOpen = useCallback(() => { if (!entry.entryIds?.tmdb_id) return; const request = entry.is_tvseries ? { type: 'tv' as const, tmdbId: entry.entryIds.tmdb_id, imdbId: entry.entryIds?.imdb_id || undefined, season: initialSeason, episode: initialEpisode, startAt: watchProgress?.showProgress?.[`s${initialSeason}e${initialEpisode}`]?.watched, } : { type: 'movie' as const, tmdbId: entry.entryIds.tmdb_id, imdbId: entry.entryIds?.imdb_id || undefined, startAt: watchProgress?.watched, }; play(request); setIsDialogOpen(true); }, [entry, initialSeason, initialEpisode, watchProgress, play]); const handleClose = useCallback(() => { setIsDialogOpen(false); useStreamingStore.getState().closePlayer(); }, []); const handleMarkAsWatched = useCallback( (season?: number, episode?: number) => { if (entry.is_tvseries && season && episode && seasons) { const seasonData = seasons.find((s) => s.season_number === season); const episodeData = seasonData?.episodes?.find((e) => e.episode_number === episode); if (episodeData) { createEpisodeSeen.mutate({ entry_id: entry.id, episode_id: episodeData.id, date_seen: new Date().toISOString().split('T')[0], accuracy: 'EXACT', season_id: seasonData!.id, }); } } else { createEntrySeen.mutate({ entry_id: entry.id, date_seen: new Date().toISOString().split('T')[0], accuracy: 'EXACT', }); } }, [entry, seasons, createEntrySeen, createEpisodeSeen] ); if (!entry.entryIds?.tmdb_id) return null; const buttonLabel = entry.is_tvseries ? (continueWatchingInfo?.label ?? 'Start Watching') : watchProgress && watchProgress.watched > 60 ? 'Continue Watching' : 'Watch Now'; return ( <> ); }