@meterbox/react 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -39,6 +39,7 @@ import {
39
39
  CreditBalance,
40
40
  CreditGrantsTable,
41
41
  CreditUtilization,
42
+ InvoiceHistory,
42
43
  UsageChart,
43
44
  CheckoutButton,
44
45
  } from "@meterbox/react";
@@ -130,6 +131,13 @@ Table of active grants: source, amount, remaining, expiry, status. Pass
130
131
  Inline SVG line + area chart of credit balance over time, fed by
131
132
  `GET /v1/customer-portal/:id/credits/timeseries`.
132
133
 
134
+ ### `<InvoiceHistory invoices? | baseUrl customerId customerToken />`
135
+
136
+ The invoices the customer received, newest first: date, number, status and
137
+ amount, with a link to download each Stripe invoice's PDF and to its hosted
138
+ page (labelled "Pay" while it's due). Self-fetches
139
+ `GET /v1/customer-portal/:id/invoices` with the customer token.
140
+
133
141
  ### `<UsageChart series valueKey? height? label? />`
134
142
 
135
143
  Tiny inline SVG bar chart of a time series, typically the day-grain rollup
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@meterbox/react",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "description": "Embeddable React components for Meterbox: pricing tables, subscription preview, credit balance and usage charts. Presentational by default, with data fetched by your backend via @meterbox/node.",
5
5
  "license": "MIT",
6
6
  "homepage": "https://meterbox.ai",
@@ -0,0 +1,172 @@
1
+ /**
2
+ * <InvoiceHistory ... />
3
+ *
4
+ * The invoices a customer received, newest first, each with its PDF and the
5
+ * payment provider's hosted page (where an open invoice can be paid).
6
+ *
7
+ * 1. **Presentational**: <InvoiceHistory invoices={...} />, with the
8
+ * `invoices` array from `GET /v1/customer-portal/:id/invoices`.
9
+ * 2. **Self-fetching** (browser-safe):
10
+ * <InvoiceHistory baseUrl="https://meterbox.ai"
11
+ * customerId="cus_42"
12
+ * customerToken={jwt} />
13
+ * Fetches that endpoint with the customer-scoped JWT.
14
+ */
15
+
16
+ const React = require("react");
17
+
18
+ const styles = {
19
+ card: {
20
+ fontFamily: "system-ui, -apple-system, sans-serif",
21
+ color: "#111",
22
+ border: "1px solid #e5e7eb",
23
+ borderRadius: 12,
24
+ padding: 16,
25
+ background: "#fff",
26
+ display: "grid",
27
+ gap: 12,
28
+ maxWidth: 720,
29
+ },
30
+ title: { fontSize: 14, fontWeight: 700 },
31
+ table: { width: "100%", borderCollapse: "collapse", fontSize: 13 },
32
+ th: {
33
+ textAlign: "left",
34
+ padding: "6px 8px",
35
+ borderBottom: "1px solid #e5e7eb",
36
+ opacity: 0.6,
37
+ fontWeight: 600,
38
+ },
39
+ td: { padding: "6px 8px", borderBottom: "1px solid #f3f4f6" },
40
+ links: { display: "flex", gap: 10 },
41
+ link: { color: "#5045e8", textDecoration: "none", fontWeight: 600 },
42
+ muted: { opacity: 0.6 },
43
+ };
44
+
45
+ const STATUS = { open: "Due", paid: "Paid", void: "Void", uncollectible: "Uncollectible" };
46
+
47
+ // Amounts arrive in the currency's minor unit (cents for USD, yen for JPY).
48
+ function money(minor, currency) {
49
+ const code = String(currency || "usd").toUpperCase();
50
+ try {
51
+ const format = new Intl.NumberFormat(undefined, { style: "currency", currency: code });
52
+ const digits = format.resolvedOptions().maximumFractionDigits;
53
+ return format.format(minor / 10 ** digits);
54
+ } catch (_) {
55
+ return `${code} ${minor}`;
56
+ }
57
+ }
58
+
59
+ async function fetchInvoices({ baseUrl, customerId, customerToken, fetchImpl }) {
60
+ const url = `${String(baseUrl).replace(/\/+$/, "")}/v1/customer-portal/${encodeURIComponent(
61
+ customerId,
62
+ )}/invoices`;
63
+ const res = await fetchImpl(url, {
64
+ method: "GET",
65
+ headers: { Authorization: `Bearer ${customerToken}` },
66
+ });
67
+ const json = await res.json();
68
+ if (!res.ok) throw new Error(json?.error || `HTTP ${res.status}`);
69
+ return json.invoices || [];
70
+ }
71
+
72
+ function InvoiceHistory({ invoices, baseUrl, customerId, customerToken, fetch: fetchImpl }) {
73
+ const selfFetch = !invoices && baseUrl && customerId && customerToken;
74
+ const [fetched, setFetched] = React.useState(null);
75
+ const [error, setError] = React.useState(null);
76
+
77
+ React.useEffect(() => {
78
+ if (!selfFetch) return undefined;
79
+ let cancelled = false;
80
+ setError(null);
81
+ fetchInvoices({ baseUrl, customerId, customerToken, fetchImpl: fetchImpl || globalThis.fetch })
82
+ .then((list) => {
83
+ if (!cancelled) setFetched(list);
84
+ })
85
+ .catch((err) => {
86
+ if (!cancelled) setError(err.message || "Could not load invoices.");
87
+ });
88
+ return () => {
89
+ cancelled = true;
90
+ };
91
+ }, [selfFetch, baseUrl, customerId, customerToken, fetchImpl]);
92
+
93
+ const list = invoices || fetched;
94
+ let body;
95
+ if (error) {
96
+ body = React.createElement("span", { role: "alert", style: { color: "#dc2626" } }, error);
97
+ } else if (!list) {
98
+ body = React.createElement("span", { style: styles.muted }, "Loading invoices…");
99
+ } else if (list.length === 0) {
100
+ body = React.createElement("span", { style: styles.muted }, "No invoices yet.");
101
+ } else {
102
+ body = React.createElement(
103
+ "table",
104
+ { style: styles.table },
105
+ React.createElement(
106
+ "thead",
107
+ null,
108
+ React.createElement(
109
+ "tr",
110
+ null,
111
+ ["Date", "Invoice", "Status", "Amount", ""].map((h) =>
112
+ React.createElement("th", { key: h || "links", style: styles.th }, h),
113
+ ),
114
+ ),
115
+ ),
116
+ React.createElement(
117
+ "tbody",
118
+ null,
119
+ list.map((inv) =>
120
+ React.createElement(
121
+ "tr",
122
+ { key: inv.id },
123
+ React.createElement("td", { style: styles.td }, String(inv.created_at).slice(0, 10)),
124
+ React.createElement("td", { style: styles.td }, inv.number || inv.id),
125
+ React.createElement("td", { style: styles.td }, STATUS[inv.status] || inv.status),
126
+ React.createElement("td", { style: styles.td }, money(inv.amount_due, inv.currency)),
127
+ React.createElement(
128
+ "td",
129
+ { style: styles.td },
130
+ React.createElement(
131
+ "span",
132
+ { style: styles.links },
133
+ inv.pdf_url &&
134
+ React.createElement(
135
+ "a",
136
+ {
137
+ href: inv.pdf_url,
138
+ target: "_blank",
139
+ rel: "noopener noreferrer",
140
+ style: styles.link,
141
+ "aria-label": `Download PDF of ${inv.number || inv.id}`,
142
+ },
143
+ "PDF",
144
+ ),
145
+ inv.hosted_invoice_url &&
146
+ React.createElement(
147
+ "a",
148
+ {
149
+ href: inv.hosted_invoice_url,
150
+ target: "_blank",
151
+ rel: "noopener noreferrer",
152
+ style: styles.link,
153
+ },
154
+ inv.status === "open" ? "Pay" : "View",
155
+ ),
156
+ ),
157
+ ),
158
+ ),
159
+ ),
160
+ ),
161
+ );
162
+ }
163
+
164
+ return React.createElement(
165
+ "div",
166
+ { className: "meterbox-invoice-history", style: styles.card },
167
+ React.createElement("div", { style: styles.title }, "Invoices"),
168
+ body,
169
+ );
170
+ }
171
+
172
+ module.exports = InvoiceHistory;
package/src/index.js CHANGED
@@ -6,8 +6,8 @@
6
6
  * `@meterbox/node` and passes it as props. No admin key in the
7
7
  * browser bundle.
8
8
  * • **Self-fetching** — `SubscriptionPreview`, `PlanChangeCard`,
9
- * `CreditBalance`, `CreditGrantsTable`, `CreditUtilization`, and
10
- * `OutcomeHistory` can fetch their own data from the
9
+ * `CreditBalance`, `CreditGrantsTable`, `CreditUtilization`,
10
+ * `OutcomeHistory` and `InvoiceHistory` can fetch their own data from the
11
11
  * `/v1/customer-portal/*` endpoints using a short-lived customer-scoped
12
12
  * JWT minted server-side (`POST /v1/tokens/:id`).
13
13
  *
@@ -26,6 +26,7 @@ const CreditBalance = require("./CreditBalance");
26
26
  const CreditGrantsTable = require("./CreditGrantsTable");
27
27
  const CreditUtilization = require("./CreditUtilization");
28
28
  const OutcomeHistory = require("./OutcomeHistory");
29
+ const InvoiceHistory = require("./InvoiceHistory");
29
30
  const UsageChart = require("./UsageChart");
30
31
  const Paywall = require("./Paywall");
31
32
  const UsageProgress = require("./UsageProgress");
@@ -41,6 +42,7 @@ module.exports = {
41
42
  CreditGrantsTable,
42
43
  CreditUtilization,
43
44
  OutcomeHistory,
45
+ InvoiceHistory,
44
46
  UsageChart,
45
47
  Paywall,
46
48
  UsageProgress,