'use client' import React, { useEffect, useRef } from 'react' import { reaction, type IReactionDisposer } from 'mobx' import { observer } from 'mobx-react-lite' import { Button, buttonVariants } from '@hanzo/ui/primitives' import { cn, type VariantProps } from '@hanzo/ui/util' import Icons from './Icons' import type { LineItem } from '../types' import { sendFBEvent, sendGAEvent } from '../util/analytics' const AddToCartWidget: React.FC<{ item: LineItem disabled?: boolean className?: string buttonClx?: string variant?: 'minimal' | 'primary' | 'primary-smaller' | 'outline' onQuantityChanged?: (sku: string, oldV: number, newV: number) => void }> = observer(({ item, variant='primary', disabled=false, className='', buttonClx='', onQuantityChanged }) => { const reactionDisposer = useRef(undefined) useEffect(() => { if (onQuantityChanged) { reactionDisposer.current = reaction( () => (item.quantity), (quantity: number, previous: number) => { onQuantityChanged(item.sku, quantity, previous) } ) } return () => { if (reactionDisposer.current) { reactionDisposer.current() } } }, []) const ROUNDED_VAL = 'lg' // no need to safelist, since its used widely const ROUNDED_CLX = ` rounded-${ROUNDED_VAL} ` const ghost = variant === 'minimal' const outline = variant === 'outline' const primary = variant === 'primary' const priSmaller = variant === 'primary-smaller' let iconClx = ghost ? 'h-4 w-4 md:h-3 md:w-3 text-muted-3 ' : (priSmaller ? 'h-4 w-6 px-0.5 opacity-70' : 'h-5 w-7 px-1 opacity-70 ') iconClx += ghost ? 'group-hover:text-foreground' : 'group-hover:opacity-100' let digitClx = ghost ? 'px-2 md:px-0.5 ' : 'sm:px-2 font-semibold ' digitClx += (ghost || outline) ? 'text-foreground ' : 'text-primary-fg ' if (disabled) { return (
{item.quantity}
) } const inc = (event: React.MouseEvent) => { item.increment() event.stopPropagation() // in case we're part of a larger selection UI sendGAEvent('add_to_cart', { items: [{ item_id: item.sku, item_name: item.title, item_category: item.familyId, price: item.price, quantity: item.quantity }], value: item.price, currency: 'USD', }) sendFBEvent('AddToCart', { content_ids: [item.sku], contents: [{ id: item.sku, quantity: item.quantity }], content_name: item.title, value: item.price, currency: 'USD', }) } const dec = (event: React.MouseEvent) => { item.decrement() event.stopPropagation() // in case we're part of a larger selection UI sendGAEvent('remove_from_cart', { items: [{ item_id: item.sku, item_name: item.title, item_category: item.familyId, price: item.price, quantity: item.quantity }], value: item.price, currency: 'USD', }) } return ( item.isInCart ? (
{item.quantity}{ghost ? '' : ' in Bag'}
) : ( )) }) export default AddToCartWidget