blob: 974a3f3125847c7b0b2f77e7e816814c0ce38398 [file] [log] [blame]
gio785c9882025-07-07 16:40:25 +00001import React, { useEffect, useState } from "react";
gio678746b2025-07-06 14:45:27 +00002import { AgentAccess } from "config";
gio785c9882025-07-07 16:40:25 +00003import { useProjectId } from "./lib/state";
gio678746b2025-07-06 14:45:27 +00004
gio007c8572025-07-08 04:27:35 +00005export function Agent({ agent, compact }: { agent: AgentAccess; compact?: boolean }): React.ReactNode {
gio785c9882025-07-07 16:40:25 +00006 const projectId = useProjectId();
7 const [ok, setOk] = useState<boolean>(false);
8 useEffect(() => {
9 let timeout: NodeJS.Timeout | null = null;
10 const check = async () => {
11 const resp = await fetch(`/api/project/${projectId}/agent/${agent.agentName}/status`);
12 if (resp.ok) {
13 const status = (await resp.json()).status;
14 if (200 <= status && status < 300) {
15 setOk(true);
16 return;
17 }
18 }
19 timeout = setTimeout(check, 500);
20 };
21 check();
22 return () => {
23 if (timeout != null) {
24 clearTimeout(timeout);
25 }
26 };
27 }, [agent.agentName, agent.address, setOk, projectId]);
28 if (!ok) {
29 return <div>Agent {agent.agentName} is loading...</div>;
30 }
gio007c8572025-07-08 04:27:35 +000031 const address = `${agent.address}${compact ? "/m" : ""}`;
32 return <iframe key={agent.name} src={address} title={agent.agentName} className="w-full h-full" />;
gio678746b2025-07-06 14:45:27 +000033}