All files / src/components sidebar.tsx

92.1% Statements 35/38
83.87% Branches 26/31
70% Functions 7/10
91.89% Lines 34/37

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200                                                                    3x                                                                     3x 28x 28x 28x 28x 28x 28x   28x 2x 2x     28x         28x 2x 2x 1x   1x   1x         28x 1x     27x           162x   162x 162x     54x 54x 27x 27x 27x     54x   162x   162x     1x                                                                           1x                                                                
import type { ReactNode } from 'react';
import { useNavigate, useLocation } from 'react-router-dom';
import {
  Eye,
  MessagesSquare,
  Wallet,
  ArrowLeftRight,
  BarChart3,
  Settings,
  UserCheck,
  LogOut,
} from 'lucide-react';
import { Badge } from '@/components/badge';
import { cn } from '@/lib/utils';
import { useLogout } from '@/features/auth/hooks/use-auth';
import { useNotificationCounts } from '@/features/notification/hooks/use-notification-counts';
import { useUIStore } from '@/store/ui-store';
import { useNotifications } from '@/hooks/use-store-sync';
import { prefetchOnInteraction } from '@/lib/route-prefetcher';
import {
  DASHBOARD_ROUTES,
  WALLET_ROUTES,
  STATS_ROUTES,
  PROFILE_ROUTES,
  AUTH_ROUTES,
} from '@/constants/route';
 
interface SidebarItem {
  icon: ReactNode;
  label: string;
  badgeType?: 'trade' | 'transaction';
  path: string;
}
 
const sidebarItems: SidebarItem[] = [
  {
    icon: <Eye className="w-5 h-5" />,
    label: 'Overview',
    path: DASHBOARD_ROUTES.OVERVIEW,
  },
  {
    icon: <MessagesSquare className="w-5 h-5" />,
    label: 'Trade',
    badgeType: 'trade',
    path: '/trade',
  },
  {
    icon: <Wallet className="w-5 h-5" />,
    label: 'Wallet',
    path: WALLET_ROUTES.WALLET,
  },
  {
    icon: <ArrowLeftRight className="w-5 h-5" />,
    label: 'Transactions',
    badgeType: 'transaction',
    path: WALLET_ROUTES.TRANSACTIONS,
  },
  {
    icon: <BarChart3 className="w-5 h-5" />,
    label: 'Statistics',
    path: STATS_ROUTES.STATISTICS,
  },
  {
    icon: <Settings className="w-5 h-5" />,
    label: 'Settings',
    path: PROFILE_ROUTES.SETTINGS,
  },
];
 
export const Sidebar = () => {
  const navigate = useNavigate();
  const location = useLocation();
  const { isSidebarOpen, setCurrentPage } = useUIStore();
  const { counts } = useNotifications();
  const { counts: notificationCounts, loading } = useNotificationCounts();
  const logoutMutation = useLogout();
 
  const handleItemClick = (path: string) => {
    setCurrentPage(path);
    navigate(path);
  };
 
  const handleItemHover = (path: string) => {
    // Prefetch the route on hover for faster navigation
    prefetchOnInteraction(path);
  };
 
  const handleLogout = async () => {
    try {
      await logoutMutation.mutateAsync();
      navigate(AUTH_ROUTES.LOGIN);
    } catch (error) {
      console.error('Logout failed:', error);
      // Navigation is still performed even if logout fails
      navigate(AUTH_ROUTES.LOGIN);
    }
  };
 
  // Don't render if collapsed (for responsive design)
  if (!isSidebarOpen) {
    return null;
  }
 
  return (
    <aside className="w-[290px] pr-4 bg-background border-r-2 border-border h-full flex flex-col">
      {/* Main Navigation */}
      <nav className="flex-1 py-6">
        <div className="space-y-2">
          {sidebarItems.map((item, index) => {
            const isActive = location.pathname === item.path;
            // Get badge count based on badgeType
            const getBadgeCount = () => {
              if (!item.badgeType) return null;
 
              // Map badgeType to notification counts
              let count = 0;
              if (item.badgeType === 'trade') {
                count = counts.trade || notificationCounts?.trade || 0;
              } else Eif (item.badgeType === 'transaction') {
                count = counts.wallet || notificationCounts?.transaction || 0;
              }
 
              return count > 0 ? count.toString() : null;
            };
            const badgeValue = getBadgeCount();
 
            return (
              <button
                key={index}
                onClick={() => handleItemClick(item.path)}
                onMouseEnter={() => handleItemHover(item.path)}
                onFocus={() => handleItemHover(item.path)}
                className={cn(
                  'group w-full h-[54px] pl-[55px] rounded-l-lg rounded-r-[100px] cursor-pointer transition-all duration-200 flex items-center justify-between',
                  isActive
                    ? 'bg-gradient-vertical text-white shadow-lg'
                    : 'text-foreground hover:bg-gradient-vertical hover:text-white'
                )}
                aria-current={isActive ? 'page' : undefined}
              >
                <div className="flex items-center">
                  {item.icon}
                  <span className="ml-3 font-medium">{item.label}</span>
                </div>
                {badgeValue && !loading && (
                  <Badge
                    variant={isActive ? 'secondary' : 'default'}
                    className={cn(
                      'w-[24px] h-[24px] px-[unset] py-[unset] mr-[21px] transition-all duration-200 text-xs text-white flex items-center justify-center',
                      isActive
                        ? 'text-foreground'
                        : 'bg-gradient-vertical group-hover:[background:white] group-hover:text-foreground'
                    )}
                  >
                    {badgeValue}
                  </Badge>
                )}
              </button>
            );
          })}
        </div>
      </nav>
 
      {/* Bottom Section */}
      <div className="border-t border-border">
        <div className="space-y-2">
          <button
            onClick={() => handleItemClick('/referral')}
            className={cn(
              'w-full h-12 pl-[55px] rounded-l-lg rounded-r-[100px] cursor-pointer transition-all duration-200 flex items-center justify-between focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2',
              'text-foreground hover:text-white hover:bg-gradient-vertical hover:shadow-sm'
            )}
          >
            <div className="flex items-center">
              <UserCheck className="w-5 h-5" />
              <span className="ml-3 font-medium">Referral</span>
            </div>
          </button>
          <button
            onClick={handleLogout}
            disabled={logoutMutation.isPending}
            className={cn(
              'w-full h-12 pl-[55px] rounded-l-lg rounded-r-[100px] cursor-pointer transition-all duration-200 flex items-center justify-between focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2',
              'text-foreground hover:text-white hover:bg-gradient-vertical hover:shadow-sm',
              logoutMutation.isPending && 'opacity-50 cursor-not-allowed'
            )}
          >
            <div className="flex items-center">
              <LogOut className="w-5 h-5" />
              <span className="ml-3 font-medium">
                {logoutMutation.isPending ? 'Logging out...' : 'Logout'}
              </span>
            </div>
          </button>
        </div>
      </div>
    </aside>
  );
};