import { Calendar } from "lucide-react"; import Link from "next/link"; import { redirect } from "next/navigation"; import { Table, TableBody, TableCaption, TableCell, TableFooter, TableHead, TableHeader, TableRow, } from "@/components/ui/table"; import { WalletTransaction } from "@/lib/backend-types"; import { cn } from "@/lib/utils"; import { getWaleltTransactions } from "@/queries/wallet"; import { tryCatch } from "@/utils/tryCatch"; import Pagination from "./pagination"; import { Badge } from "./ui/badge"; import { Button } from "./ui/button"; export async function WalletTransactionsTable({ searchParams, }: { searchParams: Promise<{ [key: string]: unknown; }>; }) { const resolvedParams = await searchParams; const page = Number.parseInt(resolvedParams.page as string) || 1; const limit = 10; const offset = (page - 1) * limit; // Build params object const apiParams: Record = {}; for (const [key, value] of Object.entries(resolvedParams)) { if (value !== undefined && value !== "") { apiParams[key] = typeof value === "number" ? value : String(value); } } apiParams.limit = limit; apiParams.offset = offset; const [error, transactions] = await tryCatch( getWaleltTransactions(apiParams), ); if (error) { if (error.message.includes("Unauthorized")) { redirect("/auth/signin"); } else { return
{JSON.stringify(error, null, 2)}
; } } const { data, meta } = transactions; return (
{data?.length === 0 ? (

No transactions yet.

) : ( <>
Table of all transactions. Description Amount Transaction Type View Details Created at {transactions?.data?.map((trx) => ( {trx.description} {trx.amount.toFixed(2)} MVR {trx.transaction_type === "CREDIT" ? ( {trx.transaction_type} ) : ( {trx.transaction_type} )} {new Date(trx.created_at).toLocaleDateString("en-US", { month: "short", day: "2-digit", year: "numeric", minute: "2-digit", hour: "2-digit", })} ))} {meta?.total === 1 ? (

Total {meta?.total} transaction.

) : (

Total {meta?.total} transactions.

)}
{data.map((trx) => ( ))}
)}
); } function MobileTransactionDetails({ trx }: { trx: WalletTransaction }) { return (
{new Date(trx.created_at).toLocaleDateString("en-US", { month: "short", day: "2-digit", year: "numeric", minute: "2-digit", hour: "2-digit", })}

{trx.description}

Amount

{trx.amount.toFixed(2)} MVR
{trx.transaction_type === "CREDIT" ? ( {trx.transaction_type} ) : ( {trx.transaction_type} )}
); }