From 1ef930557ba4427ec3e94a75869b41ee19980526 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Thu, 16 Oct 2025 14:17:21 -0500 Subject: [PATCH] Fix chat textarea performance issues by memoizing and splitting into a new ChatInput component --- src/components/Shakespeare/ChatInput.tsx | 249 +++++++++++++++++++++++ src/components/Shakespeare/ChatPane.tsx | 248 ++++------------------ 2 files changed, 290 insertions(+), 207 deletions(-) create mode 100644 src/components/Shakespeare/ChatInput.tsx diff --git a/src/components/Shakespeare/ChatInput.tsx b/src/components/Shakespeare/ChatInput.tsx new file mode 100644 index 00000000..1eb31a70 --- /dev/null +++ b/src/components/Shakespeare/ChatInput.tsx @@ -0,0 +1,249 @@ +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(async () => { + if ((!input.trim() && attachedFiles.length === 0) || isLoading) return; + if (!isConfigured || !providerModel.trim()) return; + + const currentInput = input; + const currentFiles = [...attachedFiles]; + + setInput(''); + setAttachedFiles([]); + + await 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 */} +
+