"use client" import { useEffect, useState } from "react" import { wakeBackend } from "../lib/api" export default function LoadingScreen({ onReady }: { onReady: () => void }) { const [status, setStatus] = useState<"waking" | "connecting" | "ready" | "error">("waking") const [dots, setDots] = useState("") useEffect(() => { const interval = setInterval(() => { setDots(d => (d.length >= 3 ? "" : d + ".")) }, 500) return () => clearInterval(interval) }, []) useEffect(() => { let timeoutId: NodeJS.Timeout let isActive = true async function poll() { try { const data = await wakeBackend() if (data.warm && data.groq_available) { setStatus("ready") setTimeout(() => { if (isActive) onReady() }, 1000) } else if (data.warm) { setStatus("connecting") timeoutId = setTimeout(poll, 2000) } } catch (err) { timeoutId = setTimeout(poll, 2000) } } poll() const failTimeout = setTimeout(() => { if (status !== "ready") setStatus("error") }, 60000) return () => { isActive = false clearTimeout(timeoutId) clearTimeout(failTimeout) } }, [onReady, status]) return (
🌋
{status === "waking" && `Waking up the arena${dots}`} {status === "connecting" && `Connecting to Groq${dots}`} {status === "ready" && "Ready for chaos"} {status === "error" && "Failed to start. Check your connection."}
{status === "error" && ( )}
) }