"use client"; import React, { useState } from 'react'; import { RiCloseLine, RiImageLine, RiSubtractLine, RiAddLine, RiLoader4Line } from '@remixicon/react'; import { useShopifyCart, redirectToCheckout } from '@/hooks/use-shopify-cart'; import { Sheet, SheetContent, SheetHeader, SheetTitle, SheetBody, SheetFooter, AnimatePresence, } from './ui/sheet'; import { Button } from './ui/button'; const CartDrawer: React.FC = () => { const { isOpen, items, itemCount, totalAmount, checkoutUrl, removeItem, updateItemQuantity, closeCart } = useShopifyCart(); const [isCheckingOut, setIsCheckingOut] = useState(false); const handleCheckout = async () => { if (!checkoutUrl) return; setIsCheckingOut(true); try { redirectToCheckout(checkoutUrl); } catch (error) { console.error('Error during checkout:', error); alert('Failed to proceed to checkout. Please try again.'); setIsCheckingOut(false); } }; return ( !open && closeCart()}> {isOpen && ( {/* Header */}
Shopping Cart ({itemCount})
{/* Cart Items */} {items.length === 0 ? (

Your cart is empty

Add some products to get started!

) : (
{items.map((item) => (
{/* Product Image */}
{item.merchandise.image?.url ? ( {item.merchandise.image.altText ) : (
)}
{/* Product Details */}

{item.merchandise.product.title}

{/* Variant Info */} {item.merchandise.selectedOptions && item.merchandise.selectedOptions.length > 0 && (
{item.merchandise.selectedOptions.map((option, index) => ( {option.value} {index < item.merchandise.selectedOptions.length - 1 ? ' / ' : ''} ))}
)} {/* Quantity Controls */}
{item.quantity}
{/* Price */}
${parseFloat(item.merchandise.price.amount).toFixed(2)}
{/* Remove Button */}
))}
)}
{/* Footer - Checkout Section */} {items.length > 0 && ( {/* Subtotal */}
Subtotal ${totalAmount.toFixed(2)}
Shipping and taxes calculated at checkout
{/* Action Buttons */}
)}
)}
); }; export default CartDrawer;