import { useState } 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 { Badge } from '@/components/ui/badge' import { ScrollArea } from '@/components/ui/scroll-area' import { Search, Plus, Settings, MessageSquare, Users } from 'lucide-react' import { formatTime, getInitials } from '@/lib/utils' export default function ChatSidebar() { const { user } = useAuthStore() const { chats, currentChat, selectChat, loading } = useChatStore() const [searchQuery, setSearchQuery] = useState('') const filteredChats = chats.filter(chat => { if (!searchQuery) return true const searchLower = searchQuery.toLowerCase() // Search by chat name if (chat.name?.toLowerCase().includes(searchLower)) return true // Search by participant names (for direct chats) if (chat.type === 'direct') { const otherParticipant = chat.participants.find(p => p.userId !== user?.id) if (otherParticipant?.user.displayName?.toLowerCase().includes(searchLower)) return true } return false }) const getChatDisplayName = (chat: any) => { if (chat.type === 'group') { return chat.name || 'Unnamed Group' } else { const otherParticipant = chat.participants.find((p: any) => p.userId !== user?.id) return otherParticipant?.user.displayName || 'Unknown User' } } const getChatAvatar = (chat: any) => { if (chat.type === 'group') { return chat.avatar } else { const otherParticipant = chat.participants.find((p: any) => p.userId !== user?.id) return otherParticipant?.user.avatar } } return (
{/* Header */}

Chats

{/* Search */}
setSearchQuery(e.target.value)} className="pl-10" />
{/* Chat List */}
{loading ? (
Loading chats...
) : filteredChats.length === 0 ? (

No chats found

{searchQuery ? 'Try a different search term' : 'Start a new conversation'}

) : ( filteredChats.map((chat) => (
selectChat(chat.id)} className={` flex items-center p-3 rounded-lg cursor-pointer transition-colors hover:bg-accent hover:text-accent-foreground ${currentChat?.id === chat.id ? 'bg-accent text-accent-foreground' : ''} `} >
{chat.type === 'group' ? ( ) : ( getInitials(getChatDisplayName(chat)) )} {/* Online indicator for direct chats */} {chat.type === 'direct' && (
)}

{getChatDisplayName(chat)}

{chat.lastMessage && ( {formatTime(chat.lastMessage.createdAt)} )} {chat.unreadCount > 0 && ( {chat.unreadCount > 99 ? '99+' : chat.unreadCount} )}
{chat.lastMessage && (

{chat.lastMessage.type === 'text' ? chat.lastMessage.content : `📎 ${chat.lastMessage.type}` }

)} {chat.type === 'group' && (

{chat.participants.length} members

)}
)) )}
) }