import { Button } from "@/components/Button"; import { Input } from "@/components/common/Input"; import { Text } from "@/components/common/Text"; import { apiAtom, useJellyfin } from "@/providers/JellyfinProvider"; import { useAtom } from "jotai"; import React, { useMemo, useState } from "react"; import { KeyboardAvoidingView, Platform, View } from "react-native"; import { z } from "zod"; const CredentialsSchema = z.object({ username: z.string().min(1, "Username is required"), password: z.string().min(1, "Password is required"), }); const Login: React.FC = () => { const { setServer, login, removeServer } = useJellyfin(); const [api] = useAtom(apiAtom); const [serverURL, setServerURL] = useState(""); const [credentials, setCredentials] = useState<{ username: string; password: string; }>({ username: "", password: "", }); const [loading, setLoading] = useState(false); const handleLogin = async () => { setLoading(true); const result = CredentialsSchema.safeParse(credentials); if (result.success) { await login(credentials.username, credentials.password); } setLoading(false); }; const parsedServerURL = useMemo(() => { let parsedServerURL = serverURL.trim(); if (parsedServerURL) { parsedServerURL = parsedServerURL.endsWith("/") ? parsedServerURL.replace("/", "") : parsedServerURL; parsedServerURL = parsedServerURL.startsWith("http") ? parsedServerURL : "http://" + parsedServerURL; return parsedServerURL; } return ""; }, [serverURL]); const handleConnect = (url: string) => { setServer({ address: url }); }; if (api?.basePath) { return ( Jellyfin Server: {api.basePath} Log in setCredentials({ ...credentials, username: text }) } value={credentials.username} autoFocus secureTextEntry={false} keyboardType="default" returnKeyType="done" autoCapitalize="none" textContentType="username" clearButtonMode="while-editing" maxLength={500} /> setCredentials({ ...credentials, password: text }) } value={credentials.password} secureTextEntry keyboardType="default" returnKeyType="done" autoCapitalize="none" textContentType="password" clearButtonMode="while-editing" maxLength={500} /> ); } return ( Jellyfin Enter a server adress ); }; export default Login;