'use client'; import Link from 'next/link'; import { format } from 'date-fns'; import { MoreHorizontal, Eye, Send, CreditCard, Trash2, FileText } from 'lucide-react'; import type { ColumnDef } from '@tanstack/react-table'; import { Button } from '@/components/ui/button'; import { Badge } from '@/components/ui/badge'; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, } from '@/components/ui/dropdown-menu'; export interface InvoiceRow { id: string; invoiceNumber: string; clientName: string; total: string; currency: string; status: string; paymentStatus: string | null; dueDate: string; pdfFileId: string | null; archivedAt: string | null; createdAt: string; } const STATUS_COLORS: Record = { draft: 'bg-gray-100 text-gray-700 border-gray-200', sent: 'bg-blue-100 text-blue-700 border-blue-200', paid: 'bg-green-100 text-green-700 border-green-200', overdue: 'bg-red-100 text-red-700 border-red-200', cancelled: 'bg-gray-100 text-gray-500 border-gray-200', }; interface GetColumnsOptions { portSlug: string; onSend?: (invoice: InvoiceRow) => void; onRecordPayment?: (invoice: InvoiceRow) => void; onDelete?: (invoice: InvoiceRow) => void; } export function getInvoiceColumns({ portSlug, onSend, onRecordPayment, onDelete, }: GetColumnsOptions): ColumnDef[] { const today = new Date().toISOString().split('T')[0]!; return [ { id: 'invoiceNumber', accessorKey: 'invoiceNumber', header: 'Invoice #', cell: ({ row }) => ( e.stopPropagation()} > {row.original.invoiceNumber} ), }, { id: 'clientName', accessorKey: 'clientName', header: 'Client', cell: ({ getValue }) => ( {getValue() as string} ), }, { id: 'total', header: 'Total', enableSorting: false, cell: ({ row }) => ( {Number(row.original.total).toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2, })}{' '} {row.original.currency} ), }, { id: 'status', accessorKey: 'status', header: 'Status', cell: ({ getValue }) => { const status = (getValue() as string) ?? 'draft'; const colorClass = STATUS_COLORS[status] ?? STATUS_COLORS.draft; return ( {status} ); }, }, { id: 'dueDate', accessorKey: 'dueDate', header: 'Due Date', cell: ({ row }) => { const due = row.original.dueDate; const isOverdue = row.original.status === 'sent' && due < today; return ( {format(new Date(due), 'MMM d, yyyy')} ); }, }, { id: 'actions', header: '', enableSorting: false, size: 48, cell: ({ row }) => { const invoice = row.original; return ( View {invoice.pdfFileId && ( View PDF )} {invoice.status === 'draft' && onSend && ( onSend(invoice)}> Send )} {(invoice.status === 'sent' || invoice.status === 'overdue') && onRecordPayment && ( onRecordPayment(invoice)}> Record Payment )} {invoice.status === 'draft' && onDelete && ( onDelete(invoice)} > Delete )} ); }, }, ]; }