import { _ } from '@evershop/evershop/lib/locale/translate/_'; import React from 'react'; interface ShipmentItem { uuid: string; qty: number; productSku: string | null; productName: string | null; } interface Shipment { uuid: string; shipmentId: number; carrier: string | null; carrierName: string | null; trackingNumber: string | null; trackingUrl: string | null; status: { code: string; name: string; badge: string; phase: string }; phase: string; shippedAt: { text: string } | null; deliveredAt: { text: string } | null; canceledAt: { text: string } | null; createdAt: { text: string } | null; items: ShipmentItem[]; } interface Order { orderId: number; uuid: string; orderNumber: string; createdAt: { text: string }; grandTotal: { text: string }; shipmentStatus: { name: string; code: string; badge: string } | null; status: { name: string; code: string; badge: string } | null; shippingMethodName: string | null; shippingMethodData: { snapshot?: { name?: string; cost?: number } } | null; shipments: Shipment[]; } interface OrderTrackingProps { order: Order | null; trackingTokenStatus: 'ok' | 'expired' | 'invalid' | 'mismatch' | 'no_secret'; } const StatusPill: React.FC<{ children: React.ReactNode }> = ({ children }) => ( {children} ); function whenShipped(shipment: Shipment): string { return ( shipment.shippedAt?.text ?? shipment.deliveredAt?.text ?? shipment.createdAt?.text ?? '' ); } /** * Anonymous tracking page rendered at `/orders/:uuid/track?token=...`. * The token is verified in the page middleware; this component only branches * on `trackingTokenStatus`. On 'ok' it queries the order and renders the same * shipments block as `/account/orders/:uuid`, minus the back-to-orders link * (the customer is not logged in). On 'expired' it shows a friendly CTA to * `/account/orders`. */ export default function OrderTracking({ order, trackingTokenStatus }: OrderTrackingProps) { if (trackingTokenStatus !== 'ok' || !order) { const title = trackingTokenStatus === 'expired' ? _('This tracking link has expired') : _('This tracking link is no longer valid'); const body = trackingTokenStatus === 'expired' ? _( 'Tracking links are good for a limited time. Sign in to your account to see the current status of your order.' ) : _( 'We could not verify this link. If you received it in an email from us, please try again from the most recent message, or sign in to your account.' ); return (

{title}

{body}

{_('Sign in to view your orders')}
); } return (

{_('Order #${orderNumber}', { orderNumber: order.orderNumber })}

{order.createdAt?.text}
{order.shipmentStatus?.name && ( {order.shipmentStatus.name} )} {order.status?.name && {order.status.name}}
{(order.shippingMethodName || order.shippingMethodData?.snapshot?.name) && (
{_('You paid for')}
{order.shippingMethodData?.snapshot?.name ?? order.shippingMethodName}
{order.grandTotal.text}
)}

{_('Shipments')}

{order.shipments.length === 0 ? (
{_('No shipments yet — your order is being prepared.')}
) : (
{order.shipments.map((shipment) => { const trackHref = shipment.trackingUrl; const carrierLabel = shipment.carrierName ?? shipment.carrier; const shippedText = whenShipped(shipment); return (
{_('Shipment')} #{shipment.shipmentId} {shipment.status.name}
{carrierLabel && (
{carrierLabel} {shipment.trackingNumber && ( <> · {shipment.trackingNumber} )}
)}
    {shipment.items.map((item) => (
  • {item.productName ?? item.productSku ?? _('Item')} × {item.qty}
  • ))}
{(shippedText || shipment.deliveredAt?.text || trackHref) && (
{shipment.phase === 'delivered' && shipment.deliveredAt?.text && ( {_('Delivered')} {shipment.deliveredAt.text} )} {shipment.phase === 'shipped' && shippedText && ( {_('Shipped')} {shippedText} )} {shipment.phase === 'canceled' && shipment.canceledAt?.text && ( {_('Canceled')} {shipment.canceledAt.text} )} {trackHref && ( {_('Track shipment →')} )}
)}
); })}
)}
); } export const layout = { areaId: 'content', sortOrder: 10 }; export const query = ` query Query { trackingTokenStatus order(uuid: getContextValue("orderUuid", "missing")) { orderId uuid orderNumber createdAt { text } grandTotal { text } shipmentStatus { name code badge } status { name code badge } shippingMethodName shippingMethodData { snapshot { name cost } } shipments { uuid shipmentId carrier carrierName trackingNumber trackingUrl status { code name badge phase } phase shippedAt { text } deliveredAt { text } canceledAt { text } createdAt { text } items { uuid qty productSku productName } } } } `;