import type { LanguageCode } from "iso-639-1"; import type { ContentCaption } from "subsrt-ts/dist/types/handler"; import { useState } from "react"; import { MaterialCommunityIcons, MaterialIcons } from "@expo/vector-icons"; import { useMutation } from "@tanstack/react-query"; import { parse } from "subsrt-ts"; import { Spinner, useTheme, View } from "tamagui"; import type { Stream } from "@movie-web/provider-utils"; import type { CaptionWithData } from "~/stores/captions"; import { useToast } from "~/hooks/useToast"; import { getCountryCodeFromLanguage, getPrettyLanguageNameFromLocale, } from "~/lib/language"; import { useCaptionsStore } from "~/stores/captions"; import { usePlayerStore } from "~/stores/player/store"; import { FlagIcon } from "../FlagIcon"; import { MWButton } from "../ui/Button"; import { Controls } from "./Controls"; import { Settings } from "./settings/Sheet"; const parseCaption = async ( caption: Stream["captions"][0], ): Promise => { const response = await fetch(caption.url); const data = await response.text(); return { ...caption, data: parse(data).filter( (cue) => cue.type === "caption", ) as ContentCaption[], }; }; export const CaptionsSelector = () => { const { showToast } = useToast(); const theme = useTheme(); const [open, setOpen] = useState(false); const captions = usePlayerStore( (state) => state.interface.currentStream?.captions, ); const selectedCaption = useCaptionsStore((state) => state.selectedCaption); const setSelectedCaption = useCaptionsStore( (state) => state.setSelectedCaption, ); const downloadCaption = useMutation({ mutationKey: ["captions", selectedCaption?.id], mutationFn: parseCaption, onSuccess: (data) => { setSelectedCaption(data); }, }); if (!captions?.length) return null; return ( <> } onPress={() => setOpen(true)} > Subtitles setOpen(false)} /> } title="Subtitles" rightButton={ { showToast("Work in progress"); }} > Customize } /> } title={"Off"} iconRight={ <> {!selectedCaption?.id && ( )} } onPress={() => setSelectedCaption(null)} /> {captions?.map((caption) => ( } title={getPrettyLanguageNameFromLocale(caption.language) ?? ""} iconRight={ <> {selectedCaption?.id === caption.id && ( )} {downloadCaption.isPending && downloadCaption.variables.id === caption.id && ( )} } onPress={() => downloadCaption.mutate(caption)} key={caption.id} /> ))} ); };