@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,259 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* <PricingTable plans={...} currentPlanId? onSelect? />
|
|
3
|
+
*
|
|
4
|
+
* Renders the tenant's plan catalog as a comparison grid. `plans` is the
|
|
5
|
+
* array your backend fetched from `GET /v1/plans` (via
|
|
6
|
+
* `@meterbox/node`'s `getV1Plans`). Highlights `currentPlanId` if
|
|
7
|
+
* supplied; invokes `onSelect(plan, billingInterval)` when a CTA is
|
|
8
|
+
* clicked so the tenant can route to their own checkout / contact form —
|
|
9
|
+
* `billingInterval` ("monthly" or "annual") is whichever the shopper had
|
|
10
|
+
* selected, ready to pass straight to `<CheckoutButton billingInterval>`.
|
|
11
|
+
*
|
|
12
|
+
* A monthly/annual toggle renders automatically when at least one plan
|
|
13
|
+
* has `annual_free_months` set (the same field the tenant configures via
|
|
14
|
+
* Settings -> Plans, or the catalog API — this isn't a component-only
|
|
15
|
+
* concept). Selecting "Annual" shows the one-time prepaid total, not that
|
|
16
|
+
* total divided back into a misleading "$X/mo" figure — a shopper who
|
|
17
|
+
* commits annually is billed once for the year, not monthly.
|
|
18
|
+
*
|
|
19
|
+
* Pure presentational + inline styles — no CSS pipeline required from the
|
|
20
|
+
* tenant.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
const React = require("react");
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Total charged for one year prepaid upfront, or null when the plan
|
|
27
|
+
* doesn't offer annual billing. Mirrors config-loader.js's
|
|
28
|
+
* getAnnualPriceUsd on the server.
|
|
29
|
+
*/
|
|
30
|
+
function annualPriceUsd(plan) {
|
|
31
|
+
if (plan.annual_free_months == null || plan.stripe_price_monthly_usd == null) return null;
|
|
32
|
+
return Math.round(plan.stripe_price_monthly_usd * (12 - plan.annual_free_months) * 100) / 100;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const styles = {
|
|
36
|
+
wrap: { display: "grid", gap: 16 },
|
|
37
|
+
toggle: {
|
|
38
|
+
display: "flex",
|
|
39
|
+
justifyContent: "center",
|
|
40
|
+
gap: 4,
|
|
41
|
+
padding: 4,
|
|
42
|
+
width: "fit-content",
|
|
43
|
+
margin: "0 auto",
|
|
44
|
+
background: "#f3f4f6",
|
|
45
|
+
border: "1px solid #e5e7eb",
|
|
46
|
+
borderRadius: 999,
|
|
47
|
+
},
|
|
48
|
+
toggleBtn: {
|
|
49
|
+
padding: "6px 14px",
|
|
50
|
+
borderRadius: 999,
|
|
51
|
+
border: "none",
|
|
52
|
+
background: "transparent",
|
|
53
|
+
color: "#4b5563",
|
|
54
|
+
fontSize: 13,
|
|
55
|
+
fontWeight: 600,
|
|
56
|
+
fontFamily: "system-ui, -apple-system, sans-serif",
|
|
57
|
+
cursor: "pointer",
|
|
58
|
+
},
|
|
59
|
+
toggleBtnActive: { background: "#5045e8", color: "#fff" },
|
|
60
|
+
grid: {
|
|
61
|
+
display: "grid",
|
|
62
|
+
gridTemplateColumns: "repeat(auto-fit, minmax(220px, 1fr))",
|
|
63
|
+
gap: 16,
|
|
64
|
+
fontFamily: "system-ui, -apple-system, sans-serif",
|
|
65
|
+
color: "#111",
|
|
66
|
+
},
|
|
67
|
+
card: {
|
|
68
|
+
border: "1px solid #e5e7eb",
|
|
69
|
+
borderRadius: 12,
|
|
70
|
+
padding: 20,
|
|
71
|
+
background: "#fff",
|
|
72
|
+
display: "flex",
|
|
73
|
+
flexDirection: "column",
|
|
74
|
+
gap: 12,
|
|
75
|
+
},
|
|
76
|
+
cardCurrent: { borderColor: "#5045e8", boxShadow: "0 0 0 3px #5045e822" },
|
|
77
|
+
name: { fontSize: 18, fontWeight: 600, margin: 0 },
|
|
78
|
+
price: { fontSize: 28, fontWeight: 700 },
|
|
79
|
+
perMonth: { fontSize: 12, opacity: 0.6, marginLeft: 4 },
|
|
80
|
+
features: { margin: 0, padding: 0, listStyle: "none", display: "grid", gap: 6, fontSize: 13 },
|
|
81
|
+
cta: {
|
|
82
|
+
marginTop: "auto",
|
|
83
|
+
padding: "10px 14px",
|
|
84
|
+
border: "none",
|
|
85
|
+
borderRadius: 8,
|
|
86
|
+
background: "#5045e8",
|
|
87
|
+
color: "#fff",
|
|
88
|
+
fontWeight: 600,
|
|
89
|
+
cursor: "pointer",
|
|
90
|
+
},
|
|
91
|
+
ctaCurrent: { background: "#e5e7eb", color: "#111", cursor: "default" },
|
|
92
|
+
badge: {
|
|
93
|
+
fontSize: 11,
|
|
94
|
+
fontWeight: 600,
|
|
95
|
+
background: "#5045e8",
|
|
96
|
+
color: "#fff",
|
|
97
|
+
padding: "2px 8px",
|
|
98
|
+
borderRadius: 999,
|
|
99
|
+
alignSelf: "flex-start",
|
|
100
|
+
},
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
function formatPrice(amount, currency = "USD") {
|
|
104
|
+
if (amount == null) return "—";
|
|
105
|
+
try {
|
|
106
|
+
return new Intl.NumberFormat(undefined, {
|
|
107
|
+
style: "currency",
|
|
108
|
+
currency,
|
|
109
|
+
maximumFractionDigits: amount % 1 === 0 ? 0 : 2,
|
|
110
|
+
}).format(amount);
|
|
111
|
+
} catch (_) {
|
|
112
|
+
return `${currency} ${amount}`;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
async function fetchCatalog({ baseUrl, fetchImpl }) {
|
|
117
|
+
const url = `${baseUrl.replace(/\/+$/, "")}/v1/plans`;
|
|
118
|
+
const res = await fetchImpl(url, { method: "GET" });
|
|
119
|
+
if (!res.ok) throw new Error(`pricing-table: ${res.status}`);
|
|
120
|
+
const body = await res.json();
|
|
121
|
+
return Array.isArray(body) ? body : body.plans || [];
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function PricingTable({
|
|
125
|
+
plans,
|
|
126
|
+
currentPlanId,
|
|
127
|
+
currency = "USD",
|
|
128
|
+
onSelect,
|
|
129
|
+
baseUrl,
|
|
130
|
+
fetch: fetchImpl,
|
|
131
|
+
}) {
|
|
132
|
+
const selfFetch = !Array.isArray(plans) && !!baseUrl;
|
|
133
|
+
const [fetched, setFetched] = React.useState(null);
|
|
134
|
+
const [billingInterval, setBillingInterval] = React.useState("monthly");
|
|
135
|
+
|
|
136
|
+
React.useEffect(() => {
|
|
137
|
+
if (!selfFetch) return undefined;
|
|
138
|
+
let alive = true;
|
|
139
|
+
fetchCatalog({ baseUrl, fetchImpl: fetchImpl || globalThis.fetch })
|
|
140
|
+
.then((d) => {
|
|
141
|
+
if (alive) setFetched(d);
|
|
142
|
+
})
|
|
143
|
+
.catch(() => {
|
|
144
|
+
if (alive) setFetched([]);
|
|
145
|
+
});
|
|
146
|
+
return () => {
|
|
147
|
+
alive = false;
|
|
148
|
+
};
|
|
149
|
+
}, [selfFetch, baseUrl, fetchImpl]);
|
|
150
|
+
|
|
151
|
+
const list = Array.isArray(plans) ? plans : fetched;
|
|
152
|
+
if (!Array.isArray(list) || list.length === 0) {
|
|
153
|
+
return React.createElement(
|
|
154
|
+
"div",
|
|
155
|
+
{ style: { opacity: 0.6, fontFamily: styles.grid.fontFamily } },
|
|
156
|
+
selfFetch && list == null ? "Loading plans…" : "No plans available.",
|
|
157
|
+
);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const hasAnnualOption = list.some((p) => p.annual_free_months != null);
|
|
161
|
+
|
|
162
|
+
return React.createElement(
|
|
163
|
+
"div",
|
|
164
|
+
{ className: "meterbox-pricing-table", style: styles.wrap },
|
|
165
|
+
hasAnnualOption &&
|
|
166
|
+
React.createElement(
|
|
167
|
+
"div",
|
|
168
|
+
{ style: styles.toggle },
|
|
169
|
+
["monthly", "annual"].map((interval) =>
|
|
170
|
+
React.createElement(
|
|
171
|
+
"button",
|
|
172
|
+
{
|
|
173
|
+
key: interval,
|
|
174
|
+
type: "button",
|
|
175
|
+
style:
|
|
176
|
+
billingInterval === interval
|
|
177
|
+
? { ...styles.toggleBtn, ...styles.toggleBtnActive }
|
|
178
|
+
: styles.toggleBtn,
|
|
179
|
+
"aria-pressed": billingInterval === interval,
|
|
180
|
+
onClick: () => setBillingInterval(interval),
|
|
181
|
+
},
|
|
182
|
+
interval === "monthly" ? "Monthly" : "Annual",
|
|
183
|
+
),
|
|
184
|
+
),
|
|
185
|
+
),
|
|
186
|
+
React.createElement(
|
|
187
|
+
"div",
|
|
188
|
+
{ style: styles.grid },
|
|
189
|
+
list.map((plan) => {
|
|
190
|
+
const isCurrent = plan.id === currentPlanId;
|
|
191
|
+
const localized = (plan.localized_prices || []).find(
|
|
192
|
+
(p) => (p.currency || "").toUpperCase() === currency.toUpperCase(),
|
|
193
|
+
);
|
|
194
|
+
const monthlyAmount = localized ? localized.monthly_amount : plan.stripe_price_monthly_usd;
|
|
195
|
+
const displayCurrency = localized ? localized.currency : "USD";
|
|
196
|
+
// Annual only applies when THIS plan offers it — a plan without
|
|
197
|
+
// annual_free_months always shows its monthly price, even while the
|
|
198
|
+
// toggle above is set to "Annual" for its siblings.
|
|
199
|
+
const annual = billingInterval === "annual" ? annualPriceUsd(plan) : null;
|
|
200
|
+
return React.createElement(
|
|
201
|
+
"div",
|
|
202
|
+
{
|
|
203
|
+
key: plan.id,
|
|
204
|
+
style: isCurrent ? { ...styles.card, ...styles.cardCurrent } : styles.card,
|
|
205
|
+
},
|
|
206
|
+
isCurrent && React.createElement("span", { style: styles.badge }, "Current"),
|
|
207
|
+
React.createElement("h3", { style: styles.name }, plan.name || plan.id),
|
|
208
|
+
plan.description &&
|
|
209
|
+
React.createElement(
|
|
210
|
+
"p",
|
|
211
|
+
{ style: { fontSize: 13, opacity: 0.75, margin: 0 } },
|
|
212
|
+
plan.description,
|
|
213
|
+
),
|
|
214
|
+
React.createElement(
|
|
215
|
+
"div",
|
|
216
|
+
null,
|
|
217
|
+
React.createElement(
|
|
218
|
+
"span",
|
|
219
|
+
{ style: styles.price },
|
|
220
|
+
annual != null
|
|
221
|
+
? formatPrice(annual, displayCurrency)
|
|
222
|
+
: formatPrice(monthlyAmount, displayCurrency),
|
|
223
|
+
),
|
|
224
|
+
React.createElement(
|
|
225
|
+
"span",
|
|
226
|
+
{ style: styles.perMonth },
|
|
227
|
+
annual != null ? "billed annually" : "/ month",
|
|
228
|
+
),
|
|
229
|
+
),
|
|
230
|
+
Array.isArray(plan.features) &&
|
|
231
|
+
plan.features.length > 0 &&
|
|
232
|
+
React.createElement(
|
|
233
|
+
"ul",
|
|
234
|
+
{ style: styles.features },
|
|
235
|
+
plan.features.map((f) =>
|
|
236
|
+
React.createElement("li", { key: f }, `• ${f.replace(/_/g, " ")}`),
|
|
237
|
+
),
|
|
238
|
+
),
|
|
239
|
+
React.createElement(
|
|
240
|
+
"button",
|
|
241
|
+
{
|
|
242
|
+
type: "button",
|
|
243
|
+
style: isCurrent ? { ...styles.cta, ...styles.ctaCurrent } : styles.cta,
|
|
244
|
+
disabled: isCurrent,
|
|
245
|
+
onClick: () => (onSelect ? onSelect(plan, billingInterval) : undefined),
|
|
246
|
+
"aria-label": isCurrent
|
|
247
|
+
? `Currently on ${plan.name || plan.id}`
|
|
248
|
+
: `Choose ${plan.name || plan.id}`,
|
|
249
|
+
},
|
|
250
|
+
isCurrent ? "Current plan" : "Choose plan",
|
|
251
|
+
),
|
|
252
|
+
);
|
|
253
|
+
}),
|
|
254
|
+
),
|
|
255
|
+
);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
PricingTable.annualPriceUsd = annualPriceUsd;
|
|
259
|
+
module.exports = PricingTable;
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* <SubscriptionPreview ... />
|
|
3
|
+
*
|
|
4
|
+
* Two modes of operation:
|
|
5
|
+
*
|
|
6
|
+
* 1. **Presentational** (default — backwards-compatible):
|
|
7
|
+
* <SubscriptionPreview preview={...} />
|
|
8
|
+
* The tenant fetches the preview server-side via
|
|
9
|
+
* `@meterbox/node`'s `postV1CustomersByIdSubscriptionPreview` (admin
|
|
10
|
+
* key) and passes the body in as `preview`.
|
|
11
|
+
*
|
|
12
|
+
* 2. **Self-fetching** (browser-safe):
|
|
13
|
+
* <SubscriptionPreview baseUrl="https://meterbox.ai"
|
|
14
|
+
* customerId="cus_42"
|
|
15
|
+
* customerToken={jwt}
|
|
16
|
+
* planId="pro" />
|
|
17
|
+
* The component calls `POST /v1/customer-portal/:id/subscription/preview`
|
|
18
|
+
* directly with the customer-scoped JWT (no admin key in the browser).
|
|
19
|
+
* Loading + error states are surfaced inline. Re-fetches when
|
|
20
|
+
* `customerId` or `planId` change.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
const React = require("react");
|
|
24
|
+
|
|
25
|
+
const styles = {
|
|
26
|
+
panel: {
|
|
27
|
+
fontFamily: "system-ui, -apple-system, sans-serif",
|
|
28
|
+
color: "#111",
|
|
29
|
+
border: "1px solid #e5e7eb",
|
|
30
|
+
borderRadius: 12,
|
|
31
|
+
padding: 20,
|
|
32
|
+
background: "#fff",
|
|
33
|
+
display: "grid",
|
|
34
|
+
gap: 12,
|
|
35
|
+
maxWidth: 480,
|
|
36
|
+
},
|
|
37
|
+
row: { display: "flex", justifyContent: "space-between", fontSize: 14 },
|
|
38
|
+
label: { opacity: 0.65 },
|
|
39
|
+
value: { fontWeight: 600 },
|
|
40
|
+
total: {
|
|
41
|
+
paddingTop: 10,
|
|
42
|
+
borderTop: "1px solid #e5e7eb",
|
|
43
|
+
display: "flex",
|
|
44
|
+
justifyContent: "space-between",
|
|
45
|
+
fontSize: 18,
|
|
46
|
+
fontWeight: 700,
|
|
47
|
+
},
|
|
48
|
+
delta: { fontSize: 13, fontWeight: 600 },
|
|
49
|
+
positive: { color: "#16a34a" },
|
|
50
|
+
negative: { color: "#dc2626" },
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
function dollars(n) {
|
|
54
|
+
if (n == null) return "—";
|
|
55
|
+
return `$${Number(n).toLocaleString(undefined, { maximumFractionDigits: 2 })}`;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async function fetchPreview({ baseUrl, customerId, customerToken, planId, planVersion, fetchImpl }) {
|
|
59
|
+
const url = `${String(baseUrl).replace(/\/+$/, "")}/v1/customer-portal/${encodeURIComponent(
|
|
60
|
+
customerId,
|
|
61
|
+
)}/subscription/preview`;
|
|
62
|
+
const body = planVersion != null ? { plan_id: planId, plan_version: planVersion } : { plan_id: planId };
|
|
63
|
+
const res = await fetchImpl(url, {
|
|
64
|
+
method: "POST",
|
|
65
|
+
headers: {
|
|
66
|
+
"Content-Type": "application/json",
|
|
67
|
+
Authorization: `Bearer ${customerToken}`,
|
|
68
|
+
},
|
|
69
|
+
body: JSON.stringify(body),
|
|
70
|
+
});
|
|
71
|
+
const json = await res.json();
|
|
72
|
+
if (!res.ok) {
|
|
73
|
+
const err = new Error(json?.error || `HTTP ${res.status}`);
|
|
74
|
+
err.status = res.status;
|
|
75
|
+
throw err;
|
|
76
|
+
}
|
|
77
|
+
return json;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function SubscriptionPreview({
|
|
81
|
+
preview,
|
|
82
|
+
baseUrl,
|
|
83
|
+
customerId,
|
|
84
|
+
customerToken,
|
|
85
|
+
planId,
|
|
86
|
+
planVersion,
|
|
87
|
+
fetch: fetchImpl,
|
|
88
|
+
}) {
|
|
89
|
+
const selfFetch = !preview && baseUrl && customerId && customerToken && planId;
|
|
90
|
+
const [fetched, setFetched] = React.useState(null);
|
|
91
|
+
const [error, setError] = React.useState(null);
|
|
92
|
+
const [loading, setLoading] = React.useState(false);
|
|
93
|
+
|
|
94
|
+
React.useEffect(() => {
|
|
95
|
+
if (!selfFetch) return undefined;
|
|
96
|
+
let cancelled = false;
|
|
97
|
+
setLoading(true);
|
|
98
|
+
setError(null);
|
|
99
|
+
fetchPreview({
|
|
100
|
+
baseUrl,
|
|
101
|
+
customerId,
|
|
102
|
+
customerToken,
|
|
103
|
+
planId,
|
|
104
|
+
planVersion,
|
|
105
|
+
fetchImpl: fetchImpl || globalThis.fetch,
|
|
106
|
+
})
|
|
107
|
+
.then((body) => {
|
|
108
|
+
if (!cancelled) {
|
|
109
|
+
setFetched(body);
|
|
110
|
+
setLoading(false);
|
|
111
|
+
}
|
|
112
|
+
})
|
|
113
|
+
.catch((err) => {
|
|
114
|
+
if (!cancelled) {
|
|
115
|
+
setError(err.message || "Could not load preview.");
|
|
116
|
+
setLoading(false);
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
return () => {
|
|
120
|
+
cancelled = true;
|
|
121
|
+
};
|
|
122
|
+
}, [selfFetch, baseUrl, customerId, customerToken, planId, planVersion, fetchImpl]);
|
|
123
|
+
|
|
124
|
+
const data = preview || fetched;
|
|
125
|
+
|
|
126
|
+
if (loading) {
|
|
127
|
+
return React.createElement(
|
|
128
|
+
"div",
|
|
129
|
+
{ style: { opacity: 0.6, fontFamily: styles.panel.fontFamily } },
|
|
130
|
+
"Loading preview…",
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
if (error) {
|
|
134
|
+
return React.createElement(
|
|
135
|
+
"div",
|
|
136
|
+
{ style: { color: "#dc2626", fontFamily: styles.panel.fontFamily } },
|
|
137
|
+
error,
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
if (!data) {
|
|
141
|
+
return React.createElement(
|
|
142
|
+
"div",
|
|
143
|
+
{ style: { opacity: 0.6, fontFamily: styles.panel.fontFamily } },
|
|
144
|
+
"No preview available.",
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
// Render the canonical API shape from
|
|
148
|
+
// `POST /v1/customer-portal/:id/subscription/preview` (and the equivalent
|
|
149
|
+
// admin endpoint). Field names match the backend exactly.
|
|
150
|
+
const current = data.current || null;
|
|
151
|
+
const target = data.target || null;
|
|
152
|
+
const newMrr = data.new_mrr_usd ?? data.proration?.new_plan_prorated_usd ?? null;
|
|
153
|
+
const prevMrr = current ? current.price_monthly_usd : null;
|
|
154
|
+
const mrrDelta = newMrr != null && prevMrr != null ? newMrr - prevMrr : null;
|
|
155
|
+
const amountDue = data.amount_due_usd ?? null;
|
|
156
|
+
const period = data.period || null;
|
|
157
|
+
return React.createElement(
|
|
158
|
+
"div",
|
|
159
|
+
{ className: "meterbox-subscription-preview", style: styles.panel },
|
|
160
|
+
current &&
|
|
161
|
+
React.createElement(
|
|
162
|
+
"div",
|
|
163
|
+
{ style: styles.row },
|
|
164
|
+
React.createElement("span", { style: styles.label }, "From"),
|
|
165
|
+
React.createElement(
|
|
166
|
+
"span",
|
|
167
|
+
{ style: styles.value },
|
|
168
|
+
current.name || current.plan_id,
|
|
169
|
+
),
|
|
170
|
+
),
|
|
171
|
+
target &&
|
|
172
|
+
React.createElement(
|
|
173
|
+
"div",
|
|
174
|
+
{ style: styles.row },
|
|
175
|
+
React.createElement("span", { style: styles.label }, "To"),
|
|
176
|
+
React.createElement("span", { style: styles.value }, target.name || target.plan_id),
|
|
177
|
+
),
|
|
178
|
+
period &&
|
|
179
|
+
period.start &&
|
|
180
|
+
React.createElement(
|
|
181
|
+
"div",
|
|
182
|
+
{ style: styles.row },
|
|
183
|
+
React.createElement("span", { style: styles.label }, "Effective"),
|
|
184
|
+
React.createElement("span", { style: styles.value }, period.start),
|
|
185
|
+
),
|
|
186
|
+
prevMrr != null &&
|
|
187
|
+
React.createElement(
|
|
188
|
+
"div",
|
|
189
|
+
{ style: styles.row },
|
|
190
|
+
React.createElement("span", { style: styles.label }, "MRR now"),
|
|
191
|
+
React.createElement("span", { style: styles.value }, dollars(prevMrr)),
|
|
192
|
+
),
|
|
193
|
+
newMrr != null &&
|
|
194
|
+
React.createElement(
|
|
195
|
+
"div",
|
|
196
|
+
{ style: styles.row },
|
|
197
|
+
React.createElement("span", { style: styles.label }, "MRR after"),
|
|
198
|
+
React.createElement(
|
|
199
|
+
"span",
|
|
200
|
+
{ style: styles.value },
|
|
201
|
+
`${dollars(newMrr)}`,
|
|
202
|
+
mrrDelta != null &&
|
|
203
|
+
React.createElement(
|
|
204
|
+
"span",
|
|
205
|
+
{
|
|
206
|
+
style: {
|
|
207
|
+
...styles.delta,
|
|
208
|
+
...(mrrDelta >= 0 ? styles.positive : styles.negative),
|
|
209
|
+
marginLeft: 8,
|
|
210
|
+
},
|
|
211
|
+
},
|
|
212
|
+
`(${mrrDelta >= 0 ? "+" : ""}${dollars(mrrDelta)})`,
|
|
213
|
+
),
|
|
214
|
+
),
|
|
215
|
+
),
|
|
216
|
+
amountDue != null &&
|
|
217
|
+
React.createElement(
|
|
218
|
+
"div",
|
|
219
|
+
{ style: styles.total },
|
|
220
|
+
React.createElement("span", null, "Prorated charge today"),
|
|
221
|
+
React.createElement("span", null, dollars(amountDue)),
|
|
222
|
+
),
|
|
223
|
+
);
|
|
224
|
+
}
|
|
225
|
+
module.exports = SubscriptionPreview;
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* <TrialBanner customerId customerToken baseUrl onConvert={(planId) => …} />
|
|
3
|
+
*
|
|
4
|
+
* Renders a thin banner showing time remaining on the customer's trial, with
|
|
5
|
+
* a "Convert now" CTA that the host can wire to its checkout flow. Hides
|
|
6
|
+
* itself when the customer isn't trialing.
|
|
7
|
+
*
|
|
8
|
+
* Self-fetches from `GET /v1/customer-portal/:id/me`. Alternately, pass
|
|
9
|
+
* `trialEndsAt` (ISO string) + `status` directly for a presentational use.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
const React = require("react");
|
|
13
|
+
|
|
14
|
+
const styles = {
|
|
15
|
+
banner: {
|
|
16
|
+
display: "flex",
|
|
17
|
+
alignItems: "center",
|
|
18
|
+
justifyContent: "space-between",
|
|
19
|
+
gap: 12,
|
|
20
|
+
padding: "10px 14px",
|
|
21
|
+
borderRadius: 10,
|
|
22
|
+
background: "#eef2ff",
|
|
23
|
+
color: "#3730a3",
|
|
24
|
+
fontFamily: "system-ui, -apple-system, sans-serif",
|
|
25
|
+
fontSize: 14,
|
|
26
|
+
},
|
|
27
|
+
expired: { background: "#fee2e2", color: "#991b1b" },
|
|
28
|
+
cta: {
|
|
29
|
+
padding: "6px 12px",
|
|
30
|
+
border: "none",
|
|
31
|
+
borderRadius: 6,
|
|
32
|
+
background: "#5045e8",
|
|
33
|
+
color: "#fff",
|
|
34
|
+
fontWeight: 600,
|
|
35
|
+
cursor: "pointer",
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
async function fetchMe({ baseUrl, customerId, customerToken, fetchImpl }) {
|
|
40
|
+
const url = `${baseUrl.replace(/\/+$/, "")}/v1/customer-portal/${encodeURIComponent(
|
|
41
|
+
customerId,
|
|
42
|
+
)}/me`;
|
|
43
|
+
const res = await fetchImpl(url, {
|
|
44
|
+
method: "GET",
|
|
45
|
+
headers: { Authorization: `Bearer ${customerToken}` },
|
|
46
|
+
});
|
|
47
|
+
if (!res.ok) throw new Error(`trial-banner: ${res.status}`);
|
|
48
|
+
return res.json();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function daysRemaining(ends) {
|
|
52
|
+
const ms = new Date(ends).getTime() - Date.now();
|
|
53
|
+
return Math.ceil(ms / (24 * 60 * 60 * 1000));
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function TrialBanner({
|
|
57
|
+
trialEndsAt,
|
|
58
|
+
status,
|
|
59
|
+
baseUrl,
|
|
60
|
+
customerId,
|
|
61
|
+
customerToken,
|
|
62
|
+
fetch: fetchImpl,
|
|
63
|
+
onConvert,
|
|
64
|
+
}) {
|
|
65
|
+
const selfFetch = (!trialEndsAt || !status) && baseUrl && customerId && customerToken;
|
|
66
|
+
const [me, setMe] = React.useState(null);
|
|
67
|
+
|
|
68
|
+
React.useEffect(() => {
|
|
69
|
+
if (!selfFetch) return undefined;
|
|
70
|
+
let alive = true;
|
|
71
|
+
fetchMe({
|
|
72
|
+
baseUrl,
|
|
73
|
+
customerId,
|
|
74
|
+
customerToken,
|
|
75
|
+
fetchImpl: fetchImpl || globalThis.fetch,
|
|
76
|
+
})
|
|
77
|
+
.then((d) => {
|
|
78
|
+
if (alive) setMe(d);
|
|
79
|
+
})
|
|
80
|
+
.catch(() => {
|
|
81
|
+
// Silent fail — banner just stays hidden when /me is unavailable.
|
|
82
|
+
});
|
|
83
|
+
return () => {
|
|
84
|
+
alive = false;
|
|
85
|
+
};
|
|
86
|
+
}, [selfFetch, baseUrl, customerId, customerToken, fetchImpl]);
|
|
87
|
+
|
|
88
|
+
const data = me || { trial_ends_at: trialEndsAt, status };
|
|
89
|
+
// Gate on the presence of a `trial_ends_at` rather than a synthesized
|
|
90
|
+
// status. The portal `/me` reports the registry's true status; the trial
|
|
91
|
+
// overlay is signalled by `trial_ends_at`. When the timestamp is in the
|
|
92
|
+
// past we still show the "expired" copy so the customer sees the prompt
|
|
93
|
+
// to convert.
|
|
94
|
+
if (!data.trial_ends_at) return null;
|
|
95
|
+
|
|
96
|
+
const remaining = daysRemaining(data.trial_ends_at);
|
|
97
|
+
const expired = remaining <= 0;
|
|
98
|
+
const text = expired
|
|
99
|
+
? "Your trial has ended — convert now to keep your plan active."
|
|
100
|
+
: `Your trial ends in ${remaining} day${remaining === 1 ? "" : "s"}.`;
|
|
101
|
+
|
|
102
|
+
return React.createElement(
|
|
103
|
+
"div",
|
|
104
|
+
{
|
|
105
|
+
className: "meterbox-trial-banner",
|
|
106
|
+
role: "status",
|
|
107
|
+
style: expired ? { ...styles.banner, ...styles.expired } : styles.banner,
|
|
108
|
+
},
|
|
109
|
+
React.createElement("span", null, text),
|
|
110
|
+
onConvert &&
|
|
111
|
+
React.createElement(
|
|
112
|
+
"button",
|
|
113
|
+
{ type: "button", style: styles.cta, onClick: () => onConvert(data.plan_id) },
|
|
114
|
+
"Convert now",
|
|
115
|
+
),
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
module.exports = TrialBanner;
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* <UsageChart series={...} valueKey? height? />
|
|
3
|
+
*
|
|
4
|
+
* Tiny inline SVG bar chart of a time series. `series` is the response
|
|
5
|
+
* shape of `GET /v1/ledger/:id?grain=day` — `[{period_start, amount_usd,
|
|
6
|
+
* api_calls, input_tokens, output_tokens}]`. `valueKey` (default
|
|
7
|
+
* `amount_usd`) picks which dimension to plot.
|
|
8
|
+
*
|
|
9
|
+
* No charting library — we keep `@meterbox/react`'s footprint to React
|
|
10
|
+
* itself. If a tenant wants recharts / chart.js they can roll their own;
|
|
11
|
+
* this is the embed-and-go default.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
const React = require("react");
|
|
15
|
+
|
|
16
|
+
const styles = {
|
|
17
|
+
wrap: {
|
|
18
|
+
fontFamily: "system-ui, -apple-system, sans-serif",
|
|
19
|
+
color: "#111",
|
|
20
|
+
border: "1px solid #e5e7eb",
|
|
21
|
+
borderRadius: 12,
|
|
22
|
+
padding: 16,
|
|
23
|
+
background: "#fff",
|
|
24
|
+
display: "grid",
|
|
25
|
+
gap: 8,
|
|
26
|
+
},
|
|
27
|
+
label: { fontSize: 12, opacity: 0.6, textTransform: "uppercase", letterSpacing: 0.4 },
|
|
28
|
+
total: { fontSize: 24, fontWeight: 700 },
|
|
29
|
+
bar: { fill: "#5045e8" },
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const VALUE_LABELS = {
|
|
33
|
+
amount_usd: "Revenue",
|
|
34
|
+
api_calls: "API calls",
|
|
35
|
+
input_tokens: "Input tokens",
|
|
36
|
+
output_tokens: "Output tokens",
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
function formatValue(valueKey, n) {
|
|
40
|
+
if (n == null) return "—";
|
|
41
|
+
if (valueKey === "amount_usd") {
|
|
42
|
+
return `$${Number(n).toLocaleString(undefined, { maximumFractionDigits: 2 })}`;
|
|
43
|
+
}
|
|
44
|
+
return Number(n).toLocaleString();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function UsageChart({ series, valueKey = "amount_usd", height = 120, label }) {
|
|
48
|
+
if (!Array.isArray(series) || series.length === 0) {
|
|
49
|
+
return React.createElement(
|
|
50
|
+
"div",
|
|
51
|
+
{ style: { opacity: 0.6, fontFamily: styles.wrap.fontFamily } },
|
|
52
|
+
"No usage in window.",
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
const values = series.map((p) => Number(p[valueKey] || 0));
|
|
56
|
+
const max = Math.max(...values, 0.0001);
|
|
57
|
+
const total = values.reduce((acc, v) => acc + v, 0);
|
|
58
|
+
const barW = 100 / series.length;
|
|
59
|
+
const bars = series.map((p, i) => {
|
|
60
|
+
const v = Number(p[valueKey] || 0);
|
|
61
|
+
const h = (v / max) * 100;
|
|
62
|
+
return React.createElement("rect", {
|
|
63
|
+
key: p.period_start || i,
|
|
64
|
+
x: `${i * barW + barW * 0.15}%`,
|
|
65
|
+
y: `${100 - h}%`,
|
|
66
|
+
width: `${barW * 0.7}%`,
|
|
67
|
+
height: `${h}%`,
|
|
68
|
+
style: styles.bar,
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
return React.createElement(
|
|
72
|
+
"div",
|
|
73
|
+
{ className: "meterbox-usage-chart", style: styles.wrap },
|
|
74
|
+
React.createElement("div", { style: styles.label }, label || VALUE_LABELS[valueKey] || valueKey),
|
|
75
|
+
React.createElement("div", { style: styles.total }, formatValue(valueKey, total)),
|
|
76
|
+
React.createElement(
|
|
77
|
+
"svg",
|
|
78
|
+
{
|
|
79
|
+
role: "img",
|
|
80
|
+
"aria-label": `${label || VALUE_LABELS[valueKey] || valueKey} chart`,
|
|
81
|
+
width: "100%",
|
|
82
|
+
height,
|
|
83
|
+
preserveAspectRatio: "none",
|
|
84
|
+
viewBox: "0 0 100 100",
|
|
85
|
+
},
|
|
86
|
+
bars,
|
|
87
|
+
),
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
module.exports = UsageChart;
|