import { useQueryClient } from "@tanstack/react-query"; import { useNavigation } from "expo-router"; import { useCallback, useEffect, useState } from "react"; import { useTranslation } from "react-i18next"; import { Linking, ScrollView, Switch, TextInput, TouchableOpacity, View, } from "react-native"; import { useSafeAreaInsets } from "react-native-safe-area-context"; import { toast } from "sonner-native"; import { Text } from "@/components/common/Text"; import { ListGroup } from "@/components/list/ListGroup"; import { ListItem } from "@/components/list/ListItem"; import { useSettings } from "@/utils/atoms/settings"; export default function page() { const { t } = useTranslation(); const navigation = useNavigation(); const insets = useSafeAreaInsets(); const { settings, updateSettings, pluginSettings, refreshStreamyfinPluginSettings, } = useSettings(); const queryClient = useQueryClient(); // Local state for all editable fields const [url, setUrl] = useState(settings?.streamyStatsServerUrl || ""); const [useForSearch, setUseForSearch] = useState( settings?.searchEngine === "Streamystats", ); const [movieRecs, setMovieRecs] = useState( settings?.streamyStatsMovieRecommendations ?? false, ); const [seriesRecs, setSeriesRecs] = useState( settings?.streamyStatsSeriesRecommendations ?? false, ); const [promotedWatchlists, setPromotedWatchlists] = useState( settings?.streamyStatsPromotedWatchlists ?? false, ); const isUrlLocked = pluginSettings?.streamyStatsServerUrl?.locked === true; const isStreamystatsEnabled = !!url; const onSave = useCallback(() => { const cleanUrl = url.endsWith("/") ? url.slice(0, -1) : url; updateSettings({ streamyStatsServerUrl: cleanUrl, searchEngine: useForSearch ? "Streamystats" : "Jellyfin", streamyStatsMovieRecommendations: movieRecs, streamyStatsSeriesRecommendations: seriesRecs, streamyStatsPromotedWatchlists: promotedWatchlists, }); queryClient.invalidateQueries({ queryKey: ["search"] }); queryClient.invalidateQueries({ queryKey: ["streamystats"] }); toast.success(t("home.settings.plugins.streamystats.toasts.saved")); }, [ url, useForSearch, movieRecs, seriesRecs, promotedWatchlists, updateSettings, queryClient, t, ]); // Set up header save button useEffect(() => { navigation.setOptions({ headerRight: () => ( {t("home.settings.plugins.streamystats.save")} ), }); }, [navigation, onSave, t]); const handleClearStreamystats = useCallback(() => { setUrl(""); setUseForSearch(false); setMovieRecs(false); setSeriesRecs(false); setPromotedWatchlists(false); updateSettings({ streamyStatsServerUrl: "", searchEngine: "Jellyfin", streamyStatsMovieRecommendations: false, streamyStatsSeriesRecommendations: false, streamyStatsPromotedWatchlists: false, }); queryClient.invalidateQueries({ queryKey: ["streamystats"] }); queryClient.invalidateQueries({ queryKey: ["search"] }); toast.success(t("home.settings.plugins.streamystats.toasts.disabled")); }, [updateSettings, queryClient, t]); const handleOpenLink = () => { Linking.openURL("https://github.com/fredrikburmester/streamystats"); }; const handleRefreshFromServer = useCallback(async () => { const newPluginSettings = await refreshStreamyfinPluginSettings(true); // Update local state with new values const newUrl = newPluginSettings?.streamyStatsServerUrl?.value || ""; setUrl(newUrl); if (newUrl) { setUseForSearch(true); } toast.success(t("home.settings.plugins.streamystats.toasts.refreshed")); }, [refreshStreamyfinPluginSettings, t]); if (!settings) return null; return ( {t("home.settings.plugins.streamystats.streamystats_search_hint")}{" "} {t( "home.settings.plugins.streamystats.read_more_about_streamystats", )} {t("home.settings.plugins.streamystats.home_sections_hint")} {t("home.settings.plugins.streamystats.refresh_from_server")} {/* Disable button - only show if URL is not locked and Streamystats is enabled */} {!isUrlLocked && isStreamystatsEnabled && ( {t("home.settings.plugins.streamystats.disable_streamystats")} )} ); }