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 { PairCodeResponse } from "@/features/status/queries"; 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 (
{label}
{value}
); } 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; /** Fresh pairing code minted by the daemon (`POST /api/v1/pair`). */ pairCode: PairCodeResponse | undefined; pairLoading: boolean; pairError: string | null; onRetryPair: () => void; } /** Minutes left before the minted code expires (never negative). */ export function pairMinutesLeft(expiresAt: number | null | undefined, now = Date.now()): number | null { if (typeof expiresAt !== "number" || !Number.isFinite(expiresAt)) return null; return Math.max(0, Math.ceil((expiresAt - now) / 60_000)); } /** * The live pairing code block: code, expiry, pairing URL and the QR drawn from * that URL. Failures are retried in place instead of showing `—`. */ function PairingBlock({ pairCode, loading, error, fallbackUrl, onRetry, }: { pairCode: PairCodeResponse | undefined; loading: boolean; error: string | null; fallbackUrl: string; onRetry: () => void; }) { const { t } = useTranslation(); const code = String(pairCode?.code ?? "").trim(); const url = String(pairCode?.url ?? "").trim() || (code ? `${fallbackUrl}/#pair=${code}` : ""); const minutes = pairMinutesLeft(pairCode?.expiresAt); if (error) return ; if (loading) return ; if (!code) return ; return (
{code}
{minutes != null ?
{t("status.securityExpiry", { n: minutes })}
: null} {url ? (
{url}
) : null}
{t("status.pairHint")}
); } /** Card 7 — addresses, mDNS, pairing code/QR, scopes, last connection, actions. */ export function ConnectionCard({ server, deviceName, scopes, connectedAt, loading, error, onRetry, onDisconnect, pairCode, pairLoading, pairError, onRetryPair, }: ConnectionCardProps) { const { t } = useTranslation(); const [confirmOpen, setConfirmOpen] = useState(false); return ( {error ? ( ) : loading ? ( ) : ( <>
{server.mdns} {server.endpoint} {deviceName || "—"} {scopes.length ? ( {scopes.map((scope) => ( {scope} ))} ) : ( — )}
{t("status.securityLocal")}
{t("status.securityToken")}
{t("connect.reconnect")}
)}
); }