import { useEffect, useMemo, useState } from "react"; import { ChevronsUpDown, Loader2 } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import DatasetPicker from "@/components/landing/DatasetPicker"; import { parseEpisodeList } from "@/lib/datasetApi"; import { DatasetItem, MoveDestination } from "@/lib/replayApi"; interface MoveEpisodesDialogProps { datasets: DatasetItem[]; repoId: string; totalEpisodes: number; currentEpisode: number | null; open: boolean; onOpenChange: (open: boolean) => void; onMove: (episodeIndices: number[], destination: MoveDestination) => Promise; } export default function MoveEpisodesDialog({ datasets, repoId, totalEpisodes, currentEpisode, open, onOpenChange, onMove, }: MoveEpisodesDialogProps) { const [episodesText, setEpisodesText] = useState(""); const [destination, setDestination] = useState(null); const [moving, setMoving] = useState(false); const [error, setError] = useState(null); useEffect(() => { if (open) { setEpisodesText(currentEpisode === null ? "" : String(currentEpisode)); setDestination(null); setMoving(false); setError(null); } }, [open, currentEpisode]); const candidates = useMemo(() => datasets.filter((d) => d.repo_id !== repoId), [datasets, repoId]); const episodes = parseEpisodeList(episodesText, totalEpisodes); const movesEverything = episodes !== null && episodes.length >= totalEpisodes; const canMove = episodes !== null && !movesEverything && destination !== null && !moving; const pickDestination = (next: MoveDestination) => { setError(null); setDestination(next); }; const handleOpenChange = (nextOpen: boolean) => { if (moving) return; onOpenChange(nextOpen); }; const handleMove = async () => { if (!canMove || episodes === null || destination === null) return; setMoving(true); try { await onMove(episodes, destination); onOpenChange(false); } catch (caughtError) { setError(caughtError instanceof Error ? caughtError.message : "Could not move the episodes."); } finally { setMoving(false); } }; return ( Move episodes to another dataset The episodes leave {repoId} and go to a new or an existing local dataset. The episodes left in {repoId} are renumbered.
{ setError(null); setEpisodesText(event.target.value); }} placeholder="3, 7, 10-12" disabled={moving} className="border-gray-700 bg-gray-800 text-white" /> {episodesText.trim() && episodes === null && (

Use episode numbers from 0 to {totalEpisodes - 1}, e.g. 3, 7, 10-12.

)} {movesEverything && (

At least one episode must stay in the dataset.

)}
pickDestination({ existingRepoId: item.repo_id })} onCreateNew={(name) => pickDestination({ newName: name })} > {destination !== null && (

{"newName" in destination ? ( <> Saved locally as local/{destination.newName}. ) : ( <> Merged into {destination.existingRepoId} after its own episodes. It must have the same fps, features and robot type. )}

)}
{error &&

{error}

}
); }