/** * Chat Input Component * Textarea with send button and universal file upload support. * Enter to send, Shift+Enter for new line. * Supports: native file picker, clipboard paste, drag & drop. * Files are staged to disk via IPC — only lightweight path references * are sent with the message (no base64 over WebSocket). */ import { useState, useRef, useEffect, useCallback } from 'react'; import { Send, Square, X, Paperclip, FileText, Film, Music, FileArchive, File, Loader2 } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { Textarea } from '@/components/ui/textarea'; // ── Types ──────────────────────────────────────────────────────── export interface FileAttachment { id: string; fileName: string; mimeType: string; fileSize: number; stagedPath: string; // disk path for gateway preview: string | null; // data URL for images, null for others status: 'staging' | 'ready' | 'error'; error?: string; } interface ChatInputProps { onSend: (text: string, attachments?: FileAttachment[]) => void; onStop?: () => void; disabled?: boolean; sending?: boolean; } // ── Helpers ────────────────────────────────────────────────────── function formatFileSize(bytes: number): string { if (bytes < 1024) return `${bytes} B`; if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`; if (bytes < 1024 * 1024 * 1024) return `${(bytes / (1024 * 1024)).toFixed(2)} MB`; return `${(bytes / (1024 * 1024 * 1024)).toFixed(2)} GB`; } function FileIcon({ mimeType, className }: { mimeType: string; className?: string }) { if (mimeType.startsWith('video/')) return ; if (mimeType.startsWith('audio/')) return ; if (mimeType.startsWith('text/') || mimeType === 'application/json' || mimeType === 'application/xml') return ; if (mimeType.includes('zip') || mimeType.includes('compressed') || mimeType.includes('archive') || mimeType.includes('tar') || mimeType.includes('rar') || mimeType.includes('7z')) return ; if (mimeType === 'application/pdf') return ; return ; } /** * Read a browser File object as base64 string (without the data URL prefix). */ function readFileAsBase64(file: globalThis.File): Promise { return new Promise((resolve, reject) => { const reader = new FileReader(); reader.onload = () => { const dataUrl = reader.result as string; if (!dataUrl || !dataUrl.includes(',')) { reject(new Error(`Invalid data URL from FileReader for ${file.name}`)); return; } const base64 = dataUrl.split(',')[1]; if (!base64) { reject(new Error(`Empty base64 data for ${file.name}`)); return; } resolve(base64); }; reader.onerror = () => reject(new Error(`Failed to read file: ${file.name}`)); reader.readAsDataURL(file); }); } // ── Component ──────────────────────────────────────────────────── export function ChatInput({ onSend, onStop, disabled = false, sending = false }: ChatInputProps) { const [input, setInput] = useState(''); const [attachments, setAttachments] = useState([]); const textareaRef = useRef(null); const isComposingRef = useRef(false); // Auto-resize textarea useEffect(() => { if (textareaRef.current) { textareaRef.current.style.height = 'auto'; textareaRef.current.style.height = `${Math.min(textareaRef.current.scrollHeight, 200)}px`; } }, [input]); // Focus textarea on mount (avoids Windows focus loss after session delete + native dialog) useEffect(() => { if (!disabled && textareaRef.current) { textareaRef.current.focus(); } }, [disabled]); // ── File staging via native dialog ───────────────────────────── const pickFiles = useCallback(async () => { try { const result = await window.electron.ipcRenderer.invoke('dialog:open', { properties: ['openFile', 'multiSelections'], }) as { canceled: boolean; filePaths?: string[] }; if (result.canceled || !result.filePaths?.length) return; // Add placeholder entries immediately const tempIds: string[] = []; for (const filePath of result.filePaths) { const tempId = crypto.randomUUID(); tempIds.push(tempId); // Handle both Unix (/) and Windows (\) path separators const fileName = filePath.split(/[\\/]/).pop() || 'file'; setAttachments(prev => [...prev, { id: tempId, fileName, mimeType: '', fileSize: 0, stagedPath: '', preview: null, status: 'staging' as const, }]); } // Stage all files via IPC console.log('[pickFiles] Staging files:', result.filePaths); const staged = await window.electron.ipcRenderer.invoke( 'file:stage', result.filePaths, ) as Array<{ id: string; fileName: string; mimeType: string; fileSize: number; stagedPath: string; preview: string | null; }>; console.log('[pickFiles] Stage result:', staged?.map(s => ({ id: s?.id, fileName: s?.fileName, mimeType: s?.mimeType, fileSize: s?.fileSize, stagedPath: s?.stagedPath, hasPreview: !!s?.preview }))); // Update each placeholder with real data setAttachments(prev => { let updated = [...prev]; for (let i = 0; i < tempIds.length; i++) { const tempId = tempIds[i]; const data = staged[i]; if (data) { updated = updated.map(a => a.id === tempId ? { ...data, status: 'ready' as const } : a, ); } else { console.warn(`[pickFiles] No staged data for tempId=${tempId} at index ${i}`); updated = updated.map(a => a.id === tempId ? { ...a, status: 'error' as const, error: 'Staging failed' } : a, ); } } return updated; }); } catch (err) { console.error('[pickFiles] Failed to stage files:', err); // Mark any stuck 'staging' attachments as 'error' so the user can remove them // and the send button isn't permanently blocked setAttachments(prev => prev.map(a => a.status === 'staging' ? { ...a, status: 'error' as const, error: String(err) } : a, )); } }, []); // ── Stage browser File objects (paste / drag-drop) ───────────── const stageBufferFiles = useCallback(async (files: globalThis.File[]) => { for (const file of files) { const tempId = crypto.randomUUID(); setAttachments(prev => [...prev, { id: tempId, fileName: file.name, mimeType: file.type || 'application/octet-stream', fileSize: file.size, stagedPath: '', preview: null, status: 'staging' as const, }]); try { console.log(`[stageBuffer] Reading file: ${file.name} (${file.type}, ${file.size} bytes)`); const base64 = await readFileAsBase64(file); console.log(`[stageBuffer] Base64 length: ${base64?.length ?? 'null'}`); const staged = await window.electron.ipcRenderer.invoke('file:stageBuffer', { base64, fileName: file.name, mimeType: file.type || 'application/octet-stream', }) as { id: string; fileName: string; mimeType: string; fileSize: number; stagedPath: string; preview: string | null; }; console.log(`[stageBuffer] Staged: id=${staged?.id}, path=${staged?.stagedPath}, size=${staged?.fileSize}`); setAttachments(prev => prev.map(a => a.id === tempId ? { ...staged, status: 'ready' as const } : a, )); } catch (err) { console.error(`[stageBuffer] Error staging ${file.name}:`, err); setAttachments(prev => prev.map(a => a.id === tempId ? { ...a, status: 'error' as const, error: String(err) } : a, )); } } }, []); // ── Attachment management ────────────────────────────────────── const removeAttachment = useCallback((id: string) => { setAttachments(prev => prev.filter(a => a.id !== id)); }, []); const allReady = attachments.length === 0 || attachments.every(a => a.status === 'ready'); const canSend = (input.trim() || attachments.length > 0) && allReady && !disabled && !sending; const canStop = sending && !disabled && !!onStop; const handleSend = useCallback(() => { if (!canSend) return; const readyAttachments = attachments.filter(a => a.status === 'ready'); // Capture values before clearing — clear input immediately for snappy UX, // but keep attachments available for the async send const textToSend = input.trim(); const attachmentsToSend = readyAttachments.length > 0 ? readyAttachments : undefined; console.log(`[handleSend] text="${textToSend.substring(0, 50)}", attachments=${attachments.length}, ready=${readyAttachments.length}, sending=${!!attachmentsToSend}`); if (attachmentsToSend) { console.log('[handleSend] Attachment details:', attachmentsToSend.map(a => ({ id: a.id, fileName: a.fileName, mimeType: a.mimeType, fileSize: a.fileSize, stagedPath: a.stagedPath, status: a.status, hasPreview: !!a.preview, }))); } setInput(''); setAttachments([]); if (textareaRef.current) { textareaRef.current.style.height = 'auto'; } onSend(textToSend, attachmentsToSend); }, [input, attachments, canSend, onSend]); const handleStop = useCallback(() => { if (!canStop) return; onStop?.(); }, [canStop, onStop]); const handleKeyDown = useCallback( (e: React.KeyboardEvent) => { if (e.key === 'Enter' && !e.shiftKey) { const nativeEvent = e.nativeEvent as KeyboardEvent; if (isComposingRef.current || nativeEvent.isComposing || nativeEvent.keyCode === 229) { return; } e.preventDefault(); handleSend(); } }, [handleSend], ); // Handle paste (Ctrl/Cmd+V with files) const handlePaste = useCallback( (e: React.ClipboardEvent) => { const items = e.clipboardData?.items; if (!items) return; const pastedFiles: globalThis.File[] = []; for (const item of Array.from(items)) { if (item.kind === 'file') { const file = item.getAsFile(); if (file) pastedFiles.push(file); } } if (pastedFiles.length > 0) { e.preventDefault(); stageBufferFiles(pastedFiles); } }, [stageBufferFiles], ); // Handle drag & drop const [dragOver, setDragOver] = useState(false); const handleDragOver = useCallback((e: React.DragEvent) => { e.preventDefault(); e.stopPropagation(); setDragOver(true); }, []); const handleDragLeave = useCallback((e: React.DragEvent) => { e.preventDefault(); e.stopPropagation(); setDragOver(false); }, []); const handleDrop = useCallback( (e: React.DragEvent) => { e.preventDefault(); e.stopPropagation(); setDragOver(false); if (e.dataTransfer?.files?.length) { stageBufferFiles(Array.from(e.dataTransfer.files)); } }, [stageBufferFiles], ); return ( {/* Attachment Previews */} {attachments.length > 0 && ( {attachments.map((att) => ( removeAttachment(att.id)} /> ))} )} {/* Input Row */} {/* Attach Button */} {/* Textarea */} setInput(e.target.value)} onKeyDown={handleKeyDown} onCompositionStart={() => { isComposingRef.current = true; }} onCompositionEnd={() => { isComposingRef.current = false; }} onPaste={handlePaste} placeholder={disabled ? 'Gateway not connected...' : 'Message (Enter to send, Shift+Enter for new line)'} disabled={disabled} className="min-h-[44px] max-h-[200px] resize-none pr-4" rows={1} /> {/* Send Button */} {sending ? ( ) : ( )} ); } // ── Attachment Preview ─────────────────────────────────────────── function AttachmentPreview({ attachment, onRemove, }: { attachment: FileAttachment; onRemove: () => void; }) { const isImage = attachment.mimeType.startsWith('image/') && attachment.preview; return ( {isImage ? ( // Image thumbnail ) : ( // Generic file card {attachment.fileName} {attachment.fileSize > 0 ? formatFileSize(attachment.fileSize) : '...'} )} {/* Staging overlay */} {attachment.status === 'staging' && ( )} {/* Error overlay */} {attachment.status === 'error' && ( Error )} {/* Remove button */} ); }
{attachment.fileName}
{attachment.fileSize > 0 ? formatFileSize(attachment.fileSize) : '...'}