| gio | 785c988 | 2025-07-07 16:40:25 +0000 | [diff] [blame] | 1 | import React, { useEffect, useState } from "react"; |
| gio | 678746b | 2025-07-06 14:45:27 +0000 | [diff] [blame] | 2 | import { AgentAccess } from "config"; |
| gio | 785c988 | 2025-07-07 16:40:25 +0000 | [diff] [blame] | 3 | import { useProjectId } from "./lib/state"; |
| gio | 678746b | 2025-07-06 14:45:27 +0000 | [diff] [blame] | 4 | |
| gio | 007c857 | 2025-07-08 04:27:35 +0000 | [diff] [blame^] | 5 | export function Agent({ agent, compact }: { agent: AgentAccess; compact?: boolean }): React.ReactNode { |
| gio | 785c988 | 2025-07-07 16:40:25 +0000 | [diff] [blame] | 6 | 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 | } |
| gio | 007c857 | 2025-07-08 04:27:35 +0000 | [diff] [blame^] | 31 | const address = `${agent.address}${compact ? "/m" : ""}`; |
| 32 | return <iframe key={agent.name} src={address} title={agent.agentName} className="w-full h-full" />; |
| gio | 678746b | 2025-07-06 14:45:27 +0000 | [diff] [blame] | 33 | } |