import React, { useEffect, useLayoutEffect, useRef, useState } from 'react'; import { createPortal } from 'react-dom'; import { Link, useLocation, useNavigate } from 'react-router-dom'; import { BooksMockIcon, ChatsMockIcon, ChevronDownMockIcon, CompassMockIcon, FolderMockIcon, GearMockIcon, MoonMockIcon, PathMockIcon, SearchMockIcon, SignOutMockIcon, SunMockIcon, TeacherMockIcon, TrayMockIcon, UserMockIcon, VideoMockIcon, } from '../ui/MockupIcons'; import { BellIcon, SidebarSimpleIcon } from '@phosphor-icons/react'; import { Logo } from '../Logo'; import { Avatar } from '../ui/Avatar'; import { useAuth } from '../../context/AuthContext'; import { useTheme } from '../../context/ThemeContext'; import { useCatalog } from '../../context/CatalogContext'; import { NavKey, navKeyFor } from '../../lib/navigation'; import { paths } from '../../lib/routes'; import { readPreference, writePreference } from '../../lib/storage'; import { getNotifications } from '../../services/notifications'; const GROUPS_KEY = 'ca-groups'; const COLLAPSED_KEY = 'ca-side'; type Item = { key: NavKey; to: string; label: string; icon: React.ReactNode; count?: number }; const readGroups = (): Record => { try { return JSON.parse(readPreference(GROUPS_KEY) || '{}'); } catch { return {}; } }; const SideItem: React.FC<{ item: Item; current: NavKey }> = ({ item, current }) => ( {item.icon} {item.label} {item.count ? {item.count} : null} ); export const Sidebar: React.FC = () => { const location = useLocation(); const navigate = useNavigate(); const { user, logout } = useAuth(); const { theme, toggleTheme } = useTheme(); const { courses } = useCatalog(); const current = navKeyFor(location.pathname); const [shut, setShut] = useState>(readGroups); const [collapsed, setCollapsed] = useState(() => readPreference(COLLAPSED_KEY) === '1'); const [menuOpen, setMenuOpen] = useState(false); const [unreadNotifications, setUnreadNotifications] = useState(0); const whoRef = useRef(null); const menuRef = useRef(null); const [menuStyle, setMenuStyle] = useState({}); const canTeach = user?.role === 'professor' || user?.role === 'superadmin'; const inProgress = user ? courses.filter((course) => (course.progress || 0) > 0 && (course.progress || 0) < 100).length : 0; useEffect(() => { document.documentElement.classList.toggle('collapsed', collapsed); }, [collapsed]); useEffect(() => setMenuOpen(false), [location.pathname]); useEffect(() => { if (!user) { setUnreadNotifications(0); return; } let cancelled = false; const refresh = () => { getNotifications().then((feed) => { if (!cancelled) setUnreadNotifications(feed.unread); }).catch(() => undefined); }; refresh(); window.addEventListener('compor:notifications-updated', refresh); return () => { cancelled = true; window.removeEventListener('compor:notifications-updated', refresh); }; }, [user?.id, location.pathname]); // The account menu lives on with position: fixed, so the sidebar never clips it. useLayoutEffect(() => { if (!menuOpen || !whoRef.current) return; const button = whoRef.current.getBoundingClientRect(); const sidebar = whoRef.current.closest('.sidebar')?.getBoundingClientRect(); setMenuStyle(collapsed && sidebar ? { left: sidebar.right + 10, bottom: Math.max(12, window.innerHeight - button.bottom) } : { left: Math.max(12, button.left), bottom: window.innerHeight - button.top + 8 }); }, [collapsed, menuOpen]); useEffect(() => { if (!menuOpen) return; const close = (event: MouseEvent) => { const target = event.target as Node; if (!menuRef.current?.contains(target) && !whoRef.current?.contains(target)) setMenuOpen(false); }; const onKey = (event: KeyboardEvent) => { if (event.key === 'Escape') setMenuOpen(false); }; const onResize = () => setMenuOpen(false); document.addEventListener('mousedown', close); document.addEventListener('keydown', onKey); window.addEventListener('resize', onResize); return () => { document.removeEventListener('mousedown', close); document.removeEventListener('keydown', onKey); window.removeEventListener('resize', onResize); }; }, [menuOpen]); const toggleGroup = (id: string) => setShut((state) => { const next = { ...state, [id]: !state[id] }; writePreference(GROUPS_KEY, JSON.stringify(next)); return next; }); const toggleCollapsed = () => setCollapsed((value) => { writePreference(COLLAPSED_KEY, value ? '0' : '1'); return !value; }); const signOut = () => { setMenuOpen(false); logout(); navigate(paths.explore); }; const group = (id: string, title: string, items: Array) => { const visible = items.filter(Boolean) as Item[]; if (!visible.length) return null; return (
{visible.map((item) => )}
); }; const themeButton = ( ); return ( ); };