All files / src/components/layout/MobileHeader MobileMenu.tsx

0% Statements 0/246
100% Branches 0/0
0% Functions 0/1
0% Lines 0/246

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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
'use client';

/**
 * MobileMenu Component
 *
 * A slide-out navigation menu for mobile devices.
 */

import { useEffect, useRef } from 'react';
import Link from 'next/link';
import { usePathname } from 'next/navigation';
import { useSession, signOut } from 'next-auth/react';
import { Icon, IconName } from '@/components/ui/icons';
import { cn } from '@/lib/core';
import ThemeToggle from '@/components/shared/ThemeToggle';

interface MobileMenuProps {
  isOpen: boolean;
  onClose: () => void;
}

interface MenuItem {
  href: string;
  label: string;
  icon: IconName;
  requiresAuth?: boolean;
  adminOnly?: boolean;
}

const menuItems: MenuItem[] = [
  { href: '/', label: 'Home', icon: 'home' },
  { href: '/shop', label: 'Shop', icon: 'grid' },
  { href: '/categories', label: 'Categories', icon: 'layers' },
  { href: '/deals', label: 'Deals', icon: 'tag' },
];

const accountItems: MenuItem[] = [
  { href: '/my-account', label: 'My Account', icon: 'user', requiresAuth: true },
  { href: '/account/orders', label: 'My Orders', icon: 'package', requiresAuth: true },
  { href: '/wishlist', label: 'Wishlist', icon: 'heart' },
  { href: '/my-account?tab=loyalty', label: 'Rewards', icon: 'award', requiresAuth: true },
];

const adminItems: MenuItem[] = [
  { href: '/admin', label: 'Admin Dashboard', icon: 'dashboard', adminOnly: true },
  { href: '/admin/orders', label: 'Manage Orders', icon: 'clipboard-list', adminOnly: true },
  { href: '/admin/products', label: 'Manage Products', icon: 'cube', adminOnly: true },
];

