import React, { useState, useEffect } from 'react' import { useNavigate } from 'react-router-dom' import InstancePanel from '../components/InstancePanel' import NewRunModal from '../components/NewRunModal' function Home() { const [instances, setInstances] = useState([]) const [loading, setLoading] = useState(true) const [showModal, setShowModal] = useState(false) const navigate = useNavigate() const fetchInstances = async () => { try { const response = await fetch('/api/instances') if (response.ok) { const data = await response.json() setInstances(data) } } catch (error) { console.error('Failed to fetch instances:', error) } finally { setLoading(false) } } useEffect(() => { fetchInstances() const interval = setInterval(fetchInstances, 5000) // Poll every 5 seconds return () => clearInterval(interval) }, []) const handleInstanceClick = (id) => { navigate(`/instance/${id}`) } const handleKill = async (id) => { try { const response = await fetch(`/api/instances/${id}/kill`, { method: 'POST', }) if (response.ok) { fetchInstances() } } catch (error) { console.error('Failed to kill instance:', error) } } const handleRestart = async (id) => { try { const response = await fetch(`/api/instances/${id}/restart`, { method: 'POST', }) if (response.ok) { fetchInstances() } } catch (error) { console.error('Failed to restart instance:', error) } } const handleLaunch = async (request) => { try { const response = await fetch('/api/instances/launch', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(request), }) if (response.ok) { setShowModal(false) setTimeout(fetchInstances, 2000) // Refresh after 2 seconds } } catch (error) { console.error('Failed to launch instance:', error) } } if (loading) { return (
Loading instances...
) } return (

Running Instances ({instances.length})

{instances.length === 0 ? (

No running instances. Click "New Run" to start a g3 instance.

) : (
{instances.map((instance) => ( handleInstanceClick(instance.instance.id)} onKill={() => handleKill(instance.instance.id)} onRestart={() => handleRestart(instance.instance.id)} /> ))}
)} {showModal && ( setShowModal(false)} onLaunch={handleLaunch} /> )}
) } export default Home