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 (
{searchQuery ? 'Try a different search term' : 'Start a new conversation'}
{chat.lastMessage.type === 'text' ? chat.lastMessage.content : `📎 ${chat.lastMessage.type}` }
)} {chat.type === 'group' && ({chat.participants.length} members
)}