export function MobileMenu({ isOpen, onClose }: MobileMenuProps) {
  const pathname = usePathname();
  const { data: session } = useSession();
  const menuRef = useRef<HTMLDivElement>(null);

  const isAdmin = session?.user?.role === 'ADMIN';

  // Handle escape key and click outside
  useEffect(() => {
    const handleEscape = (e: KeyboardEvent) => {
      if (e.key === 'Escape' && isOpen) {
        onClose();
      }
    };

    const handleClickOutside = (e: MouseEvent) => {
      if (menuRef.current && !menuRef.current.contains(e.target as Node)) {
        onClose();
      }
    };

    if (isOpen) {
      document.addEventListener('keydown', handleEscape);
      document.addEventListener('mousedown', handleClickOutside);
      document.body.style.overflow = 'hidden';
    }

    return () => {
      document.removeEventListener('keydown', handleEscape);
      document.removeEventListener('mousedown', handleClickOutside);
      document.body.style.overflow = '';
    };
  }, [isOpen, onClose]);

  const handleSignOut = async () => {
    await signOut({ callbackUrl: '/' });
    onClose();
  };

  const renderMenuItem = (item: MenuItem) => {
    const isActive = pathname === item.href;

    if (item.requiresAuth && !session) return null;
    if (item.adminOnly && !isAdmin) return null;

    return (
      <Link
        key={item.href}
        href={item.href}
        onClick={onClose}
        className={cn(
          'flex items-center gap-4 px-4 py-3',
          'min-h-[48px] rounded-lg',
          'transition-colors',
          isActive
            ? 'bg-primary-50 dark:bg-primary-900/20 text-primary-600 dark:text-primary-400'
            : 'text-gray-700 dark:text-gray-300 hover:bg-gray-100 dark:hover:bg-gray-800'
        )}
      >
        <Icon name={item.icon} size={24} />
        <span className="font-medium">{item.label}</span>
        {isActive && (
          <Icon name="chevron-right" size={20} className="ml-auto opacity-50" />
        )}
      </Link>
    );
  };

  return (
    <>
      {/* Backdrop */}
      <div
        className={cn(
          'fixed inset-0 z-40 bg-black/50 transition-opacity duration-300',
          isOpen ? 'opacity-100' : 'opacity-0 pointer-events-none'
        )}
        aria-hidden="true"
      />

      {/* Menu panel */}
      <div
        ref={menuRef}
        className={cn(
          'fixed inset-y-0 left-0 z-50 w-[85%] max-w-sm',
          'bg-white dark:bg-gray-900',
          'transform transition-transform duration-300 ease-out',
          'flex flex-col',
          isOpen ? 'translate-x-0' : '-translate-x-full'
        )}
        role="dialog"
        aria-modal="true"
        aria-label="Navigation menu"
      >
        {/* Header */}
        <div className="flex items-center justify-between px-4 py-4 border-b border-gray-200 dark:border-gray-800">
          <div className="flex items-center gap-3">
            {session ? (
              <>
                <div className="w-10 h-10 rounded-full bg-primary-100 dark:bg-primary-900/30 flex items-center justify-center">
                  <Icon name="user" size={24} className="text-primary-600 dark:text-primary-400" />
                </div>
                <div>
                  <p className="font-medium text-gray-900 dark:text-white">
                    {session.user?.name || 'User'}
                  </p>
                  <p className="text-sm text-gray-500 dark:text-gray-400 truncate max-w-[150px]">
                    {session.user?.email}
                  </p>
                </div>
              </>
            ) : (
              <Link
                href="/signin"
                onClick={onClose}
                className="flex items-center gap-3"
              >
                <div className="w-10 h-10 rounded-full bg-gray-100 dark:bg-gray-800 flex items-center justify-center">
                  <Icon name="user" size={24} className="text-gray-500" />
                </div>
                <span className="font-medium text-gray-900 dark:text-white">
                  Sign In
                </span>
              </Link>
            )}
          </div>
          <button
            onClick={onClose}
            className="p-2 min-h-[44px] min-w-[44px] rounded-lg hover:bg-gray-100 dark:hover:bg-gray-800 transition-colors"
            aria-label="Close menu"
          >
            <Icon name="close" size={24} />
          </button>
        </div>

        {/* Menu content */}
        <nav className="flex-1 overflow-y-auto py-4 px-2">
          {/* Main navigation */}
          <div className="space-y-1 mb-6">
            {menuItems.map(renderMenuItem)}
          </div>

          {/* Account section */}
          <div className="border-t border-gray-200 dark:border-gray-800 pt-4 mb-6">
            <p className="px-4 pb-2 text-xs font-medium text-gray-400 uppercase tracking-wider">
              Account
            </p>
            <div className="space-y-1">
              {accountItems.map(renderMenuItem)}
            </div>
          </div>

          {/* Admin section */}
          {isAdmin && (
            <div className="border-t border-gray-200 dark:border-gray-800 pt-4 mb-6">
              <p className="px-4 pb-2 text-xs font-medium text-gray-400 uppercase tracking-wider">
                Admin
              </p>
              <div className="space-y-1">
                {adminItems.map(renderMenuItem)}
              </div>
            </div>
          )}
        </nav>

        {/* Footer */}
        <div className="border-t border-gray-200 dark:border-gray-800 p-4 space-y-3">
          {/* Theme toggle */}
          <div className="flex items-center justify-between px-4 py-2">
            <span className="text-gray-700 dark:text-gray-300 font-medium">
              Dark Mode
            </span>
            <ThemeToggle size="md" />
          </div>

          {/* Sign out */}
          {session && (
            <button
              onClick={handleSignOut}
              className={cn(
                'flex items-center gap-4 w-full px-4 py-3',
                'min-h-[48px] rounded-lg',
                'text-red-600 dark:text-red-400',
                'hover:bg-red-50 dark:hover:bg-red-900/20',
                'transition-colors'
              )}
            >
              <Icon name="logout" size={24} />
              <span className="font-medium">Sign Out</span>
            </button>
          )}
        </div>
      </div>
    </>
  );
}

export default MobileMenu;