2024-04-09 16:21:05 +05:30
|
|
|
import { cn } from '@/lib/utils';
|
2025-05-13 01:40:47 -06:00
|
|
|
import { Check, Pencil, X } from 'lucide-react';
|
2025-05-09 01:12:31 -06:00
|
|
|
import { useState } from 'react';
|
|
|
|
|
import { Message } from './ChatWindow';
|
|
|
|
|
import MessageTabs from './MessageTabs';
|
2025-05-03 16:00:08 -06:00
|
|
|
|
2024-04-09 16:21:05 +05:30
|
|
|
const MessageBox = ({
|
|
|
|
|
message,
|
|
|
|
|
messageIndex,
|
|
|
|
|
history,
|
|
|
|
|
loading,
|
|
|
|
|
isLast,
|
|
|
|
|
rewrite,
|
2024-05-18 13:11:15 +05:30
|
|
|
sendMessage,
|
2025-05-13 01:40:47 -06:00
|
|
|
handleEditMessage,
|
2025-08-03 15:48:34 -06:00
|
|
|
onThinkBoxToggle,
|
2024-04-09 16:21:05 +05:30
|
|
|
}: {
|
|
|
|
|
message: Message;
|
|
|
|
|
messageIndex: number;
|
|
|
|
|
history: Message[];
|
|
|
|
|
loading: boolean;
|
|
|
|
|
isLast: boolean;
|
|
|
|
|
rewrite: (messageId: string) => void;
|
2025-04-30 22:58:19 -06:00
|
|
|
sendMessage: (
|
|
|
|
|
message: string,
|
|
|
|
|
options?: {
|
|
|
|
|
messageId?: string;
|
|
|
|
|
rewriteIndex?: number;
|
|
|
|
|
suggestions?: string[];
|
|
|
|
|
},
|
|
|
|
|
) => void;
|
2025-05-13 01:40:47 -06:00
|
|
|
handleEditMessage: (messageId: string, content: string) => void;
|
2025-08-03 15:48:34 -06:00
|
|
|
onThinkBoxToggle: (
|
|
|
|
|
messageId: string,
|
|
|
|
|
thinkBoxId: string,
|
|
|
|
|
expanded: boolean,
|
|
|
|
|
) => void;
|
2024-04-09 16:21:05 +05:30
|
|
|
}) => {
|
2025-05-13 01:40:47 -06:00
|
|
|
// Local state for editing functionality
|
|
|
|
|
const [isEditing, setIsEditing] = useState(false);
|
|
|
|
|
const [editedContent, setEditedContent] = useState('');
|
|
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
};
|
2024-04-09 16:21:05 +05:30
|
|
|
return (
|
|
|
|
|
<div>
|
|
|
|
|
{message.role === 'user' && (
|
2025-03-14 22:05:07 +05:30
|
|
|
<div
|
|
|
|
|
className={cn(
|
|
|
|
|
'w-full',
|
|
|
|
|
messageIndex === 0 ? 'pt-16' : 'pt-8',
|
|
|
|
|
'break-words',
|
|
|
|
|
)}
|
|
|
|
|
>
|
2025-05-13 01:40:47 -06:00
|
|
|
{isEditing ? (
|
|
|
|
|
<div className="w-full">
|
|
|
|
|
<textarea
|
2025-08-09 17:30:12 -06:00
|
|
|
className="w-full p-3 text-lg bg-surface rounded-lg transition duration-200 min-h-[120px] font-medium text-fg placeholder:text-fg/40 border border-surface-2 focus:outline-none focus:ring-2 focus:ring-accent/40"
|
2025-05-13 01:40:47 -06:00
|
|
|
value={editedContent}
|
|
|
|
|
onChange={(e) => setEditedContent(e.target.value)}
|
2025-05-14 11:19:06 -06:00
|
|
|
placeholder="Edit your message..."
|
2025-05-13 01:40:47 -06:00
|
|
|
autoFocus
|
|
|
|
|
/>
|
|
|
|
|
<div className="flex flex-row space-x-2 mt-3 justify-end">
|
|
|
|
|
<button
|
|
|
|
|
onClick={cancelEditMessage}
|
2025-08-09 17:30:12 -06:00
|
|
|
className="p-2 rounded-full bg-surface hover:bg-surface-2 border border-surface-2 transition duration-200 text-fg/80"
|
2025-05-13 01:40:47 -06:00
|
|
|
aria-label="Cancel"
|
|
|
|
|
title="Cancel"
|
|
|
|
|
>
|
|
|
|
|
<X size={18} />
|
|
|
|
|
</button>
|
|
|
|
|
<button
|
|
|
|
|
onClick={saveEditMessage}
|
2025-08-09 17:30:12 -06:00
|
|
|
className="p-2 rounded-full bg-accent hover:bg-accent-700 transition duration-200 text-white disabled:opacity-50 disabled:cursor-not-allowed"
|
2025-05-13 01:40:47 -06:00
|
|
|
aria-label="Save changes"
|
|
|
|
|
title="Save changes"
|
|
|
|
|
disabled={!editedContent.trim()}
|
|
|
|
|
>
|
2025-08-09 17:30:12 -06:00
|
|
|
<Check size={18} />
|
2025-05-13 01:40:47 -06:00
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
) : (
|
|
|
|
|
<>
|
|
|
|
|
<div className="flex items-center">
|
2025-08-09 17:30:12 -06:00
|
|
|
<h2 className="font-medium text-3xl" onClick={startEditMessage}>
|
2025-05-13 01:40:47 -06:00
|
|
|
{message.content}
|
|
|
|
|
</h2>
|
|
|
|
|
<button
|
|
|
|
|
onClick={startEditMessage}
|
2025-08-09 17:30:12 -06:00
|
|
|
className="ml-3 p-2 rounded-xl bg-surface hover:bg-surface-2 border border-surface-2 flex-shrink-0"
|
2025-05-13 01:40:47 -06:00
|
|
|
aria-label="Edit message"
|
|
|
|
|
title="Edit message"
|
|
|
|
|
>
|
|
|
|
|
<Pencil size={18} />
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
</>
|
|
|
|
|
)}
|
2024-04-09 16:21:05 +05:30
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
{message.role === 'assistant' && (
|
2025-05-09 01:12:31 -06:00
|
|
|
<MessageTabs
|
|
|
|
|
query={history[messageIndex - 1].content}
|
|
|
|
|
chatHistory={history.slice(0, messageIndex - 1)}
|
|
|
|
|
messageId={message.messageId}
|
|
|
|
|
message={message}
|
|
|
|
|
isLast={isLast}
|
|
|
|
|
loading={loading}
|
|
|
|
|
rewrite={rewrite}
|
|
|
|
|
sendMessage={sendMessage}
|
2025-08-03 15:48:34 -06:00
|
|
|
onThinkBoxToggle={onThinkBoxToggle}
|
2025-05-09 01:12:31 -06:00
|
|
|
/>
|
2024-04-09 16:21:05 +05:30
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default MessageBox;
|