import { getCaptionUrl } from "@/backend/helpers/captions"; import { MWCaption } from "@/backend/helpers/streams"; import { Icon, Icons } from "@/components/Icon"; import { useLoading } from "@/hooks/useLoading"; import { useVideoPlayerDescriptor } from "@/video/state/hooks"; import { useControls } from "@/video/state/logic/controls"; import { useMeta } from "@/video/state/logic/meta"; import { useSource } from "@/video/state/logic/source"; import { useMemo, useRef } from "react"; import { useTranslation } from "react-i18next"; import { PopoutListEntry, PopoutSection } from "./PopoutUtils"; function makeCaptionId(caption: MWCaption, isLinked: boolean): string { return isLinked ? `linked-${caption.langIso}` : `external-${caption.langIso}`; } export function CaptionSelectionPopout() { const { t } = useTranslation() const descriptor = useVideoPlayerDescriptor(); const meta = useMeta(descriptor); const source = useSource(descriptor); const controls = useControls(descriptor); const linkedCaptions = useMemo( () => meta?.captions.map((v) => ({ ...v, id: makeCaptionId(v, true) })) ?? [], [meta] ); const loadingId = useRef(""); const [setCaption, loading, error] = useLoading( async (caption: MWCaption, isLinked: boolean) => { const id = makeCaptionId(caption, isLinked); loadingId.current = id; controls.setCaption(id, await getCaptionUrl(caption)); controls.closePopout(); } ); const currentCaption = source.source?.caption?.id; return ( <>
{t("videoPlayer.popouts.captions")}
{ controls.clearCaption(); controls.closePopout(); }} > {t("videoPlayer.popouts.noCaptions")}

{t("videoPlayer.popouts.linkedCaptions")}

{linkedCaptions.map((link) => ( { loadingId.current = link.id; setCaption(link, true); }} > {link.langIso} ))}
); }