@moonbase.sh/react 0.0.0-next-20261111171533 → 0.0.0-next-20261111172117
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 +59 -11
- package/dist/index.cjs +236 -67
- package/dist/index.d.cts +86 -2
- package/dist/index.d.ts +86 -2
- package/dist/index.js +197 -36
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -97,6 +97,9 @@ identity across renders, so it can go in a dependency list or to a memoized chil
|
|
|
97
97
|
- `useInventory()` - owned products, licenses and the computers they're activated on, downloads, and subscriptions:
|
|
98
98
|
cancelling, renewing, changing plan and changing the card they're paid with.
|
|
99
99
|
- `useOrders()` - the signed-in customer's completed orders, newest first, and their receipt PDFs.
|
|
100
|
+
- `useOwnedProducts()`, `useOwnedProduct(id)`, `useProductActivations(id)`, `useProductLicenses(id)`,
|
|
101
|
+
`useOrderHistory()`, `useSubscriptions()`, `useSubscription(id)` - the signed-in customer's account data, loaded for
|
|
102
|
+
you (see [Account pages](#account-pages)).
|
|
100
103
|
- `useVoucher()` - what a voucher code gives, and redeeming it.
|
|
101
104
|
- `useActivationRequest(token, options)` - activating a product in the browser: the request the app made, granting it a
|
|
102
105
|
license or a trial, and waiting for the app to pick it up. With `{ autoFulfill: true }` it activates with the signed-in
|
|
@@ -109,9 +112,11 @@ identity across renders, so it can go in a dependency list or to a memoized chil
|
|
|
109
112
|
with the catalog, and `hasRating(rating)` says whether there is a score worth showing yet.
|
|
110
113
|
- `useCommunicationPreferences()` - newsletter sign-ups, and the preferences page an email's footer links to.
|
|
111
114
|
- `useVendor()` - who runs the store.
|
|
115
|
+
- `useAttributionConsent()` - what a cookie banner of your own needs: whether the store asks this visitor, their
|
|
116
|
+
remembered answer, and `setAnswer`. See [Attribution and consent](#attribution-and-consent).
|
|
112
117
|
- `useStorefront()` - the storefront itself, and `useStorefrontState(state => ...)` for any part of its state
|
|
113
|
-
(`attributionConsentRequired`, `attributionConsentStatus`, `catalog`, `catalogLoaded`,
|
|
114
|
-
`userLoaded`, `utm`).
|
|
118
|
+
(`attributionConsentAnswer`, `attributionConsentRequired`, `attributionConsentStatus`, `catalog`, `catalogLoaded`,
|
|
119
|
+
`closed`, `order`, `user`, `userLoaded`, `utm`).
|
|
115
120
|
|
|
116
121
|
### Where checkout happens
|
|
117
122
|
|
|
@@ -158,6 +163,45 @@ function CheckoutButton() {
|
|
|
158
163
|
}
|
|
159
164
|
```
|
|
160
165
|
|
|
166
|
+
## Account pages
|
|
167
|
+
|
|
168
|
+
The account data hooks load what they show for whoever is signed in, and load it again when that changes. Each returns
|
|
169
|
+
`{ data, loading, error, reload }`, and the lists, which Moonbase hands out a page at a time, add `hasMore` and
|
|
170
|
+
`loadMore`, with `data` holding every page loaded so far:
|
|
171
|
+
|
|
172
|
+
```tsx
|
|
173
|
+
import { useOrderHistory } from '@moonbase.sh/react'
|
|
174
|
+
|
|
175
|
+
function Purchases() {
|
|
176
|
+
const orders = useOrderHistory()
|
|
177
|
+
|
|
178
|
+
if (orders.error)
|
|
179
|
+
return <p role="alert">{orders.error.message}</p>
|
|
180
|
+
if (!orders.data)
|
|
181
|
+
return <p>Loading…</p>
|
|
182
|
+
|
|
183
|
+
return (
|
|
184
|
+
<>
|
|
185
|
+
{orders.data.length === 0 && <p>No purchases yet.</p>}
|
|
186
|
+
<ul>
|
|
187
|
+
{orders.data.map(order => <li key={order.id}>{order.id}</li>)}
|
|
188
|
+
</ul>
|
|
189
|
+
{orders.hasMore && (
|
|
190
|
+
<button disabled={orders.loading} onClick={orders.loadMore}>Show more</button>
|
|
191
|
+
)}
|
|
192
|
+
</>
|
|
193
|
+
)
|
|
194
|
+
}
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
`data` is null while it first loads, with nobody signed in, and for an ID of `null` or `undefined`, which asks for nothing.
|
|
198
|
+
`loading` is true while a request is in flight and until the storefront knows who is signed in, so a page doesn't flash
|
|
199
|
+
"Nothing here" before it does. What a hook loaded is kept for as long as the same customer is signed in: a component
|
|
200
|
+
that mounts again, such as a tab the customer comes back to, shows it at once and asks again in the background.
|
|
201
|
+
Components asking for the same thing at once share one request. After changing something, call `reload`, which goes
|
|
202
|
+
back to a list's first page. `reload` and `loadMore` never reject: a failure lands in `error`, and `data` keeps what was
|
|
203
|
+
there.
|
|
204
|
+
|
|
161
205
|
## Checkout events
|
|
162
206
|
|
|
163
207
|
```tsx
|
|
@@ -245,28 +289,32 @@ cookies as each request is made) and the page's origin and path. `storefront.adS
|
|
|
245
289
|
or `'unknown'`). When your banner reports a refusal for a signed-in customer, the storefront tells Moonbase, once per
|
|
246
290
|
tab.
|
|
247
291
|
|
|
248
|
-
|
|
249
|
-
once the browser has loaded the catalog for them, and `undefined`
|
|
250
|
-
|
|
292
|
+
A cookie banner of your own gets what it needs from `useAttributionConsent()`. `required` says whether the store's
|
|
293
|
+
setting asks this visitor at all: `true` or `false` once the browser has loaded the catalog for them, and `undefined`
|
|
294
|
+
until then, since a catalog fetched on a server describes where the server is. `answer` is the visitor's answer, or
|
|
295
|
+
`null` until they give one, and `setAnswer` records it. The storefront remembers the answer in the browser
|
|
296
|
+
(`moonbase_attribution_consent` in local storage) and starts from it on the next visit, unless you pass
|
|
297
|
+
`attributionConsent` to `createStorefront`:
|
|
251
298
|
|
|
252
299
|
```tsx
|
|
253
300
|
function CookieBanner() {
|
|
254
|
-
const required =
|
|
255
|
-
const status = useStorefrontState(state => state.attributionConsentStatus)
|
|
256
|
-
const storefront = useStorefront()
|
|
301
|
+
const { required, answer, setAnswer } = useAttributionConsent()
|
|
257
302
|
|
|
258
|
-
if (!required ||
|
|
303
|
+
if (!required || answer !== null)
|
|
259
304
|
return null
|
|
260
305
|
|
|
261
306
|
return (
|
|
262
307
|
<div role="dialog">
|
|
263
|
-
<button onClick={() =>
|
|
264
|
-
<button onClick={() =>
|
|
308
|
+
<button onClick={() => setAnswer(true)}>Accept</button>
|
|
309
|
+
<button onClick={() => setAnswer(false)}>Decline</button>
|
|
265
310
|
</div>
|
|
266
311
|
)
|
|
267
312
|
}
|
|
268
313
|
```
|
|
269
314
|
|
|
315
|
+
A consent manager that reports on every page keeps calling `storefront.setAttributionConsent` instead, which remembers
|
|
316
|
+
nothing.
|
|
317
|
+
|
|
270
318
|
## Server rendering
|
|
271
319
|
|
|
272
320
|
`fetchCatalog(endpoint, options)` fetches the catalog on a server, as anyone sees it, for the `initialCatalog` a page's
|