import { Button } from '@components/common/ui/Button.js'; import { ConfirmDialog } from '@components/common/ui/ConfirmDialog.js'; import { Label } from '@components/common/ui/Label.js'; import { toast } from '@components/common/ui/Sonner.js'; import { _ } from '@evershop/evershop/lib/locale/translate/_'; import axios from 'axios'; import { Check, Clock, Edit2, ExternalLink, X } from 'lucide-react'; import React from 'react'; import { hasTrackingCapability } from './carrierCaps.js'; import { PhaseBadge } from './phaseBadge.js'; export interface ShipmentItem { uuid: string; orderItemId: number; qty: number; productSku: string | null; productName: string | null; thumbnail: string | null; } export interface Shipment { uuid: string; shipmentId: number; carrier: string | null; carrierName: string | null; trackingNumber: string | null; trackingUrl: string | null; labelUrl: string | null; status: { code: string; name: string; badge: string; phase: 'pending' | 'shipped' | 'delivered' | 'canceled' | string; }; phase: 'pending' | 'shipped' | 'delivered' | 'canceled' | string; shippedAt: { text: string | null } | null; deliveredAt: { text: string | null } | null; canceledAt: { text: string | null } | null; createdAt: { text: string | null }; items: ShipmentItem[]; markDeliveredApi: string; cancelShipmentApi: string; voidShipmentLabelApi: string; } interface Carrier { code: string; name: string; capabilities?: { createLabel?: boolean; generateTrackingUrl?: boolean; voidLabel?: boolean; fetchStatus?: boolean; }; } /** * Pulls the formatted `text` off a Shipment DateTime field. The GraphQL * `DateTime` type is an object (`{ value, timezone, text(format) }`) — the * query asks for `text(format: "LLL dd, yyyy")` so this just unwraps it. * Returns empty string when the field is null (e.g. shippedAt on a shipment * that went direct pending→delivered). */ function dateText(d: { text: string | null } | null): string { return d?.text ?? ''; } /** * Per-shipment block inside the Shipments card. * * Layout per the design: * - Top row: "Shipment #N" + phase status badge | carrier · tracking * - Middle: items list (thumbnail + name + × qty) * - Timeline: Created / Shipped / Delivered with clock icons * - Action row: Mark delivered (when phase=shipped) | Edit tracking | * Cancel shipment (until delivered/canceled) | Track → (when URL) * * Editing tracking swaps the action row for an inline `EditTrackingForm` * (carrier select + tracking number, Cancel + Save). Canceled shipments * dim out and drop all actions. */ export function ShipmentRow({ shipment, carriers }: { shipment: Shipment; carriers: Carrier[]; }) { const [editing, setEditing] = React.useState(false); const isCanceled = shipment.phase === 'canceled'; const carrierEntry = shipment.carrier ? carriers.find((c) => c.code === shipment.carrier) : undefined; const carrierDisplay = shipment.carrierName ?? carrierEntry?.name ?? shipment.carrier; const trackHref = shipment.trackingUrl; const canVoidLabel = !!shipment.labelUrl && // Shipments are born in the `shipped` phase (no `pending` state under the // current model), and voidShipmentLabel's service-side gate also requires // `shipped`. The old `pending` check meant the button never rendered. shipment.phase === 'shipped' && !!carrierEntry?.capabilities?.voidLabel; const timeline = [ { label: _('Created'), date: dateText(shipment.createdAt) }, shipment.shippedAt?.text && { label: _('Shipped'), date: dateText(shipment.shippedAt) }, shipment.deliveredAt?.text && { label: _('Delivered'), date: dateText(shipment.deliveredAt) }, shipment.canceledAt?.text && { label: _('Canceled'), date: dateText(shipment.canceledAt) } ].filter(Boolean) as Array<{ label: string; date: string }>; // Confirmation now lives in the wrapping ConfirmDialog component // (shared, shadcn AlertDialog). These handlers just do the API call + toast + // reload; the dialog closes itself after onConfirm resolves. const handleMarkDelivered = async () => { try { await axios.post(shipment.markDeliveredApi); toast.success(_('Shipment marked delivered')); window.location.reload(); } catch (e) { const err = e as { response?: { data?: { error?: { message?: string } } }; }; toast.error( err.response?.data?.error?.message ?? _('Failed to mark delivered') ); } }; const handleCancel = async () => { try { await axios.post(shipment.cancelShipmentApi); toast.success(_('Shipment canceled')); window.location.reload(); } catch (e) { const err = e as { response?: { data?: { error?: { message?: string } } }; }; toast.error( err.response?.data?.error?.message ?? _('Failed to cancel shipment') ); } }; const handleVoidLabel = async () => { try { await axios.delete(shipment.voidShipmentLabelApi); toast.success(_('Label voided')); window.location.reload(); } catch (e) { const err = e as { response?: { data?: { error?: { message?: string } } }; }; toast.error( err.response?.data?.error?.message ?? _('Failed to void label') ); } }; return (