'use client'; import React, { useEffect, useState } from 'react'; import OrderHistory from '@/components/shopify/order-history'; import type { Customer } from '@/services/shopify/customer'; export interface AccountOrdersProps { title?: string; /** Shown while signed out — the route guard normally redirects first. */ signedOutMessage?: string; emptyMessage?: string; limit?: number; } /** * Client-side wrapper so order history can live in a page.json alongside the * other blocks. `/account/page.tsx` still guards the route server-side; this * re-reads the session through `/api/account/orders` so the block works the * same whether it is rendered by the page or previewed in the editor. */ const AccountOrders: React.FC = ({ title = 'Order history', signedOutMessage = 'Sign in to see your orders.', emptyMessage = "You haven't placed any orders yet.", limit = 20, }) => { const [customer, setCustomer] = useState(null); const [loading, setLoading] = useState(true); useEffect(() => { let cancelled = false; fetch(`/api/account/orders?orders=${limit}`) .then((response) => response.json()) .then((data) => { if (!cancelled) setCustomer(data.customer ?? null); }) .catch(() => { if (!cancelled) setCustomer(null); }) .finally(() => { if (!cancelled) setLoading(false); }); return () => { cancelled = true; }; }, [limit]); const orderCount = customer?.orders?.edges.length ?? 0; return (

{title}

{customer && (

{customer.displayName} · {customer.email}

)}
{loading ? (
{Array.from({ length: 3 }).map((_, index) => (
))}
) : !customer ? (

{signedOutMessage}

) : orderCount === 0 ? (

{emptyMessage}

) : ( )}
); }; export default AccountOrders;