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 {
downloadFromApi,
formatMoney,
getApiClient,
useAuth,
useMoneyLocale,
} from '@spree/dashboard-core'
import {
Badge,
Button,
CardContent,
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
toastManager,
useConfirm,
} from '@spree/dashboard-ui'
import {
EllipsisVerticalIcon,
PrinterIcon,
ReceiptTextIcon,
TrashIcon,
} from '@spree/dashboard-ui/icons'
import type { ShippingLabel } from '@spree/seller-sdk'
import { useTranslation } from 'react-i18next'
import { useFulfillmentActions } from '../../hooks/use-fulfillments'
import { useOrder } from '../../hooks/use-order'
/**
* The label bound to a parcel: what it cost and how to print it.
*
* Refunding is absent by design — postage bought through the marketplace's
* carrier account is the operator's to reverse, and the endpoint refuses it.
* Only a label the seller uploaded can be removed, since that is the only one
* no carrier holds a record of.
*/
export function ShippingLabelRow({
orderId,
fulfillmentId,
label,
}: {
orderId: string
fulfillmentId: string
label: ShippingLabel
}) {
const { t } = useTranslation()
const { token } = useAuth()
const confirm = useConfirm()
const { deleteLabel } = useFulfillmentActions(orderId)
const locale = useMoneyLocale()
const { data: order } = useOrder(orderId)
// A label with no currency of its own was bought in the order's.
const labelCurrency = label.currency ?? order?.currency
const cost = labelCurrency ? formatMoney(label.cost, labelCurrency, locale) : ''
const deletable = label.source === 'uploaded'
async function print() {
if (!label.download_url) return
// A rejected download inside a click handler is otherwise unhandled, so
// the file silently never arrives.
try {
await downloadFromApi(
token,
label.download_url,
`${label.tracking_number ?? 'label'}.${label.format ?? 'pdf'}`,
getApiClient().downloadHeaders?.() ?? {},
)
} catch {
// Deliberately not the thrown message: `downloadFromApi` reports the
// transport in English ("Download failed: 404"), which would land
// untranslated in a localized panel and tells the merchant nothing they
// can act on.
toastManager.add({ type: 'error', title: t('admin.errors.label_download_failed') })
}
}
async function handleDelete() {
const ok = await confirm({
message: t('orders.fulfillments.confirm_delete_label'),
variant: 'destructive',
confirmLabel: t('common.delete'),
})
if (!ok) return
deleteLabel.mutate({ fulfillmentId, labelId: label.id })
}
return (
<CardContent className="flex flex-wrap items-center justify-between gap-2 border-border-subtle border-b py-3 text-sm">
<div className="flex flex-wrap items-center gap-2">
<ReceiptTextIcon className="size-4 text-muted-foreground" />
<span className="text-muted-foreground">{t('orders.fulfillments.label_title')}</span>
{label.source === 'uploaded' && (
<Badge variant="secondary">{t('orders.fulfillments.label_uploaded')}</Badge>
)}
{cost && (
<span className="text-muted-foreground">
{t('orders.fulfillments.label_cost', { amount: cost })}
</span>
)}
</div>
<div className="flex items-center gap-2">
{label.download_url && (
<Button type="button" variant="outline" size="sm" onClick={print}>
<PrinterIcon data-icon="inline-start" />
{t('orders.fulfillments.print_label')}
</Button>
)}
{deletable && (
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button variant="ghost" size="icon-xs">
<EllipsisVerticalIcon className="size-4" />
<span className="sr-only">{t('admin.actions.actions_menu')}</span>
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
<DropdownMenuItem
variant="destructive"
disabled={deleteLabel.isPending}
onClick={handleDelete}
>
<TrashIcon className="size-4" />
{t('orders.fulfillments.delete_label')}
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
)}
</div>
</CardContent>
)
}