import { useState, useEffect, useRef } from 'react'; import { useAuth } from '../contexts/AuthContext.jsx'; import { useSocket } from '../contexts/SocketContext.jsx'; import { api, parseTS } from '../utils/api.js'; import { useToast } from '../contexts/ToastContext.jsx'; import Avatar from './Avatar.jsx'; import './Sidebar.css'; function useTheme() { const [dark, setDark] = useState(() => localStorage.getItem('jama-theme') === 'dark'); useEffect(() => { document.documentElement.setAttribute('data-theme', dark ? 'dark' : 'light'); localStorage.setItem('jama-theme', dark ? 'dark' : 'light'); }, [dark]); return [dark, setDark]; } function useAppSettings() { const [settings, setSettings] = useState({ app_name: 'jama', logo_url: '' }); const fetchSettings = () => { api.getSettings().then(({ settings }) => setSettings(settings)).catch(() => {}); }; useEffect(() => { fetchSettings(); window.addEventListener('jama:settings-changed', fetchSettings); return () => window.removeEventListener('jama:settings-changed', fetchSettings); }, []); useEffect(() => { const name = settings.app_name || 'jama'; document.title = name; const logoUrl = settings.logo_url; const faviconUrl = logoUrl || '/icons/jama.png'; let link = document.querySelector("link[rel~='icon']"); if (!link) { link = document.createElement('link'); link.rel = 'icon'; document.head.appendChild(link); } link.href = faviconUrl; }, [settings]); return settings; } export default function Sidebar({ groups, activeGroupId, onSelectGroup, notifications, unreadGroups = new Map(), onNewChat, onProfile, onUsers, onSettings: onOpenSettings, onGroupsUpdated, isMobile, onAbout }) { const { user, logout } = useAuth(); const { connected } = useSocket(); const toast = useToast(); const [showMenu, setShowMenu] = useState(false); const settings = useAppSettings(); const [dark, setDark] = useTheme(); const menuRef = useRef(null); const footerBtnRef = useRef(null); // Fix 6: swipe right to go back on mobile — handled in ChatWindow, but prevent sidebar swipe exit // Close menu on click outside useEffect(() => { if (!showMenu) return; const handler = (e) => { if (menuRef.current && !menuRef.current.contains(e.target) && footerBtnRef.current && !footerBtnRef.current.contains(e.target)) { setShowMenu(false); } }; document.addEventListener('mousedown', handler); document.addEventListener('touchstart', handler); return () => { document.removeEventListener('mousedown', handler); document.removeEventListener('touchstart', handler); }; }, [showMenu]); const appName = settings.app_name || 'jama'; const logoUrl = settings.logo_url; const allGroups = [ ...(groups.publicGroups || []), ...(groups.privateGroups || []) ]; const publicFiltered = allGroups.filter(g => g.type === 'public'); const privateFiltered = allGroups.filter(g => g.type === 'private'); const getNotifCount = (groupId) => notifications.filter(n => n.groupId === groupId).length; const handleLogout = async () => { await logout(); }; const GroupItem = ({ group }) => { const notifs = getNotifCount(group.id); const unreadCount = unreadGroups.get(group.id) || 0; const hasUnread = unreadCount > 0; const isActive = group.id === activeGroupId; return (
onSelectGroup(group.id)}>
{group.type === 'public' ? '#' : group.name[0]?.toUpperCase()}
{group.name} {group.last_message_at && ( {formatTime(group.last_message_at)} )}
{(group.last_message || '').replace(/@\[([^\]]+)\]/g, '@$1') || (group.is_readonly ? '📢 Read-only' : 'No messages yet')} {notifs > 0 && {notifs}} {hasUnread && notifs === 0 && {unreadCount}}
); }; return (
{/* New Chat button replacing search bar */}
{!isMobile && ( )}
{/* Groups list */}
{publicFiltered.length > 0 && (
PUBLIC MESSAGES
{publicFiltered.map(g => )}
)} {privateFiltered.length > 0 && (
DIRECT MESSAGES
{privateFiltered.map(g => )}
)} {allGroups.length === 0 && (
No chats yet
)}
{/* Mobile FAB: New Chat button floats above user footer */} {isMobile && ( )} {/* User footer */}
{showMenu && (
{user?.role === 'admin' && ( <> )}

)}
); } function formatTime(dateStr) { if (!dateStr) return ''; const date = parseTS(dateStr); const now = new Date(); const diff = now - date; if (diff < 86400000 && date.getDate() === now.getDate()) { return date.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' }); } if (diff < 604800000) { return date.toLocaleDateString([], { weekday: 'short' }); } return date.toLocaleDateString([], { month: 'short', day: 'numeric' }); }