import { useEffect, useRef, useState } from "react";
import { useTranslation } from "react-i18next";
import { Icon } from "@/components/icons";
import { ConfirmDialog } from "@/components/shared/confirm-dialog";
import { RelativeTime } from "@/components/shared/relative-time";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import { KV, KVList } from "@/components/ui/kv";
import { toast } from "@/components/ui/toast";
import { drawQr } from "@/features/status/qr";
import type { ServerIdentity } from "@/features/status/server";
import { ErrorBlock, LoadingBlock, StatusCard } from "./card-parts";
function scopeTone(scope: string): "default" | "info" | "ok" {
if (scope === "read") return "default";
if (scope === "steer") return "info";
if (scope === "apply") return "ok";
return "default";
}
function CopyButton({ value, label }: { value: string; label: string }) {
const { t } = useTranslation();
async function copy() {
try {
if (!navigator.clipboard?.writeText) throw new Error("clipboard unavailable");
await navigator.clipboard.writeText(value);
toast.success(t("common.copied", { label }));
} catch {
toast.error(t("common.copyFailed"));
}
}
return (
);
}
function AddressBlock({ label, value }: { label: string; value: string }) {
return (
);
}
function QrCanvas({ seed, label }: { seed: string; label: string }) {
const ref = useRef(null);
useEffect(() => {
const canvas = ref.current;
if (!canvas) return;
const draw = () => drawQr(canvas, seed);
draw();
if (typeof MutationObserver === "undefined") return;
const observer = new MutationObserver(draw);
observer.observe(document.documentElement, { attributes: true, attributeFilter: ["data-theme"] });
return () => observer.disconnect();
}, [seed]);
return ;
}
export interface ConnectionCardProps {
server: ServerIdentity;
deviceName: string;
scopes: string[];
connectedAt: number | null;
loading: boolean;
error: string | null;
onRetry: () => void;
onDisconnect: () => void;
}
/** Card 7 — addresses, mDNS, pairing code/QR, scopes, last connection, actions. */
export function ConnectionCard({
server,
deviceName,
scopes,
connectedAt,
loading,
error,
onRetry,
onDisconnect,
}: ConnectionCardProps) {
const { t } = useTranslation();
const [confirmOpen, setConfirmOpen] = useState(false);
const qrSeed = `${server.networkAddress}/#pair=${server.pairingCode}`;
return (
{error ? (
) : loading ? (
) : (
<>
{server.mdns}
{server.endpoint}
{deviceName || "—"}
{scopes.length ? (
{scopes.map((scope) => (
{scope}
))}
) : (
—
)}
{server.pairingCode}
{t("status.pairHint")}
{t("status.securityLocal")}
{t("status.securityToken")}
>
)}
);
}