import { useState, useEffect, useRef } from "react"; import { ACPClient, DisconnectRequestedError } from "../acp/client"; import type { ConnectionState } from "../acp/types"; import { ACPMain } from "../../components/ACPMain"; interface ACPDirectViewProps { url: string; token: string; onBack: () => void; } export function ACPDirectView({ url, token, onBack }: ACPDirectViewProps) { const [client, setClient] = useState(null); const [connectionState, setConnectionState] = useState("disconnected"); const [error, setError] = useState(null); const clientRef = useRef(null); useEffect(() => { const acpClient = new ACPClient({ proxyUrl: url, token }); acpClient.setConnectionStateHandler((state, err) => { setConnectionState(state); setError(err || null); }); clientRef.current = acpClient; setClient(acpClient); acpClient.connect().catch((e) => { if (e instanceof DisconnectRequestedError) return; setError((e as Error).message); setConnectionState("error"); }); return () => { acpClient.disconnect(); clientRef.current = null; setClient(null); setConnectionState("disconnected"); }; }, [url, token]); return (
{error && connectionState === "error" && (
{error}
)} {connectionState === "connecting" && (

Connecting to ACP agent...

)} {connectionState === "error" && !client && (

Connection Failed

{error}

)} {client && connectionState === "connected" && ( )}
); }