import { useState, useCallback, memo } 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 { Square, ArrowUp } from 'lucide-react'; import { ModelSelector } from '@/components/ModelSelector'; interface ChatInputProps { isLoading: boolean; isConfigured: 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; } export const ChatInput = memo(function ChatInput({ isLoading, isConfigured, providerModel, onProviderModelChange, onSend, onStop, onFocus, onFirstInteraction, isModelSelectorOpen, onModelSelectorOpenChange, contextUsagePercentage, currentModelContextLength, lastInputTokens, totalCost, isDragOver, onDragEnter, onDragOver, onDragLeave, onDrop, }: ChatInputProps) { const { t } = useTranslation(); const [input, setInput] = useState(''); const [attachedFiles, setAttachedFiles] = useState([]); 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 handleKeyDown = useCallback((e: React.KeyboardEvent) => { if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); handleSend(); } }, [handleSend]); const handlePaste = useCallback(async (e: React.ClipboardEvent) => { const items = e.clipboardData?.items; if (!items) return; for (let i = 0; i < items.length; i++) { const item = items[i]; // Check if the item is an image if (item.type.startsWith('image/')) { e.preventDefault(); // Prevent default paste behavior for images const file = item.getAsFile(); if (file) { // Generate a filename with timestamp const timestamp = new Date().toISOString().replace(/[:.]/g, '-'); const extension = file.type.split('/')[1] || 'png'; const filename = `pasted-image-${timestamp}.${extension}`; // Create a new File object with the generated name const namedFile = new File([file], filename, { type: file.type }); // Add to attached files setAttachedFiles(prev => [...prev, namedFile]); } break; // Only handle the first image found } } }, []); const handleInputChange = useCallback((e: React.ChangeEvent) => { setInput(e.target.value); }, []); return (
{/* Chat Input Container */}