import { useEffect, useMemo, useState } from 'react' import { X } from 'lucide-react' import type { Order, OrderTransaction, TransactionStatus, TransactionType } from '../services/orderService' import { formatIrtPrice } from '../utils/irtPrice' import modalStyles from './VariationsModal.module.css' import styles from './OrderTransactionsModal.module.css' interface OrderTransactionsModalProps { open: boolean order: Order | null onClose: () => void } const ANIMATION_MS = 220 function transactionTypeLabel(type: TransactionType) { switch (type) { case 'pos': return 'POS' case 'cash': return 'Cash' case 'transfer': return 'Transfer' case 'e_payment_gate': return 'E-payment gateway' default: return type } } function statusClass(status: TransactionStatus) { switch (status) { case 'completed': return styles.statusCompleted case 'failed': return styles.statusFailed case 'refunded': return styles.statusRefunded case 'pending': default: return styles.statusPending } } function formatDateTime(value: string) { const d = new Date(value) if (Number.isNaN(d.getTime())) return value return d.toLocaleString('en-US', { year: 'numeric', month: 'short', day: '2-digit', hour: '2-digit', minute: '2-digit', }) } function transactionDetails(transaction: OrderTransaction) { const rows: { label: string; value: string }[] = [ { label: 'Status', value: transaction.status }, { label: 'Date', value: formatDateTime(transaction.createdAt) }, ] if (transaction.type === 'pos' && transaction.posType) { rows.push({ label: 'POS type', value: transaction.posType }) } if (transaction.type === 'transfer') { if (transaction.transferAccount) { rows.push({ label: 'Account number', value: transaction.transferAccount }) } if (transaction.transferRefNumber) { rows.push({ label: 'Ref code', value: transaction.transferRefNumber }) } } if (transaction.type === 'e_payment_gate' && transaction.gatewayType) { rows.push({ label: 'Gateway', value: transaction.gatewayType }) } if (transaction.notes) { rows.push({ label: 'Notes', value: transaction.notes }) } return rows } export function OrderTransactionsModal({ open, order, onClose }: OrderTransactionsModalProps) { const [mounted, setMounted] = useState(open) const [closing, setClosing] = useState(false) const transactions = order?.transactions ?? [] const totalPaid = useMemo( () => transactions.reduce((sum, tx) => sum + tx.amount, 0), [transactions], ) useEffect(() => { if (open) { setMounted(true) setClosing(false) } else if (mounted) { setClosing(true) const timer = setTimeout(() => { setMounted(false) setClosing(false) }, ANIMATION_MS) return () => clearTimeout(timer) } }, [open, mounted]) useEffect(() => { if (!mounted || closing) return const onKey = (e: KeyboardEvent) => { if (e.key === 'Escape') onClose() } document.addEventListener('keydown', onKey) return () => document.removeEventListener('keydown', onKey) }, [mounted, closing, onClose]) if (!mounted || !order) return null return (
{order.orderNumber}
No payment transactions recorded for this order.
) : (