import { useState } from "react"; import { Button, Card, Center, Group, Stack, Text, TextInput, Title, Alert, ThemeIcon, } from "@mantine/core"; import { IconBolt, IconPlugConnected, IconAlertTriangle } from "@tabler/icons-react"; import { GatewayClient } from "../api"; import type { Peer } from "../types"; const LS_GATEWAY = "unibus.gateway"; const LS_PEER = "unibus.peer"; interface Props { onConnect: (client: GatewayClient, peer: Peer) => void; } // ConnectScreen asks for the gateway URL and the identity (peer name) to connect // as. Both persist in localStorage so a reload reconnects with one click. The // gateway hosts the real Go bus peer; the browser only drives it. export function ConnectScreen({ onConnect }: Props) { const [gateway, setGateway] = useState( () => localStorage.getItem(LS_GATEWAY) ?? "http://localhost:7700", ); const [name, setName] = useState(() => localStorage.getItem(LS_PEER) ?? ""); const [busy, setBusy] = useState(false); const [error, setError] = useState(null); const connect = async () => { const trimmed = name.trim(); if (!trimmed) { setError("Elige un nombre de identidad"); return; } setBusy(true); setError(null); try { const client = new GatewayClient(gateway.trim()); const peer = await client.connect(trimmed); localStorage.setItem(LS_GATEWAY, client.baseURL); localStorage.setItem(LS_PEER, trimmed); onConnect(client, peer); } catch (e) { setError(e instanceof Error ? e.message : String(e)); } finally { setBusy(false); } }; return (
unibus chat cifrado extremo a extremo sobre NATS
setGateway(e.currentTarget.value)} disabled={busy} /> setName(e.currentTarget.value)} onKeyDown={(e) => e.key === "Enter" && connect()} disabled={busy} data-autofocus /> {error && ( } title="No se pudo conectar" > {error} )}
); }