/**
* @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 */}
);
}