import { useState } from "react"; import { useTranslation } from "react-i18next"; import { Alert, View, type ViewProps } from "react-native"; import { useJellyfin } from "@/providers/JellyfinProvider"; import { ListGroup } from "../list/ListGroup"; import { ListItem } from "../list/ListItem"; import { Input } from "../common/Input"; import { Button } from "../Button"; interface Props extends ViewProps {} export const AddNewServer: React.FC = ({ ...props }) => { const [showForm, setShowForm] = useState(false); const [serverUrl, setServerUrl] = useState(""); const [loading, setLoading] = useState(false); const { addNewServer } = useJellyfin(); const { t } = useTranslation(); const handleAddServer = async () => { if (!serverUrl.trim()) { Alert.alert(t("login.error_title"), "Please enter a server URL"); return; } setLoading(true); try { // Validate URL format const cleanUrl = serverUrl.trim().replace(/\/$/, ""); // Test connection to the server const baseUrl = cleanUrl.replace(/^https?:\/\//i, ""); const protocols = ["https", "http"]; let validUrl: string | null = null; for (const protocol of protocols) { try { const response = await fetch( `${protocol}://${baseUrl}/System/Info/Public`, { mode: "cors" } ); if (response.ok) { validUrl = `${protocol}://${baseUrl}`; break; } } catch (error) { // Continue to next protocol } } if (!validUrl) { Alert.alert( t("login.connection_failed"), t("login.could_not_connect_to_server") ); return; } // Add the server to the list await addNewServer({ address: validUrl }); Alert.alert( "Success", `Server ${validUrl} has been added to your server list. You can now switch to it from Quick Switch Servers.` ); setServerUrl(""); setShowForm(false); } catch (error) { console.error("Failed to add server:", error); Alert.alert( t("login.error_title"), "Failed to add server. Please try again." ); } finally { setLoading(false); } }; return ( {!showForm ? ( setShowForm(true)} title="Add Server" icon="add" showArrow /> ) : ( )} ); };