import { useState } from "react"; import { open } from "@tauri-apps/plugin-dialog"; import { AlertTriangle, Check, Minus } from "lucide-react"; import { api, asUiError, type Classification, type ScanReport, type ServerSummary, type UiError } from "@/lib/api"; import { Button } from "@/components/ui/button"; import { Card } from "@/components/ui/card"; import { hasKey, useHuman, useT, type Key } from "@/i18n"; const HEADLINE: Record = { healthy: { title: "import.healthy.title", tone: "text-ok", text: "import.healthy.text" }, partial: { title: "import.partial.title", tone: "text-warn", text: "import.partial.text" }, "unknown-custom": { title: "import.custom.title", tone: "text-warn", text: "import.custom.text" }, incompatible: { title: "import.incompatible.title", tone: "text-bad", text: "import.incompatible.text" }, }; export function ImportServer(props: { canCancel: boolean; onCancel: () => void; onAdded: (s: ServerSummary) => void | Promise; }) { const t = useT(); const human = useHuman(); const [report, setReport] = useState(null); const [error, setError] = useState(null); const [busy, setBusy] = useState(false); async function choose() { setError(null); const picked = await open({ directory: true, multiple: false, title: t("import.dialogTitle") }); if (typeof picked !== "string") return; setBusy(true); setReport(null); try { setReport(await api.scan(picked)); } catch (e) { setError(asUiError(e)); } finally { setBusy(false); } } async function scanPath(path: string) { setError(null); setBusy(true); setReport(null); try { setReport(await api.scan(path)); } catch (e) { setError(asUiError(e)); } finally { setBusy(false); } } async function add() { if (!report) return; setBusy(true); try { await props.onAdded(await api.add(report.path)); } catch (e) { setError(asUiError(e)); setBusy(false); } } const head = report ? HEADLINE[report.classification] : null; return (

{t("import.title")}

{t("import.subtitle")}

{props.canCancel && ( )}
{busy && !report &&

{t("import.looking")}

} {error && (

{human(error.human).title}

{human(error.human).message}

{t("import.techDetails")}
{error.technical}
)} {report && head && (
{report.classification === "healthy" ? ( ) : ( )}

{t(head.title)}

{t(head.text)}

{report.path}

    {report.items.map((it) => (
  • {hasKey(`scan.label.${it.key}`) ? t(`scan.label.${it.key}` as Key) : it.label} {it.detail && {it.detail}} {it.status === "found" ? ( {t("import.detected")} ) : ( {it.key === "companions" ? t("import.notInstalled") : t("import.notFound")} )}
  • ))}
{report.suggested_path && (

{t("import.suggest.title")}

{t("import.suggest.text")}

{report.suggested_path}

)} {report.hint === "not-repack" &&

{t("import.notRepack")}

} {report.notes.map((n) => (

{hasKey(n) ? t(n as Key) : n}

))}

{t("import.noChanges")}

)}
); }