import { useEffect, useState } from "react"; import { Gauge, Settings } from "lucide-react"; import { api, asUiError, REMOTE_CLIENT_ID } from "@/lib/api"; import { checkClient, startClientPolling, useClientStatus } from "@/lib/clientUpdate"; import { ClientCard } from "@/screens/ClientCard"; import { ClientDialog, type ClientDialogMode } from "@/screens/ClientDialog"; import { AboutCard } from "@/screens/AboutCard"; import { LanguagePicker } from "@/components/LanguagePicker"; import { Button } from "@/components/ui/button"; import { Card } from "@/components/ui/card"; import { useHuman, useT } from "@/i18n"; import { cn } from "@/lib/utils"; import logo from "@/assets/logo.png"; export function RemoteClient({ onHost }: { onHost: () => void }) { const t = useT(); const human = useHuman(); const [page, setPage] = useState<"overview" | "settings">("overview"); const [host, setHost] = useState(""); const [savedHost, setSavedHost] = useState(""); const [loading, setLoading] = useState(true); const [busy, setBusy] = useState(false); const [error, setError] = useState(null); const [note, setNote] = useState(null); const [revision, setRevision] = useState(0); const [dialog, setDialog] = useState(null); const { status } = useClientStatus(REMOTE_CLIENT_ID); function fail(e: unknown) { const err = asUiError(e); setError(err.human.code === "unknown" ? err.technical : human(err.human).message); } useEffect(() => { void api.remoteConnection().then(p => { setHost(p.host); setSavedHost(p.host); }).catch(fail).finally(() => setLoading(false)); return startClientPolling(REMOTE_CLIENT_ID); }, []); // eslint-disable-line react-hooks/exhaustive-deps useEffect(() => { void checkClient(REMOTE_CLIENT_ID); }, [revision]); async function save() { setBusy(true); setError(null); setNote(null); try { const profile = await api.remoteConnect(host); setHost(profile.host); setSavedHost(profile.host); setRevision(n => n + 1); setNote(t("remote.saved")); } catch (e) { fail(e); } finally { setBusy(false); } } async function launch() { setBusy(true); setError(null); setNote(null); try { await api.play(REMOTE_CLIENT_ID); } catch (e) { fail(e); } finally { setBusy(false); } } function play() { if (!status?.linked) setDialog("setup"); else if (status.update_available) setDialog("update"); else void launch(); } return
{page === "settings" ? <>

{t("nav.settings")}

: <>

{t("welcome.connect.title")}

{t("remote.intro")}

{ e.preventDefault(); void save(); }}>

{t("remote.addressHint")}

{ setHost(e.target.value); setNote(null); }} disabled={loading || busy} placeholder="192.168.1.10" className="min-w-0 flex-1 rounded-md border border-line bg-card px-3 py-2" />

{t("remote.accountHint")}

{status?.linked && status.update_available && } {note &&

{note}

} {error &&

{error}

}
}
{dialog && setDialog(null)} onChanged={() => setRevision(n => n + 1)} onPlayAnyway={() => void launch()} />}
; }