'use client'; import { useMemo, useState } from 'react'; import Link from 'next/link'; import { ORDER_STATUSES, displayOrderId, adminStatusLabel } from '@/app/lib/orders'; import { OrderMessageTime } from '@/app/components/OrderMessageTime'; type Row = { id: string; user_email: string; status: string; price_cents: number; config: { styleId?: string; productId?: string; size?: string } | null; created_at: string; custom_style_url: string | null; customerMessageAt: string | null; prodigi_order_id: string | null; printerUrl: string | null; }; // Statuses that require us to do something: // pending_design — generate + publish a first preview // revision_requested — customer rejected, generate + publish a fresh one // approved — customer approved, do a final review and send // to printer (Prodigi isn't auto-triggered) const ATTENTION_STATUSES = ['pending_design', 'revision_requested', 'approved']; // pending_payment + abandoned = customer started checkout but never paid. // Not studio work — surfaced separately. (pending_payment is the in-flight // cart; abandoned is the same row after the 7-day pg_cron sweep.) const PRE_STUDIO = new Set(['pending_payment', 'abandoned']); type Category = 'attention' | 'active' | 'cancelled' | 'prestudio'; function categoryOf({ status, customerMessageAt }: Row): Category { if (status === 'cancelled') return 'cancelled'; if (PRE_STUDIO.has(status)) return 'prestudio'; if (ATTENTION_STATUSES.includes(status) || customerMessageAt) return 'attention'; return 'active'; } const PAGE_SIZE = 25; function searchBlob(o: Row): string { const cfg = o.config ?? {}; return [ o.id, displayOrderId(o.id), o.user_email, o.status, o.prodigi_order_id, adminStatusLabel(o.status), cfg.styleId, cfg.productId, cfg.size, o.custom_style_url ? 'custom style' : '', ].filter(Boolean).join(' ').toLowerCase(); } export function AdminOrdersList({ orders }: { orders: Row[] }) { const [query, setQuery] = useState(''); const [statusFilter, setStatusFilter] = useState('all'); const q = query.trim().toLowerCase(); const filtering = q.length > 0 || statusFilter !== 'all'; const statusCounts = useMemo(() => { const m = new Map(); for (const o of orders) m.set(o.status, (m.get(o.status) ?? 0) + 1); return m; }, [orders]); const filtered = useMemo( () => orders.filter(o => { if (statusFilter !== 'all' && o.status !== statusFilter) return false; if (q && !searchBlob(o).includes(q)) return false; return true; }), [orders, q, statusFilter], ); const attention = orders.filter(o => categoryOf(o) === 'attention').sort((a, b) => Date.parse(b.customerMessageAt ?? b.created_at) - Date.parse(a.customerMessageAt ?? a.created_at)); const active = orders.filter(o => categoryOf(o) === 'active'); const cancelled = orders.filter(o => categoryOf(o) === 'cancelled'); const preStudio = orders.filter(o => categoryOf(o) === 'prestudio'); return ( <>
setQuery(e.target.value)} /> {filtering && ( )}
{filtering ? (

{filtered.length} {filtered.length === 1 ? 'result' : 'results'}

{filtered.length === 0 ? (

No orders match.

) : ( // Remount on filter change so paging resets to page 1. )}
) : ( <>

Needs your attention ({attention.length})

{attention.length === 0 ? (

Nothing waiting. ✓

) : ( )}

Active commissions ({active.length})

{active.length === 0 ? (

No active commissions.

) : ( )}
{cancelled.length > 0 && (

Cancelled ({cancelled.length})

Refunded and closed. Kept for the record — not active work.

)} {preStudio.length > 0 && (

Abandoned (pre-payment) ({preStudio.length})

Customer started checkout but never paid. Not in the studio. Rows older than 7 days are auto-swept to abandoned.

)} )} ); } // Client-side pagination over an already-fetched list. The pager only // appears once a list exceeds PAGE_SIZE, so short sections (attention, // abandoned) render untouched. Callers remount via `key` to reset paging // when the underlying set changes (e.g. on a new search). function PaginatedList({ items, pageSize = PAGE_SIZE }: { items: Row[]; pageSize?: number }) { const [page, setPage] = useState(1); const pageCount = Math.max(1, Math.ceil(items.length / pageSize)); const current = Math.min(page, pageCount); // clamp if items shrank const start = (current - 1) * pageSize; const slice = items.slice(start, start + pageSize); return ( <> {items.length > pageSize && (
{start + 1}–{Math.min(start + pageSize, items.length)} of {items.length}
)} ); } function AdminRow({ order }: { order: Row }) { const cfg = order.config ?? {}; const hasCustomStyle = !!order.custom_style_url; return (
  • {displayOrderId(order.id)} {order.user_email} {order.customerMessageAt && categoryOf(order) === 'attention' && ( {['awaiting_approval', 'revision_requested'].includes(order.status) ? 'Customer feedback' : 'Customer message'} {' · '} )} {hasCustomStyle && ( ✦ Custom style )} {adminStatusLabel(order.status)}
    {hasCustomStyle ? custom : (cfg.styleId ?? '—')} · {cfg.productId ?? '—'} {cfg.size ?? ''} ${(order.price_cents / 100).toFixed(2)} · {new Date(order.created_at).toLocaleDateString()}
    {order.printerUrl && (
    Submitted to Prodigi · {order.prodigi_order_id} View printer order ↗
    )}
  • ); }