-
Notifications
You must be signed in to change notification settings - Fork 5
/
InvoicesDataTable.tsx
135 lines (130 loc) · 4.04 KB
/
InvoicesDataTable.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import { Button, Menu, NumberFormatter, Text } from "@mantine/core"
import { useNavigate, useParams } from "@remix-run/react"
import { DataTable } from "mantine-datatable"
import React from "react"
import ContextMenuTarget from "~/components/ContextMenuTarget"
import { Stripe } from "~/models/stripe/stripe.server"
import { AnalyticActions, AnalyticCategories, trackEvent } from "~/utils/analytics"
import {
formatStripeDate,
getStripeAmount,
INVOICE_STATUS_COLOR,
} from "~/utils/billingUtils"
type InvoicesDataTableProps = {
invoices: Stripe.Invoice[]
usageRecords?: Stripe.UsageRecordSummary[]
}
const InvoicesDataTable = ({ invoices, usageRecords }: InvoicesDataTableProps) => {
const navigate = useNavigate()
const { accountId } = useParams()
const invoicesWithUSage = invoices.map((invoice) => {
const invoiceUsageRecord = usageRecords?.find(
(usageRecord) => usageRecord.invoice === invoice.id,
)
return {
id: invoice.id,
number: invoice.number,
status: invoice.status,
amount_due: invoice.amount_due,
period_start: invoice.period_start,
period_end: invoice.period_end,
total_usage: invoiceUsageRecord?.total_usage,
charge: invoice.charge,
invoice_pdf: invoice.invoice_pdf,
}
})
return (
<DataTable
highlightOnHover
className="clickable-table"
columns={[
{
accessor: "number",
title: "Invoice ID",
},
{
accessor: "status",
render: ({ status }) => (
<Text c={status ? INVOICE_STATUS_COLOR[status] : ""} tt="capitalize">
{status}
</Text>
),
title: "Status",
},
{
accessor: "total_usage",
title: "Total Relays Billed",
noWrap: true,
render: ({ total_usage }) =>
total_usage ? <NumberFormatter thousandSeparator value={total_usage} /> : "—",
},
{
accessor: "amount_due",
title: "Amount in USD",
noWrap: true,
render: ({ amount_due }) => (
<NumberFormatter
thousandSeparator
prefix="USD $"
value={getStripeAmount(amount_due)}
/>
),
},
{
accessor: "period_start",
title: "Start Date",
render: ({ period_start }) =>
formatStripeDate(period_start, "Do MMM YYYY, HH:mm"),
},
{
accessor: "period_end",
title: "End Date",
render: ({ period_end }) => formatStripeDate(period_end, "Do MMM YYYY, HH:mm"),
},
{
accessor: "action",
title: "",
render: ({ invoice_pdf, id }) => (
<Menu>
<ContextMenuTarget variant="subtle" />
<Menu.Dropdown>
<Menu.Item>
<Button
color="gray"
component="a"
href={invoice_pdf ?? ""}
rel="noreferrer"
size="compact-sm"
target="_blank"
variant="subtle"
onClick={() => {
trackEvent({
category: AnalyticCategories.account,
action: AnalyticActions.account_billing_invoice_download,
label: `Account: ${accountId} / Invoice: ${id}`,
})
}}
>
Download Invoice PDF
</Button>
</Menu.Item>
</Menu.Dropdown>
</Menu>
),
},
]}
minHeight={500}
records={invoicesWithUSage}
verticalSpacing={5}
onRowClick={({ record }) => {
trackEvent({
category: AnalyticCategories.account,
action: AnalyticActions.account_billing_invoice_view,
label: `Account: ${accountId} / Invoice: ${record.id}`,
})
navigate(`${record.id}`)
}}
/>
)
}
export default InvoicesDataTable