import { useState, useRef, useEffect } from 'react'; import { Terminal, Image, Play, FileText, ArrowUp } from 'lucide-react'; const SLASH_COMMANDS = [ { cmd: '/run', desc: '执行 pipeline 阶段', icon: Play }, { cmd: '/status', desc: '查看管线进度', icon: Terminal }, { cmd: '/images', desc: '生成图片', icon: Image }, { cmd: '/list', desc: '列出可用账号', icon: FileText }, { cmd: '/help', desc: '显示帮助', icon: Terminal }, ]; export function ChatInput({ onSend, disabled }: { onSend: (content: string) => void; disabled?: boolean }) { const [input, setInput] = useState(''); const [showCmds, setShowCmds] = useState(false); const [cmdIdx, setCmdIdx] = useState(0); const ref = useRef(null); const matchingCmds = input.startsWith('/') ? SLASH_COMMANDS.filter((c) => c.cmd.startsWith(input.split(' ')[0])) : []; // Auto-show/hide slash command menu useEffect(() => { if (input.startsWith('/') && matchingCmds.length > 0) { setShowCmds(true); setCmdIdx(0); } else { setShowCmds(false); } }, [input, matchingCmds.length]); const handleSend = () => { if (!input.trim() || disabled) return; onSend(input.trim()); setInput(''); setShowCmds(false); ref.current?.focus(); }; const handleKeyDown = (e: React.KeyboardEvent) => { if (showCmds && matchingCmds.length > 0) { if (e.key === 'Tab') { e.preventDefault(); setInput(matchingCmds[cmdIdx % matchingCmds.length].cmd + ' '); setShowCmds(false); return; } if (e.key === 'ArrowDown') { e.preventDefault(); setCmdIdx((i) => (i + 1) % matchingCmds.length); return; } if (e.key === 'ArrowUp') { e.preventDefault(); setCmdIdx((i) => (i - 1 + matchingCmds.length) % matchingCmds.length); return; } } if (e.key === 'Escape') { setShowCmds(false); return; } if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); handleSend(); } }; const canSend = input.trim().length > 0 && !disabled; return (
{/* Slash command menu */} {showCmds && matchingCmds.length > 0 && (
{matchingCmds.map((c, i) => { const Icon = c.icon; return ( ); })}
)} {/* Input bar */}