import React from "react"; import { Check, ChevronsUpDown, Pencil } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover"; import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList } from "@/components/ui/command"; import { cn } from "@/lib/utils"; import { DatasetItem } from "@/lib/replayApi"; interface Props { datasets: DatasetItem[]; loading: boolean; value: string | null; onChange: (repoId: string | null) => void; } const REPO_ID_RE = /^[\w.\-]+\/[\w.\-]+$/; const DatasetCombobox: React.FC = ({ datasets, loading, value, onChange }) => { const [open, setOpen] = React.useState(false); const [customMode, setCustomMode] = React.useState(false); const [customValue, setCustomValue] = React.useState(""); const submitCustom = () => { const v = customValue.trim(); if (REPO_ID_RE.test(v)) { onChange(v); setCustomMode(false); } }; const localDatasets = datasets.filter((d) => d.source === "local" || d.source === "both"); const hubDatasets = datasets.filter((d) => d.source === "hub"); const renderItem = (d: DatasetItem) => ( { onChange(d.repo_id); setOpen(false); }} className="text-white aria-selected:bg-gray-700" > {d.repo_id} {d.source === "both" && on Hub} {d.private && private} ); if (customMode) { return (
setCustomValue(e.target.value)} onKeyDown={(e) => { if (e.key === "Enter") submitCustom(); }} placeholder="org/dataset-name" className="bg-gray-800 border-gray-600 text-white" />
); } return ( {loading ? "Loading…" : "No datasets."} {localDatasets.length > 0 && ( {localDatasets.map(renderItem)} )} {hubDatasets.length > 0 && ( {hubDatasets.map(renderItem)} )} { setCustomMode(true); setOpen(false); }} className="text-purple-300 aria-selected:bg-gray-700" > Use custom repo ID… ); }; export default DatasetCombobox;