import React, { useState } from "react"; import Layout from "@/components/layout/layout"; import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card"; import { Input } from "@/components/ui/input"; import { Button } from "@/components/ui/button"; import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"; import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; import { ScrollArea } from "@/components/ui/scroll-area"; import { Badge } from "@/components/ui/badge"; import { Separator } from "@/components/ui/separator"; import { Send, Search, Phone, Video, MessageSquare, Inbox, Paperclip, MoreHorizontal, Plus, Users } from "lucide-react"; interface Message { id: string; sender: string; content: string; timestamp: string; isRead: boolean; isOutgoing: boolean; avatar?: string; } interface Conversation { id: string; participant: string; lastMessage: string; timestamp: string; unreadCount: number; avatar?: string; isOnline: boolean; isGroup?: boolean; members?: string[]; } const Messages = () => { const [conversations, setConversations] = useState([ { id: "1", participant: "Team Alpha", lastMessage: "Let's discuss the project timeline", timestamp: "10:30 AM", unreadCount: 3, isOnline: true, isGroup: true, members: ["Jane Smith", "Alex Johnson", "Sarah Williams"] }, { id: "2", participant: "Jane Smith", lastMessage: "I've finished the UI designs", timestamp: "Yesterday", unreadCount: 0, isOnline: true }, { id: "3", participant: "Alex Johnson", lastMessage: "Can we review the backlog items?", timestamp: "Yesterday", unreadCount: 2, isOnline: false }, { id: "4", participant: "Sarah Williams", lastMessage: "The API integration is complete", timestamp: "Oct 15", unreadCount: 0, isOnline: false }, { id: "5", participant: "Project Leads", lastMessage: "Meeting scheduled for tomorrow", timestamp: "Oct 14", unreadCount: 0, isOnline: true, isGroup: true, members: ["Michael Brown", "Emma Davis", "David Wilson"] } ]); const [activeConversation, setActiveConversation] = useState("1"); const [messageInput, setMessageInput] = useState(""); const [searchQuery, setSearchQuery] = useState(""); const [messages, setMessages] = useState>({ "1": [ { id: "1-1", sender: "Alex Johnson", content: "Hey team, we should discuss the project timeline", timestamp: "10:15 AM", isRead: true, isOutgoing: false }, { id: "1-2", sender: "Sarah Williams", content: "I agree. I'm available this afternoon", timestamp: "10:20 AM", isRead: true, isOutgoing: false }, { id: "1-3", sender: "You", content: "Let's schedule a meeting for 2 PM", timestamp: "10:25 AM", isRead: true, isOutgoing: true }, { id: "1-4", sender: "Jane Smith", content: "Works for me. I'll prepare the agenda", timestamp: "10:30 AM", isRead: false, isOutgoing: false } ], "2": [ { id: "2-1", sender: "Jane Smith", content: "I've been working on the UI designs", timestamp: "Yesterday, 4:30 PM", isRead: true, isOutgoing: false }, { id: "2-2", sender: "You", content: "How's it coming along?", timestamp: "Yesterday, 4:35 PM", isRead: true, isOutgoing: true }, { id: "2-3", sender: "Jane Smith", content: "Just finished! I'll share the files", timestamp: "Yesterday, 4:40 PM", isRead: true, isOutgoing: false }, { id: "2-4", sender: "Jane Smith", content: "I've finished the UI designs and shared them in the design folder", timestamp: "Yesterday, 4:45 PM", isRead: true, isOutgoing: false } ] }); const filteredConversations = conversations.filter( (conversation) => conversation.participant.toLowerCase().includes(searchQuery.toLowerCase()) ); const handleSendMessage = () => { if (messageInput.trim() === "") return; const newMessage: Message = { id: `${activeConversation}-${Date.now()}`, sender: "You", content: messageInput, timestamp: "Just now", isRead: true, isOutgoing: true }; setMessages((prevMessages) => ({ ...prevMessages, [activeConversation]: [ ...(prevMessages[activeConversation] || []), newMessage ] })); // Update last message in conversation list setConversations( conversations.map((conversation) => conversation.id === activeConversation ? { ...conversation, lastMessage: messageInput, timestamp: "Just now", unreadCount: 0 } : conversation ) ); setMessageInput(""); }; const markAsRead = (conversationId: string) => { setConversations( conversations.map((conversation) => conversation.id === conversationId ? { ...conversation, unreadCount: 0 } : conversation ) ); }; const handleConversationClick = (conversationId: string) => { setActiveConversation(conversationId); markAsRead(conversationId); }; const getInitials = (name: string) => { return name .split(" ") .map((n) => n[0]) .join("") .toUpperCase(); }; const currentConversation = conversations.find( (conversation) => conversation.id === activeConversation ); return (
{/* Sidebar */}
setSearchQuery(e.target.value)} />
All Unread Groups {filteredConversations.map((conversation) => (
handleConversationClick(conversation.id)} >
{conversation.isGroup ? ( ) : ( getInitials(conversation.participant) )} {conversation.isOnline && ( )}
{conversation.participant} {conversation.timestamp}

{conversation.lastMessage}

{conversation.unreadCount > 0 && ( {conversation.unreadCount} )}
))}
{filteredConversations .filter((c) => c.unreadCount > 0) .map((conversation) => (
handleConversationClick(conversation.id)} > {conversation.isGroup ? ( ) : ( getInitials(conversation.participant) )}
{conversation.participant} {conversation.timestamp}

{conversation.lastMessage}

{conversation.unreadCount}
))}
{filteredConversations .filter((c) => c.isGroup) .map((conversation) => (
handleConversationClick(conversation.id)} >
{conversation.participant} {conversation.timestamp}

{conversation.members?.join(", ")}

{conversation.unreadCount > 0 && ( {conversation.unreadCount} )}
))}
{/* Main Content */}
{currentConversation ? ( <> {/* Chat Header */}
{currentConversation.isGroup ? ( ) : ( getInitials(currentConversation.participant) )}

{currentConversation.participant} {currentConversation.isOnline && ( )}

{currentConversation.isGroup && (

{currentConversation.members?.join(", ")}

)}
{/* Messages */}
{messages[activeConversation]?.map((message) => (
{!message.isOutgoing && ( {getInitials(message.sender)} )}
{!message.isOutgoing && (

{message.sender}

)}

{message.content}

{message.timestamp}

))}
{/* Message Input */}
setMessageInput(e.target.value)} onKeyPress={(e) => { if (e.key === "Enter") handleSendMessage(); }} />
) : (

Your Messages

Select a conversation or start a new one

)}
); }; export default Messages;