'use client'; import React from 'react'; import { useCartStore, redirectToCheckout } from '@/hooks/use-shopify-cart'; import { Button } from '@/components/ui/button'; import { Loader } from '@/app/components/ui/loader'; import { Sheet, SheetContent, SheetHeader, SheetTitle, SheetBody, AnimatePresence, } from '@/components/ui/sheet'; import { RiCloseLine, RiImageLine, RiSubtractLine, RiAddLine, RiShoppingBagLine, } from '@remixicon/react'; import { Empty, EmptyHeader, EmptyTitle, EmptyDescription, EmptyContent, } from '@/components/ui/empty'; const CartDrawer: React.FC = () => { const isOpen = useCartStore((s) => s.isOpen); const closeCart = useCartStore((s) => s.closeCart); const loading = useCartStore((s) => s.loading); const cart = useCartStore((s) => s.cart); const removeItem = useCartStore((s) => s.removeItem); const updateItemQuantity = useCartStore((s) => s.updateItemQuantity); const items = cart?.lines?.edges?.map((edge) => edge.node) ?? []; const itemCount = items.reduce((sum, item) => sum + item.quantity, 0); const totalAmount = parseFloat(cart?.cost?.totalAmount?.amount ?? '0'); const checkoutUrl = cart?.checkoutUrl ?? null; const handleCheckout = () => { if (checkoutUrl) { redirectToCheckout(checkoutUrl); } }; const getItemImage = (item: (typeof items)[0]) => { return item.merchandise.image?.url; }; const getSelectedOptions = (item: (typeof items)[0]) => { return item.merchandise.selectedOptions ?? []; }; return ( !open && closeCart()} side="right" > {isOpen && ( {/* Header */}
Bag ({itemCount})
{/* Cart Items */} {loading && items.length === 0 ? (
) : items.length === 0 ? ( Your bag is empty Add some products to get started! ) : (
{items.map((item) => { const image = getItemImage(item); const selectedOptions = getSelectedOptions(item); return (
{/* Product Image */}
{image ? ( {item.merchandise.product.title} ) : (
)}
{/* Product Details */}

{item.merchandise.product.title}

{/* Variant Info */} {selectedOptions.length > 0 && (
{selectedOptions.map((option, index) => ( {option.value} {index < 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;