@meterbox/react 0.1.7 → 0.1.9

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
@@ -14,8 +14,8 @@ Each component supports two modes:
14
14
  `<CreditBalance>`, `<CreditGrantsTable>`, and `<CreditUtilization>` can
15
15
  fetch their own data from the `/v1/customer-portal/*` endpoints using a
16
16
  short-lived customer-scoped JWT minted server-side
17
- (`POST /v1/tokens/:id`). Pass `baseUrl` + `customerId` +
18
- `customerToken` instead of the data prop.
17
+ (`POST /v1/tokens/:id`). Pass `customerId` + `customerToken` instead of
18
+ the data prop.
19
19
 
20
20
  For a complete hosted-feel portal in one component, see
21
21
  [`@meterbox/customer-portal`](https://www.npmjs.com/package/@meterbox/customer-portal).
@@ -45,9 +45,9 @@ import {
45
45
  } from "@meterbox/react";
46
46
  ```
47
47
 
48
- ### `<PricingTable plans currentPlanId? currency? onSelect? customerEmail? branding? />` or `<PricingTable baseUrl publishableKey … />`
48
+ ### `<PricingTable plans currentPlanId? currency? onSelect? customerEmail? branding? />` or `<PricingTable publishableKey … />`
49
49
 
50
- On a public page with no backend of its own, pass `baseUrl` and a
50
+ On a public page with no backend of its own, pass a
51
51
  publishable key (`mb_pub_...`, from Settings > API keys) instead of `plans`.
52
52
  The table then reads the public pricing snapshot,
53
53
  `GET /v1/catalog/pricing-page`: published plans with display fields only. A
@@ -74,6 +74,18 @@ pay for usage: the amounts included each month, what happens past them
74
74
  (billed, stopped until next month, or an upgrade), and every model's input
75
75
  and output rates per 1M tokens, tier ladders included.
76
76
 
77
+ A plan lists its included credits first ("1,500 Bits a month", in the tenant's credit unit), then
78
+ its `bullets`: the tenant's own pricing-page bullets, else its features' names. A pricing-page
79
+ plan with a public promotion shows its promotional price, the regular one struck through, and the
80
+ code to use ("Save 50% with code LAUNCH50 for 3 months").
81
+
82
+ ### Base URL: `<MeterboxProvider baseUrl>`
83
+
84
+ The components call `https://meterbox.ai`, so there's nothing to set. To
85
+ point them elsewhere, wrap them once in `<MeterboxProvider baseUrl="…">`; a
86
+ component's own `baseUrl` prop wins over both. `useBaseUrl()` reads the
87
+ resolved URL for your own calls.
88
+
77
89
  ### Branding: `<BrandingProvider branding>`
78
90
 
79
91
  Every component styles itself from `--mbx-*` CSS variables (`--mbx-accent`,
@@ -81,7 +93,7 @@ Every component styles itself from `--mbx-*` CSS variables (`--mbx-accent`,
81
93
  variables), with Meterbox's defaults as fallbacks. Wrap them in
82
94
  `<BrandingProvider branding={branding}>` to apply your branding from
83
95
  Settings → Branding (`GET /v1/settings/branding`): logo, colors, font and
84
- button style. `<PricingTable baseUrl publishableKey>` applies it from the
96
+ button style. `<PricingTable publishableKey>` applies it from the
85
97
  pricing snapshot by itself, and any component's surroundings can set the
86
98
  variables in CSS. `useBranding()` reads the values (e.g. the logo), and
87
99
  `theme` exports the variable references for styling your own elements to
@@ -100,7 +112,7 @@ keys and English text are in [`src/i18n.js`](./src/i18n.js). Plan names,
100
112
  features and offers are shown as you entered them. `useI18n()` gives your own
101
113
  elements the same `t(key, vars)`, `date(iso)` and `money(amount, currency)`.
102
114
 
103
- ### `<BillingDetailsButton baseUrl customerId customerToken returnUrl? label? onSession? onError? />`
115
+ ### `<BillingDetailsButton customerId customerToken returnUrl? label? onSession? onError? />`
104
116
 
105
117
  Sends the customer to Stripe's own Billing Portal to update their payment
106
118
  method and billing details (address, tax IDs, email, name, phone) and see
@@ -117,7 +129,7 @@ works once the customer has a Stripe account and the tenant has enabled
117
129
  Side-by-side comparison of two plan records with added / removed feature
118
130
  deltas. Pair with `<SubscriptionPreview>` for the prorated math.
119
131
 
120
- ### `<CheckoutButton planId successUrl cancelUrl baseUrl customerId customerToken billingInterval? label? onSession? onError? />`
132
+ ### `<CheckoutButton planId successUrl cancelUrl customerId customerToken billingInterval? label? onSession? onError? />`
121
133
 
122
134
  One-tap Stripe Checkout for a plan change. POSTs to
123
135
  `/v1/customer-portal/:id/checkout` (customer JWT, no API key in the
@@ -126,13 +138,13 @@ browser) and redirects to the returned Stripe URL. `billingInterval`
126
138
  `annual_free_months` configured; requesting "annual" on a plan that
127
139
  doesn't offer it surfaces a 400 via `onError` rather than redirecting.
128
140
 
129
- ### `<SubscriptionPreview preview? | baseUrl customerId customerToken planId planVersion? />`
141
+ ### `<SubscriptionPreview preview? | customerId customerToken planId planVersion? />`
130
142
 
131
143
  Renders the response of `POST /v1/customers/:id/subscription/preview` (admin)
132
144
  or `POST /v1/customer-portal/:id/subscription/preview` (customer JWT):
133
145
  prorated charge, MRR delta, effective date.
134
146
 
135
- ### `<PlanChangeCard preview? scheduledChange? allowSchedule? | baseUrl customerId customerToken planId planVersion? fetch? onConfirm? onCancelScheduled? onChanged? />`
147
+ ### `<PlanChangeCard preview? scheduledChange? allowSchedule? | customerId customerToken planId planVersion? fetch? onConfirm? onCancelScheduled? onChanged? />`
136
148
 
137
149
  Composes `<SubscriptionPreview>` with the actions it doesn't have: actually
138
150
  applying or scheduling the change, and cancelling a pending scheduled one.
@@ -142,7 +154,7 @@ Two modes:
142
154
  `onConfirm(effective)` / `onCancelScheduled()` callbacks that return
143
155
  promises; you own how the request is actually made (e.g. proxied through
144
156
  your own backend, session-authenticated, holding no customer JWT).
145
- - **Self-fetching**: `baseUrl` + `customerId` + `customerToken` + `planId`,
157
+ - **Self-fetching**: `customerId` + `customerToken` + `planId`,
146
158
  no `onConfirm`/`onCancelScheduled` supplied: fetches its own preview AND
147
159
  posts confirm/cancel directly to `POST /v1/customer-portal/:id/subscription/change`
148
160
  and `DELETE /v1/customer-portal/:id/subscription/scheduled`.
@@ -158,23 +170,23 @@ mounted; keep whatever render condition shows the card independent of the
158
170
  plan id it just changed to, or the card (and the confirmation message it's
159
171
  showing) unmounts the instant your own state catches up.
160
172
 
161
- ### `<CreditBalance balance? | baseUrl customerId customerToken; threshold? onTopUp? />`
173
+ ### `<CreditBalance balance? | customerId customerToken; threshold? onTopUp? />`
162
174
 
163
175
  Compact balance widget. If `balance_usd < threshold`, shows a top-up CTA
164
176
  wired to `onTopUp`. Self-fetches `GET /v1/customer-portal/:id/credits` in
165
177
  self-fetch mode.
166
178
 
167
- ### `<CreditGrantsTable grants? balance_usd? | baseUrl customerId customerToken; filter? />`
179
+ ### `<CreditGrantsTable grants? balance_usd? | customerId customerToken; filter? />`
168
180
 
169
181
  Table of active grants: source, amount, remaining, expiry, status. Pass
170
182
  `filter={(g) => ...}` to slice the rows.
171
183
 
172
- ### `<CreditUtilization series? | baseUrl customerId customerToken from? to? granularity?; height? />`
184
+ ### `<CreditUtilization series? | customerId customerToken from? to? granularity?; height? />`
173
185
 
174
186
  Inline SVG line + area chart of credit balance over time, fed by
175
187
  `GET /v1/customer-portal/:id/credits/timeseries`.
176
188
 
177
- ### `<InvoiceHistory invoices? | baseUrl customerId customerToken />`
189
+ ### `<InvoiceHistory invoices? | customerId customerToken />`
178
190
 
179
191
  The invoices the customer received, newest first: date, number, status and
180
192
  amount, with a link to download each Stripe invoice's PDF and to its hosted
@@ -195,10 +207,7 @@ from `GET /v1/ledger/:id?grain=day`. `valueKey` picks the dimension
195
207
  // app/server.tsx (Next.js Server Component, or any backend handler)
196
208
  import { getV1Plans, MeterboxClient } from "@meterbox/node";
197
209
 
198
- new MeterboxClient({
199
- baseUrl: process.env.METERBOX_BASE_URL, // e.g. https://meterbox.ai
200
- apiKey: process.env.METERBOX_API_KEY,
201
- });
210
+ new MeterboxClient({ apiKey: process.env.METERBOX_API_KEY });
202
211
 
203
212
  export default async function PricingPage() {
204
213
  const { data } = await getV1Plans();
@@ -232,13 +241,11 @@ export function PortalBits({ customerJwt }) {
232
241
  return (
233
242
  <>
234
243
  <SubscriptionPreview
235
- baseUrl="https://meterbox.ai"
236
244
  customerId="cus_42"
237
245
  customerToken={customerJwt}
238
246
  planId="pro"
239
247
  />
240
248
  <CreditBalance
241
- baseUrl="https://meterbox.ai"
242
249
  customerId="cus_42"
243
250
  customerToken={customerJwt}
244
251
  threshold={10}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@meterbox/react",
3
- "version": "0.1.7",
3
+ "version": "0.1.9",
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",
@@ -17,7 +17,7 @@
17
17
  "access": "public"
18
18
  },
19
19
  "dependencies": {
20
- "@meterbox/node": "0.1.0"
20
+ "@meterbox/node": "0.1.1"
21
21
  },
22
22
  "peerDependencies": {
23
23
  "react": ">=17",
@@ -1,5 +1,5 @@
1
1
  /**
2
- * <BillingDetailsButton baseUrl customerId customerToken returnUrl? label? />
2
+ * <BillingDetailsButton customerId customerToken returnUrl? label? />
3
3
  *
4
4
  * Sends the customer to Stripe's own Billing Portal to update their payment
5
5
  * method and billing details (address, tax IDs, email, name, phone) and see
@@ -17,6 +17,7 @@
17
17
 
18
18
  const React = require("react");
19
19
  const theme = require("./theme");
20
+ const { useBaseUrl } = require("./config");
20
21
  const { useI18n } = require("./i18n");
21
22
 
22
23
  const styles = {
@@ -50,7 +51,7 @@ async function openBillingDetails({ baseUrl, customerId, customerToken, returnUr
50
51
  }
51
52
 
52
53
  function BillingDetailsButton({
53
- baseUrl,
54
+ baseUrl: baseUrlProp,
54
55
  customerId,
55
56
  customerToken,
56
57
  returnUrl,
@@ -59,9 +60,10 @@ function BillingDetailsButton({
59
60
  onSession,
60
61
  onError,
61
62
  }) {
63
+ const baseUrl = useBaseUrl(baseUrlProp);
62
64
  const [busy, setBusy] = React.useState(false);
63
65
  const { t } = useI18n();
64
- const ready = baseUrl && customerId && customerToken;
66
+ const ready = customerId && customerToken;
65
67
 
66
68
  async function onClick() {
67
69
  setBusy(true);
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * <CheckoutButton planId="pro" successUrl="..." cancelUrl="..."
3
- * customerId customerToken baseUrl billingInterval? />
3
+ * customerId customerToken billingInterval? />
4
4
  *
5
5
  * One-tap Stripe Checkout for a plan change. POSTs to
6
6
  * `/v1/customer-portal/:id/checkout` and redirects the browser to the
@@ -20,6 +20,7 @@
20
20
 
21
21
  const React = require("react");
22
22
  const theme = require("./theme");
23
+ const { useBaseUrl } = require("./config");
23
24
 
24
25
  const styles = {
25
26
  btn: {
@@ -74,13 +75,14 @@ function CheckoutButton({
74
75
  cancelUrl,
75
76
  billingInterval,
76
77
  label,
77
- baseUrl,
78
+ baseUrl: baseUrlProp,
78
79
  customerId,
79
80
  customerToken,
80
81
  fetch: fetchImpl,
81
82
  onSession,
82
83
  onError,
83
84
  }) {
85
+ const baseUrl = useBaseUrl(baseUrlProp);
84
86
  const [busy, setBusy] = React.useState(false);
85
87
 
86
88
  const onClick = React.useCallback(async () => {
@@ -118,7 +120,7 @@ function CheckoutButton({
118
120
  onError,
119
121
  ]);
120
122
 
121
- const ready = baseUrl && customerId && customerToken && planId && successUrl && cancelUrl;
123
+ const ready = customerId && customerToken && planId && successUrl && cancelUrl;
122
124
  return React.createElement(
123
125
  "button",
124
126
  {
@@ -9,8 +9,7 @@
9
9
  * — `{balance_usd, grants}`.
10
10
  *
11
11
  * 2. **Self-fetching** (browser-safe):
12
- * <CreditBalance baseUrl="https://meterbox.ai"
13
- * customerId="cus_42"
12
+ * <CreditBalance customerId="cus_42"
14
13
  * customerToken={jwt}
15
14
  * threshold={10} onTopUp={...} />
16
15
  * Fetches `GET /v1/customer-portal/:id/credits` with the customer JWT.
@@ -21,6 +20,7 @@
21
20
 
22
21
  const React = require("react");
23
22
  const theme = require("./theme");
23
+ const { useBaseUrl } = require("./config");
24
24
  const { useI18n } = require("./i18n");
25
25
 
26
26
  async function fetchBalance({ baseUrl, customerId, customerToken, fetchImpl }) {
@@ -72,12 +72,13 @@ function CreditBalance({
72
72
  balance,
73
73
  threshold,
74
74
  onTopUp,
75
- baseUrl,
75
+ baseUrl: baseUrlProp,
76
76
  customerId,
77
77
  customerToken,
78
78
  fetch: fetchImpl,
79
79
  }) {
80
- const selfFetch = !balance && baseUrl && customerId && customerToken;
80
+ const baseUrl = useBaseUrl(baseUrlProp);
81
+ const selfFetch = !balance && customerId && customerToken;
81
82
  const [fetched, setFetched] = React.useState(null);
82
83
  const [error, setError] = React.useState(null);
83
84
  const [loading, setLoading] = React.useState(false);
@@ -10,8 +10,7 @@
10
10
  * in as props.
11
11
  *
12
12
  * 2. **Self-fetching** (browser-safe):
13
- * <CreditGrantsTable baseUrl="https://meterbox.ai"
14
- * customerId="cus_42"
13
+ * <CreditGrantsTable customerId="cus_42"
15
14
  * customerToken={jwt} />
16
15
  * Fetches `GET /v1/customer-portal/:id/credits` directly with the
17
16
  * customer-scoped JWT. Re-fetches when `customerId` changes.
@@ -22,6 +21,7 @@
22
21
 
23
22
  const React = require("react");
24
23
  const theme = require("./theme");
24
+ const { useBaseUrl } = require("./config");
25
25
  const { useI18n } = require("./i18n");
26
26
 
27
27
  const styles = {
@@ -80,12 +80,13 @@ function CreditGrantsTable({
80
80
  grants,
81
81
  balance_usd,
82
82
  filter,
83
- baseUrl,
83
+ baseUrl: baseUrlProp,
84
84
  customerId,
85
85
  customerToken,
86
86
  fetch: fetchImpl,
87
87
  }) {
88
- const selfFetch = !grants && baseUrl && customerId && customerToken;
88
+ const baseUrl = useBaseUrl(baseUrlProp);
89
+ const selfFetch = !grants && customerId && customerToken;
89
90
  const [fetched, setFetched] = React.useState(null);
90
91
  const [error, setError] = React.useState(null);
91
92
  const [loading, setLoading] = React.useState(false);
@@ -11,7 +11,7 @@
11
11
  * <CreditUtilization series={timeseries} />
12
12
  *
13
13
  * 2. **Self-fetching**:
14
- * <CreditUtilization baseUrl="..." customerId="cus_42"
14
+ * <CreditUtilization customerId="cus_42"
15
15
  * customerToken={jwt}
16
16
  * from="2026-05-01" to="2026-06-01"
17
17
  * granularity="day" />
@@ -19,6 +19,7 @@
19
19
 
20
20
  const React = require("react");
21
21
  const theme = require("./theme");
22
+ const { useBaseUrl } = require("./config");
22
23
  const { useI18n } = require("./i18n");
23
24
 
24
25
  const styles = {
@@ -71,7 +72,7 @@ async function fetchTimeseries({
71
72
 
72
73
  function CreditUtilization({
73
74
  series,
74
- baseUrl,
75
+ baseUrl: baseUrlProp,
75
76
  customerId,
76
77
  customerToken,
77
78
  from,
@@ -80,7 +81,8 @@ function CreditUtilization({
80
81
  height = 120,
81
82
  fetch: fetchImpl,
82
83
  }) {
83
- const selfFetch = !series && baseUrl && customerId && customerToken;
84
+ const baseUrl = useBaseUrl(baseUrlProp);
85
+ const selfFetch = !series && customerId && customerToken;
84
86
  const [fetched, setFetched] = React.useState(null);
85
87
  const [error, setError] = React.useState(null);
86
88
  const [loading, setLoading] = React.useState(false);
@@ -7,14 +7,14 @@
7
7
  * 1. **Presentational**: <InvoiceHistory invoices={...} />, with the
8
8
  * `invoices` array from `GET /v1/customer-portal/:id/invoices`.
9
9
  * 2. **Self-fetching** (browser-safe):
10
- * <InvoiceHistory baseUrl="https://meterbox.ai"
11
- * customerId="cus_42"
10
+ * <InvoiceHistory customerId="cus_42"
12
11
  * customerToken={jwt} />
13
12
  * Fetches that endpoint with the customer-scoped JWT.
14
13
  */
15
14
 
16
15
  const React = require("react");
17
16
  const theme = require("./theme");
17
+ const { useBaseUrl } = require("./config");
18
18
  const { useI18n } = require("./i18n");
19
19
 
20
20
  const styles = {
@@ -69,8 +69,9 @@ async function fetchInvoices({ baseUrl, customerId, customerToken, fetchImpl })
69
69
  return json.invoices || [];
70
70
  }
71
71
 
72
- function InvoiceHistory({ invoices, baseUrl, customerId, customerToken, fetch: fetchImpl }) {
73
- const selfFetch = !invoices && baseUrl && customerId && customerToken;
72
+ function InvoiceHistory({ invoices, baseUrl: baseUrlProp, customerId, customerToken, fetch: fetchImpl }) {
73
+ const baseUrl = useBaseUrl(baseUrlProp);
74
+ const selfFetch = !invoices && customerId && customerToken;
74
75
  const [fetched, setFetched] = React.useState(null);
75
76
  const [error, setError] = React.useState(null);
76
77
  const { t, locale, date } = useI18n();
@@ -9,8 +9,7 @@
9
9
  * `@meterbox/node` and passes `outcomes` in as a prop.
10
10
  *
11
11
  * 2. **Self-fetching** (browser-safe):
12
- * <OutcomeHistory baseUrl="https://meterbox.ai"
13
- * customerId="cus_42"
12
+ * <OutcomeHistory customerId="cus_42"
14
13
  * customerToken={jwt} />
15
14
  * Fetches `GET /v1/customer-portal/:id/outcomes` directly with the
16
15
  * customer-scoped JWT. Re-fetches when `customerId` changes.
@@ -26,6 +25,7 @@
26
25
 
27
26
  const React = require("react");
28
27
  const theme = require("./theme");
28
+ const { useBaseUrl } = require("./config");
29
29
 
30
30
  const styles = {
31
31
  card: {
@@ -96,12 +96,13 @@ async function fetchOutcomes({ baseUrl, customerId, customerToken, fetchImpl })
96
96
  function OutcomeHistory({
97
97
  outcomes,
98
98
  filter,
99
- baseUrl,
99
+ baseUrl: baseUrlProp,
100
100
  customerId,
101
101
  customerToken,
102
102
  fetch: fetchImpl,
103
103
  }) {
104
- const selfFetch = !outcomes && baseUrl && customerId && customerToken;
104
+ const baseUrl = useBaseUrl(baseUrlProp);
105
+ const selfFetch = !outcomes && customerId && customerToken;
105
106
  const [fetched, setFetched] = React.useState(null);
106
107
  const [error, setError] = React.useState(null);
107
108
  const [loading, setLoading] = React.useState(false);
package/src/Paywall.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * <Paywall feature="vision" customerId="..." customerToken="..." baseUrl="...">
2
+ * <Paywall feature="vision" customerId="..." customerToken="...">
3
3
  * <YourFeature />
4
4
  * </Paywall>
5
5
  *
@@ -8,7 +8,7 @@
8
8
  * prompt). Self-fetches from `GET /v1/customer-portal/:id/entitlements`.
9
9
  *
10
10
  * Modes:
11
- * • **Self-fetching** (default) — provide `baseUrl`, `customerId`,
11
+ * • **Self-fetching** (default) — provide `customerId` and
12
12
  * `customerToken`. Fetches the entitlements once on mount and again
13
13
  * whenever the customer or token changes.
14
14
  * • **Presentational** — pass `features` (array of feature ids the customer
@@ -23,6 +23,7 @@
23
23
 
24
24
  const React = require("react");
25
25
  const theme = require("./theme");
26
+ const { useBaseUrl } = require("./config");
26
27
 
27
28
  const defaultStyles = {
28
29
  fallback: {
@@ -67,13 +68,14 @@ function Paywall({
67
68
  children,
68
69
  fallback,
69
70
  onUpgradeClick,
70
- baseUrl,
71
+ baseUrl: baseUrlProp,
71
72
  customerId,
72
73
  customerToken,
73
74
  fetch: fetchImpl,
74
75
  failOpen = true,
75
76
  }) {
76
- const selfFetch = !features && baseUrl && customerId && customerToken;
77
+ const baseUrl = useBaseUrl(baseUrlProp);
78
+ const selfFetch = !features && customerId && customerToken;
77
79
  const [fetched, setFetched] = React.useState(null);
78
80
  const [error, setError] = React.useState(null);
79
81
 
@@ -17,7 +17,7 @@
17
17
  * (e.g. Meterbox's own Settings page proxies through its control
18
18
  * plane, session-authenticated, holding no customer JWT at all).
19
19
  *
20
- * 2. **Self-fetching** — `baseUrl` + `customerId` + `customerToken` +
20
+ * 2. **Self-fetching** — `customerId` + `customerToken` +
21
21
  * `planId`, no `onConfirm`/`onCancelScheduled` supplied. The panel
22
22
  * fetches its own preview AND posts the confirm/cancel actions
23
23
  * directly to `/v1/customer-portal/:id/subscription/change` and
@@ -38,6 +38,7 @@
38
38
 
39
39
  const React = require("react");
40
40
  const theme = require("./theme");
41
+ const { useBaseUrl } = require("./config");
41
42
  const SubscriptionPreview = require("./SubscriptionPreview");
42
43
  const { useI18n } = require("./i18n");
43
44
 
@@ -125,7 +126,7 @@ function PlanChangeCard({
125
126
  preview,
126
127
  scheduledChange,
127
128
  allowSchedule = true,
128
- baseUrl,
129
+ baseUrl: baseUrlProp,
129
130
  customerId,
130
131
  customerToken,
131
132
  planId,
@@ -135,8 +136,9 @@ function PlanChangeCard({
135
136
  onCancelScheduled,
136
137
  onChanged,
137
138
  }) {
139
+ const baseUrl = useBaseUrl(baseUrlProp);
138
140
  const fetcher = fetchImpl || globalThis.fetch;
139
- const canSelfFetch = Boolean(baseUrl && customerId && customerToken);
141
+ const canSelfFetch = Boolean(customerId && customerToken);
140
142
  const [busy, setBusy] = React.useState(false);
141
143
  const [err, setErr] = React.useState(null);
142
144
  const [message, setMessage] = React.useState(null);
@@ -1,11 +1,11 @@
1
1
  /**
2
2
  * <PricingTable plans={...} currentPlanId? onSelect? />
3
- * <PricingTable baseUrl publishableKey />
3
+ * <PricingTable publishableKey />
4
4
  *
5
5
  * Renders the tenant's plan catalog as a comparison grid. `plans` is the
6
6
  * array your backend fetched from `GET /v1/plans` (via
7
7
  * `@meterbox/node`'s `getV1Plans`). Or, on a public page with no backend of
8
- * its own, pass `baseUrl` + `publishableKey` (`mb_pub_...`, Settings > API
8
+ * its own, pass `publishableKey` (`mb_pub_...`, Settings > API
9
9
  * keys) and it reads the public pricing snapshot instead
10
10
  * (`GET /v1/catalog/pricing-page`: published plans, display fields only).
11
11
  * Highlights `currentPlanId` if
@@ -35,12 +35,20 @@
35
35
  * each plan's button is its call-to-action link (`customerEmail` fills
36
36
  * {CUSTOMER_EMAIL} in it).
37
37
  *
38
+ * A plan's list is its `bullets` (the snapshot and the customer portal's plan
39
+ * list: the tenant's own bullets, else its features' labels), else its
40
+ * `display_features`, else its feature ids made readable; its included
41
+ * credits come first ("1,500 Bits a month"). A snapshot plan's `promotion`
42
+ * shows its monthly price with the regular one struck through, and how to
43
+ * get it ("Save 50% with code LAUNCH50 for 3 months").
44
+ *
38
45
  * Pure presentational + inline styles — no CSS pipeline required from the
39
46
  * tenant.
40
47
  */
41
48
 
42
49
  const React = require("react");
43
50
  const theme = require("./theme");
51
+ const { useBaseUrl } = require("./config");
44
52
  const { brandingVars } = require("./branding");
45
53
  const { useI18n } = require("./i18n");
46
54
 
@@ -115,6 +123,8 @@ const styles = {
115
123
  cursor: "default",
116
124
  },
117
125
  trial: { fontSize: 13, fontWeight: 600 },
126
+ wasPrice: { fontSize: 16, opacity: 0.5, marginRight: 6 },
127
+ promo: { fontSize: 12, fontWeight: 600, color: theme.accent },
118
128
  usage: {
119
129
  display: "grid",
120
130
  gap: 4,
@@ -133,6 +143,34 @@ const styles = {
133
143
  },
134
144
  };
135
145
 
146
+ /** What a plan lists: its bullets, else its own display text, else its feature ids. */
147
+ function planBullets(plan) {
148
+ if (Array.isArray(plan.bullets)) return plan.bullets;
149
+ if (plan.display_features?.length) return plan.display_features;
150
+ return (plan.features || []).map((f) => f.replace(/_/g, " "));
151
+ }
152
+
153
+ /** "1,500 Bits a month", or "$15 of credit a month" without a credit unit. */
154
+ function creditsText({ t, number, money }, c) {
155
+ return c.credit_units != null && c.credit_unit_name
156
+ ? t("pricing.credits.units", { n: number(c.credit_units), unit: c.credit_unit_name })
157
+ : t("pricing.credits.usd", { amount: money(c.amount_usd) });
158
+ }
159
+
160
+ /** "Save 50% with code LAUNCH50 for 3 months" */
161
+ function promoText({ t, money }, c) {
162
+ const save =
163
+ c.percent_off != null
164
+ ? t("pricing.promo.percent", { pct: c.percent_off })
165
+ : t("pricing.promo.amount", { amount: money(c.amount_off_usd) });
166
+ const offer = t("pricing.promo.code", { save, code: c.code });
167
+ if (c.duration === "once") return t("pricing.promo.once", { offer });
168
+ if (c.duration === "repeating" && c.duration_in_months) {
169
+ return t("pricing.promo.months", { offer, count: c.duration_in_months });
170
+ }
171
+ return offer;
172
+ }
173
+
136
174
  /** "$3.00 to 10M then $2.50" — a tier ladder priced per 1,000 units × `scale`. */
137
175
  function ladderText({ t, money, number }, tiers, scale) {
138
176
  const rate = (perUnit) => {
@@ -225,6 +263,9 @@ function fromSnapshot(p) {
225
263
  name: p.name,
226
264
  description: p.description || null,
227
265
  features: p.features || [],
266
+ bullets: p.bullets || null,
267
+ included_credits: p.included_credits || null,
268
+ promotion: p.promotion || null,
228
269
  stripe_price_monthly_usd: p.price ? p.price.monthly_usd : 0,
229
270
  annual_free_months: p.annual_free_months == null ? null : p.annual_free_months,
230
271
  localized_prices: p.localized_prices || [],
@@ -253,14 +294,16 @@ function PricingTable({
253
294
  onSelect,
254
295
  customerEmail,
255
296
  branding,
256
- baseUrl,
297
+ baseUrl: baseUrlProp,
257
298
  publishableKey,
258
299
  fetch: fetchImpl,
259
300
  }) {
260
- const selfFetch = !Array.isArray(plans) && !!baseUrl && !!publishableKey;
301
+ const baseUrl = useBaseUrl(baseUrlProp);
302
+ const selfFetch = !Array.isArray(plans) && !!publishableKey;
261
303
  const [fetched, setFetched] = React.useState(null);
262
304
  const [billingInterval, setBillingInterval] = React.useState("monthly");
263
- const { t, money } = useI18n();
305
+ const i18n = useI18n();
306
+ const { t, money } = i18n;
264
307
 
265
308
  React.useEffect(() => {
266
309
  if (!selfFetch) return undefined;
@@ -330,6 +373,12 @@ function PricingTable({
330
373
  // toggle above is set to "Annual" for its siblings.
331
374
  const annual = billingInterval === "annual" ? annualPriceUsd(plan) : null;
332
375
  const link = !onSelect && !isCurrent ? ctaLink(plan, customerEmail) : null;
376
+ // A promotion is on the monthly USD price.
377
+ const promo = annual == null && displayCurrency === "USD" ? plan.promotion : null;
378
+ const bullets = [
379
+ ...(plan.included_credits ? [creditsText(i18n, plan.included_credits)] : []),
380
+ ...planBullets(plan),
381
+ ];
333
382
  return React.createElement(
334
383
  "div",
335
384
  {
@@ -351,10 +400,14 @@ function PricingTable({
351
400
  React.createElement(
352
401
  "div",
353
402
  null,
403
+ promo && React.createElement("s", { style: styles.wasPrice }, money(monthlyAmount)),
354
404
  React.createElement(
355
405
  "span",
356
406
  { style: styles.price },
357
- money(annual != null ? annual : monthlyAmount, displayCurrency),
407
+ money(
408
+ promo ? promo.monthly_usd : annual != null ? annual : monthlyAmount,
409
+ displayCurrency,
410
+ ),
358
411
  ),
359
412
  React.createElement(
360
413
  "span",
@@ -362,20 +415,18 @@ function PricingTable({
362
415
  annual != null ? t("pricing.billedAnnually") : t("pricing.perMonth"),
363
416
  ),
364
417
  ),
418
+ promo && React.createElement("div", { style: styles.promo }, promoText(i18n, promo)),
365
419
  plan.trial_days > 0 &&
366
420
  React.createElement(
367
421
  "div",
368
422
  { style: styles.trial },
369
423
  t("pricing.trial", { days: plan.trial_days }),
370
424
  ),
371
- Array.isArray(plan.features) &&
372
- plan.features.length > 0 &&
425
+ bullets.length > 0 &&
373
426
  React.createElement(
374
427
  "ul",
375
428
  { style: styles.features },
376
- plan.features.map((f) =>
377
- React.createElement("li", { key: f }, `• ${f.replace(/_/g, " ")}`),
378
- ),
429
+ bullets.map((b, i) => React.createElement("li", { key: `${i}:${b}` }, `• ${b}`)),
379
430
  ),
380
431
  plan.usage && React.createElement(PlanUsage, { usage: plan.usage }),
381
432
  link
@@ -10,8 +10,7 @@
10
10
  * key) and passes the body in as `preview`.
11
11
  *
12
12
  * 2. **Self-fetching** (browser-safe):
13
- * <SubscriptionPreview baseUrl="https://meterbox.ai"
14
- * customerId="cus_42"
13
+ * <SubscriptionPreview customerId="cus_42"
15
14
  * customerToken={jwt}
16
15
  * planId="pro" />
17
16
  * The component calls `POST /v1/customer-portal/:id/subscription/preview`
@@ -22,6 +21,7 @@
22
21
 
23
22
  const React = require("react");
24
23
  const theme = require("./theme");
24
+ const { useBaseUrl } = require("./config");
25
25
  const { useI18n } = require("./i18n");
26
26
 
27
27
  const styles = {
@@ -76,14 +76,15 @@ async function fetchPreview({ baseUrl, customerId, customerToken, planId, planVe
76
76
 
77
77
  function SubscriptionPreview({
78
78
  preview,
79
- baseUrl,
79
+ baseUrl: baseUrlProp,
80
80
  customerId,
81
81
  customerToken,
82
82
  planId,
83
83
  planVersion,
84
84
  fetch: fetchImpl,
85
85
  }) {
86
- const selfFetch = !preview && baseUrl && customerId && customerToken && planId;
86
+ const baseUrl = useBaseUrl(baseUrlProp);
87
+ const selfFetch = !preview && customerId && customerToken && planId;
87
88
  const [fetched, setFetched] = React.useState(null);
88
89
  const [error, setError] = React.useState(null);
89
90
  const [loading, setLoading] = React.useState(false);
@@ -1,5 +1,5 @@
1
1
  /**
2
- * <TrialBanner customerId customerToken baseUrl onConvert={(planId) => …} />
2
+ * <TrialBanner customerId customerToken onConvert={(planId) => …} />
3
3
  *
4
4
  * Renders a thin banner showing time remaining on the customer's trial, with
5
5
  * a "Convert now" CTA that the host can wire to its checkout flow. Hides
@@ -11,6 +11,7 @@
11
11
 
12
12
  const React = require("react");
13
13
  const theme = require("./theme");
14
+ const { useBaseUrl } = require("./config");
14
15
 
15
16
  const styles = {
16
17
  banner: {
@@ -55,13 +56,14 @@ function daysRemaining(ends) {
55
56
  function TrialBanner({
56
57
  trialEndsAt,
57
58
  status,
58
- baseUrl,
59
+ baseUrl: baseUrlProp,
59
60
  customerId,
60
61
  customerToken,
61
62
  fetch: fetchImpl,
62
63
  onConvert,
63
64
  }) {
64
- const selfFetch = (!trialEndsAt || !status) && baseUrl && customerId && customerToken;
65
+ const baseUrl = useBaseUrl(baseUrlProp);
66
+ const selfFetch = (!trialEndsAt || !status) && customerId && customerToken;
65
67
  const [me, setMe] = React.useState(null);
66
68
 
67
69
  React.useEffect(() => {
@@ -1,5 +1,5 @@
1
1
  /**
2
- * <UsageProgress metric="input_tokens_per_month" customerId customerToken baseUrl />
2
+ * <UsageProgress metric="input_tokens_per_month" customerId customerToken />
3
3
  *
4
4
  * Real-time bar chart of consumption against the customer's current plan
5
5
  * limit. Self-fetches from `GET /v1/customer-portal/:id/usage`, or accept
@@ -17,6 +17,7 @@
17
17
 
18
18
  const React = require("react");
19
19
  const theme = require("./theme");
20
+ const { useBaseUrl } = require("./config");
20
21
 
21
22
  const styles = {
22
23
  wrap: {
@@ -58,12 +59,13 @@ function UsageProgress({
58
59
  limits,
59
60
  warnAt = 0.8,
60
61
  dangerAt = 1.0,
61
- baseUrl,
62
+ baseUrl: baseUrlProp,
62
63
  customerId,
63
64
  customerToken,
64
65
  fetch: fetchImpl,
65
66
  }) {
66
- const selfFetch = (!usage || !limits) && baseUrl && customerId && customerToken;
67
+ const baseUrl = useBaseUrl(baseUrlProp);
68
+ const selfFetch = (!usage || !limits) && customerId && customerToken;
67
69
  const [fetched, setFetched] = React.useState(null);
68
70
  const [stale, setStale] = React.useState(false);
69
71
 
package/src/config.js ADDED
@@ -0,0 +1,26 @@
1
+ /**
2
+ * config.js — where the components reach Meterbox.
3
+ *
4
+ * <MeterboxProvider baseUrl="https://billing.example.com">…</MeterboxProvider>
5
+ *
6
+ * sets the API's base URL once for every component inside it. Without one,
7
+ * they call https://meterbox.ai; a component's own `baseUrl` prop still wins.
8
+ */
9
+
10
+ const React = require("react");
11
+
12
+ const DEFAULT_BASE_URL = "https://meterbox.ai";
13
+
14
+ const BaseUrlContext = React.createContext(null);
15
+
16
+ function MeterboxProvider({ baseUrl, children }) {
17
+ return React.createElement(BaseUrlContext.Provider, { value: baseUrl || null }, children);
18
+ }
19
+
20
+ /** The base URL to call, without a trailing slash: `override`, else the provider's, else meterbox.ai. */
21
+ function useBaseUrl(override) {
22
+ const provided = React.useContext(BaseUrlContext);
23
+ return String(override || provided || DEFAULT_BASE_URL).replace(/\/+$/, "");
24
+ }
25
+
26
+ module.exports = { MeterboxProvider, useBaseUrl, DEFAULT_BASE_URL };
package/src/i18n.js CHANGED
@@ -89,6 +89,14 @@ const en = {
89
89
  "pricing.apiCallRates": "API calls: {ladder} per 1K",
90
90
  "pricing.upTo": "{rate} to {n}",
91
91
  "pricing.then": "then",
92
+ "pricing.promo.percent": "Save {pct}%",
93
+ "pricing.promo.amount": "Save {amount}",
94
+ "pricing.promo.code": "{save} with code {code}",
95
+ "pricing.promo.once": "{offer} on your first month",
96
+ "pricing.promo.months.one": "{offer} for {count} month",
97
+ "pricing.promo.months.other": "{offer} for {count} months",
98
+ "pricing.credits.units": "{n} {unit} a month",
99
+ "pricing.credits.usd": "{amount} of credit a month",
92
100
 
93
101
  "planChange.scheduledOn": "Scheduled: applies on {date}.",
94
102
  "planChange.changed": "Plan changed.",
@@ -231,6 +239,14 @@ const es = {
231
239
  "pricing.apiCallRates": "Llamadas a la API: {ladder} por cada 1000",
232
240
  "pricing.upTo": "{rate} hasta {n}",
233
241
  "pricing.then": "luego",
242
+ "pricing.promo.percent": "Ahorra {pct}%",
243
+ "pricing.promo.amount": "Ahorra {amount}",
244
+ "pricing.promo.code": "{save} con el código {code}",
245
+ "pricing.promo.once": "{offer} el primer mes",
246
+ "pricing.promo.months.one": "{offer} durante {count} mes",
247
+ "pricing.promo.months.other": "{offer} durante {count} meses",
248
+ "pricing.credits.units": "{n} {unit} al mes",
249
+ "pricing.credits.usd": "{amount} de crédito al mes",
234
250
 
235
251
  "planChange.scheduledOn": "Programado: se aplica el {date}.",
236
252
  "planChange.changed": "Plan cambiado.",
@@ -373,6 +389,14 @@ const fr = {
373
389
  "pricing.apiCallRates": "Appels API : {ladder} les 1 000",
374
390
  "pricing.upTo": "{rate} jusqu'à {n}",
375
391
  "pricing.then": "puis",
392
+ "pricing.promo.percent": "Économisez {pct} %",
393
+ "pricing.promo.amount": "Économisez {amount}",
394
+ "pricing.promo.code": "{save} avec le code {code}",
395
+ "pricing.promo.once": "{offer} le premier mois",
396
+ "pricing.promo.months.one": "{offer} pendant {count} mois",
397
+ "pricing.promo.months.other": "{offer} pendant {count} mois",
398
+ "pricing.credits.units": "{n} {unit} par mois",
399
+ "pricing.credits.usd": "{amount} de crédit par mois",
376
400
 
377
401
  "planChange.scheduledOn": "Programmé : s'applique le {date}.",
378
402
  "planChange.changed": "Forfait modifié.",
@@ -515,6 +539,14 @@ const de = {
515
539
  "pricing.apiCallRates": "API-Aufrufe: {ladder} pro 1.000",
516
540
  "pricing.upTo": "{rate} bis {n}",
517
541
  "pricing.then": "danach",
542
+ "pricing.promo.percent": "Sparen Sie {pct} %",
543
+ "pricing.promo.amount": "Sparen Sie {amount}",
544
+ "pricing.promo.code": "{save} mit dem Code {code}",
545
+ "pricing.promo.once": "{offer} im ersten Monat",
546
+ "pricing.promo.months.one": "{offer} für {count} Monat",
547
+ "pricing.promo.months.other": "{offer} für {count} Monate",
548
+ "pricing.credits.units": "{n} {unit} pro Monat",
549
+ "pricing.credits.usd": "{amount} Guthaben pro Monat",
518
550
 
519
551
  "planChange.scheduledOn": "Geplant: gilt ab {date}.",
520
552
  "planChange.changed": "Tarif geändert.",
package/src/index.js CHANGED
@@ -11,6 +11,10 @@
11
11
  * `/v1/customer-portal/*` endpoints using a short-lived customer-scoped
12
12
  * JWT minted server-side (`POST /v1/tokens/:id`).
13
13
  *
14
+ * Base URL: the components call https://meterbox.ai; `<MeterboxProvider
15
+ * baseUrl>` points every component inside it elsewhere, and a component's own
16
+ * `baseUrl` prop wins over both.
17
+ *
14
18
  * Branding: `<BrandingProvider branding>` themes every component inside it
15
19
  * with the tenant's branding (GET /v1/settings/branding); `theme` is the
16
20
  * --mbx-* CSS variables they read, for matching your own elements.
@@ -43,6 +47,7 @@ const CheckoutButton = require("./CheckoutButton");
43
47
  const BillingDetailsButton = require("./BillingDetailsButton");
44
48
  const { BrandingProvider, useBranding, brandingVars, logoUrl } = require("./branding");
45
49
  const { LocaleProvider, useI18n } = require("./i18n");
50
+ const { MeterboxProvider, useBaseUrl } = require("./config");
46
51
  const theme = require("./theme");
47
52
 
48
53
  module.exports = {
@@ -67,6 +72,8 @@ module.exports = {
67
72
  logoUrl,
68
73
  LocaleProvider,
69
74
  useI18n,
75
+ MeterboxProvider,
76
+ useBaseUrl,
70
77
  theme,
71
78
  sdk,
72
79
  MeterboxClient: sdk.MeterboxClient,