| // src/components/messages/ContactsPanel.tsx | |
| import { useContacts } from "@/hooks/useContacts"; | |
| import { useState, useMemo, useEffect, useCallback } from "react"; | |
| import { Contact, User } from "@/types"; | |
| import { | |
| Search, UserPlus, MessageCircle, X, Loader2, | |
| Star, UserCheck, UserX, MoreVertical, Shield, | |
| Ban, Flag, Copy, Share2, Filter, Users, | |
| Phone, Video, Mail, Calendar, Clock, Award, | |
| Check, Settings, Volume2, VolumeX, Pin, Trash2 | |
| } from "lucide-react"; | |
| import { OnlineStatusIndicator } from "./OnlineStatusIndicator"; | |
| import { useWebSocket } from "@/context/WebSocketContext"; | |
| import { useAuth } from "@/context/AuthContext"; | |
| import { useContextMenu } from "@/hooks/useContextMenu"; | |
| import { ContextMenu } from "@/components/ui/ContextMenu"; | |
| import { motion, AnimatePresence } from "framer-motion"; | |
| import { toast } from "react-toastify"; | |
| import api from "@/lib/api"; | |
| interface ContactsPanelProps { | |
| onStartChat: (username: string) => void; | |
| onShowProfile: (username: string) => void; | |
| onClose: () => void; | |
| onAddContact?: () => void; | |
| onInviteContact?: () => void; | |
| onBlockContact?: (username: string) => void; | |
| onUnblockContact?: (username: string) => void; | |
| onFavoriteContact?: (username: string, isFavorite: boolean) => void; | |
| filter?: "all" | "online" | "favorites" | "blocked"; | |
| showGroups?: boolean; | |
| groups?: any[]; | |
| onGroupClick?: (groupId: string) => void; | |
| } | |
| interface ContactWithStatus extends Contact { | |
| isFavorite?: boolean; | |
| isBlocked?: boolean; | |
| lastInteraction?: string; | |
| mutualFriends?: number; | |
| commonInterests?: string[]; | |
| relationship?: "following" | "follower" | "mutual" | "none"; | |
| } | |
| export function ContactsPanel({ | |
| onStartChat, | |
| onShowProfile, | |
| onClose, | |
| onAddContact, | |
| onInviteContact, | |
| onBlockContact, | |
| onUnblockContact, | |
| onFavoriteContact, | |
| filter = "all", | |
| showGroups = false, | |
| groups = [], | |
| onGroupClick, | |
| }: ContactsPanelProps) { | |
| const { user } = useAuth(); | |
| const { sendMessage, subscribe, connectionStatus } = useWebSocket(); | |
| const { contacts, loading, error, refetch } = useContacts(); | |
| const { contextMenu, showContextMenu, hideContextMenu } = useContextMenu(); | |
| const [search, setSearch] = useState(""); | |
| const [activeFilter, setActiveFilter] = useState<"all" | "online" | "favorites" | "blocked">(filter); | |
| const [favorites, setFavorites] = useState<Set<string>>(new Set()); | |
| const [blocked, setBlocked] = useState<Set<string>>(new Set()); | |
| const [onlineStatuses, setOnlineStatuses] = useState<Record<string, { isOnline: boolean; lastSeen?: string }>>({}); | |
| const [typingUsers, setTypingUsers] = useState<Set<string>>(new Set()); | |
| const [viewMode, setViewMode] = useState<"grid" | "list">("list"); | |
| const [selectedContact, setSelectedContact] = useState<Contact | null>(null); | |
| const [showContactDetails, setShowContactDetails] = useState(false); | |
| const handleAddContactDefault = async () => { | |
| const username = prompt("Enter contact's username:"); | |
| if (!username || !username.trim()) return; | |
| try { | |
| await api.post(`/contacts/${username.trim()}`); | |
| toast.success(`Contact ${username} added successfully`); | |
| refetch(); | |
| } catch (err: any) { | |
| toast.error(err.response?.data?.error || "Failed to add contact"); | |
| } | |
| }; | |
| const filterTabs: Array<{ id: "all" | "online" | "favorites" | "blocked"; label: string; icon: React.ElementType }> = [ | |
| { id: "all", label: "All", icon: Users }, | |
| { id: "online", label: "Online", icon: Check }, | |
| { id: "favorites", label: "Favorites", icon: Star }, | |
| { id: "blocked", label: "Blocked", icon: Ban }, | |
| ]; | |
| // Subscribe to real-time events | |
| useEffect(() => { | |
| const unsubscribePresence = subscribe("presence", (data) => { | |
| setOnlineStatuses(prev => ({ | |
| ...prev, | |
| [data.username]: { isOnline: data.isOnline, lastSeen: data.lastSeen } | |
| })); | |
| }); | |
| const unsubscribeTyping = subscribe("typing", (data) => { | |
| if (data.isTyping) { | |
| setTypingUsers(prev => new Set(prev).add(data.username)); | |
| } else { | |
| setTypingUsers(prev => { | |
| const newSet = new Set(prev); | |
| newSet.delete(data.username); | |
| return newSet; | |
| }); | |
| } | |
| }); | |
| return () => { | |
| unsubscribePresence(); | |
| unsubscribeTyping(); | |
| }; | |
| }, [subscribe]); | |
| // Load favorites and blocked list | |
| useEffect(() => { | |
| const loadUserData = async () => { | |
| try { | |
| const [favRes, blockRes] = await Promise.all([ | |
| api.get("/user/favorites"), | |
| api.get("/user/blocked") | |
| ]); | |
| setFavorites(new Set(favRes.data.map((f: any) => f.username))); | |
| setBlocked(new Set(blockRes.data.map((b: any) => b.username))); | |
| } catch (error) { | |
| console.error("Failed to load user data:", error); | |
| } | |
| }; | |
| if (user) loadUserData(); | |
| }, [user]); | |
| // Filter contacts | |
| const filteredContacts = useMemo(() => { | |
| let filtered = [...contacts]; | |
| // Apply search | |
| if (search.trim()) { | |
| const q = search.toLowerCase(); | |
| filtered = filtered.filter( | |
| (c) => | |
| c.username.toLowerCase().includes(q) || | |
| (c.fullName && c.fullName.toLowerCase().includes(q)) || | |
| (c.email && c.email.toLowerCase().includes(q)) | |
| ); | |
| } | |
| // Apply status filter | |
| switch (activeFilter) { | |
| case "online": | |
| filtered = filtered.filter(c => onlineStatuses[c.username]?.isOnline || c.isOnline); | |
| break; | |
| case "favorites": | |
| filtered = filtered.filter(c => favorites.has(c.username)); | |
| break; | |
| case "blocked": | |
| filtered = filtered.filter(c => blocked.has(c.username)); | |
| break; | |
| default: | |
| filtered = filtered.filter(c => !blocked.has(c.username)); | |
| } | |
| return filtered; | |
| }, [contacts, search, activeFilter, onlineStatuses, favorites, blocked]); | |
| // Sort contacts: favorites first, then online, then alphabetically | |
| const sortedContacts = useMemo(() => { | |
| return [...filteredContacts].sort((a, b) => { | |
| const aIsFavorite = favorites.has(a.username); | |
| const bIsFavorite = favorites.has(b.username); | |
| if (aIsFavorite && !bIsFavorite) return -1; | |
| if (!aIsFavorite && bIsFavorite) return 1; | |
| const aIsOnline = onlineStatuses[a.username]?.isOnline || a.isOnline; | |
| const bIsOnline = onlineStatuses[b.username]?.isOnline || b.isOnline; | |
| if (aIsOnline && !bIsOnline) return -1; | |
| if (!aIsOnline && bIsOnline) return 1; | |
| return (a.fullName || a.username).localeCompare(b.fullName || b.username); | |
| }); | |
| }, [filteredContacts, favorites, onlineStatuses]); | |
| // Group by online status for list view | |
| const { online, offline, favorites: favContacts } = useMemo(() => { | |
| const online: Contact[] = []; | |
| const offline: Contact[] = []; | |
| const favs: Contact[] = []; | |
| sortedContacts.forEach((c) => { | |
| const isOnline = onlineStatuses[c.username]?.isOnline || c.isOnline; | |
| const isFavorite = favorites.has(c.username); | |
| if (isFavorite) favs.push(c); | |
| else if (isOnline) online.push(c); | |
| else offline.push(c); | |
| }); | |
| return { online, offline, favorites: favs }; | |
| }, [sortedContacts, onlineStatuses, favorites]); | |
| const getStatusText = (contact: Contact): string => { | |
| const isOnline = onlineStatuses[contact.username]?.isOnline || contact.isOnline; | |
| if (isOnline) return "Online"; | |
| const lastSeen = onlineStatuses[contact.username]?.lastSeen || contact.lastSeen; | |
| if (lastSeen) { | |
| const date = new Date(lastSeen); | |
| const now = new Date(); | |
| const diff = now.getTime() - date.getTime(); | |
| const hours = diff / (1000 * 60 * 60); | |
| if (hours < 24) { | |
| return `Last seen ${date.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })}`; | |
| } | |
| if (hours < 168) { | |
| return `Last seen ${date.toLocaleDateString([], { weekday: 'short' })}`; | |
| } | |
| return `Last seen ${date.toLocaleDateString()}`; | |
| } | |
| return "Offline"; | |
| }; | |
| const handleToggleFavorite = async (username: string) => { | |
| const isFavorite = favorites.has(username); | |
| try { | |
| if (isFavorite) { | |
| await api.delete(`/user/favorites/${username}`); | |
| setFavorites(prev => { | |
| const newSet = new Set(prev); | |
| newSet.delete(username); | |
| return newSet; | |
| }); | |
| toast.success(`${username} removed from favorites`); | |
| } else { | |
| await api.post(`/user/favorites/${username}`); | |
| setFavorites(prev => new Set(prev).add(username)); | |
| toast.success(`${username} added to favorites`); | |
| } | |
| onFavoriteContact?.(username, !isFavorite); | |
| } catch (error) { | |
| toast.error("Operation failed"); | |
| } | |
| }; | |
| const handleBlockUser = async (username: string) => { | |
| if (confirm(`Block ${username}? You won't receive messages from them.`)) { | |
| try { | |
| await api.post(`/user/block/${username}`); | |
| setBlocked(prev => new Set(prev).add(username)); | |
| toast.success(`${username} blocked`); | |
| onBlockContact?.(username); | |
| } catch (error) { | |
| toast.error("Failed to block user"); | |
| } | |
| } | |
| }; | |
| const handleUnblockUser = async (username: string) => { | |
| try { | |
| await api.delete(`/user/block/${username}`); | |
| setBlocked(prev => { | |
| const newSet = new Set(prev); | |
| newSet.delete(username); | |
| return newSet; | |
| }); | |
| toast.success(`${username} unblocked`); | |
| onUnblockContact?.(username); | |
| } catch (error) { | |
| toast.error("Failed to unblock user"); | |
| } | |
| }; | |
| const handleStartChat = (username: string) => { | |
| onStartChat(username); | |
| onClose(); | |
| }; | |
| const handleCopyUsername = (username: string) => { | |
| navigator.clipboard.writeText(username); | |
| toast.success("Username copied to clipboard"); | |
| }; | |
| const handleShareContact = async (contact: Contact) => { | |
| try { | |
| await navigator.share({ | |
| title: contact.fullName || contact.username, | |
| text: `Connect with ${contact.fullName || contact.username} on Research Platform`, | |
| url: `${window.location.origin}/users/${contact.username}` | |
| }); | |
| } catch (error) { | |
| handleCopyUsername(contact.username); | |
| } | |
| }; | |
| // Context menu for contact | |
| const getContactContextMenuItems = (contact: Contact) => [ | |
| { label: "View Profile", icon: <Users className="w-4 h-4" />, onClick: () => onShowProfile(contact.username) }, | |
| { label: "Send Message", icon: <MessageCircle className="w-4 h-4" />, onClick: () => handleStartChat(contact.username) }, | |
| { divider: true }, | |
| { | |
| label: favorites.has(contact.username) ? "Remove from Favorites" : "Add to Favorites", | |
| icon: <Star className="w-4 h-4" />, | |
| onClick: () => handleToggleFavorite(contact.username) | |
| }, | |
| { label: "Copy Username", icon: <Copy className="w-4 h-4" />, onClick: () => handleCopyUsername(contact.username) }, | |
| { label: "Share Contact", icon: <Share2 className="w-4 h-4" />, onClick: () => handleShareContact(contact) }, | |
| { divider: true }, | |
| { label: "Block User", icon: <Ban className="w-4 h-4" />, onClick: () => handleBlockUser(contact.username), danger: true }, | |
| { label: "Report User", icon: <Flag className="w-4 h-4" />, onClick: () => {}, danger: true }, | |
| ]; | |
| if (loading) { | |
| return ( | |
| <div className="flex flex-col h-full"> | |
| <div className="p-3 border-b border-gray-200 dark:border-gray-700 flex items-center justify-between"> | |
| <h2 className="text-lg font-bold animate-pulse bg-gray-200 dark:bg-gray-700 h-6 w-20 rounded"></h2> | |
| <div className="flex gap-2"> | |
| <div className="w-8 h-8 bg-gray-200 dark:bg-gray-700 rounded-lg animate-pulse"></div> | |
| <div className="w-8 h-8 bg-gray-200 dark:bg-gray-700 rounded-lg animate-pulse"></div> | |
| </div> | |
| </div> | |
| <div className="p-8 text-center"> | |
| <Loader2 className="w-6 h-6 animate-spin mx-auto text-primary" /> | |
| <p className="text-gray-500 text-sm mt-2">Loading contacts...</p> | |
| </div> | |
| </div> | |
| ); | |
| } | |
| return ( | |
| <div className="flex flex-col h-full bg-white dark:bg-gray-900"> | |
| {/* Header */} | |
| <div className="p-3 border-b border-gray-200 dark:border-gray-700 flex items-center justify-between bg-white dark:bg-gray-900 sticky top-0 z-10"> | |
| <h2 className="text-lg font-bold text-gray-800 dark:text-gray-100 flex items-center gap-2"> | |
| <Users className="w-5 h-5 text-primary" /> | |
| Contacts | |
| <span className="text-xs text-gray-400 font-normal"> | |
| ({sortedContacts.length}) | |
| </span> | |
| </h2> | |
| <div className="flex gap-1"> | |
| <button | |
| onClick={() => setViewMode(viewMode === "list" ? "grid" : "list")} | |
| className="p-1.5 hover:bg-gray-100 dark:hover:bg-gray-700 rounded-lg text-gray-500 transition" | |
| title={viewMode === "list" ? "Grid view" : "List view"} | |
| > | |
| {viewMode === "list" ? ( | |
| <svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"> | |
| <rect x="3" y="3" width="7" height="7" rx="1" /> | |
| <rect x="14" y="3" width="7" height="7" rx="1" /> | |
| <rect x="3" y="14" width="7" height="7" rx="1" /> | |
| <rect x="14" y="14" width="7" height="7" rx="1" /> | |
| </svg> | |
| ) : ( | |
| <svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"> | |
| <line x1="8" y1="6" x2="21" y2="6" /> | |
| <line x1="8" y1="12" x2="21" y2="12" /> | |
| <line x1="8" y1="18" x2="21" y2="18" /> | |
| <line x1="3" y1="6" x2="3.01" y2="6" /> | |
| <line x1="3" y1="12" x2="3.01" y2="12" /> | |
| <line x1="3" y1="18" x2="3.01" y2="18" /> | |
| </svg> | |
| )} | |
| </button> | |
| <button | |
| onClick={onAddContact || handleAddContactDefault} | |
| className="p-1.5 hover:bg-gray-100 dark:hover:bg-gray-700 rounded-lg transition" | |
| title="Add person" | |
| > | |
| <UserPlus className="w-5 h-5 text-gray-500" /> | |
| </button> | |
| <button | |
| onClick={onClose} | |
| className="p-1.5 hover:bg-gray-100 dark:hover:bg-gray-700 rounded-lg transition" | |
| > | |
| <X className="w-5 h-5 text-gray-500" /> | |
| </button> | |
| </div> | |
| </div> | |
| {/* Connection Status */} | |
| {connectionStatus !== "connected" && ( | |
| <div className="bg-yellow-500 text-white text-xs text-center py-1"> | |
| {connectionStatus === "connecting" ? "Connecting..." : "Reconnecting..."} | |
| </div> | |
| )} | |
| {/* Search Bar */} | |
| <div className="p-2 border-b border-gray-100 dark:border-gray-800"> | |
| <div className="relative"> | |
| <Search className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-gray-400" /> | |
| <input | |
| type="text" | |
| placeholder="Search by name, username, or email..." | |
| className="w-full pl-9 pr-3 py-2 text-sm bg-gray-100 dark:bg-gray-800 rounded-xl focus:outline-none focus:ring-2 focus:ring-primary transition" | |
| value={search} | |
| onChange={(e) => setSearch(e.target.value)} | |
| /> | |
| {search && ( | |
| <button | |
| onClick={() => setSearch("")} | |
| className="absolute right-3 top-1/2 -translate-y-1/2 text-gray-400 hover:text-gray-600" | |
| > | |
| <X className="w-4 h-4" /> | |
| </button> | |
| )} | |
| </div> | |
| </div> | |
| {/* Filter Tabs */} | |
| <div className="px-2 py-2 flex gap-1 overflow-x-auto border-b border-gray-100 dark:border-gray-800"> | |
| {filterTabs.map((tab) => ( | |
| <button | |
| key={tab.id} | |
| onClick={() => setActiveFilter(tab.id)} | |
| className={`px-3 py-1.5 rounded-full text-xs font-medium transition flex items-center gap-1 ${ | |
| activeFilter === tab.id | |
| ? "bg-primary text-white shadow-sm" | |
| : "bg-gray-100 dark:bg-gray-800 text-gray-600 dark:text-gray-400 hover:bg-gray-200 dark:hover:bg-gray-700" | |
| }`} | |
| > | |
| <tab.icon className="w-3 h-3" /> | |
| {tab.label} | |
| {tab.id === "all" && ( | |
| <span className="ml-1 text-xs opacity-75">{sortedContacts.length}</span> | |
| )} | |
| {tab.id === "online" && ( | |
| <span className="ml-1 text-xs opacity-75">{online.length}</span> | |
| )} | |
| {tab.id === "favorites" && ( | |
| <span className="ml-1 text-xs opacity-75">{favorites.size}</span> | |
| )} | |
| {tab.id === "blocked" && ( | |
| <span className="ml-1 text-xs opacity-75">{blocked.size}</span> | |
| )} | |
| </button> | |
| ))} | |
| </div> | |
| {/* Groups Section (Optional) */} | |
| {showGroups && groups.length > 0 && ( | |
| <div className="border-b border-gray-100 dark:border-gray-800"> | |
| <div className="px-3 py-2 text-xs font-semibold text-gray-500 uppercase flex items-center gap-2"> | |
| <Users className="w-3 h-3" /> | |
| Groups | |
| </div> | |
| {groups.map((group) => ( | |
| <button | |
| key={group.id} | |
| onClick={() => onGroupClick?.(group.id)} | |
| className="w-full px-3 py-2 flex items-center gap-3 hover:bg-gray-50 dark:hover:bg-gray-800 transition" | |
| > | |
| <div className="w-8 h-8 rounded-full bg-gradient-to-br from-purple-500 to-pink-500 text-white flex items-center justify-center text-sm font-bold"> | |
| {group.name[0]?.toUpperCase()} | |
| </div> | |
| <div className="flex-1 text-left"> | |
| <p className="text-sm font-medium truncate">{group.name}</p> | |
| <p className="text-xs text-gray-400">{group.memberCount} members</p> | |
| </div> | |
| <MessageCircle className="w-4 h-4 text-gray-400" /> | |
| </button> | |
| ))} | |
| </div> | |
| )} | |
| {/* Contacts List */} | |
| <div className="flex-1 overflow-y-auto"> | |
| {error ? ( | |
| <div className="p-8 text-center"> | |
| <div className="w-12 h-12 rounded-full bg-red-100 dark:bg-red-900/20 flex items-center justify-center mx-auto mb-3"> | |
| <X className="w-6 h-6 text-red-500" /> | |
| </div> | |
| <p className="text-red-500 text-sm">{error}</p> | |
| <button onClick={refetch} className="text-primary text-sm underline mt-2"> | |
| Try again | |
| </button> | |
| </div> | |
| ) : sortedContacts.length === 0 ? ( | |
| <div className="p-12 text-center"> | |
| <div className="w-16 h-16 rounded-full bg-gray-100 dark:bg-gray-800 flex items-center justify-center mx-auto mb-3"> | |
| <Users className="w-8 h-8 text-gray-400" /> | |
| </div> | |
| <p className="text-gray-500 text-sm mt-2"> | |
| {search | |
| ? "No contacts match your search." | |
| : activeFilter === "favorites" | |
| ? "No favorites yet. Star contacts you chat with often." | |
| : activeFilter === "blocked" | |
| ? "No blocked users." | |
| : "No contacts yet. Add someone to start a conversation."} | |
| </p> | |
| {!search && activeFilter === "all" && ( | |
| <button | |
| onClick={onAddContact || handleAddContactDefault} | |
| className="mt-4 text-primary text-sm hover:underline inline-flex items-center gap-1" | |
| > | |
| <UserPlus className="w-4 h-4" /> Add your first contact | |
| </button> | |
| )} | |
| </div> | |
| ) : viewMode === "grid" ? ( | |
| <div className="grid grid-cols-2 gap-2 p-3"> | |
| {sortedContacts.map((contact, index) => ( | |
| <motion.div | |
| key={contact.username} | |
| initial={{ opacity: 0, scale: 0.9 }} | |
| animate={{ opacity: 1, scale: 1 }} | |
| transition={{ delay: index * 0.02 }} | |
| onContextMenu={(e) => showContextMenu(e, getContactContextMenuItems(contact))} | |
| className="bg-gray-50 dark:bg-gray-800 rounded-xl p-3 text-center cursor-pointer hover:shadow-md transition" | |
| onClick={() => onShowProfile(contact.username)} | |
| > | |
| <div className="relative inline-block"> | |
| <div className="w-16 h-16 rounded-full bg-gradient-to-br from-primary to-secondary text-white flex items-center justify-center text-2xl font-bold mx-auto"> | |
| {(contact.username[0] || "?").toUpperCase()} | |
| </div> | |
| <div className="absolute -bottom-1 -right-1"> | |
| <OnlineStatusIndicator isOnline={onlineStatuses[contact.username]?.isOnline || contact.isOnline} /> | |
| </div> | |
| </div> | |
| <p className="font-medium text-sm mt-2 truncate">{contact.fullName || contact.username}</p> | |
| <p className="text-xs text-gray-400 truncate">@{contact.username}</p> | |
| <button | |
| onClick={(e) => { | |
| e.stopPropagation(); | |
| handleStartChat(contact.username); | |
| }} | |
| className="mt-2 w-full py-1.5 text-xs bg-primary/10 text-primary rounded-lg hover:bg-primary/20 transition" | |
| > | |
| Message | |
| </button> | |
| </motion.div> | |
| ))} | |
| </div> | |
| ) : ( | |
| <> | |
| {/* Favorites Section (if not filtering by favorites) */} | |
| {activeFilter !== "favorites" && favorites.size > 0 && ( | |
| <> | |
| <div className="px-3 pt-3 pb-1 text-xs font-semibold text-gray-500 uppercase flex items-center gap-2"> | |
| <Star className="w-3 h-3 text-yellow-500" /> | |
| Favorites — {favorites.size} | |
| </div> | |
| {sortedContacts.filter(c => favorites.has(c.username)).map((contact) => ( | |
| <ContactRow | |
| key={`fav-${contact.username}`} | |
| contact={contact} | |
| isFavorite={true} | |
| isOnline={onlineStatuses[contact.username]?.isOnline || contact.isOnline} | |
| statusText={getStatusText(contact)} | |
| onShowProfile={onShowProfile} | |
| onStartChat={handleStartChat} | |
| onToggleFavorite={handleToggleFavorite} | |
| onBlockUser={handleBlockUser} | |
| onCopyUsername={handleCopyUsername} | |
| onShareContact={handleShareContact} | |
| onContextMenu={(e) => showContextMenu(e, getContactContextMenuItems(contact))} | |
| /> | |
| ))} | |
| </> | |
| )} | |
| {/* Online Section */} | |
| {activeFilter !== "online" && online.length > 0 && activeFilter !== "favorites" && ( | |
| <> | |
| <div className="px-3 pt-3 pb-1 text-xs font-semibold text-gray-500 uppercase flex items-center gap-2"> | |
| <div className="w-2 h-2 bg-green-500 rounded-full animate-pulse"></div> | |
| Online — {online.length} | |
| </div> | |
| {online.map((contact) => ( | |
| <ContactRow | |
| key={`online-${contact.username}`} | |
| contact={contact} | |
| isFavorite={favorites.has(contact.username)} | |
| isOnline={true} | |
| statusText="Online" | |
| onShowProfile={onShowProfile} | |
| onStartChat={handleStartChat} | |
| onToggleFavorite={handleToggleFavorite} | |
| onBlockUser={handleBlockUser} | |
| onCopyUsername={handleCopyUsername} | |
| onShareContact={handleShareContact} | |
| onContextMenu={(e) => showContextMenu(e, getContactContextMenuItems(contact))} | |
| /> | |
| ))} | |
| </> | |
| )} | |
| {/* Offline Section */} | |
| {offline.length > 0 && activeFilter !== "online" && activeFilter !== "favorites" && ( | |
| <> | |
| <div className="px-3 pt-3 pb-1 text-xs font-semibold text-gray-500 uppercase flex items-center gap-2"> | |
| <Clock className="w-3 h-3" /> | |
| Offline — {offline.length} | |
| </div> | |
| {offline.map((contact) => ( | |
| <ContactRow | |
| key={`offline-${contact.username}`} | |
| contact={contact} | |
| isFavorite={favorites.has(contact.username)} | |
| isOnline={false} | |
| statusText={getStatusText(contact)} | |
| onShowProfile={onShowProfile} | |
| onStartChat={handleStartChat} | |
| onToggleFavorite={handleToggleFavorite} | |
| onBlockUser={handleBlockUser} | |
| onCopyUsername={handleCopyUsername} | |
| onShareContact={handleShareContact} | |
| onContextMenu={(e) => showContextMenu(e, getContactContextMenuItems(contact))} | |
| /> | |
| ))} | |
| </> | |
| )} | |
| </> | |
| )} | |
| </div> | |
| {/* Context Menu */} | |
| <ContextMenu | |
| items={contextMenu.items} | |
| position={contextMenu.position} | |
| onClose={hideContextMenu} | |
| /> | |
| {/* Contact Details Modal */} | |
| {showContactDetails && selectedContact && ( | |
| <ContactDetailsModal | |
| contact={selectedContact} | |
| isOnline={onlineStatuses[selectedContact.username]?.isOnline || selectedContact.isOnline} | |
| isFavorite={favorites.has(selectedContact.username)} | |
| onClose={() => setShowContactDetails(false)} | |
| onStartChat={() => handleStartChat(selectedContact.username)} | |
| onToggleFavorite={() => handleToggleFavorite(selectedContact.username)} | |
| onBlockUser={() => handleBlockUser(selectedContact.username)} | |
| onShowProfile={() => onShowProfile(selectedContact.username)} | |
| /> | |
| )} | |
| </div> | |
| ); | |
| } | |
| // ---- ContactRow Sub-component ---- | |
| interface ContactRowProps { | |
| contact: Contact; | |
| isFavorite: boolean; | |
| isOnline: boolean; | |
| statusText: string; | |
| onShowProfile: (username: string) => void; | |
| onStartChat: (username: string) => void; | |
| onToggleFavorite: (username: string) => void; | |
| onBlockUser: (username: string) => void; | |
| onCopyUsername: (username: string) => void; | |
| onShareContact: (contact: Contact) => void; | |
| onContextMenu?: (e: React.MouseEvent) => void; | |
| } | |
| function ContactRow({ | |
| contact, | |
| isFavorite, | |
| isOnline, | |
| statusText, | |
| onShowProfile, | |
| onStartChat, | |
| onToggleFavorite, | |
| onBlockUser, | |
| onCopyUsername, | |
| onShareContact, | |
| onContextMenu, | |
| }: ContactRowProps) { | |
| const [showActions, setShowActions] = useState(false); | |
| return ( | |
| <div | |
| className="px-3 py-2.5 flex items-center gap-3 hover:bg-gray-50 dark:hover:bg-gray-800/50 cursor-pointer group transition relative" | |
| onClick={() => onShowProfile(contact.username)} | |
| onContextMenu={onContextMenu} | |
| onMouseEnter={() => setShowActions(true)} | |
| onMouseLeave={() => setShowActions(false)} | |
| > | |
| {/* Avatar with online status */} | |
| <div className="relative flex-shrink-0"> | |
| <div className="w-10 h-10 rounded-full bg-gradient-to-br from-primary to-secondary text-white flex items-center justify-center font-bold shadow-md"> | |
| {(contact.username[0] || "?").toUpperCase()} | |
| </div> | |
| <div className="absolute -bottom-0.5 -right-0.5 border-2 border-white dark:border-gray-900 rounded-full"> | |
| <OnlineStatusIndicator isOnline={isOnline} /> | |
| </div> | |
| </div> | |
| {/* Contact Info */} | |
| <div className="flex-1 min-w-0"> | |
| <div className="flex items-center gap-1"> | |
| <p className="font-medium text-gray-800 dark:text-gray-200 truncate"> | |
| {contact.fullName || contact.username} | |
| </p> | |
| {isFavorite && ( | |
| <Star className="w-3 h-3 text-yellow-500 fill-yellow-500 flex-shrink-0" /> | |
| )} | |
| </div> | |
| <p className="text-xs text-gray-400 truncate">@{contact.username}</p> | |
| <p className="text-xs text-gray-500 mt-0.5">{statusText}</p> | |
| </div> | |
| {/* Action Buttons (appear on hover) */} | |
| <div className={`flex gap-1 transition-opacity ${showActions ? "opacity-100" : "opacity-0"} flex-shrink-0`}> | |
| <button | |
| onClick={(e) => { | |
| e.stopPropagation(); | |
| onToggleFavorite(contact.username); | |
| }} | |
| className="p-1.5 rounded-lg hover:bg-gray-200 dark:hover:bg-gray-700 transition" | |
| title={isFavorite ? "Remove from favorites" : "Add to favorites"} | |
| > | |
| <Star className={`w-4 h-4 ${isFavorite ? "text-yellow-500 fill-yellow-500" : "text-gray-400"}`} /> | |
| </button> | |
| <button | |
| onClick={(e) => { | |
| e.stopPropagation(); | |
| onStartChat(contact.username); | |
| }} | |
| className="p-1.5 rounded-lg bg-primary/10 text-primary hover:bg-primary/20 transition" | |
| title="Send message" | |
| > | |
| <MessageCircle className="w-4 h-4" /> | |
| </button> | |
| </div> | |
| </div> | |
| ); | |
| } | |
| // ---- Contact Details Modal ---- | |
| function ContactDetailsModal({ | |
| contact, | |
| isOnline, | |
| isFavorite, | |
| onClose, | |
| onStartChat, | |
| onToggleFavorite, | |
| onBlockUser, | |
| onShowProfile, | |
| }: any) { | |
| return ( | |
| <div className="fixed inset-0 z-50 flex items-center justify-center bg-black/50" onClick={onClose}> | |
| <div className="bg-white dark:bg-gray-800 rounded-xl shadow-xl max-w-md w-full mx-4 overflow-hidden" onClick={(e) => e.stopPropagation()}> | |
| <div className="bg-gradient-to-r from-primary to-secondary p-6 text-center"> | |
| <div className="w-20 h-20 rounded-full bg-white/20 backdrop-blur text-white flex items-center justify-center text-4xl font-bold mx-auto"> | |
| {(contact.username[0] || "?").toUpperCase()} | |
| </div> | |
| <h2 className="text-xl font-bold text-white mt-3">{contact.fullName || contact.username}</h2> | |
| <p className="text-white/80 text-sm">@{contact.username}</p> | |
| <div className="inline-flex items-center gap-1 mt-2 px-2 py-1 bg-white/20 rounded-full text-xs text-white"> | |
| <OnlineStatusIndicator isOnline={isOnline} /> | |
| <span>{isOnline ? "Online" : "Offline"}</span> | |
| </div> | |
| </div> | |
| <div className="p-4 space-y-3"> | |
| {contact.email && ( | |
| <div className="flex items-center gap-2 text-sm"> | |
| <Mail className="w-4 h-4 text-gray-400" /> | |
| <span>{contact.email}</span> | |
| </div> | |
| )} | |
| {contact.bio && ( | |
| <div className="text-sm text-gray-600 dark:text-gray-400"> | |
| {contact.bio} | |
| </div> | |
| )} | |
| <div className="flex gap-2 pt-2"> | |
| <button | |
| onClick={() => { onStartChat(); onClose(); }} | |
| className="flex-1 py-2 bg-primary text-white rounded-lg hover:bg-primary/90 transition" | |
| > | |
| Send Message | |
| </button> | |
| <button | |
| onClick={() => { onShowProfile(); onClose(); }} | |
| className="flex-1 py-2 border border-gray-300 dark:border-gray-600 rounded-lg hover:bg-gray-50 dark:hover:bg-gray-700 transition" | |
| > | |
| View Profile | |
| </button> | |
| </div> | |
| <div className="flex gap-2"> | |
| <button | |
| onClick={() => { onToggleFavorite(); }} | |
| className={`flex-1 py-2 rounded-lg transition flex items-center justify-center gap-2 ${ | |
| isFavorite | |
| ? "bg-yellow-100 text-yellow-700 dark:bg-yellow-900/20 dark:text-yellow-400" | |
| : "bg-gray-100 text-gray-700 dark:bg-gray-700 dark:text-gray-300 hover:bg-gray-200" | |
| }`} | |
| > | |
| <Star className={`w-4 h-4 ${isFavorite ? "fill-yellow-500" : ""}`} /> | |
| {isFavorite ? "Favorited" : "Add to Favorites"} | |
| </button> | |
| <button | |
| onClick={() => { onBlockUser(); onClose(); }} | |
| className="py-2 px-4 rounded-lg bg-red-50 text-red-600 hover:bg-red-100 transition" | |
| > | |
| <Ban className="w-4 h-4" /> | |
| </button> | |
| </div> | |
| </div> | |
| </div> | |
| </div> | |
| ); | |
| } | |
Xet Storage Details
- Size:
- 32.6 kB
- Xet hash:
- d6b4f66b70a72adaad1d7b098cae605d3348319ddaa91850cd775332c6744cf7
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.