blob: 371880555367e701a9420f3f66cecf54fd2d9e2f [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
5export function Agent({ agent }: { agent: AgentAccess }): React.ReactNode {
gio785c9882025-07-07 16:40:25 +00006 return <AgentIframe agent={agent} />;
7}
8
9export function AgentIframe({ agent }: { agent: AgentAccess }): React.ReactNode {
10 const projectId = useProjectId();
11 const [ok, setOk] = useState<boolean>(false);
12 useEffect(() => {
13 let timeout: NodeJS.Timeout | null = null;
14 const check = async () => {
15 const resp = await fetch(`/api/project/${projectId}/agent/${agent.agentName}/status`);
16 if (resp.ok) {
17 const status = (await resp.json()).status;
18 if (200 <= status && status < 300) {
19 setOk(true);
20 return;
21 }
22 }
23 timeout = setTimeout(check, 500);
24 };
25 check();
26 return () => {
27 if (timeout != null) {
28 clearTimeout(timeout);
29 }
30 };
31 }, [agent.agentName, agent.address, setOk, projectId]);
32 if (!ok) {
33 return <div>Agent {agent.agentName} is loading...</div>;
34 }
gio678746b2025-07-06 14:45:27 +000035 return <iframe key={agent.name} src={`${agent.address}?m`} title={agent.agentName} className="w-full h-full" />;
36}