"use client" import { Popover, PopoverButton, PopoverPanel, Transition, } from "@headlessui/react" import { convertToLocale } from "@lib/util/money" import { HttpTypes } from "@medusajs/types" import { Button } from "@medusajs/ui" import * as MedusaIcons from "@medusajs/icons" import DeleteButton from "@modules/common/components/delete-button" import LineItemOptions from "@modules/common/components/line-item-options" import LineItemPrice from "@modules/common/components/line-item-price" import LocalizedClientLink from "@modules/common/components/localized-client-link" import Thumbnail from "@modules/products/components/thumbnail" import { usePathname } from "next/navigation" import { Fragment, useEffect, useRef, useState } from "react" const CartDropdown = ({ cart: cartState, iconName, }: { cart?: HttpTypes.StoreCart | null iconName?: string }) => { const [activeTimer, setActiveTimer] = useState( undefined ) const [cartDropdownOpen, setCartDropdownOpen] = useState(false) const open = () => setCartDropdownOpen(true) const close = () => setCartDropdownOpen(false) const totalItems = cartState?.items?.reduce((acc, item) => { return acc + item.quantity }, 0) || 0 const subtotal = cartState?.subtotal ?? 0 const itemRef = useRef(totalItems || 0) const timedOpen = () => { open() const timer = setTimeout(close, 5000) setActiveTimer(timer) } const openAndCancel = () => { if (activeTimer) { clearTimeout(activeTimer) } open() } // Clean up the timer when the component unmounts useEffect(() => { return () => { if (activeTimer) { clearTimeout(activeTimer) } } }, [activeTimer]) const pathname = usePathname() // open cart dropdown when modifying the cart items, but only if we're not on the cart page useEffect(() => { if (itemRef.current !== totalItems && !pathname.includes("/cart")) { timedOpen() } // eslint-disable-next-line react-hooks/exhaustive-deps }, [totalItems, itemRef.current]) const Icon = iconName ? (MedusaIcons as Record>)[iconName] : undefined return (
{Icon ? ( ) : ( {`Cart (${totalItems})`} )}

Cart

{cartState && cartState.items?.length ? ( <>
{cartState.items .sort((a, b) => { return (a.created_at ?? "") > (b.created_at ?? "") ? -1 : 1 }) .map((item) => (

{item.title}

Quantity: {item.quantity}
Remove
))}
Subtotal{" "} (excl. taxes) {convertToLocale({ amount: subtotal, currency_code: cartState.currency_code, })}
) : (
0
Your shopping bag is empty.
<> Go to all products page
)}
) } export default CartDropdown