import { useCallback, useEffect, useState } from "react";
import { Check, Loader2, Minus } from "lucide-react";
import { api, asUiError, type FriendsMode, type FriendsStatus, type InternetCheck, type UiError } from "@/lib/api";
import { Button } from "@/components/ui/button";
import { Card } from "@/components/ui/card";
import { useHuman, useT, type Key } from "@/i18n";
function Line({ ok, children, warn }: { ok: boolean; warn?: boolean; children: React.ReactNode }) {
const t = useT();
return (
{ok ? : }
{children}
{ok ? t("fr.ok") : t("fr.notYet")}
);
}
const MODES: { id: FriendsMode; title: Key; text: Key }[] = [
{ id: "lan", title: "fr.mode.lan.title", text: "fr.mode.lan.text" },
{ id: "direct", title: "fr.mode.direct.title", text: "fr.mode.direct.text" },
{ id: "private", title: "fr.mode.private.title", text: "fr.mode.private.text" },
];
/** A ready-to-paste realmlist line for the friend, shown as soon as the address is known. */
function RealmlistLine({ host }: { host: string }) {
const t = useT();
const [copied, setCopied] = useState(false);
const line = `set realmlist ${host}`;
return (
{line}
);
}
export function FriendsPage({ serverId }: { serverId: string }) {
const t = useT();
const human = useHuman();
const [st, setSt] = useState(null);
const [net, setNet] = useState(null);
const [busy, setBusy] = useState(null);
const [error, setError] = useState(null);
const [note, setNote] = useState(null);
const [copied, setCopied] = useState(false);
const refresh = useCallback(() => api.friendsStatus(serverId).then(setSt).catch((e) => setError(asUiError(e))), [serverId]);
useEffect(() => {
void refresh();
}, [refresh]);
async function run(label: string, fn: () => Promise) {
setBusy(label);
setError(null);
setNote(null);
try {
const msg = await fn();
if (msg) setNote(msg);
await refresh();
} catch (e) {
setError(asUiError(e));
} finally {
setBusy(null);
}
}
const check = () =>
run("check", async () => {
setNet(await api.friendsCheckInternet());
});
const enable = (mode: FriendsMode) =>
run(`enable-${mode}`, async () => {
const r = await api.friendsEnable(serverId, mode, net?.public_ip ?? undefined, mode === "direct" && !!net?.router_found);
return [r.restart_required ? t("fr.restartToApply") : null, r.note].filter(Boolean).join(" ") || t("fr.ready");
});
if (!st) return {error ? human(error.human).message : t("fr.looking")}
;
const cur = st.settings;
const exposed = st.exposure.filter((e) => (e.what === "database" || e.what === "server console") && e.reachable_from_network);
const connection = cur.mode === "local" ? null : `set realmlist ${cur.host}`;
return (
{t("fr.title")}
{t("q.friends")}
{exposed.length > 0 && (
{t("fr.exposedTitle")}
{t("fr.exposedText", { what: exposed.map((e) => t(e.what === "database" ? "fr.what.database" : "fr.what.console")).join(t("fr.and")) })}
)}
{t("fr.status")}
{t("fr.st.running")}
{t("fr.st.open")}
{t("fr.st.firewall")}
e.what === "database" || e.what === "server console").every((e) => !e.reachable_from_network)}>
{t("fr.st.private")}
{MODES.map((m) => (
{t(m.title)} {cur.mode === m.id && {t("fr.inUse")}}
{t(m.text)}
{m.id === "lan" && (
<>
{t("fr.yourLan")} {st.lan_ip ?? t("fr.unknown")}
{st.lan_ip && cur.mode !== m.id &&
}
>
)}
{m.id === "direct" && (
{net && net.reachability === "cgnat" && (
{t("fr.cgnat")}
)}
{net?.public_ip && net.reachability !== "cgnat" && (
{t("fr.publicAddr")} {net.public_ip}{!net.router_found && {t("fr.noRouter")}}
)}
{net?.public_ip && net.reachability !== "cgnat" && cur.mode !== m.id &&
}
{net && !net.public_ip &&
{t("fr.noInternet")}
}
)}
{m.id === "private" && (
{!st.tailscale.installed && {t("fr.tsMissing")}}
{st.tailscale.installed && !st.tailscale.connected && {t("fr.tsOff")}}
{st.tailscale.connected && <>{t("fr.privateAddr")} {st.tailscale.ip}>}
)}
{m.id === "private" && st.tailscale.connected && st.tailscale.ip && cur.mode !== m.id &&
}
))}
{cur.mode !== "local" && (
)}
{connection && (
{t("fr.giveTitle")}
{t("fr.giveText")}
{connection}
)}
{note &&
{note}
}
{error &&
{error.human.code === "unknown" ? error.technical : human(error.human).message}
}
);
}