import React, { useState } from "react"; import { Plus, Check, ChevronsUpDown } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Popover, PopoverContent, PopoverTrigger, } from "@/components/ui/popover"; import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, } from "@/components/ui/command"; import { cn } from "@/lib/utils"; interface RobotSelectorProps { selectedName: string | null; availableNames: string[]; onSelect: (name: string) => void; onCreateNew: (name: string) => Promise; isLoading: boolean; } const RobotSelector: React.FC = ({ selectedName, availableNames, onSelect, onCreateNew, isLoading, }) => { const [open, setOpen] = useState(false); const [query, setQuery] = useState(""); const trimmed = query.trim(); const matchesExisting = availableNames.some( (n) => n.toLowerCase() === trimmed.toLowerCase() ); const canCreate = trimmed.length > 0 && !matchesExisting; const createDisabled = !canCreate; const createLabel = matchesExisting ? "Already exists" : trimmed === "" ? "Create new robot…" : `Create "${trimmed}"`; const reset = () => { setQuery(""); setOpen(false); }; const handlePickExisting = (name: string) => { onSelect(name); reset(); }; const handleCreate = async () => { if (!canCreate) return; const ok = await onCreateNew(trimmed); if (ok) reset(); }; return ( { if (e.key === "Enter" && canCreate) { e.preventDefault(); handleCreate(); } }} className="text-white" /> {availableNames.length === 0 && ( No robots yet. Type a name to create one. )} {availableNames.length > 0 && ( {availableNames.map((name) => ( handlePickExisting(name)} className="text-white aria-selected:bg-gray-700" > {name} ))} )} ); }; export default RobotSelector;