'use client' import { useState, useEffect } from 'react' import Link from 'next/link' import { usePathname } from 'next/navigation' import { Home, ShoppingBag, ShoppingCart, User, MoreHorizontal } from 'lucide-react' import { cn } from '@/lib/utils' import { Badge } from '@/components/ui/badge' import CartSidebar from '@/components/shop/CartSidebar' import { cartManager } from '@/lib/cart' const tabs = [ { id: 'home', label: 'Home', icon: Home, href: '/', activeRoutes: ['/'] }, { id: 'products', label: 'Products', icon: ShoppingBag, href: '/products', activeRoutes: ['/products', '/products/[slug]'] }, { id: 'cart', label: 'Cart', icon: ShoppingCart, href: '/cart', activeRoutes: ['/cart', '/checkout'] }, { id: 'account', label: 'Account', icon: User, href: '/dashboard', activeRoutes: ['/dashboard'] }, // { // id: 'more', // label: 'More', // icon: MoreHorizontal, // href: '/more', // activeRoutes: ['/more', '/about', '/contact', '/partnership', '/bulk-supply'] // } ] export default function MobileTabBar() { const pathname = usePathname() const [isVisible, setIsVisible] = useState(true) const [lastScrollY, setLastScrollY] = useState(0) const [cartItemCount, setCartItemCount] = useState(0) // Load cart count const loadCartCount = () => { const cart = cartManager.getCart() const count = cart.reduce((sum, item) => sum + item.quantity, 0) setCartItemCount(count) } useEffect(() => { loadCartCount() const handleCartUpdate = () => loadCartCount() window.addEventListener('cartUpdated', handleCartUpdate) return () => { window.removeEventListener('cartUpdated', handleCartUpdate) } }, []) // Hide/show tab bar on scroll useEffect(() => { const handleScroll = () => { const currentScrollY = window.scrollY if (currentScrollY > lastScrollY && currentScrollY > 100) { setIsVisible(false) // Hide when scrolling down } else { setIsVisible(true) // Show when scrolling up } setLastScrollY(currentScrollY) } window.addEventListener('scroll', handleScroll, { passive: true }) return () => window.removeEventListener('scroll', handleScroll) }, [lastScrollY]) const isActiveTab = (tab: typeof tabs[0]) => { if (tab.activeRoutes.includes(pathname)) return true return tab.activeRoutes.some(route => { if (route.includes('[slug]')) { const baseRoute = route.replace('/[slug]', '') return pathname.startsWith(baseRoute) && pathname !== baseRoute } return false }) } return (
{tabs.map((tab) => { const Icon = tab.icon const isActive = isActiveTab(tab) // Special handling for cart tab if (tab.id === 'cart') { return (
{cartItemCount > 0 && ( {cartItemCount > 99 ? '99+' : cartItemCount} )}
{tab.label}
) } // Regular tabs return ( {tab.label} {isActive && (
)} ) })}
{/* Safe area padding for devices with home indicator */}
) }