import { useEffect, useState } from "react"; import * as api from "../../services/api"; interface Props { instanceId?: string; emptyMessage?: string; } export default function InstanceLogsPanel({ instanceId, emptyMessage = "No instance logs available.", }: Props) { const [logs, setLogs] = useState(""); const [loading, setLoading] = useState(false); useEffect(() => { if (!instanceId) { setLogs(""); setLoading(false); return; } let cancelled = false; setLoading(true); api .fetchInstanceLogs(instanceId) .then((nextLogs) => { if (!cancelled) { setLogs(nextLogs); } }) .catch((error) => { console.error("Failed to load instance logs", error); if (!cancelled) { setLogs(""); } }) .finally(() => { if (!cancelled) { setLoading(false); } }); return () => { cancelled = true; }; }, [instanceId]); useEffect(() => { if (!instanceId) { return; } return api.subscribeToInstanceLogs(instanceId, { onLogs: (nextLogs) => setLogs(nextLogs), }); }, [instanceId]); return (
{loading && !logs ? (
Loading logs...
) : logs ? (
          {logs}
        
) : (
{emptyMessage}
)}
); }