'use client' import { useState, useRef, useEffect } from 'react' import { cn } from '@/lib/utils' import { Button } from '@/components/ui/button' import { Textarea } from '@/components/ui/textarea' import { Skeleton } from '@/components/ui/skeleton' import { Send, MessageSquare } from 'lucide-react' interface Message { id: string message: string createdAt: Date | string isRead: boolean sender: { id: string name: string | null email: string role?: string } } interface MentorChatProps { messages: Message[] currentUserId: string onSendMessage: (message: string) => Promise isLoading?: boolean isSending?: boolean className?: string } export function MentorChat({ messages, currentUserId, onSendMessage, isLoading, isSending, className, }: MentorChatProps) { const [newMessage, setNewMessage] = useState('') const messagesEndRef = useRef(null) const textareaRef = useRef(null) const scrollToBottom = () => { messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' }) } useEffect(() => { scrollToBottom() }, [messages]) const handleSend = async () => { const text = newMessage.trim() if (!text || isSending) return setNewMessage('') await onSendMessage(text) } const handleKeyDown = (e: React.KeyboardEvent) => { if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault() handleSend() } } const formatTime = (date: Date | string) => { const d = typeof date === 'string' ? new Date(date) : date return d.toLocaleString('en-US', { month: 'short', day: 'numeric', hour: 'numeric', minute: '2-digit', }) } if (isLoading) { return (
) } return (
{/* Messages */}
{messages.length === 0 ? (

No messages yet

Send a message to start the conversation

) : ( messages.map((msg) => { const isOwn = msg.sender.id === currentUserId return (
{!isOwn && (

{msg.sender.name || msg.sender.email} {msg.sender.role === 'MENTOR' && ( Mentor )}

)}

{msg.message}

{formatTime(msg.createdAt)}

) }) )}
{/* Input */}