import { useState, useRef, useEffect } from 'react' import { useChatStore } from '@/store/chatStore' import { useAuthStore } from '@/store/authStore' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar' import { ScrollArea } from '@/components/ui/scroll-area' import { Send, Paperclip, Smile, Phone, Video, MoreVertical, Users, Info } from 'lucide-react' import { formatTime, getInitials } from '@/lib/utils' export default function ChatWindow() { const { user } = useAuthStore() const { currentChat, messages, sendMessage, loading } = useChatStore() const [messageText, setMessageText] = useState('') const messagesEndRef = useRef(null) const inputRef = useRef(null) const chatMessages = currentChat ? messages[currentChat.id] || [] : [] useEffect(() => { scrollToBottom() }, [chatMessages]) const scrollToBottom = () => { messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' }) } const handleSendMessage = async () => { if (!messageText.trim() || !currentChat) return try { await sendMessage(currentChat.id, messageText.trim()) setMessageText('') inputRef.current?.focus() } catch (error) { console.error('Failed to send message:', error) } } const handleKeyPress = (e: React.KeyboardEvent) => { if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault() handleSendMessage() } } const getChatDisplayName = () => { if (!currentChat) return '' if (currentChat.type === 'group') { return currentChat.name || 'Unnamed Group' } else { const otherParticipant = currentChat.participants.find(p => p.userId !== user?.id) return otherParticipant?.user.displayName || 'Unknown User' } } const getChatAvatar = () => { if (!currentChat) return undefined if (currentChat.type === 'group') { return currentChat.avatar } else { const otherParticipant = currentChat.participants.find(p => p.userId !== user?.id) return otherParticipant?.user.avatar } } const isMessageFromCurrentUser = (message: any) => { return message.senderId === user?.id } if (!currentChat) { return (
💬

Welcome to ChatApp

Select a chat to start messaging

) } return (
{/* Header */}
{currentChat.type === 'group' ? ( ) : ( getInitials(getChatDisplayName()) )}

{getChatDisplayName()}

{currentChat.type === 'group' ? `${currentChat.participants.length} members` : 'Online' }

{/* Messages */}
{loading ? (
Loading messages...
) : chatMessages.length === 0 ? (
👋

No messages yet. Start the conversation!

) : ( chatMessages.map((message, index) => { const isFromCurrentUser = isMessageFromCurrentUser(message) const showAvatar = !isFromCurrentUser && ( index === 0 || chatMessages[index - 1]?.senderId !== message.senderId ) return (
{showAvatar && !isFromCurrentUser && ( {getInitials(message.sender?.displayName || 'U')} )}
{!isFromCurrentUser && showAvatar && currentChat.type === 'group' && (

{message.sender?.displayName}

)}

{message.content}

{formatTime(message.createdAt)} {isFromCurrentUser && ( {message.status === 'sent' && '✓'} {message.status === 'delivered' && '✓✓'} {message.status === 'read' && '✓✓'} )}
) }) )}
{/* Message Input */}
setMessageText(e.target.value)} onKeyPress={handleKeyPress} className="pr-10" />
) }