import { FC, useEffect } from 'react'; import { Link, useLocation } from 'react-router'; import { useMyTeams, useAuthStore, supabase, } from '@imphnen-frontend-service/service'; import { useNavigate } from 'react-router'; import { toast } from 'sonner'; import { useTheme } from './theme-provider'; import { Icon } from '@iconify/react'; interface NavItem { name: string; path: string; icon: React.ReactNode; show: boolean; } interface SidebarProps { isOpen?: boolean; onClose?: () => void; } export const Sidebar: FC = ({ isOpen = true, onClose }) => { const location = useLocation(); const navigate = useNavigate(); const { session, clearSession } = useAuthStore(); const { data: teamsData } = useMyTeams(); const { theme, setTheme, resolvedTheme } = useTheme(); const user = session?.user; const myTeams = teamsData?.data || []; const hasTeam = myTeams.length > 0; // Close sidebar on route change (mobile) useEffect(() => { if (onClose) { onClose(); } }, [location.pathname]); const handleLogout = async () => { try { await supabase.auth.signOut(); clearSession(); localStorage.clear(); toast.success('Logged out successfully'); navigate('/auth/login'); } catch (error) { console.error('Logout error:', error); clearSession(); localStorage.clear(); navigate('/auth/login'); } }; const navItems: NavItem[] = [ { name: 'Dashboard', path: '/dashboard', icon: , show: true, }, { name: 'My Teams', path: '/teams/' + myTeams[0]?.id, icon: , show: myTeams.length > 0, }, { name: 'Browse Teams', path: '/teams/browse', icon: , show: true, }, { name: 'Create Team', path: '/teams/create', icon: , show: !hasTeam, }, { name: 'Edit Profile', path: '/profile', icon: ( ), show: false, }, ]; const cycleTheme = () => { if (theme === 'light') setTheme('dark'); else if (theme === 'dark') setTheme('system'); else setTheme('light'); }; const getThemeIcon = () => { if (theme === 'system') { return ( ); } if (resolvedTheme === 'dark') { return ( ); } return ( ); }; const getThemeLabel = () => { if (theme === 'system') return 'System'; if (theme === 'dark') return 'Dark'; return 'Light'; }; const sidebarContent = (
{/* Logo / Brand with Close Button */}

Hackathon

{onClose && ( )}
{/* User Info */}
{user?.avatar ? ( {user.fullname ) : (
)}

{user?.fullname || user?.email?.split('@')[0] || 'User'}

{user?.email}

{/* Navigation */} {/* Theme Toggle & Logout */}
); return ( <> {/* Desktop Sidebar - Always visible on lg+, sticky position */}
{sidebarContent}
{/* Mobile Sidebar - Overlay */} {isOpen && (
{/* Backdrop */}
{/* Sidebar */}
{sidebarContent}
)} ); }; export default Sidebar;