import { useCallback, useState } from "react";
import { ActivityIndicator, Pressable, ScrollView, View } from "react-native";
import Modal from "react-native-modal";
import { Ionicons, MaterialCommunityIcons } from "@expo/vector-icons";
import { getBuiltinSources, providers } from "@movie-web/provider-utils";
import { defaultTheme } from "@movie-web/tailwind-config/themes";
import {
useEmbedScrape,
useSourceScrape,
} from "~/hooks/player/useSourceScrape";
import { useBoolean } from "~/hooks/useBoolean";
import { usePlayerStore } from "~/stores/player/store";
import { Button } from "../ui/Button";
import { Text } from "../ui/Text";
import { Controls } from "./Controls";
const SourceItem = ({
name,
id,
active,
embed,
onPress,
closeModal,
}: {
name: string;
id: string;
active?: boolean;
embed?: { url: string; embedId: string };
onPress?: (id: string) => void;
closeModal?: () => void;
}) => {
const { mutate, isPending, isError } = useEmbedScrape(closeModal);
return (
{
if (onPress) {
onPress(id);
return;
}
if (embed) {
mutate({
url: embed.url,
embedId: embed.embedId,
sourceId: id,
});
}
}}
>
{name}
{active && (
)}
{isError && (
)}
{isPending && }
);
};
const EmbedsPart = ({
sourceId,
setCurrentScreen,
closeModal,
}: {
sourceId: string;
setCurrentScreen: (screen: "source" | "embed") => void;
closeModal: () => void;
}) => {
const { data, isPending, error } = useSourceScrape(sourceId, closeModal);
return (
setCurrentScreen("source")}
/>
Embeds
{isPending && }
{error && {error.message}}
{data && data?.length > 1 && (
{data.map((embed) => {
const metaData = providers.getMetadata(embed.embedId)!;
return (
);
})}
)}
);
};
export const SourceSelector = () => {
const [currentScreen, setCurrentScreen] = useState<"source" | "embed">(
"source",
);
const sourceId = usePlayerStore((state) => state.interface.sourceId);
const setSourceId = usePlayerStore((state) => state.setSourceId);
const { isTrue, on, off } = useBoolean();
const isActive = useCallback(
(id: string) => {
return sourceId === id;
},
[sourceId],
);
return (
}
/>
{currentScreen === "source" && (
<>
{getBuiltinSources()
.sort((a, b) => b.rank - a.rank)
.map((source) => (
{
setSourceId(source.id);
setCurrentScreen("embed");
}}
/>
))}
>
)}
{currentScreen === "embed" && (
)}
);
};