import { useState, useCallback, memo, useRef, useEffect } from 'react'; import { useTranslation } from 'react-i18next'; import { Button } from '@/components/ui/button'; import { Textarea } from '@/components/ui/textarea'; import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from '@/components/ui/tooltip'; import { CircularProgress } from '@/components/ui/circular-progress'; import { FileAttachment } from '@/components/ui/file-attachment'; import { Command, CommandGroup, CommandItem, CommandList } from '@/components/ui/command'; import { Square, ArrowUp } from 'lucide-react'; import { ModelSelector } from '@/components/ModelSelector'; export interface SlashCommand { name: string; description: string; action: () => void; } interface ChatInputProps { isLoading: boolean; isConfigured: boolean; isLoadingSettings: boolean; providerModel: string; onProviderModelChange: (model: string) => void; onSend: (input: string, files: File[]) => void; onStop: () => void; onFocus: () => void; onFirstInteraction: () => void; isModelSelectorOpen: boolean; onModelSelectorOpenChange: (open: boolean) => void; contextUsagePercentage: number; currentModelContextLength?: number; lastInputTokens?: number; totalCost: number; isDragOver: boolean; onDragEnter: (e: React.DragEvent) => void; onDragOver: (e: React.DragEvent) => void; onDragLeave: (e: React.DragEvent) => void; onDrop: (e: React.DragEvent) => void; slashCommands?: SlashCommand[]; } export const ChatInput = memo(function ChatInput({ isLoading, isConfigured, isLoadingSettings, providerModel, onProviderModelChange, onSend, onStop, onFocus, onFirstInteraction, isModelSelectorOpen, onModelSelectorOpenChange, contextUsagePercentage, currentModelContextLength, lastInputTokens, totalCost, isDragOver, onDragEnter, onDragOver, onDragLeave, onDrop, slashCommands = [], }: ChatInputProps) { const { t } = useTranslation(); const [input, setInput] = useState(''); const [attachedFiles, setAttachedFiles] = useState([]); const [showSlashCommands, setShowSlashCommands] = useState(false); const [filteredCommands, setFilteredCommands] = useState([]); const [selectedCommandIndex, setSelectedCommandIndex] = useState(0); const textareaRef = useRef(null); const handleFileSelect = useCallback((file: File) => { setAttachedFiles(prev => [...prev, file]); }, []); const handleFileRemove = useCallback((fileToRemove: File) => { setAttachedFiles(prev => prev.filter(file => file !== fileToRemove)); }, []); const handleSend = useCallback(() => { if ((!input.trim() && attachedFiles.length === 0) || isLoading) return; if (!isConfigured || !providerModel.trim()) return; const currentInput = input; const currentFiles = [...attachedFiles]; setInput(''); setAttachedFiles([]); onSend(currentInput, currentFiles); }, [input, attachedFiles, isLoading, isConfigured, providerModel, onSend]); const executeSlashCommand = useCallback((command: SlashCommand) => { setInput(''); setShowSlashCommands(false); setSelectedCommandIndex(0); command.action(); }, []); const handleKeyDown = useCallback((e: React.KeyboardEvent) => { // Handle arrow key navigation when slash commands are showing if (showSlashCommands && filteredCommands.length > 0) { if (e.key === 'ArrowDown') { e.preventDefault(); setSelectedCommandIndex(prev => prev < filteredCommands.length - 1 ? prev + 1 : 0 ); return; } if (e.key === 'ArrowUp') { e.preventDefault(); setSelectedCommandIndex(prev => prev > 0 ? prev - 1 : filteredCommands.length - 1 ); return; } // If slash commands are showing and user presses Enter or Tab, select the highlighted command if ((e.key === 'Enter' && !e.shiftKey) || e.key === 'Tab') { e.preventDefault(); executeSlashCommand(filteredCommands[selectedCommandIndex]); return; } // If slash commands are showing and user presses Escape, close the popover if (e.key === 'Escape') { e.preventDefault(); setShowSlashCommands(false); setSelectedCommandIndex(0); return; } } if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); handleSend(); } }, [handleSend, showSlashCommands, filteredCommands, selectedCommandIndex, executeSlashCommand]); const handleInputChange = useCallback((e: React.ChangeEvent) => { const value = e.target.value; setInput(value); // Check if slash command autocomplete should be shown if (value.startsWith('/') && !value.includes(' ') && slashCommands.length > 0) { const query = value.slice(1).toLowerCase(); const filtered = slashCommands.filter(cmd => cmd.name.toLowerCase().includes(query) ); setFilteredCommands(filtered); setShowSlashCommands(filtered.length > 0); setSelectedCommandIndex(0); // Reset selection when filtering } else { setShowSlashCommands(false); setSelectedCommandIndex(0); } }, [slashCommands]); const handleDrop = useCallback((e: React.DragEvent) => { e.preventDefault(); e.stopPropagation(); // Call parent's drop handler for visual state management onDrop(e); // Extract files from the drop event const files = Array.from(e.dataTransfer.files); if (files.length === 0) return; // Add all files without validation setAttachedFiles(prev => [...prev, ...files]); }, [onDrop]); // Close popover when clicking outside useEffect(() => { if (!showSlashCommands) return; const handleClickOutside = (e: MouseEvent) => { // Check if click is outside the popover and textarea const target = e.target as Node; if (textareaRef.current && !textareaRef.current.contains(target)) { setShowSlashCommands(false); } }; // Use a slight delay to avoid closing immediately on focus const timeoutId = setTimeout(() => { document.addEventListener('click', handleClickOutside); }, 100); return () => { clearTimeout(timeoutId); document.removeEventListener('click', handleClickOutside); }; }, [showSlashCommands]); return (
{/* Chat Input Container */}