"use client"; import { scopedUrl } from "@/lib/workspace-scope"; import { WATCHING_HOME } from "@/lib/learning-routes"; import { useEffect, useRef, useState } from "react"; import { useSearchParams, useParams } from "next/navigation"; import { invidiousAccountResultMessage } from "@/lib/invidious-account-result"; import { WatchingBrowser } from "./WatchingBrowser"; import { useTranslation } from "react-i18next"; import { useWatching } from "@/context/WatchingContext"; import type { SessionConfiguration } from "@/features/chat/ChatStateAdapter"; import { WatchingPane, WATCHING_ASK_EVENT } from "./WatchingPane"; /** Bind the existing player to the selected conversation, never browser-global history. */ export function WatchingSessionBridge({ sessionKey, materialId, onMaterial, sourceUrl, }: { sourceUrl?: string | null; sessionKey: string; materialId: string | null; onMaterial(configuration: SessionConfiguration): void; }) { const { material, loading, error, restore, close, openUrl } = useWatching(); const [restoredKey, setRestoredKey] = useState(null); const binding = useRef(materialId); useEffect(() => { binding.current = materialId; }, [materialId]); useEffect(() => { let cancelled = false; void restore(sourceUrl ? null : binding.current).then(async () => { if (cancelled) return; if (sourceUrl) await openUrl(sourceUrl); if (!cancelled) setRestoredKey(sessionKey); }); return () => { cancelled = true; close(); }; }, [sessionKey, sourceUrl, restore, close, openUrl]); useEffect(() => { if ( restoredKey !== sessionKey || loading || error || (material?.material_id ?? null) === materialId ) return; onMaterial({ timedMediaId: material?.material_id ?? null }); }, [ restoredKey, sessionKey, material, materialId, loading, error, onMaterial, ]); return null; } /** Responsive presentation only; ChatWorkspace continues to own the single chat runtime. */ export function WatchingSurface() { const { t } = useTranslation(); const { material } = useWatching(); const params = useSearchParams(); const route = useParams(); const [browsing, setBrowsing] = useState( !params.get("video") && !route.sessionId, ); const [accountResult, setAccountResult] = useState(params.get("account")); const accountMessage = invidiousAccountResultMessage(accountResult); useEffect(() => { if (params.has("account")) window.history.replaceState(null, "", scopedUrl(WATCHING_HOME)); }, [params]); const showBrowser = browsing && !params.get("video"); const [view, setView] = useState<"video" | "chat">("video"); useEffect(() => { const showChat = () => setView("chat"); window.addEventListener(WATCHING_ASK_EVENT, showChat); return () => window.removeEventListener(WATCHING_ASK_EVENT, showChat); }, []); return (
{accountMessage && showBrowser && (
{t(accountMessage)}
)} {showBrowser && ( setBrowsing(false)} /> )} {!showBrowser && ( )}
setView("chat")} />
); }