'use client' import React, { useLayoutEffect, type PropsWithChildren } from 'react' import { reaction } from 'mobx' import { observer } from 'mobx-react-lite' import { Button, ScrollArea } from '@hanzo/ui/primitives' import { cn } from '@hanzo/ui/util' import type { LineItem } from '../../../types' import { useCommerce } from '../../../service/context' import { sendFBEvent, sendGAEvent } from '../../../util/analytics' import CartLineItem from './cart-line-item' import TotalArea from './total-area' const CartPanel: React.FC void }> = observer(({ /** If provided, 'children' is rendered above the items. eg, a heading. */ children, scrollAfter=5, scrollHeightClx='h-[70vh]', imgSizePx=40, className='', itemClx='', listClx='', noItemsClx='', totalClx='', buttonClx='', showPromoCode=false, showShipping=false, selectItems=false, handleCheckout, }) => { const cmmc = useCommerce() useLayoutEffect(() => { if (!cmmc) { return } if (!cmmc.cartEmpty && !cmmc.currentItem) { cmmc.setCurrentItem(cmmc.cartItems[0].sku) } // return mobx disposer return reaction(() => ( cmmc.cartItems.length ), (itemCount) => { if (itemCount > 0) { cmmc.setCurrentItem(cmmc.cartItems[0].sku) } }) }, []) if (!cmmc) { return
} const _handleCheckout = () => { sendGAEvent('begin_checkout', { currency: 'USD', value: cmmc.cartTotal, items: cmmc.cartItems.map((item) => ({ item_id: item.sku, item_name: item.title, item_category: item.familyId, price: item.price, quantity: item.quantity })), }) sendFBEvent('InitiateCheckout', { content_ids: cmmc.cartItems.map((item) => item.sku), contents: cmmc.cartItems.map(item => ({ id: item.sku, quantity: item.quantity })), num_items: cmmc.cartItems.length, value: cmmc.cartTotal, currency: 'USD', }) handleCheckout && handleCheckout() } const itemClicked = (item: LineItem) => { cmmc.setCurrentItem(item.sku) } const MainStuff: React.FC = observer(() => (<> {cmmc.cartEmpty ? (

No items

) : (<> {cmmc.cartItems.map((item) => ( ))} )} )) const scrolling = (cmmc.cartItems.length > scrollAfter) return (
{children} {scrolling ? ( ) : (
)} {handleCheckout && !cmmc.cartEmpty && ( )}
) }) export default CartPanel