import { cn } from '@/lib/utils'; import { Check, Pencil, X } from 'lucide-react'; import { useEffect, useRef, useState } from 'react'; import { Message } from './ChatWindow'; import MessageTabs from './MessageTabs'; const MessageBox = ({ message, messageIndex, history, loading, isLast, rewrite, sendMessage, handleEditMessage, onThinkBoxToggle, }: { message: Message; messageIndex: number; history: Message[]; loading: boolean; isLast: boolean; rewrite: (messageId: string) => void; sendMessage: ( message: string, options?: { messageId?: string; rewriteIndex?: number; suggestions?: string[]; }, ) => void; handleEditMessage: (messageId: string, content: string) => void; onThinkBoxToggle: ( messageId: string, thinkBoxId: string, expanded: boolean, ) => void; }) => { // Local state for editing functionality const [isEditing, setIsEditing] = useState(false); const [editedContent, setEditedContent] = useState(''); // State for truncation toggle of long user prompts const [isExpanded, setIsExpanded] = useState(false); const [isOverflowing, setIsOverflowing] = useState(false); const contentRef = useRef(null); // Measure overflow compared to a 3-line clamped state useEffect(() => { const measureOverflow = () => { const el = contentRef.current; if (!el) return; const hadClamp = el.classList.contains('line-clamp-3'); if (!hadClamp) el.classList.add('line-clamp-3'); const overflowing = el.scrollHeight > el.clientHeight + 1; setIsOverflowing(overflowing); if (!hadClamp) el.classList.remove('line-clamp-3'); }; measureOverflow(); window.addEventListener('resize', measureOverflow); return () => { window.removeEventListener('resize', measureOverflow); }; }, [message.content]); // Initialize editing const startEditMessage = () => { setIsEditing(true); setEditedContent(message.content); }; // Cancel editing const cancelEditMessage = () => { setIsEditing(false); setEditedContent(''); }; // Save edits const saveEditMessage = () => { handleEditMessage(message.messageId, editedContent); setIsEditing(false); }; return (
{message.role === 'user' && (
{isEditing ? (