import React, { useEffect, useRef, useState } from "react"; import { Bot, Loader2, Square } from "lucide-react"; import { Button } from "@/components/ui/button"; import { useApi } from "@/contexts/ApiContext"; import { useToast } from "@/hooks/use-toast"; import { useRobots } from "@/hooks/useRobots"; import { apiRequest } from "@/lib/apiClient"; interface ReplayResponse { success: boolean; message: string; } interface ReplayStatus { replay_active: boolean; error: string | null; } interface ReplayButtonProps { repoId: string; episodeIndex: number; /** Called once the arm starts moving, so the footage can run alongside it. */ onStart: () => void; /** Called once the arm is released, whether the replay ended, failed or was stopped. */ onEnd: () => void; } /** * Plays the episode's recorded actions on the selected follower arm. Only * shown when that arm is ready to move. */ const ReplayButton: React.FC = ({ repoId, episodeIndex, onStart, onEnd }) => { const { baseUrl, fetchWithHeaders } = useApi(); const { selectedRecord: robot } = useRobots(); const { toast } = useToast(); const [phase, setPhase] = useState<"idle" | "starting" | "playing">("idle"); // Paging to another episode while the arm connects must not drag the // footage back to the one being replayed. const episodeRef = useRef(episodeIndex); episodeRef.current = episodeIndex; // A replay keeps running when this button unmounts (another dataset, another // page), so pick it back up to keep it stoppable. useEffect(() => { apiRequest(baseUrl, fetchWithHeaders, "/replay-status") .then((status) => { if (status.replay_active) setPhase("playing"); }) .catch(() => undefined); }, [baseUrl, fetchWithHeaders]); // Poll only while the arm moves, to notice when the episode ends or fails. useEffect(() => { if (phase !== "playing") return; const timer = setInterval(async () => { try { const status = await apiRequest(baseUrl, fetchWithHeaders, "/replay-status"); if (status.replay_active) return; setPhase("idle"); onEnd(); if (status.error) { toast({ title: "Replay failed", description: status.error, variant: "destructive" }); } } catch { // Keep the Stop button: the next poll retries. } }, 500); return () => clearInterval(timer); }, [phase, baseUrl, fetchWithHeaders, toast, onEnd]); // Back to Play only once the status poll sees the arm released. const stop = async () => { try { const res = await apiRequest(baseUrl, fetchWithHeaders, "/stop-replay", { method: "POST", action: "Stop", }); if (!res.success) throw new Error(res.message); } catch (e) { toast({ title: "Could not stop the robot", description: e instanceof Error ? e.message : String(e), variant: "destructive", }); } }; // A running replay stays stoppable even once the robot is no longer selected. if (phase === "playing") { return ( ); } if (!robot?.is_clean) return null; const start = async () => { setPhase("starting"); try { const res = await apiRequest(baseUrl, fetchWithHeaders, "/start-replay", { method: "POST", body: { follower_port: robot.follower_port, follower_config: robot.follower_config, dataset_repo_id: repoId, episode_index: episodeIndex, }, action: "Replay", }); if (!res.success) throw new Error(res.message); setPhase("playing"); if (episodeRef.current === episodeIndex) onStart(); } catch (e) { setPhase("idle"); toast({ title: "Could not replay this episode", description: e instanceof Error ? e.message : String(e), variant: "destructive", }); } }; return ( ); }; export default ReplayButton;