import { useQueryClient } from "@tanstack/react-query"; import { useCallback, useEffect, useRef, useState } from "react"; import { api } from "../lib/api"; import { getAuthToken } from "../lib/auth-client"; import { MachineRuntimeBadges } from "./MachineRuntimes"; import { Button } from "./ui/button"; interface AddMachineStepsProps { apiKey: string; apiKeyId: string; onDone: () => void; onConnected?: (machine: any) => void; } export function AddMachineSteps({ apiKey, apiKeyId, onDone, onConnected }: AddMachineStepsProps) { const [connected, setConnected] = useState(false); const [connectedMachine, setConnectedMachine] = useState(null); const pollRef = useRef | null>(null); const queryClient = useQueryClient(); const apiUrl = window.location.origin; const stopPolling = useCallback(() => { if (pollRef.current) { clearInterval(pollRef.current); pollRef.current = null; } }, []); useEffect(() => { pollRef.current = setInterval(async () => { const res = await fetch(`/api/auth/api-key/get?id=${apiKeyId}`, { headers: { Authorization: `Bearer ${getAuthToken()}` }, }); if (!res.ok) return; const keyData = (await res.json()) as any; const machineId = keyData?.metadata?.machineId; if (!machineId) return; const m = await api.machines.get(machineId); if (m && m.status === "online") { queryClient.invalidateQueries({ queryKey: ["machines"] }); setConnected(true); setConnectedMachine(m); stopPolling(); onConnected?.(m); } }, 3000); return stopPolling; }, [apiKeyId, stopPolling, onConnected, queryClient]); if (connected && connectedMachine) { return (

Machine connected!

Name {connectedMachine.name}
Status
Online
{connectedMachine.os && (
OS {connectedMachine.os}
)} {connectedMachine.runtimes && (
Runtimes
)}
); } return (

Run this command in your terminal:

terminal
$ npx vtit-agent-coding start \
--api-url {apiUrl} \
--api-key {apiKey}
Waiting for connection...
); }