'use client' import { useState, useEffect, useRef } from 'react' import { Copy, User } from 'lucide-react' import { Button } from '@/components/ui/button' import { motion } from 'framer-motion' import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar' import ReactMarkdown from 'react-markdown' // For rendering markdown content import remarkGfm from 'remark-gfm' // To support GitHub-flavored markdown (e.g., tables, strikethrough, etc.) export default function ChatArea({ messages, chatAreaRef }) { const [copiedId, setCopiedId] = useState(null) // State to track the copied message const bottomRef = useRef(null) // Ref for auto-scroll to bottom // Function to copy message content to the clipboard const handleCopy = (content, id) => { navigator.clipboard.writeText(content) setCopiedId(id) setTimeout(() => setCopiedId(null), 2000) // Reset copied state after 2 seconds } // Automatically scroll to the bottom whenever new messages arrive useEffect(() => { bottomRef.current?.scrollIntoView({ behavior: 'smooth' }) }, [messages]) return (
{messages.map((message) => (
{/* Avatar for user or bot */} {message.sender === 'user' ? ( ) : ( )} {message.sender === 'user' ? : 'K'} {/* Message container */}
{message.sender === 'user' ? 'You' : 'Karma'} {/* Copy Button */} {/* Copy confirmation */} {copiedId === message.id ? ( Copied ✅ ) : ( )}
{/* Image rendering with link */} {message.imageUrl && (
Generated or uploaded image {message.imageText && (

{message.imageText}

)}
)} {/* Markdown rendering for message content */} {message.content && (
{message.content}
)} {/* Metadata (model and timestamp) */}

model: {message.model?.description || 'N/A'} [{new Date(message.timestamp).toLocaleString()}] - {message.seed}

))} {/* Ref for auto-scroll to bottom */}
) }