"use client"; import React, { useEffect, useState, useMemo } from 'react'; import { X, Folder, FolderOpen, ChevronDown, ArrowRight, LayoutGrid } from 'lucide-react'; import { motion, AnimatePresence } from 'framer-motion'; import Link from 'next/link'; import { catalogService, CategoryResponse } from '@/services/api/catalogService'; interface MobileDrawerProps { isOpen: boolean; onClose: () => void; } export const MobileDrawer: React.FC = ({ isOpen, onClose }) => { const [categories, setCategories] = useState([]); const [expandedCats, setExpandedCats] = useState>({}); useEffect(() => { if (isOpen) { catalogService.getCategories().then((res) => { if (Array.isArray(res)) setCategories(res); }); } }, [isOpen]); const toggleCategory = (catId: string) => { setExpandedCats((prev) => ({ ...prev, [catId]: !prev[catId], })); }; const parentCategories = useMemo(() => { const parents = categories.filter((c) => !c.parent_category_id); return parents.map((parent) => { const nestedSubs = parent.sub_categories || []; const flatSubs = categories.filter((c) => c.parent_category_id === parent.category_id); const subCategories = nestedSubs.length > 0 ? nestedSubs : flatSubs; return { ...parent, subCategories, }; }); }, [categories]); React.useEffect(() => { if (!isOpen) return; const handleKeyDown = (e: KeyboardEvent) => { if (e.key === 'Escape') onClose(); }; document.addEventListener('keydown', handleKeyDown); return () => document.removeEventListener('keydown', handleKeyDown); }, [isOpen, onClose]); // Lock background scroll while the drawer is open (same pattern as CartDrawer), // and restore the previous value on close. React.useEffect(() => { if (!isOpen) return; const prev = document.body.style.overflow; document.body.style.overflow = 'hidden'; return () => { document.body.style.overflow = prev; }; }, [isOpen]); const links = [ { name: 'Home', href: '/' }, { name: 'Shop', href: '/shop' }, { name: 'Top Deals', href: '/shop?tag=deals' }, { name: 'Services', href: '/services' }, ]; return ( {isOpen && ( <> {/* Backdrop */} {/* Drawer Panel */}
{/* Header */}
iFixKart
{/* Navigation Links */}

Navigation

    {links.map((link) => (
  • {link.name}
  • ))}
{/* Categories */} {parentCategories.length > 0 && (

Categories

    {parentCategories.map((cat) => { const hasSubs = cat.subCategories && cat.subCategories.length > 0; const isExpanded = !!expandedCats[cat.category_id]; return (
  • {hasSubs ? (
    {/* Subcategories Accordion */} {isExpanded && ( {/* View All Parent Category */}
  • All {cat.name}
  • {/* Subcategories */} {cat.subCategories.map((sub) => (
  • {sub.name}
  • ))} )}
) : ( {cat.name} )} ); })}
)} {/* Footer — honors iOS home indicator / Android gesture bar */}
© {new Date().getFullYear()} iFixKart. All Rights Reserved.
)}
); };