|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import React, { useState } from 'react'; |
| 4 | +import { useQuery } from '@tanstack/react-query'; |
| 5 | +import PaginationControls from './PaginationControls'; |
| 6 | +import SkeletonRow from './SkeletonRow'; |
| 7 | + |
| 8 | +interface Invoice { |
| 9 | + id: string; |
| 10 | + riskScore: number; |
| 11 | + status: string; |
| 12 | + amount: number; |
| 13 | +} |
| 14 | + |
| 15 | +interface PaginationInfo { |
| 16 | + currentPage: number; |
| 17 | + totalPages: number; |
| 18 | + totalItems: number; |
| 19 | + itemsPerPage: number; |
| 20 | + hasNextPage: boolean; |
| 21 | + hasPreviousPage: boolean; |
| 22 | +} |
| 23 | + |
| 24 | +interface InvoicesResponse { |
| 25 | + data: Invoice[]; |
| 26 | + pagination: PaginationInfo; |
| 27 | +} |
| 28 | + |
| 29 | +const InvoiceTable: React.FC = () => { |
| 30 | + const [currentPage, setCurrentPage] = useState(1); |
| 31 | + const itemsPerPage = 20; |
| 32 | + |
| 33 | + const { |
| 34 | + data: invoicesData, |
| 35 | + isLoading, |
| 36 | + isFetching, |
| 37 | + error, |
| 38 | + } = useQuery<InvoicesResponse>({ |
| 39 | + queryKey: ['invoices', currentPage, itemsPerPage], |
| 40 | + queryFn: async () => { |
| 41 | + const response = await fetch( |
| 42 | + `/api/invoices?page=${currentPage}&limit=${itemsPerPage}` |
| 43 | + ); |
| 44 | + if (!response.ok) { |
| 45 | + throw new Error('Failed to fetch invoices'); |
| 46 | + } |
| 47 | + return response.json(); |
| 48 | + }, |
| 49 | + keepPreviousData: true, // Prevents UI from flashing empty while fetching next page |
| 50 | + staleTime: 1000 * 60 * 5, // 5 minutes |
| 51 | + }); |
| 52 | + |
| 53 | + const handlePageChange = (page: number) => { |
| 54 | + setCurrentPage(page); |
| 55 | + }; |
| 56 | + |
| 57 | + if (error) { |
| 58 | + return ( |
| 59 | + <div className="bg-red-500/10 border border-red-500/30 rounded-lg p-4 text-red-400"> |
| 60 | + <p className="font-medium">Error loading invoices</p> |
| 61 | + <p className="text-sm opacity-80">{error.message}</p> |
| 62 | + </div> |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + return ( |
| 67 | + <div className="bg-tradeflow-secondary rounded-2xl border border-tradeflow-muted overflow-hidden"> |
| 68 | + <div className="p-6 border-b border-slate-700"> |
| 69 | + <h2 className="text-xl font-semibold">Verified Asset Pipeline</h2> |
| 70 | + </div> |
| 71 | + |
| 72 | + <div className="relative"> |
| 73 | + <table className="w-full text-left"> |
| 74 | + <thead className="bg-tradeflow-dark/50 text-tradeflow-muted text-sm uppercase sticky top-0 z-10"> |
| 75 | + <tr> |
| 76 | + <th className="p-4">Invoice ID</th> |
| 77 | + <th className="p-4">Risk Score</th> |
| 78 | + <th className="p-4">Status</th> |
| 79 | + <th className="p-4">Amount</th> |
| 80 | + </tr> |
| 81 | + </thead> |
| 82 | + <tbody className="relative"> |
| 83 | + {isLoading && !invoicesData ? ( |
| 84 | + // Show skeleton rows on initial load |
| 85 | + Array.from({ length: itemsPerPage }).map((_, index) => ( |
| 86 | + <SkeletonRow key={`skeleton-${index}`} /> |
| 87 | + )) |
| 88 | + ) : ( |
| 89 | + invoicesData?.data.map((invoice) => ( |
| 90 | + <tr |
| 91 | + key={invoice.id} |
| 92 | + className={`border-b border-tradeflow-muted/50 hover:bg-tradeflow-muted/20 transition ${isFetching ? 'opacity-60' : '' |
| 93 | + }`} |
| 94 | + > |
| 95 | + <td className="p-4 font-mono text-sm text-blue-300"> |
| 96 | + #{invoice.id.slice(-6)} |
| 97 | + </td> |
| 98 | + <td className="p-4"> |
| 99 | + <div className="w-full bg-tradeflow-muted h-2 rounded-full max-w-25"> |
| 100 | + <div |
| 101 | + className="bg-blue-500 h-2 rounded-full transition-all duration-300" |
| 102 | + style={{ width: `${invoice.riskScore}%` }} |
| 103 | + ></div> |
| 104 | + </div> |
| 105 | + </td> |
| 106 | + <td className="p-4 text-sm font-medium"> |
| 107 | + <span |
| 108 | + className={`px-3 py-1 rounded-full ${invoice.status === "Approved" |
| 109 | + ? "bg-tradeflow-success/20 text-tradeflow-success" |
| 110 | + : "bg-tradeflow-warning/20 text-tradeflow-warning" |
| 111 | + }`} |
| 112 | + > |
| 113 | + {invoice.status} |
| 114 | + </span> |
| 115 | + </td> |
| 116 | + <td className="p-4 font-bold text-lg">${invoice.amount.toLocaleString()}</td> |
| 117 | + </tr> |
| 118 | + )) |
| 119 | + )} |
| 120 | + </tbody> |
| 121 | + </table> |
| 122 | + |
| 123 | + {/* Loading overlay for page changes */} |
| 124 | + {isFetching && !isLoading && ( |
| 125 | + <div className="absolute inset-0 bg-slate-900/20 flex items-center justify-center z-20"> |
| 126 | + <div className="text-blue-400 text-sm font-medium">Loading...</div> |
| 127 | + </div> |
| 128 | + )} |
| 129 | + </div> |
| 130 | + |
| 131 | + {/* Pagination Controls */} |
| 132 | + {invoicesData && ( |
| 133 | + <PaginationControls |
| 134 | + pagination={invoicesData.pagination} |
| 135 | + onPageChange={handlePageChange} |
| 136 | + isLoading={isFetching} |
| 137 | + /> |
| 138 | + )} |
| 139 | + </div> |
| 140 | + ); |
| 141 | +}; |
| 142 | + |
| 143 | +export default InvoiceTable; |
0 commit comments