@meterbox/react 0.1.0
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/LICENSE +21 -0
- package/README.md +187 -0
- package/package.json +33 -0
- package/src/CheckoutButton.js +137 -0
- package/src/CreditBalance.js +165 -0
- package/src/CreditGrantsTable.js +209 -0
- package/src/CreditUtilization.js +177 -0
- package/src/OutcomeHistory.js +206 -0
- package/src/Paywall.js +146 -0
- package/src/PlanChangeCard.js +270 -0
- package/src/PlanComparison.js +118 -0
- package/src/PricingTable.js +259 -0
- package/src/SubscriptionPreview.js +225 -0
- package/src/TrialBanner.js +119 -0
- package/src/UsageChart.js +91 -0
- package/src/UsageProgress.js +124 -0
- package/src/index.js +51 -0
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* <CreditGrantsTable ... />
|
|
3
|
+
*
|
|
4
|
+
* Two modes of operation:
|
|
5
|
+
*
|
|
6
|
+
* 1. **Presentational** (default):
|
|
7
|
+
* <CreditGrantsTable grants={...} />
|
|
8
|
+
* The tenant fetches `GET /v1/customers/:id/credits` server-side via
|
|
9
|
+
* `@meterbox/node` and passes `grants` (and optionally `balance_usd`)
|
|
10
|
+
* in as props.
|
|
11
|
+
*
|
|
12
|
+
* 2. **Self-fetching** (browser-safe):
|
|
13
|
+
* <CreditGrantsTable baseUrl="https://meterbox.ai"
|
|
14
|
+
* customerId="cus_42"
|
|
15
|
+
* customerToken={jwt} />
|
|
16
|
+
* Fetches `GET /v1/customer-portal/:id/credits` directly with the
|
|
17
|
+
* customer-scoped JWT. Re-fetches when `customerId` changes.
|
|
18
|
+
*
|
|
19
|
+
* Renders one row per grant — amount, remaining, source, expiry, applied-to
|
|
20
|
+
* scope. Pass `filter={(g) => ...}` to slice (e.g. active-only).
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
const React = require("react");
|
|
24
|
+
|
|
25
|
+
const styles = {
|
|
26
|
+
card: {
|
|
27
|
+
fontFamily: "system-ui, -apple-system, sans-serif",
|
|
28
|
+
color: "#111",
|
|
29
|
+
border: "1px solid #e5e7eb",
|
|
30
|
+
borderRadius: 12,
|
|
31
|
+
padding: 16,
|
|
32
|
+
background: "#fff",
|
|
33
|
+
display: "grid",
|
|
34
|
+
gap: 12,
|
|
35
|
+
maxWidth: 720,
|
|
36
|
+
},
|
|
37
|
+
header: { display: "flex", justifyContent: "space-between", alignItems: "baseline" },
|
|
38
|
+
title: { fontSize: 14, fontWeight: 700 },
|
|
39
|
+
balance: { fontSize: 13, opacity: 0.7 },
|
|
40
|
+
table: { width: "100%", borderCollapse: "collapse", fontSize: 13 },
|
|
41
|
+
th: { textAlign: "left", padding: "6px 8px", borderBottom: "1px solid #e5e7eb", opacity: 0.6, fontWeight: 600 },
|
|
42
|
+
td: { padding: "6px 8px", borderBottom: "1px solid #f3f4f6" },
|
|
43
|
+
pill: {
|
|
44
|
+
display: "inline-block",
|
|
45
|
+
padding: "1px 8px",
|
|
46
|
+
borderRadius: 999,
|
|
47
|
+
fontSize: 11,
|
|
48
|
+
fontWeight: 600,
|
|
49
|
+
background: "#eef2ff",
|
|
50
|
+
color: "#5045e8",
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
function dollars(n) {
|
|
55
|
+
if (n == null) return "—";
|
|
56
|
+
return `$${Number(n).toLocaleString(undefined, { maximumFractionDigits: 2 })}`;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function remaining(g) {
|
|
60
|
+
const amt = Number(g.amount_usd || 0);
|
|
61
|
+
const consumed = Number(g.consumed_usd || 0);
|
|
62
|
+
return Math.max(amt - consumed, 0);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function fmtDate(iso) {
|
|
66
|
+
if (!iso) return "—";
|
|
67
|
+
const d = new Date(iso);
|
|
68
|
+
if (Number.isNaN(d.getTime())) return iso;
|
|
69
|
+
return d.toISOString().slice(0, 10);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async function fetchCredits({ baseUrl, customerId, customerToken, fetchImpl }) {
|
|
73
|
+
const url = `${String(baseUrl).replace(/\/+$/, "")}/v1/customer-portal/${encodeURIComponent(
|
|
74
|
+
customerId,
|
|
75
|
+
)}/credits`;
|
|
76
|
+
const res = await fetchImpl(url, {
|
|
77
|
+
method: "GET",
|
|
78
|
+
headers: { Authorization: `Bearer ${customerToken}` },
|
|
79
|
+
});
|
|
80
|
+
const json = await res.json();
|
|
81
|
+
if (!res.ok) {
|
|
82
|
+
const err = new Error(json?.error || `HTTP ${res.status}`);
|
|
83
|
+
err.status = res.status;
|
|
84
|
+
throw err;
|
|
85
|
+
}
|
|
86
|
+
return json;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function CreditGrantsTable({
|
|
90
|
+
grants,
|
|
91
|
+
balance_usd,
|
|
92
|
+
filter,
|
|
93
|
+
baseUrl,
|
|
94
|
+
customerId,
|
|
95
|
+
customerToken,
|
|
96
|
+
fetch: fetchImpl,
|
|
97
|
+
}) {
|
|
98
|
+
const selfFetch = !grants && baseUrl && customerId && customerToken;
|
|
99
|
+
const [fetched, setFetched] = React.useState(null);
|
|
100
|
+
const [error, setError] = React.useState(null);
|
|
101
|
+
const [loading, setLoading] = React.useState(false);
|
|
102
|
+
|
|
103
|
+
React.useEffect(() => {
|
|
104
|
+
if (!selfFetch) return undefined;
|
|
105
|
+
let cancelled = false;
|
|
106
|
+
setLoading(true);
|
|
107
|
+
setError(null);
|
|
108
|
+
fetchCredits({
|
|
109
|
+
baseUrl,
|
|
110
|
+
customerId,
|
|
111
|
+
customerToken,
|
|
112
|
+
fetchImpl: fetchImpl || globalThis.fetch,
|
|
113
|
+
})
|
|
114
|
+
.then((body) => {
|
|
115
|
+
if (!cancelled) {
|
|
116
|
+
setFetched(body);
|
|
117
|
+
setLoading(false);
|
|
118
|
+
}
|
|
119
|
+
})
|
|
120
|
+
.catch((err) => {
|
|
121
|
+
if (!cancelled) {
|
|
122
|
+
setError(err.message || "Could not load credits.");
|
|
123
|
+
setLoading(false);
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
return () => {
|
|
127
|
+
cancelled = true;
|
|
128
|
+
};
|
|
129
|
+
}, [selfFetch, baseUrl, customerId, customerToken, fetchImpl]);
|
|
130
|
+
|
|
131
|
+
if (loading) {
|
|
132
|
+
return React.createElement(
|
|
133
|
+
"div",
|
|
134
|
+
{ style: { opacity: 0.6, fontFamily: styles.card.fontFamily } },
|
|
135
|
+
"Loading credit grants…",
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
if (error) {
|
|
139
|
+
return React.createElement(
|
|
140
|
+
"div",
|
|
141
|
+
{ style: { color: "#dc2626", fontFamily: styles.card.fontFamily } },
|
|
142
|
+
error,
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
const list = (grants || fetched?.grants) ?? null;
|
|
146
|
+
const bal = balance_usd ?? fetched?.balance_usd ?? null;
|
|
147
|
+
if (!Array.isArray(list)) {
|
|
148
|
+
return React.createElement(
|
|
149
|
+
"div",
|
|
150
|
+
{ style: { opacity: 0.6, fontFamily: styles.card.fontFamily } },
|
|
151
|
+
"No credit grants.",
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
const rows = typeof filter === "function" ? list.filter(filter) : list;
|
|
155
|
+
return React.createElement(
|
|
156
|
+
"div",
|
|
157
|
+
{ className: "meterbox-credit-grants-table", style: styles.card },
|
|
158
|
+
React.createElement(
|
|
159
|
+
"div",
|
|
160
|
+
{ style: styles.header },
|
|
161
|
+
React.createElement("div", { style: styles.title }, "Credit grants"),
|
|
162
|
+
bal != null && React.createElement("div", { style: styles.balance }, `Balance ${dollars(bal)}`),
|
|
163
|
+
),
|
|
164
|
+
rows.length === 0
|
|
165
|
+
? React.createElement(
|
|
166
|
+
"div",
|
|
167
|
+
{ style: { opacity: 0.6, fontSize: 13 } },
|
|
168
|
+
"No matching grants.",
|
|
169
|
+
)
|
|
170
|
+
: React.createElement(
|
|
171
|
+
"table",
|
|
172
|
+
{ style: styles.table },
|
|
173
|
+
React.createElement(
|
|
174
|
+
"thead",
|
|
175
|
+
null,
|
|
176
|
+
React.createElement(
|
|
177
|
+
"tr",
|
|
178
|
+
null,
|
|
179
|
+
React.createElement("th", { style: styles.th }, "Source"),
|
|
180
|
+
React.createElement("th", { style: styles.th }, "Amount"),
|
|
181
|
+
React.createElement("th", { style: styles.th }, "Remaining"),
|
|
182
|
+
React.createElement("th", { style: styles.th }, "Expires"),
|
|
183
|
+
React.createElement("th", { style: styles.th }, "Status"),
|
|
184
|
+
),
|
|
185
|
+
),
|
|
186
|
+
React.createElement(
|
|
187
|
+
"tbody",
|
|
188
|
+
null,
|
|
189
|
+
rows.map((g) =>
|
|
190
|
+
React.createElement(
|
|
191
|
+
"tr",
|
|
192
|
+
{ key: g.id },
|
|
193
|
+
React.createElement("td", { style: styles.td }, g.reason || g.source || g.type || "—"),
|
|
194
|
+
React.createElement("td", { style: styles.td }, dollars(g.amount_usd)),
|
|
195
|
+
React.createElement("td", { style: styles.td }, dollars(remaining(g))),
|
|
196
|
+
React.createElement("td", { style: styles.td }, fmtDate(g.expires_at)),
|
|
197
|
+
React.createElement(
|
|
198
|
+
"td",
|
|
199
|
+
{ style: styles.td },
|
|
200
|
+
React.createElement("span", { style: styles.pill }, g.status || "active"),
|
|
201
|
+
),
|
|
202
|
+
),
|
|
203
|
+
),
|
|
204
|
+
),
|
|
205
|
+
),
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
module.exports = CreditGrantsTable;
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* <CreditUtilization ... />
|
|
3
|
+
*
|
|
4
|
+
* Inline SVG line chart of credit balance over time. Backend shape matches
|
|
5
|
+
* `GET /v1/customers/:id/credits/timeseries` (and the equivalent customer-
|
|
6
|
+
* portal endpoint): `{from, to, granularity, points: [{timestamp, balance_usd}]}`.
|
|
7
|
+
*
|
|
8
|
+
* Two modes:
|
|
9
|
+
*
|
|
10
|
+
* 1. **Presentational**:
|
|
11
|
+
* <CreditUtilization series={timeseries} />
|
|
12
|
+
*
|
|
13
|
+
* 2. **Self-fetching**:
|
|
14
|
+
* <CreditUtilization baseUrl="..." customerId="cus_42"
|
|
15
|
+
* customerToken={jwt}
|
|
16
|
+
* from="2026-05-01" to="2026-06-01"
|
|
17
|
+
* granularity="day" />
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
const React = require("react");
|
|
21
|
+
|
|
22
|
+
const styles = {
|
|
23
|
+
wrap: {
|
|
24
|
+
fontFamily: "system-ui, -apple-system, sans-serif",
|
|
25
|
+
color: "#111",
|
|
26
|
+
border: "1px solid #e5e7eb",
|
|
27
|
+
borderRadius: 12,
|
|
28
|
+
padding: 16,
|
|
29
|
+
background: "#fff",
|
|
30
|
+
display: "grid",
|
|
31
|
+
gap: 8,
|
|
32
|
+
maxWidth: 480,
|
|
33
|
+
},
|
|
34
|
+
label: { fontSize: 12, opacity: 0.6, textTransform: "uppercase", letterSpacing: 0.4 },
|
|
35
|
+
current: { fontSize: 24, fontWeight: 700 },
|
|
36
|
+
area: { fill: "rgba(67, 56, 202, 0.15)" },
|
|
37
|
+
line: { fill: "none", stroke: "#5045e8", strokeWidth: 1.5 },
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
function dollars(n) {
|
|
41
|
+
if (n == null) return "—";
|
|
42
|
+
return `$${Number(n).toLocaleString(undefined, { maximumFractionDigits: 2 })}`;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
async function fetchTimeseries({
|
|
46
|
+
baseUrl,
|
|
47
|
+
customerId,
|
|
48
|
+
customerToken,
|
|
49
|
+
from,
|
|
50
|
+
to,
|
|
51
|
+
granularity,
|
|
52
|
+
fetchImpl,
|
|
53
|
+
}) {
|
|
54
|
+
const qs = new URLSearchParams();
|
|
55
|
+
if (from) qs.set("from", from);
|
|
56
|
+
if (to) qs.set("to", to);
|
|
57
|
+
if (granularity) qs.set("granularity", granularity);
|
|
58
|
+
const q = qs.toString() ? `?${qs.toString()}` : "";
|
|
59
|
+
const url = `${String(baseUrl).replace(/\/+$/, "")}/v1/customer-portal/${encodeURIComponent(
|
|
60
|
+
customerId,
|
|
61
|
+
)}/credits/timeseries${q}`;
|
|
62
|
+
const res = await fetchImpl(url, {
|
|
63
|
+
method: "GET",
|
|
64
|
+
headers: { Authorization: `Bearer ${customerToken}` },
|
|
65
|
+
});
|
|
66
|
+
const json = await res.json();
|
|
67
|
+
if (!res.ok) {
|
|
68
|
+
const err = new Error(json?.error || `HTTP ${res.status}`);
|
|
69
|
+
err.status = res.status;
|
|
70
|
+
throw err;
|
|
71
|
+
}
|
|
72
|
+
return json;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function CreditUtilization({
|
|
76
|
+
series,
|
|
77
|
+
baseUrl,
|
|
78
|
+
customerId,
|
|
79
|
+
customerToken,
|
|
80
|
+
from,
|
|
81
|
+
to,
|
|
82
|
+
granularity,
|
|
83
|
+
height = 120,
|
|
84
|
+
fetch: fetchImpl,
|
|
85
|
+
}) {
|
|
86
|
+
const selfFetch = !series && baseUrl && customerId && customerToken;
|
|
87
|
+
const [fetched, setFetched] = React.useState(null);
|
|
88
|
+
const [error, setError] = React.useState(null);
|
|
89
|
+
const [loading, setLoading] = React.useState(false);
|
|
90
|
+
|
|
91
|
+
React.useEffect(() => {
|
|
92
|
+
if (!selfFetch) return undefined;
|
|
93
|
+
let cancelled = false;
|
|
94
|
+
setLoading(true);
|
|
95
|
+
setError(null);
|
|
96
|
+
fetchTimeseries({
|
|
97
|
+
baseUrl,
|
|
98
|
+
customerId,
|
|
99
|
+
customerToken,
|
|
100
|
+
from,
|
|
101
|
+
to,
|
|
102
|
+
granularity,
|
|
103
|
+
fetchImpl: fetchImpl || globalThis.fetch,
|
|
104
|
+
})
|
|
105
|
+
.then((body) => {
|
|
106
|
+
if (!cancelled) {
|
|
107
|
+
setFetched(body);
|
|
108
|
+
setLoading(false);
|
|
109
|
+
}
|
|
110
|
+
})
|
|
111
|
+
.catch((err) => {
|
|
112
|
+
if (!cancelled) {
|
|
113
|
+
setError(err.message || "Could not load utilization.");
|
|
114
|
+
setLoading(false);
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
return () => {
|
|
118
|
+
cancelled = true;
|
|
119
|
+
};
|
|
120
|
+
}, [selfFetch, baseUrl, customerId, customerToken, from, to, granularity, fetchImpl]);
|
|
121
|
+
|
|
122
|
+
if (loading) {
|
|
123
|
+
return React.createElement(
|
|
124
|
+
"div",
|
|
125
|
+
{ style: { opacity: 0.6, fontFamily: styles.wrap.fontFamily } },
|
|
126
|
+
"Loading utilization…",
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
if (error) {
|
|
130
|
+
return React.createElement(
|
|
131
|
+
"div",
|
|
132
|
+
{ style: { color: "#dc2626", fontFamily: styles.wrap.fontFamily } },
|
|
133
|
+
error,
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
const points = Array.isArray(series) ? series : fetched?.points;
|
|
137
|
+
if (!Array.isArray(points) || points.length === 0) {
|
|
138
|
+
return React.createElement(
|
|
139
|
+
"div",
|
|
140
|
+
{ style: { opacity: 0.6, fontFamily: styles.wrap.fontFamily } },
|
|
141
|
+
"No utilization in window.",
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
const values = points.map((p) => Number(p.balance_usd || 0));
|
|
145
|
+
const max = Math.max(...values, 0.0001);
|
|
146
|
+
const last = values[values.length - 1];
|
|
147
|
+
// Build a polyline path across the SVG viewBox (0..100 in both axes).
|
|
148
|
+
const stepX = points.length === 1 ? 100 : 100 / (points.length - 1);
|
|
149
|
+
const coords = values.map((v, i) => {
|
|
150
|
+
const x = i * stepX;
|
|
151
|
+
const y = 100 - (v / max) * 100;
|
|
152
|
+
return [x, y];
|
|
153
|
+
});
|
|
154
|
+
const linePath = coords.map(([x, y], i) => `${i === 0 ? "M" : "L"}${x.toFixed(2)},${y.toFixed(2)}`).join(" ");
|
|
155
|
+
const areaPath = `${linePath} L100,100 L0,100 Z`;
|
|
156
|
+
return React.createElement(
|
|
157
|
+
"div",
|
|
158
|
+
{ className: "meterbox-credit-utilization", style: styles.wrap },
|
|
159
|
+
React.createElement("div", { style: styles.label }, "Credit balance"),
|
|
160
|
+
React.createElement("div", { style: styles.current }, dollars(last)),
|
|
161
|
+
React.createElement(
|
|
162
|
+
"svg",
|
|
163
|
+
{
|
|
164
|
+
role: "img",
|
|
165
|
+
"aria-label": "Credit balance over time",
|
|
166
|
+
width: "100%",
|
|
167
|
+
height,
|
|
168
|
+
preserveAspectRatio: "none",
|
|
169
|
+
viewBox: "0 0 100 100",
|
|
170
|
+
},
|
|
171
|
+
React.createElement("path", { d: areaPath, style: styles.area }),
|
|
172
|
+
React.createElement("path", { d: linePath, style: styles.line }),
|
|
173
|
+
),
|
|
174
|
+
);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
module.exports = CreditUtilization;
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* <OutcomeHistory ... />
|
|
3
|
+
*
|
|
4
|
+
* Two modes of operation:
|
|
5
|
+
*
|
|
6
|
+
* 1. **Presentational** (default):
|
|
7
|
+
* <OutcomeHistory outcomes={...} />
|
|
8
|
+
* The tenant fetches `GET /v1/customers/:id/outcomes` server-side via
|
|
9
|
+
* `@meterbox/node` and passes `outcomes` in as a prop.
|
|
10
|
+
*
|
|
11
|
+
* 2. **Self-fetching** (browser-safe):
|
|
12
|
+
* <OutcomeHistory baseUrl="https://meterbox.ai"
|
|
13
|
+
* customerId="cus_42"
|
|
14
|
+
* customerToken={jwt} />
|
|
15
|
+
* Fetches `GET /v1/customer-portal/:id/outcomes` directly with the
|
|
16
|
+
* customer-scoped JWT. Re-fetches when `customerId` changes.
|
|
17
|
+
*
|
|
18
|
+
* Renders one row per claimed outcome — name, amount, status, claimed date.
|
|
19
|
+
* `dispute_reason` is never shown even if present in `outcomes` passed as a
|
|
20
|
+
* prop (it's typically internal tenant reasoning) — matches what the
|
|
21
|
+
* self-fetching /customer-portal endpoint already strips server-side.
|
|
22
|
+
* Requires the tenant's advanced_outcomes_enabled toggle; the self-fetching
|
|
23
|
+
* mode surfaces the resulting 409 as its error state.
|
|
24
|
+
* Pass `filter={(o) => ...}` to slice (e.g. confirmed-only).
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
const React = require("react");
|
|
28
|
+
|
|
29
|
+
const styles = {
|
|
30
|
+
card: {
|
|
31
|
+
fontFamily: "system-ui, -apple-system, sans-serif",
|
|
32
|
+
color: "#111",
|
|
33
|
+
border: "1px solid #e5e7eb",
|
|
34
|
+
borderRadius: 12,
|
|
35
|
+
padding: 16,
|
|
36
|
+
background: "#fff",
|
|
37
|
+
display: "grid",
|
|
38
|
+
gap: 12,
|
|
39
|
+
maxWidth: 720,
|
|
40
|
+
},
|
|
41
|
+
title: { fontSize: 14, fontWeight: 700 },
|
|
42
|
+
table: { width: "100%", borderCollapse: "collapse", fontSize: 13 },
|
|
43
|
+
th: {
|
|
44
|
+
textAlign: "left",
|
|
45
|
+
padding: "6px 8px",
|
|
46
|
+
borderBottom: "1px solid #e5e7eb",
|
|
47
|
+
opacity: 0.6,
|
|
48
|
+
fontWeight: 600,
|
|
49
|
+
},
|
|
50
|
+
td: { padding: "6px 8px", borderBottom: "1px solid #f3f4f6" },
|
|
51
|
+
pill: {
|
|
52
|
+
display: "inline-block",
|
|
53
|
+
padding: "1px 8px",
|
|
54
|
+
borderRadius: 999,
|
|
55
|
+
fontSize: 11,
|
|
56
|
+
fontWeight: 600,
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const STATUS_STYLE = {
|
|
61
|
+
pending_verification: { background: "#fef3c7", color: "#92400e" },
|
|
62
|
+
confirmed: { background: "#dcfce7", color: "#166534" },
|
|
63
|
+
disputed: { background: "#fee2e2", color: "#991b1b" },
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
function dollars(n) {
|
|
67
|
+
if (n == null) return "—";
|
|
68
|
+
return `$${Number(n).toLocaleString(undefined, { maximumFractionDigits: 2 })}`;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function fmtDate(iso) {
|
|
72
|
+
if (!iso) return "—";
|
|
73
|
+
const d = new Date(iso);
|
|
74
|
+
if (Number.isNaN(d.getTime())) return iso;
|
|
75
|
+
return d.toISOString().slice(0, 10);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
async function fetchOutcomes({ baseUrl, customerId, customerToken, fetchImpl }) {
|
|
79
|
+
const url = `${String(baseUrl).replace(/\/+$/, "")}/v1/customer-portal/${encodeURIComponent(
|
|
80
|
+
customerId,
|
|
81
|
+
)}/outcomes`;
|
|
82
|
+
const res = await fetchImpl(url, {
|
|
83
|
+
method: "GET",
|
|
84
|
+
headers: { Authorization: `Bearer ${customerToken}` },
|
|
85
|
+
});
|
|
86
|
+
const json = await res.json();
|
|
87
|
+
if (!res.ok) {
|
|
88
|
+
const err = new Error(json?.error || `HTTP ${res.status}`);
|
|
89
|
+
err.status = res.status;
|
|
90
|
+
throw err;
|
|
91
|
+
}
|
|
92
|
+
return json;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function OutcomeHistory({
|
|
96
|
+
outcomes,
|
|
97
|
+
filter,
|
|
98
|
+
baseUrl,
|
|
99
|
+
customerId,
|
|
100
|
+
customerToken,
|
|
101
|
+
fetch: fetchImpl,
|
|
102
|
+
}) {
|
|
103
|
+
const selfFetch = !outcomes && baseUrl && customerId && customerToken;
|
|
104
|
+
const [fetched, setFetched] = React.useState(null);
|
|
105
|
+
const [error, setError] = React.useState(null);
|
|
106
|
+
const [loading, setLoading] = React.useState(false);
|
|
107
|
+
|
|
108
|
+
React.useEffect(() => {
|
|
109
|
+
if (!selfFetch) return undefined;
|
|
110
|
+
let cancelled = false;
|
|
111
|
+
setLoading(true);
|
|
112
|
+
setError(null);
|
|
113
|
+
fetchOutcomes({
|
|
114
|
+
baseUrl,
|
|
115
|
+
customerId,
|
|
116
|
+
customerToken,
|
|
117
|
+
fetchImpl: fetchImpl || globalThis.fetch,
|
|
118
|
+
})
|
|
119
|
+
.then((body) => {
|
|
120
|
+
if (!cancelled) {
|
|
121
|
+
setFetched(body);
|
|
122
|
+
setLoading(false);
|
|
123
|
+
}
|
|
124
|
+
})
|
|
125
|
+
.catch((err) => {
|
|
126
|
+
if (!cancelled) {
|
|
127
|
+
setError(err.message || "Could not load outcome history.");
|
|
128
|
+
setLoading(false);
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
return () => {
|
|
132
|
+
cancelled = true;
|
|
133
|
+
};
|
|
134
|
+
}, [selfFetch, baseUrl, customerId, customerToken, fetchImpl]);
|
|
135
|
+
|
|
136
|
+
if (loading) {
|
|
137
|
+
return React.createElement(
|
|
138
|
+
"div",
|
|
139
|
+
{ style: { opacity: 0.6, fontFamily: styles.card.fontFamily } },
|
|
140
|
+
"Loading outcome history…",
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
if (error) {
|
|
144
|
+
return React.createElement(
|
|
145
|
+
"div",
|
|
146
|
+
{ style: { color: "#dc2626", fontFamily: styles.card.fontFamily } },
|
|
147
|
+
error,
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
const list = (outcomes || fetched?.outcomes) ?? null;
|
|
151
|
+
if (!Array.isArray(list)) {
|
|
152
|
+
return React.createElement(
|
|
153
|
+
"div",
|
|
154
|
+
{ style: { opacity: 0.6, fontFamily: styles.card.fontFamily } },
|
|
155
|
+
"No outcome history.",
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
const rows = typeof filter === "function" ? list.filter(filter) : list;
|
|
159
|
+
return React.createElement(
|
|
160
|
+
"div",
|
|
161
|
+
{ className: "meterbox-outcome-history", style: styles.card },
|
|
162
|
+
React.createElement("div", { style: styles.title }, "Outcome history"),
|
|
163
|
+
rows.length === 0
|
|
164
|
+
? React.createElement("div", { style: { opacity: 0.6, fontSize: 13 } }, "No outcomes yet.")
|
|
165
|
+
: React.createElement(
|
|
166
|
+
"table",
|
|
167
|
+
{ style: styles.table },
|
|
168
|
+
React.createElement(
|
|
169
|
+
"thead",
|
|
170
|
+
null,
|
|
171
|
+
React.createElement(
|
|
172
|
+
"tr",
|
|
173
|
+
null,
|
|
174
|
+
React.createElement("th", { style: styles.th }, "Outcome"),
|
|
175
|
+
React.createElement("th", { style: styles.th }, "Amount"),
|
|
176
|
+
React.createElement("th", { style: styles.th }, "Status"),
|
|
177
|
+
React.createElement("th", { style: styles.th }, "Claimed"),
|
|
178
|
+
),
|
|
179
|
+
),
|
|
180
|
+
React.createElement(
|
|
181
|
+
"tbody",
|
|
182
|
+
null,
|
|
183
|
+
rows.map((o) =>
|
|
184
|
+
React.createElement(
|
|
185
|
+
"tr",
|
|
186
|
+
{ key: o.id },
|
|
187
|
+
React.createElement("td", { style: styles.td }, o.outcome_name || o.outcome_id),
|
|
188
|
+
React.createElement("td", { style: styles.td }, dollars(o.amount_usd)),
|
|
189
|
+
React.createElement(
|
|
190
|
+
"td",
|
|
191
|
+
{ style: styles.td },
|
|
192
|
+
React.createElement(
|
|
193
|
+
"span",
|
|
194
|
+
{ style: { ...styles.pill, ...(STATUS_STYLE[o.status] || {}) } },
|
|
195
|
+
o.status,
|
|
196
|
+
),
|
|
197
|
+
),
|
|
198
|
+
React.createElement("td", { style: styles.td }, fmtDate(o.claimed_at)),
|
|
199
|
+
),
|
|
200
|
+
),
|
|
201
|
+
),
|
|
202
|
+
),
|
|
203
|
+
);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
module.exports = OutcomeHistory;
|