"use client" import { format, isToday, isYesterday, isThisWeek, isThisYear } from "date-fns" import { Search, Pin, VolumeX, MoreVertical, Users, Hash, Settings, UserPlus, Filter } from "lucide-react" import { cn } from "@/lib/utils" import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar" import { Badge } from "@/components/ui/badge" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { ScrollArea } from "@/components/ui/scroll-area" import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuSeparator, DropdownMenuTrigger } from "@/components/ui/dropdown-menu" import { useChat, type Conversation } from "@/app/chat/use-chat" interface ConversationListProps { conversations: Conversation[] selectedConversation: string | null onSelectConversation: (conversationId: string) => void } // Enhanced time formatting function function formatMessageTime(timestamp: string): string { const date = new Date(timestamp) if (isToday(date)) { return format(date, 'h:mm a') // 3:30 PM } else if (isYesterday(date)) { return 'Yesterday' } else if (isThisWeek(date)) { return format(date, 'EEEE') // Day name } else if (isThisYear(date)) { return format(date, 'MMM d') // Jan 15 } else { return format(date, 'dd/MM/yy') // 15/01/24 } } export function ConversationList({ conversations, selectedConversation, onSelectConversation }: ConversationListProps) { const { searchQuery, setSearchQuery } = useChat() const filteredConversations = conversations.filter((conversation) => conversation.name.toLowerCase().includes(searchQuery.toLowerCase()) ) const sortedConversations = filteredConversations.sort((a, b) => { // Pinned conversations first if (a.isPinned && !b.isPinned) return -1 if (!a.isPinned && b.isPinned) return 1 // Then by last message timestamp return new Date(b.lastMessage.timestamp).getTime() - new Date(a.lastMessage.timestamp).getTime() }) const getOnlineStatus = (conversation: Conversation) => { if (conversation.type === "direct" && conversation.participants.length === 1) { // In a real app, you'd check user online status return Math.random() > 0.5 // Mock online status } return false } return (
{/* Header - Hidden on mobile (handled by parent) */}

Messages

New Chat Filter Messages Chat Settings
{/* Search */}
setSearchQuery(e.target.value)} className="pl-9 cursor-text" />
{/* Conversations */}
{sortedConversations.map((conversation) => (
onSelectConversation(conversation.id)} > {/* Avatar with online indicator */}
{conversation.type === "group" ? ( ) : ( conversation.name.split(' ').map(n => n[0]).join('').slice(0, 2) )} {/* Online indicator for direct messages */} {conversation.type === "direct" && getOnlineStatus(conversation) && (
)} {/* Group indicator */} {conversation.type === "group" && (
)}
{/* Content */}

{conversation.name}

{conversation.isPinned && ( )} {conversation.isMuted && ( )}
{formatMessageTime(conversation.lastMessage.timestamp)}

{conversation.lastMessage.content}

{/* Unread count */} {conversation.unreadCount > 0 && ( {conversation.unreadCount > 99 ? "99+" : conversation.unreadCount} )}
))}
) }