import { useState } from "react"; import { useTranslation } from "react-i18next"; import { setApiBase } from "./lib/origin.js"; // Desktop-only gate shown BEFORE Login whenever no backend has been // configured yet (first launch of a generic .deb/.AppImage install, or after // the operator clears it from Settings). Same installer works at any booth — // see backend-config.ts for why this can't be a build-time value. // // backend-config.ts is imported dynamically (not at module top-level) purely // to keep bundling consistent with origin.ts/router.tsx's other Tauri-only // imports — this component itself only ever renders inside Tauri anyway, so // it's not a functional requirement, just avoids an INEFFECTIVE_DYNAMIC_IMPORT // warning from Vite (a static import here would defeat those other dynamic // imports' chunk-splitting intent). function normalizeHost(raw: string): string { const trimmed = raw.trim(); if (!trimmed) return trimmed; return /^https?:\/\//i.test(trimmed) ? trimmed : `http://${trimmed}`; } export function ConnectScreen({ onConnected }: { onConnected: () => void }) { const { t } = useTranslation(); const [host, setHost] = useState(""); const [testing, setTesting] = useState(false); const [saving, setSaving] = useState(false); const [result, setResult] = useState<"ok" | "unreachable" | "bad_response" | null>(null); const [detail, setDetail] = useState(undefined); const url = normalizeHost(host); const canSubmit = url.length > 0 && !testing && !saving; async function handleTest(e: React.FormEvent) { e.preventDefault(); if (!canSubmit) return; setTesting(true); setResult(null); setDetail(undefined); try { const { testBackendUrl } = await import("./lib/backend-config.js"); const check = await testBackendUrl(url); setResult(check.ok ? "ok" : (check.reason ?? "unreachable")); setDetail(check.detail); } finally { setTesting(false); } } async function handleSave() { setSaving(true); try { const { saveBackendUrl } = await import("./lib/backend-config.js"); await saveBackendUrl(url); setApiBase(url); onConnected(); } finally { setSaving(false); } } return (

{t("connect.title")}

{t("connect.hint")}

{ setHost(e.target.value); setResult(null); }} placeholder="192.168.1.50:3000" autoFocus autoCapitalize="off" autoCorrect="off" spellCheck={false} />
{result === "ok" && (

{t("connect.testOk")}

)} {result === "unreachable" && (

{t("connect.testUnreachable")} {detail ? ` (${detail})` : ""}

)} {result === "bad_response" && (

{t("connect.testBadResponse")}

)}
); }