/** * @component CartDrawer * @purpose Slide-over cart preview with quantity controls, subtotal, and checkout CTA. */ 'use client'; import { useEffect } from 'react'; import Link from 'next/link'; import { X, Trash2, Plus, Minus, ShoppingBag, ArrowRight, Truck } from 'lucide-react'; import { useCartStore } from '@/store/cartStore'; import { formatCurrency, getImageUrl } from '@/lib/utils'; import { requireLoginForCheckout } from '@/lib/requireLoginForCheckout'; function resolveItemImage(item: { product: { images?: { image_url?: string }[]; name?: string }; selectedVariant: { images?: { image_url?: string }[] }; }): string { const fromVariant = item.selectedVariant?.images?.[0]?.image_url; const fromProduct = item.product?.images?.[0]?.image_url; return getImageUrl(fromVariant || fromProduct || ''); } export function CartDrawer() { const isOpen = useCartStore((state) => state.cartDrawerOpen); const setIsOpen = useCartStore((state) => state.setCartDrawerOpen); const cart = useCartStore((state) => state.cart); const removeFromCart = useCartStore((state) => state.removeFromCart); const updateQuantity = useCartStore((state) => state.updateQuantity); const getCartTotal = useCartStore((state) => state.getCartTotal); useEffect(() => { if (!isOpen) return; const prev = document.body.style.overflow; document.body.style.overflow = 'hidden'; const onKey = (e: KeyboardEvent) => { if (e.key === 'Escape') setIsOpen(false); }; document.addEventListener('keydown', onKey); return () => { document.body.style.overflow = prev; document.removeEventListener('keydown', onKey); }; }, [isOpen, setIsOpen]); if (!isOpen) return null; const total = getCartTotal(); const itemCount = cart.reduce((sum, i) => sum + (i.quantity || 1), 0); const freeShippingThreshold = 999; const progressPercent = Math.min(100, (total / freeShippingThreshold) * 100); const amountNeeded = Math.max(0, freeShippingThreshold - total); return (
{/* Backdrop */}
{/* Free shipping bar */} {cart.length > 0 && (
{amountNeeded > 0 ? ( Add{' '} {formatCurrency(amountNeeded)}{' '} more for FREE Shipping! ) : ( You unlocked FREE Delivery! )}
)} {/* Scrollable items */}
{cart.length === 0 ? (

Your cart is empty

Add products from the shop to see them here.

) : ( )}
{/* Footer */} {cart.length > 0 && (
Subtotal: {formatCurrency(total)}

Shipping & taxes calculated at checkout

setIsOpen(false)} className="bg-gray-100 hover:bg-gray-200 text-primary text-[13px] font-bold py-3 rounded-md text-center transition-colors" > View Cart
)}
); }