|
| 1 | +import dayjs from "dayjs"; |
| 2 | +import { AlertCircle, ArrowDownIcon, ArrowUpIcon } from "lucide-react"; |
| 3 | +import FormattedFiatAmount from "src/components/FormattedFiatAmount"; |
| 4 | +import { Alert, AlertDescription, AlertTitle } from "src/components/ui/alert"; |
| 5 | +import { |
| 6 | + Card, |
| 7 | + CardContent, |
| 8 | + CardHeader, |
| 9 | + CardTitle, |
| 10 | +} from "src/components/ui/card"; |
| 11 | +import { Table, TableBody, TableCell, TableRow } from "src/components/ui/table"; |
| 12 | +import { useOnchainTransactions } from "src/hooks/useOnchainTransactions"; |
| 13 | +import { cn } from "src/lib/utils"; |
| 14 | + |
| 15 | +export function OnchainTransactionsTable() { |
| 16 | + const { data: transactions, error, isLoading } = useOnchainTransactions(); |
| 17 | + |
| 18 | + if (isLoading) { |
| 19 | + return null; |
| 20 | + } |
| 21 | + |
| 22 | + if (error) { |
| 23 | + return ( |
| 24 | + <Alert variant="destructive" className="mt-4"> |
| 25 | + <AlertCircle className="h-4 w-4" /> |
| 26 | + <AlertTitle>Error loading on-chain transactions</AlertTitle> |
| 27 | + <AlertDescription>{error.message}</AlertDescription> |
| 28 | + </Alert> |
| 29 | + ); |
| 30 | + } |
| 31 | + |
| 32 | + if (!transactions?.length) { |
| 33 | + return null; |
| 34 | + } |
| 35 | + |
| 36 | + return ( |
| 37 | + <Card className="mt-6"> |
| 38 | + <CardHeader> |
| 39 | + <CardTitle className="text-2xl">On-Chain Transactions</CardTitle> |
| 40 | + </CardHeader> |
| 41 | + <CardContent> |
| 42 | + <Table> |
| 43 | + <TableBody> |
| 44 | + {transactions.map((tx) => { |
| 45 | + const Icon = tx.type == "outgoing" ? ArrowUpIcon : ArrowDownIcon; |
| 46 | + return ( |
| 47 | + <TableRow |
| 48 | + key={tx.txId} |
| 49 | + className="cursor-pointer" |
| 50 | + onClick={() => { |
| 51 | + window.open( |
| 52 | + `https://mempool.space/tx/${tx.txId}`, |
| 53 | + "_blank" |
| 54 | + ); |
| 55 | + }} |
| 56 | + > |
| 57 | + <TableCell className="flex items-center gap-2"> |
| 58 | + <div |
| 59 | + className={cn( |
| 60 | + "flex justify-center items-center rounded-full w-10 h-10 relative", |
| 61 | + tx.state === "unconfirmed" |
| 62 | + ? "bg-blue-100 dark:bg-sky-950 animate-pulse" |
| 63 | + : tx.type === "outgoing" |
| 64 | + ? "bg-orange-100 dark:bg-amber-950" |
| 65 | + : "bg-green-100 dark:bg-emerald-950" |
| 66 | + )} |
| 67 | + title={`${tx.numConfirmations} confirmations`} |
| 68 | + > |
| 69 | + <Icon |
| 70 | + strokeWidth={3} |
| 71 | + className={cn( |
| 72 | + "w-6 h-6", |
| 73 | + tx.state === "unconfirmed" |
| 74 | + ? "stroke-blue-500 dark:stroke-sky-500" |
| 75 | + : tx.type === "outgoing" |
| 76 | + ? "stroke-orange-500 dark:stroke-amber-500" |
| 77 | + : "stroke-green-500 dark:stroke-teal-500" |
| 78 | + )} |
| 79 | + /> |
| 80 | + </div> |
| 81 | + <div className="md:flex md:gap-2 md:items-center"> |
| 82 | + <p className="font-semibold text-lg"> |
| 83 | + {tx.type == "outgoing" |
| 84 | + ? tx.state === "confirmed" |
| 85 | + ? "Sent" |
| 86 | + : "Sending" |
| 87 | + : tx.state === "confirmed" |
| 88 | + ? "Received" |
| 89 | + : "Receiving"} |
| 90 | + </p> |
| 91 | + <p |
| 92 | + className="text-muted-foreground" |
| 93 | + title={dayjs(tx.updatedAt * 1000) |
| 94 | + .local() |
| 95 | + .format("D MMMM YYYY, HH:mm")} |
| 96 | + > |
| 97 | + {dayjs(tx.updatedAt * 1000) |
| 98 | + .local() |
| 99 | + .fromNow()} |
| 100 | + </p> |
| 101 | + </div> |
| 102 | + </TableCell> |
| 103 | + |
| 104 | + <TableCell> |
| 105 | + <div className="flex flex-col items-end"> |
| 106 | + <div className="flex flex-row gap-1"> |
| 107 | + <p |
| 108 | + className={cn( |
| 109 | + tx.type == "incoming" && |
| 110 | + "text-green-600 dark:text-emerald-500" |
| 111 | + )} |
| 112 | + > |
| 113 | + {tx.type == "outgoing" ? "-" : "+"} |
| 114 | + <span className="font-medium"> |
| 115 | + {new Intl.NumberFormat().format(tx.amountSat)} |
| 116 | + </span> |
| 117 | + </p> |
| 118 | + <p className="text-muted-foreground"> |
| 119 | + {tx.amountSat == 1 ? "sat" : "sats"} |
| 120 | + </p> |
| 121 | + </div> |
| 122 | + <FormattedFiatAmount |
| 123 | + className="text-xs" |
| 124 | + amount={tx.amountSat} |
| 125 | + /> |
| 126 | + </div> |
| 127 | + </TableCell> |
| 128 | + </TableRow> |
| 129 | + ); |
| 130 | + })} |
| 131 | + </TableBody> |
| 132 | + </Table> |
| 133 | + </CardContent> |
| 134 | + </Card> |
| 135 | + ); |
| 136 | +} |
0 commit comments