Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
import { isZeroMoney } from '@spree/core'
import {
AddressFormDialog,
formatMoney,
formatStoreDateTime,
isPositiveMoney,
useMoneyLocale,
} from '@spree/dashboard-core'
import {
AddressBlock,
Button,
Card,
CardAction,
CardHeader,
CardTitle,
cn,
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
Separator,
} from '@spree/dashboard-ui'
import { EllipsisVerticalIcon, PencilIcon } from '@spree/dashboard-ui/icons'
import type { Order, OrderAddressParams } from '@spree/seller-sdk'
import { type ReactNode, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { sellerClient } from '../../api-client'
import { useOrderMutation } from '../../hooks/use-order'
import { useStoreTimezone } from '../../hooks/use-store-timezone'
function SummaryRow({
label,
value,
bold,
danger,
highlight,
}: {
label: string
value: ReactNode
bold?: boolean
danger?: boolean
highlight?: boolean
}) {
return (
<div
className={cn('flex items-center justify-between px-5 py-2.5', highlight && 'bg-accent/50')}
>
<span className="text-sm">{label}</span>
<span
className={cn('text-sm tabular-nums', bold && 'font-bold', danger && 'text-destructive')}
>
{value}
</span>
</div>
)
}
// The marketplace's zone, not the browser's: a seller trading away from the
// marketplace would otherwise see an order fall on the wrong day.
function formatDate(iso: string | null | undefined, timezone: string) {
if (!iso) return '—'
return formatStoreDateTime(iso, timezone)
}
/**
* What the sale came to, and what has been paid against it.
*
* The same rows the operator sees on their own order page, so a seller
* reconciling a payout is reading the same arithmetic — a row appears only
* when it carries a figure, which is why a simple order shows three lines and
* a discounted, taxed, shipped one shows eight.
*
* The items themselves are listed on the parcels above rather than repeated
* here: what a seller needs from a line is which parcel it travels in.
*/
export function OrderSummaryCard({ order }: { order: Order }) {
const { t } = useTranslation()
const locale = useMoneyLocale()
const money = (amount: string | null) => formatMoney(amount, order.currency, locale)
const timezone = useStoreTimezone()
const outstanding = isPositiveMoney(order.amount_due)
const placed = Boolean(order.completed_at)
return (
<Card>
<CardHeader>
<CardTitle>{t('orders.summary.title')}</CardTitle>
</CardHeader>
<div className="py-1">
<SummaryRow
label={t('orders.summary.created_at')}
value={formatDate(order.created_at, timezone)}
/>
{order.completed_at && (
<SummaryRow
label={t('orders.summary.completed_at')}
value={formatDate(order.completed_at, timezone)}
/>
)}
{order.canceled_at && (
<>
<Separator />
<SummaryRow
label={t('orders.summary.canceled_at')}
value={formatDate(order.canceled_at, timezone)}
/>
{order.cancel_reason_name && (
<SummaryRow
label={t('orders.summary.cancel_reason')}
value={order.cancel_reason_name}
/>
)}
{order.cancel_note && (
<SummaryRow label={t('orders.summary.cancel_note')} value={order.cancel_note} />
)}
</>
)}
<Separator />
<SummaryRow label={t('orders.summary.currency')} value={order.currency} />
<Separator />
<SummaryRow label={t('orders.summary.subtotal')} value={money(order.item_total)} />
{isPositiveMoney(order.delivery_total) && (
<SummaryRow label={t('orders.summary.shipping')} value={money(order.delivery_total)} />
)}
{!isZeroMoney(order.discount_total) && (
<SummaryRow label={t('orders.summary.promotions')} value={money(order.discount_total)} />
)}
{!isZeroMoney(order.adjustment_total) && (
<SummaryRow
label={t('orders.summary.adjustments')}
value={money(order.adjustment_total)}
/>
)}
{isPositiveMoney(order.included_tax_total) && (
<SummaryRow
label={t('orders.summary.tax_included')}
value={money(order.included_tax_total)}
/>
)}
{/* Shown on a placed order even at zero, so "no tax was charged" reads
as an answer rather than a missing row. */}
{(isPositiveMoney(order.additional_tax_total) ||
(placed && isZeroMoney(order.included_tax_total))) && (
<SummaryRow
label={t('orders.summary.tax_additional')}
value={money(order.additional_tax_total)}
/>
)}
<Separator />
<SummaryRow label={t('orders.summary.total')} value={money(order.total)} bold />
{/* What the marketplace charged the seller on this sale. Below the
order total because it is not part of what the buyer paid — it is
billed to the seller separately.
Always qualified as a marketplace fee, never a bare "fee":
Spree::Fee is a buyer-facing charge (handling, gift wrap, COD)
that does roll into the order total, and a bare "total fee" beside
an order would reasonably be taken for that.
The tax is its own row because the seller reclaims it as input
tax, and that has to be attributable to the sale that produced
it.
Shown on every placed order, including one whose fee came to
zero: a zero-rated or exempt rate is an answer the seller needs,
and every order here is a seller's by definition. */}
{placed && (
<>
<Separator />
<SummaryRow
label={t('orders.summary.commission_fee')}
value={money(order.commission_amount_total)}
/>
{isPositiveMoney(order.commission_tax_total) && (
<SummaryRow
label={t('orders.summary.commission_tax')}
value={money(order.commission_tax_total)}
/>
)}
<SummaryRow
label={t('orders.summary.commission_total')}
value={money(order.commission_total)}
bold
/>
</>
)}
<Separator />
<SummaryRow
label={t('orders.summary.payment_total')}
value={money(order.payment_total)}
highlight
/>
<SummaryRow
label={t('orders.summary.outstanding_balance')}
value={money(order.amount_due)}
highlight
danger={outstanding}
/>
</div>
</Card>
)
}
/**
* Where the parcel goes and who the invoice is for, and correcting either.
*
* The seller is merchant of record for their own child order, so a delivery
* address the buyer got wrong is theirs to fix and the invoice address is
* what they bill against.
*
* No email address: a seller reaching the customer about a delivery has the
* phone on the shipping address, and an email is the one contact detail that
* would let a marketplace's customer be taken off it.
*/
export function OrderCustomerCard({ order }: { order: Order }) {
const { t } = useTranslation()
const [editing, setEditing] = useState<'shipping_address' | 'billing_address' | null>(null)
const save = useOrderMutation(order.id, (params: { type: string; address: OrderAddressParams }) =>
sellerClient().orders.address(order.id, { [params.type]: params.address }),
)
const editable = !order.canceled_at
return (
<>
<Card>
<CardHeader>
<CardTitle>{t('orders.customer.title')}</CardTitle>
{editable && (
<CardAction>
<DropdownMenu>
<DropdownMenuTrigger
render={
<Button variant="ghost" size="icon-xs" aria-label={t('common.actions')}>
<EllipsisVerticalIcon className="size-4" />
</Button>
}
/>
<DropdownMenuContent align="end">
<DropdownMenuItem onClick={() => setEditing('shipping_address')}>
<PencilIcon className="size-4" />
{t('orders.address_edit.shipping_title')}
</DropdownMenuItem>
<DropdownMenuItem onClick={() => setEditing('billing_address')}>
<PencilIcon className="size-4" />
{t('orders.address_edit.billing_title')}
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
</CardAction>
)}
</CardHeader>
<div className="flex flex-col gap-4 px-6 pb-6">
<AddressBlock title={t('orders.shipping_address')} address={order.shipping_address} />
<AddressBlock title={t('orders.billing_address')} address={order.billing_address} />
</div>
</Card>
{/* Mounted only while open, so the form seeds from the address as it is
now rather than freezing at first render. */}
{editing && (
<AddressFormDialog
title={
editing === 'shipping_address'
? t('orders.address_edit.shipping_title')
: t('orders.address_edit.billing_title')
}
address={editing === 'shipping_address' ? order.shipping_address : order.billing_address}
open
onOpenChange={(open) => !open && setEditing(null)}
onSave={(address) =>
save.mutate(
{ type: editing, address },
// Kept open on failure so the entered address is not lost.
{ onSuccess: () => setEditing(null) },
)
}
isPending={save.isPending}
/>
)}
</>
)
}