@meterbox/react 0.1.0 → 0.1.2

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
@@ -44,7 +44,13 @@ import {
44
44
  } from "@meterbox/react";
45
45
  ```
46
46
 
47
- ### `<PricingTable plans currentPlanId? currency? onSelect? />`
47
+ ### `<PricingTable plans currentPlanId? currency? onSelect? />` or `<PricingTable baseUrl publishableKey … />`
48
+
49
+ On a public page with no backend of its own, pass `baseUrl` and a
50
+ publishable key (`mb_pub_...`, from Settings > API keys) instead of `plans`.
51
+ The table then reads the public pricing snapshot,
52
+ `GET /v1/catalog/pricing-page`: published plans with display fields only. A
53
+ publishable key is safe in page source and unlocks nothing else.
48
54
 
49
55
  Renders the plan catalog as a comparison grid. Highlights `currentPlanId`
50
56
  if supplied, picks a localized price when `currency` matches a row in
@@ -56,6 +62,12 @@ whichever interval was selected, ready to hand straight to
56
62
  `<CheckoutButton billingInterval>`. `PricingTable.annualPriceUsd(plan)` is
57
63
  exposed as a static if you need the same total outside the grid.
58
64
 
65
+ Plans from the snapshot (or the customer portal's plan list) also carry
66
+ `usage`, and each card then shows what an AI product's customers actually
67
+ pay for usage: the amounts included each month, what happens past them
68
+ (billed, stopped until next month, or an upgrade), and every model's input
69
+ and output rates per 1M tokens, tier ladders included.
70
+
59
71
  ### `<PlanComparison from to onSwitch? />`
60
72
 
61
73
  Side-by-side comparison of two plan records with added / removed feature
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@meterbox/react",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
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",
@@ -1,9 +1,14 @@
1
1
  /**
2
2
  * <PricingTable plans={...} currentPlanId? onSelect? />
3
+ * <PricingTable baseUrl publishableKey />
3
4
  *
4
5
  * Renders the tenant's plan catalog as a comparison grid. `plans` is the
5
6
  * array your backend fetched from `GET /v1/plans` (via
6
- * `@meterbox/node`'s `getV1Plans`). Highlights `currentPlanId` if
7
+ * `@meterbox/node`'s `getV1Plans`). Or, on a public page with no backend of
8
+ * its own, pass `baseUrl` + `publishableKey` (`mb_pub_...`, Settings > API
9
+ * keys) and it reads the public pricing snapshot instead
10
+ * (`GET /v1/catalog/pricing-page`: published plans, display fields only).
11
+ * Highlights `currentPlanId` if
7
12
  * supplied; invokes `onSelect(plan, billingInterval)` when a CTA is
8
13
  * clicked so the tenant can route to their own checkout / contact form —
9
14
  * `billingInterval` ("monthly" or "annual") is whichever the shopper had
@@ -16,6 +21,11 @@
16
21
  * total divided back into a misleading "$X/mo" figure — a shopper who
17
22
  * commits annually is billed once for the year, not monthly.
18
23
  *
24
+ * A plan with `usage` (the pricing snapshot and the customer portal's plan
25
+ * list carry it) also shows its included amounts, what happens past them,
26
+ * and its models' input and output rates per 1M tokens, tier ladders
27
+ * included.
28
+ *
19
29
  * Pure presentational + inline styles — no CSS pipeline required from the
20
30
  * tenant.
21
31
  */
@@ -89,6 +99,13 @@ const styles = {
89
99
  cursor: "pointer",
90
100
  },
91
101
  ctaCurrent: { background: "#e5e7eb", color: "#111", cursor: "default" },
102
+ usage: {
103
+ display: "grid",
104
+ gap: 4,
105
+ fontSize: 12,
106
+ borderTop: "1px solid #e5e7eb",
107
+ paddingTop: 10,
108
+ },
92
109
  badge: {
93
110
  fontSize: 11,
94
111
  fontWeight: 600,
@@ -113,12 +130,86 @@ function formatPrice(amount, currency = "USD") {
113
130
  }
114
131
  }
115
132
 
116
- async function fetchCatalog({ baseUrl, fetchImpl }) {
117
- const url = `${baseUrl.replace(/\/+$/, "")}/v1/plans`;
133
+ const PAST_INCLUDED = {
134
+ billed: "Past that, per 1M tokens:",
135
+ blocked: "Usage stops there until next month.",
136
+ upgrade: "Past that, your plan is upgraded automatically.",
137
+ };
138
+
139
+ function compact(n) {
140
+ return new Intl.NumberFormat("en", { notation: "compact" }).format(n);
141
+ }
142
+
143
+ function rate(perUnit) {
144
+ const v = Math.round(perUnit * 1e6) / 1e6;
145
+ return `$${v > 0 && v < 0.01 ? v.toPrecision(2) : v.toFixed(2)}`;
146
+ }
147
+
148
+ /** "$3.00 to 10M then $2.50" — a tier ladder priced per 1,000 units × `scale`. */
149
+ function ladderText(tiers, scale) {
150
+ return tiers
151
+ .map((t) => rate(t.per_1k * scale) + (t.up_to == null ? "" : ` to ${compact(t.up_to)}`))
152
+ .join(" then ");
153
+ }
154
+
155
+ /** Included amounts, what happens past them, and the models' rates. */
156
+ function PlanUsage({ usage }) {
157
+ const { included, overage, models, api_call_overage_tiers: callTiers } = usage;
158
+ const parts = [
159
+ [included.input_tokens, "input tokens"],
160
+ [included.output_tokens, "output tokens"],
161
+ [included.api_calls, "API calls"],
162
+ ]
163
+ .filter(([n]) => n > 0)
164
+ .map(([n, label]) => `${compact(n)} ${label}`);
165
+ const billed = overage === "billed";
166
+ return React.createElement(
167
+ "div",
168
+ { className: "meterbox-plan-usage", style: styles.usage },
169
+ parts.length > 0 && React.createElement("div", null, `Includes ${parts.join(", ")} a month`),
170
+ React.createElement(
171
+ "div",
172
+ { style: { opacity: 0.75 } },
173
+ billed && parts.length === 0 ? "Per 1M tokens:" : PAST_INCLUDED[overage],
174
+ ),
175
+ billed
176
+ ? models.map((m) =>
177
+ React.createElement(
178
+ "div",
179
+ { key: m.id },
180
+ `${m.name}: ${ladderText(m.input_tiers, 1000)} in, ${ladderText(m.output_tiers, 1000)} out`,
181
+ ),
182
+ )
183
+ : models.length > 0 &&
184
+ React.createElement("div", null, `Models: ${models.map((m) => m.name).join(", ")}`),
185
+ billed &&
186
+ callTiers &&
187
+ React.createElement("div", null, `API calls: ${ladderText(callTiers, 1)} per 1K`),
188
+ );
189
+ }
190
+
191
+ /** A plan from the public snapshot, in the shape the table renders. */
192
+ function fromSnapshot(p) {
193
+ return {
194
+ id: p.id,
195
+ name: p.name,
196
+ description: p.description || null,
197
+ features: p.features || [],
198
+ stripe_price_monthly_usd: p.price ? p.price.monthly_usd : 0,
199
+ annual_free_months: p.annual_free_months == null ? null : p.annual_free_months,
200
+ localized_prices: p.localized_prices || [],
201
+ usage: p.usage || null,
202
+ };
203
+ }
204
+
205
+ async function fetchCatalog({ baseUrl, publishableKey, fetchImpl }) {
206
+ const url = `${baseUrl.replace(/\/+$/, "")}/v1/catalog/pricing-page?key=${encodeURIComponent(
207
+ publishableKey,
208
+ )}`;
118
209
  const res = await fetchImpl(url, { method: "GET" });
119
210
  if (!res.ok) throw new Error(`pricing-table: ${res.status}`);
120
211
  const body = await res.json();
121
- return Array.isArray(body) ? body : body.plans || [];
212
+ return (body.plans || []).map(fromSnapshot);
122
213
  }
123
214
 
124
215
  function PricingTable({
@@ -127,16 +218,17 @@ function PricingTable({
127
218
  currency = "USD",
128
219
  onSelect,
129
220
  baseUrl,
221
+ publishableKey,
130
222
  fetch: fetchImpl,
131
223
  }) {
132
- const selfFetch = !Array.isArray(plans) && !!baseUrl;
224
+ const selfFetch = !Array.isArray(plans) && !!baseUrl && !!publishableKey;
133
225
  const [fetched, setFetched] = React.useState(null);
134
226
  const [billingInterval, setBillingInterval] = React.useState("monthly");
135
227
 
136
228
  React.useEffect(() => {
137
229
  if (!selfFetch) return undefined;
138
230
  let alive = true;
139
- fetchCatalog({ baseUrl, fetchImpl: fetchImpl || globalThis.fetch })
231
+ fetchCatalog({ baseUrl, publishableKey, fetchImpl: fetchImpl || globalThis.fetch })
140
232
  .then((d) => {
141
233
  if (alive) setFetched(d);
142
234
  })
@@ -146,7 +238,7 @@ function PricingTable({
146
238
  return () => {
147
239
  alive = false;
148
240
  };
149
- }, [selfFetch, baseUrl, fetchImpl]);
241
+ }, [selfFetch, baseUrl, publishableKey, fetchImpl]);
150
242
 
151
243
  const list = Array.isArray(plans) ? plans : fetched;
152
244
  if (!Array.isArray(list) || list.length === 0) {
@@ -236,6 +328,7 @@ function PricingTable({
236
328
  React.createElement("li", { key: f }, `• ${f.replace(/_/g, " ")}`),
237
329
  ),
238
330
  ),
331
+ plan.usage && React.createElement(PlanUsage, { usage: plan.usage }),
239
332
  React.createElement(
240
333
  "button",
241
334
  {