@moonbase.sh/react 1.1.0 → 1.2.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/README.md +127 -16
- package/dist/index.cjs +467 -88
- package/dist/index.d.cts +256 -16
- package/dist/index.d.ts +256 -16
- package/dist/index.js +440 -66
- package/package.json +2 -4
package/dist/index.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import { MoonbaseClient } from "@moonbase.sh/storefront-api";
|
|
3
3
|
|
|
4
4
|
// package.json
|
|
5
|
-
var version = "1.
|
|
5
|
+
var version = "1.2.0";
|
|
6
6
|
|
|
7
7
|
// src/storefront.ts
|
|
8
8
|
import {
|
|
@@ -10,6 +10,7 @@ import {
|
|
|
10
10
|
closeCheckout as closeCheckout2,
|
|
11
11
|
copyConsent,
|
|
12
12
|
forgetPurchase as forgetPurchase3,
|
|
13
|
+
globalPrivacyControlRefuses,
|
|
13
14
|
grantsClickIds,
|
|
14
15
|
hasCampaignData,
|
|
15
16
|
isSameArrival,
|
|
@@ -23,11 +24,16 @@ import {
|
|
|
23
24
|
parseLanding,
|
|
24
25
|
parseOffSiteReferrer,
|
|
25
26
|
pruneRememberedPurchase,
|
|
27
|
+
readBrowserIdentifiers,
|
|
28
|
+
readCookies,
|
|
26
29
|
readStorage,
|
|
30
|
+
refusedByGlobalPrivacyControl,
|
|
31
|
+
refusesAny,
|
|
27
32
|
removeStorage,
|
|
28
33
|
resolveDisplayCurrency as resolveDisplayCurrency3,
|
|
29
34
|
resolveUtm,
|
|
30
35
|
schemas as schemas2,
|
|
36
|
+
sendsGlobalPrivacyControl,
|
|
31
37
|
StoreClosedError,
|
|
32
38
|
TokenStore,
|
|
33
39
|
withoutClickIds,
|
|
@@ -37,12 +43,27 @@ import {
|
|
|
37
43
|
|
|
38
44
|
// src/auth.ts
|
|
39
45
|
import { forgetPurchase, schemas } from "@moonbase.sh/storefront-api";
|
|
46
|
+
var confirmationReuse = 5e3;
|
|
40
47
|
function createAuth(storefront, internals) {
|
|
41
48
|
const { setUser } = internals;
|
|
42
49
|
const signedIn = () => storefront.getState().user !== null;
|
|
43
50
|
const refreshCatalog = () => {
|
|
44
51
|
storefront.refreshCatalog().catch((err) => console.error("Could not load the Moonbase catalog", err));
|
|
45
52
|
};
|
|
53
|
+
const confirmations = /* @__PURE__ */ new Map();
|
|
54
|
+
const confirmOnce = (kind, email, code, confirm) => {
|
|
55
|
+
const key = JSON.stringify([kind, email, code]);
|
|
56
|
+
const pending = confirmations.get(key);
|
|
57
|
+
if (pending)
|
|
58
|
+
return pending;
|
|
59
|
+
const result = confirm();
|
|
60
|
+
confirmations.set(key, result);
|
|
61
|
+
const forget = () => {
|
|
62
|
+
setTimeout(() => confirmations.delete(key), confirmationReuse);
|
|
63
|
+
};
|
|
64
|
+
result.then(forget, forget);
|
|
65
|
+
return result;
|
|
66
|
+
};
|
|
46
67
|
return {
|
|
47
68
|
signIn: async (email, password) => {
|
|
48
69
|
const user = await storefront.client.identity.signIn(email, password);
|
|
@@ -97,19 +118,19 @@ function createAuth(storefront, internals) {
|
|
|
97
118
|
setPassword: (currentPassword, newPassword) => storefront.client.identity.setPassword(currentPassword, newPassword),
|
|
98
119
|
forgotPassword: (email) => storefront.client.identity.forgotPassword(email),
|
|
99
120
|
resetPassword: (email, newPassword, code) => storefront.client.identity.resetPassword(email, newPassword, code),
|
|
100
|
-
confirmAccount:
|
|
121
|
+
confirmAccount: (email, code) => confirmOnce("account", email, code, async () => {
|
|
101
122
|
const result = await storefront.client.identity.confirmAccount(email, code);
|
|
102
123
|
if (result.status === "SignedIn") {
|
|
103
124
|
setUser(schemas.identity.userSchema.parse(storefront.client.tokenStore.user));
|
|
104
125
|
refreshCatalog();
|
|
105
126
|
}
|
|
106
127
|
return result;
|
|
107
|
-
},
|
|
108
|
-
confirmEmail: (email, code) => storefront.client.identity.confirmEmail(email, code),
|
|
109
|
-
confirmEmailChange:
|
|
128
|
+
}),
|
|
129
|
+
confirmEmail: (email, code) => confirmOnce("email", email, code, () => storefront.client.identity.confirmEmail(email, code)),
|
|
130
|
+
confirmEmailChange: (email, code) => confirmOnce("email-change", email, code, async () => {
|
|
110
131
|
await storefront.client.identity.confirmEmailChange(email, code);
|
|
111
132
|
return await storefront.refreshUser();
|
|
112
|
-
},
|
|
133
|
+
}),
|
|
113
134
|
getConnectionUrl: (provider, redirectUri, loginId) => storefront.client.identity.getConnectionUrl(provider, redirectUri, loginId),
|
|
114
135
|
disconnectAccount: async (provider) => {
|
|
115
136
|
await storefront.client.identity.disconnectAccount(provider);
|
|
@@ -148,6 +169,7 @@ import {
|
|
|
148
169
|
lineItemId,
|
|
149
170
|
MoonbaseError,
|
|
150
171
|
mountCheckout,
|
|
172
|
+
randomUuid,
|
|
151
173
|
recordCheckoutGroups,
|
|
152
174
|
rememberPurchase,
|
|
153
175
|
resolveRedirect,
|
|
@@ -158,7 +180,6 @@ import {
|
|
|
158
180
|
watchForTakeover,
|
|
159
181
|
withoutHandshakeParams
|
|
160
182
|
} from "@moonbase.sh/storefront-api";
|
|
161
|
-
import { v4 as uuidv4 } from "uuid";
|
|
162
183
|
var wrapperId = "moonbase-checkout-wrapper";
|
|
163
184
|
function checkoutInProgress() {
|
|
164
185
|
return typeof window !== "undefined" && !!document.getElementById(wrapperId);
|
|
@@ -239,13 +260,13 @@ async function checkout(storefront, items, options = {}) {
|
|
|
239
260
|
let resumed = await resumePurchase(storefront.client, userId, options.couponCode);
|
|
240
261
|
let order;
|
|
241
262
|
try {
|
|
242
|
-
order = await pushOrder((_d = resumed == null ? void 0 : resumed.orderId) != null ? _d :
|
|
263
|
+
order = await pushOrder((_d = resumed == null ? void 0 : resumed.orderId) != null ? _d : randomUuid());
|
|
243
264
|
} catch (err) {
|
|
244
265
|
if (!resumed || !isClosedOrderRefusal(err))
|
|
245
266
|
throw err;
|
|
246
267
|
forgetPurchase2(resumed.orderId);
|
|
247
268
|
resumed = null;
|
|
248
|
-
order = await pushOrder(
|
|
269
|
+
order = await pushOrder(randomUuid());
|
|
249
270
|
}
|
|
250
271
|
await recordCheckoutGroups(storefront.client, order.id, options.groupIds);
|
|
251
272
|
let ownCoupon = (_e = resumed == null ? void 0 : resumed.couponCode) != null ? _e : null;
|
|
@@ -289,8 +310,7 @@ async function checkout(storefront, items, options = {}) {
|
|
|
289
310
|
}
|
|
290
311
|
|
|
291
312
|
// src/order.ts
|
|
292
|
-
import { CartUtils as CartUtils2, OfferUtils, OrderStatus, resolveDisplayCurrency } from "@moonbase.sh/storefront-api";
|
|
293
|
-
import { v4 as uuidv42 } from "uuid";
|
|
313
|
+
import { CartUtils as CartUtils2, OfferUtils, OrderStatus, randomUuid as randomUuid2, resolveDisplayCurrency } from "@moonbase.sh/storefront-api";
|
|
294
314
|
function emptyCatalog() {
|
|
295
315
|
return {
|
|
296
316
|
suggestedCurrency: "",
|
|
@@ -303,7 +323,7 @@ function emptyCatalog() {
|
|
|
303
323
|
}
|
|
304
324
|
function newOrder(currency = "", items = []) {
|
|
305
325
|
return {
|
|
306
|
-
id:
|
|
326
|
+
id: randomUuid2(),
|
|
307
327
|
status: OrderStatus.Open,
|
|
308
328
|
currency,
|
|
309
329
|
items,
|
|
@@ -608,6 +628,10 @@ var _MoonbaseStorefront = class _MoonbaseStorefront {
|
|
|
608
628
|
this.client = client;
|
|
609
629
|
this.listeners = /* @__PURE__ */ new Set();
|
|
610
630
|
this.refreshOrderAbortController = new AbortController();
|
|
631
|
+
/** Aborted by `dispose`, which takes the storefront's window listeners with it. */
|
|
632
|
+
this.lifetime = new AbortController();
|
|
633
|
+
/** Whether the storefront took the hosted checkout's parameters off the address as it was made. */
|
|
634
|
+
this.tookCheckoutReturn = false;
|
|
611
635
|
// Catalog refreshes are numbered as they start, and one only lands if no later one has landed first.
|
|
612
636
|
// They overlap: a checkout's billing details and its completion each start one a moment apart, and
|
|
613
637
|
// when the first to start is the last to answer, its catalog is from before the purchase and would
|
|
@@ -629,7 +653,18 @@ var _MoonbaseStorefront = class _MoonbaseStorefront {
|
|
|
629
653
|
this.listeners.delete(listener);
|
|
630
654
|
};
|
|
631
655
|
};
|
|
632
|
-
|
|
656
|
+
/**
|
|
657
|
+
* Records the visitor's answer to the site's own cookie banner: remembered in this browser, so the
|
|
658
|
+
* storefront starts from it on their next visit, and passed on with `setAttributionConsent`. Other tabs
|
|
659
|
+
* follow it. `useAttributionConsent` hands it out. A banner that asks for Google's two consent mode
|
|
660
|
+
* signals separately passes them to `setAttributionConsent` instead.
|
|
661
|
+
*/
|
|
662
|
+
this.answerAttributionConsent = (granted) => {
|
|
663
|
+
writeStorage(_MoonbaseStorefront.attributionConsentKey, String(granted));
|
|
664
|
+
this.setState({ attributionConsentAnswer: granted });
|
|
665
|
+
this.setAttributionConsent(granted);
|
|
666
|
+
};
|
|
667
|
+
var _a, _b, _c;
|
|
633
668
|
this.autoApplyItemOffers = (_a = options.autoApplyItemOffers) != null ? _a : false;
|
|
634
669
|
this.catalogLoadedPromise = new Promise((resolve) => this.resolveCatalogLoaded = resolve);
|
|
635
670
|
this.userLoadedPromise = new Promise((resolve) => this.resolveUserLoaded = resolve);
|
|
@@ -637,6 +672,10 @@ var _MoonbaseStorefront = class _MoonbaseStorefront {
|
|
|
637
672
|
this.cart = createCart(this, { setOrder: (order2) => this.setState({ order: order2 }) });
|
|
638
673
|
const initialCatalog = options.initialCatalog ? _MoonbaseStorefront.normalizeCatalog(options.initialCatalog) : emptyCatalog();
|
|
639
674
|
this.serverState = {
|
|
675
|
+
attributionConsentAnswer: null,
|
|
676
|
+
attributionConsentRequired: void 0,
|
|
677
|
+
attributionConsentStatus: "unknown",
|
|
678
|
+
attributionConsentStatusByPlatform: { meta: "unknown", googleAnalytics: "unknown" },
|
|
640
679
|
catalog: initialCatalog,
|
|
641
680
|
catalogLoaded: !!options.initialCatalog,
|
|
642
681
|
closed: false,
|
|
@@ -652,8 +691,12 @@ var _MoonbaseStorefront = class _MoonbaseStorefront {
|
|
|
652
691
|
this.resolveUserLoaded();
|
|
653
692
|
return;
|
|
654
693
|
}
|
|
655
|
-
|
|
656
|
-
|
|
694
|
+
const { signal } = this.lifetime;
|
|
695
|
+
window.addEventListener("storage", (event) => this.handleStorageUpdate(event), { signal });
|
|
696
|
+
this.client.tokenStore.onSessionEnded(() => {
|
|
697
|
+
if (!this.disposed)
|
|
698
|
+
this.endSession();
|
|
699
|
+
});
|
|
657
700
|
const urlParams = new URLSearchParams(window.location.search);
|
|
658
701
|
const fresh = {
|
|
659
702
|
referrer: parseOffSiteReferrer(window.document.referrer, window.location.origin),
|
|
@@ -673,7 +716,8 @@ var _MoonbaseStorefront = class _MoonbaseStorefront {
|
|
|
673
716
|
ttclid: urlParams.get("ttclid") || void 0
|
|
674
717
|
};
|
|
675
718
|
fresh.adClicks = adClicksOf(fresh);
|
|
676
|
-
|
|
719
|
+
const answer = _MoonbaseStorefront.parseAnswer(readStorage(_MoonbaseStorefront.attributionConsentKey));
|
|
720
|
+
this.attributionConsentSetting = copyConsent((_c = (_b = configuration.attributionConsent) != null ? _b : answer) != null ? _c : void 0);
|
|
677
721
|
const sessionRaw = readStorage(_MoonbaseStorefront.utmKey, "session");
|
|
678
722
|
const sessionCached = _MoonbaseStorefront.parseCachedUtm(sessionRaw);
|
|
679
723
|
const cached = sessionCached != null ? sessionCached : configuration.persistUtm && this.attributionConsent ? _MoonbaseStorefront.parseCachedUtm(readStorage(_MoonbaseStorefront.utmKey, "local")) : void 0;
|
|
@@ -703,7 +747,7 @@ var _MoonbaseStorefront = class _MoonbaseStorefront {
|
|
|
703
747
|
}
|
|
704
748
|
const cachedCatalogJson = readStorage(_MoonbaseStorefront.storefrontKey);
|
|
705
749
|
const cachedCatalog = cachedCatalogJson ? _MoonbaseStorefront.parseCachedCatalog(cachedCatalogJson) : null;
|
|
706
|
-
const catalog = cachedCatalog
|
|
750
|
+
const catalog = cachedCatalog ? { ...cachedCatalog, tracking: void 0 } : this.serverState.catalog;
|
|
707
751
|
const cachedOrder = _MoonbaseStorefront.parseCachedOrder(readStorage(_MoonbaseStorefront.sessionKey));
|
|
708
752
|
const order = cachedOrder != null ? cachedOrder : newOrder();
|
|
709
753
|
let user = null;
|
|
@@ -716,6 +760,9 @@ var _MoonbaseStorefront = class _MoonbaseStorefront {
|
|
|
716
760
|
}
|
|
717
761
|
}
|
|
718
762
|
this.state = {
|
|
763
|
+
attributionConsentAnswer: answer,
|
|
764
|
+
attributionConsentRequired: void 0,
|
|
765
|
+
...this.consentStatus(catalog.tracking),
|
|
719
766
|
catalog,
|
|
720
767
|
catalogLoaded: !!cachedCatalog || this.serverState.catalogLoaded,
|
|
721
768
|
closed: false,
|
|
@@ -728,6 +775,7 @@ var _MoonbaseStorefront = class _MoonbaseStorefront {
|
|
|
728
775
|
};
|
|
729
776
|
if (this.state.catalogLoaded)
|
|
730
777
|
this.resolveCatalogLoaded();
|
|
778
|
+
void this.reportAdConsentRefusal();
|
|
731
779
|
if (cachedOrder) {
|
|
732
780
|
void this.refreshOrder();
|
|
733
781
|
} else if (this.hasUtmOrReferrer && this.attributionConsent) {
|
|
@@ -747,21 +795,22 @@ var _MoonbaseStorefront = class _MoonbaseStorefront {
|
|
|
747
795
|
else
|
|
748
796
|
mountSubscription(finalEndpoint.toString());
|
|
749
797
|
window.history.replaceState(window.history.state, "", withoutHandshakeParams2(window.location.href));
|
|
798
|
+
this.tookCheckoutReturn = true;
|
|
750
799
|
}
|
|
751
800
|
window.addEventListener("moonbase-checkout-billing-details-updated", () => {
|
|
752
801
|
this.refreshInBackground();
|
|
753
|
-
});
|
|
802
|
+
}, { signal });
|
|
754
803
|
window.addEventListener("moonbase-checkout-completed", (event) => {
|
|
755
804
|
const completed = event.detail.order;
|
|
756
805
|
if (completed.id === this.state.order.id)
|
|
757
806
|
this.resetOrder();
|
|
758
807
|
forgetPurchase3(completed.id);
|
|
759
808
|
this.refreshInBackground();
|
|
760
|
-
});
|
|
809
|
+
}, { signal });
|
|
761
810
|
}
|
|
762
811
|
/**
|
|
763
812
|
* Calls `callback` each time the storefront reports `event`, and returns the function that stops it.
|
|
764
|
-
* In a component, `useStorefrontEvent` does the subscribing and the cleaning up.
|
|
813
|
+
* `dispose` stops it too. In a component, `useStorefrontEvent` does the subscribing and the cleaning up.
|
|
765
814
|
*/
|
|
766
815
|
on(event, callback) {
|
|
767
816
|
if (typeof window === "undefined")
|
|
@@ -769,9 +818,50 @@ var _MoonbaseStorefront = class _MoonbaseStorefront {
|
|
|
769
818
|
};
|
|
770
819
|
const [name, read] = windowEvents[event];
|
|
771
820
|
const listener = (e) => callback(...read(e.detail));
|
|
772
|
-
window.addEventListener(name, listener);
|
|
821
|
+
window.addEventListener(name, listener, { signal: this.lifetime.signal });
|
|
773
822
|
return () => window.removeEventListener(name, listener);
|
|
774
823
|
}
|
|
824
|
+
/** Whether `dispose` has stopped the storefront. */
|
|
825
|
+
get disposed() {
|
|
826
|
+
return this.lifetime.signal.aborted;
|
|
827
|
+
}
|
|
828
|
+
/**
|
|
829
|
+
* Stops the storefront, for a test or for a page that swaps it for another: it stops following other
|
|
830
|
+
* tabs, the checkout and the customer's session, stops calling what `on` subscribed, stops renewing
|
|
831
|
+
* their sign-in, and ignores what requests still in flight bring back. A cart change still waiting to
|
|
832
|
+
* be sent goes at once. What it stored in the browser stays, for the next storefront to pick up.
|
|
833
|
+
* `getStorefront` makes a new one after this.
|
|
834
|
+
*/
|
|
835
|
+
dispose() {
|
|
836
|
+
var _a;
|
|
837
|
+
if (this.disposed)
|
|
838
|
+
return;
|
|
839
|
+
this.lifetime.abort();
|
|
840
|
+
this.refreshOrderAbortController.abort();
|
|
841
|
+
clearTimeout(this.pushTimeout);
|
|
842
|
+
(_a = this.pendingPush) == null ? void 0 : _a.call(this);
|
|
843
|
+
this.client.tokenStore.dispose();
|
|
844
|
+
this.listeners.clear();
|
|
845
|
+
}
|
|
846
|
+
/**
|
|
847
|
+
* Takes the hosted checkout's parameters off the address again, when the storefront took them off as
|
|
848
|
+
* it was made and a router has since put them back, and has the router read the address again.
|
|
849
|
+
* `<StorefrontProvider>` calls it once the page has hydrated, which is all a page needs.
|
|
850
|
+
*
|
|
851
|
+
* Next.js's router is why: it reads the address before any of the page's own code runs, and writes it
|
|
852
|
+
* back as the page hydrates. It is told with a `popstate`, as for Back, since it ignores an address
|
|
853
|
+
* changed under the history entry it wrote, and `useSearchParams()` would go on listing the parameters.
|
|
854
|
+
*/
|
|
855
|
+
clearCheckoutReturn() {
|
|
856
|
+
if (!this.tookCheckoutReturn || this.disposed || typeof window === "undefined")
|
|
857
|
+
return;
|
|
858
|
+
this.tookCheckoutReturn = false;
|
|
859
|
+
const address = withoutHandshakeParams2(window.location.href);
|
|
860
|
+
if (address === window.location.href)
|
|
861
|
+
return;
|
|
862
|
+
window.history.replaceState(window.history.state, "", address);
|
|
863
|
+
window.dispatchEvent(new PopStateEvent("popstate", { state: window.history.state }));
|
|
864
|
+
}
|
|
775
865
|
get hasUtm() {
|
|
776
866
|
const utm = this.state.utm;
|
|
777
867
|
return !!utm && !!(utm.source || utm.medium || utm.campaign || utm.term || utm.content || utm.id || utm.gadSource || utm.gadCampaignId);
|
|
@@ -812,6 +902,8 @@ var _MoonbaseStorefront = class _MoonbaseStorefront {
|
|
|
812
902
|
const before = this.attributionConsent;
|
|
813
903
|
this.attributionConsentSetting = copyConsent(granted);
|
|
814
904
|
this.attributionConsentChanged(before);
|
|
905
|
+
this.setState(this.consentStatus(this.state.catalog.tracking));
|
|
906
|
+
void this.reportAdConsentRefusal();
|
|
815
907
|
if (grantsClickIds(this.attributionConsentSetting) === false && typeof window !== "undefined") {
|
|
816
908
|
if (this.hasUtmOrReferrer)
|
|
817
909
|
writeStorage(_MoonbaseStorefront.utmKey, JSON.stringify(this.state.utm), "session");
|
|
@@ -824,14 +916,17 @@ var _MoonbaseStorefront = class _MoonbaseStorefront {
|
|
|
824
916
|
/**
|
|
825
917
|
* What the storefront's requests say of the visit for ad measurement, beside `utm`: what the site last
|
|
826
918
|
* reported of the visitor's consent, left out until it says, and when and on which page the visit's
|
|
827
|
-
* tagged arrival landed, left out for a visit without tags.
|
|
919
|
+
* tagged arrival landed, left out for a visit without tags. Where the store connected Meta or Google
|
|
920
|
+
* Analytics in Moonbase, and while the visitor's consent lets click IDs go, also their browser IDs and
|
|
921
|
+
* the page the request is made on.
|
|
828
922
|
*/
|
|
829
923
|
adSignals() {
|
|
830
924
|
const landing = hasCampaignData(this.state.utm) ? this.landing : void 0;
|
|
831
925
|
return {
|
|
832
926
|
consent: copyConsent(this.attributionConsentSetting),
|
|
833
927
|
arrivedAt: landing == null ? void 0 : landing.arrivedAt,
|
|
834
|
-
landingPage: landing == null ? void 0 : landing.landingPage
|
|
928
|
+
landingPage: landing == null ? void 0 : landing.landingPage,
|
|
929
|
+
...this.browserIdentifiers(landing)
|
|
835
930
|
};
|
|
836
931
|
}
|
|
837
932
|
/** Asks Moonbase who is signed in. `state.user` is null when nobody is, or the session has run out. */
|
|
@@ -861,10 +956,11 @@ var _MoonbaseStorefront = class _MoonbaseStorefront {
|
|
|
861
956
|
} catch (err) {
|
|
862
957
|
if (!(err instanceof StoreClosedError))
|
|
863
958
|
throw err;
|
|
864
|
-
this.
|
|
959
|
+
if (!this.disposed)
|
|
960
|
+
this.closeStore();
|
|
865
961
|
return this.state.catalog;
|
|
866
962
|
}
|
|
867
|
-
if (refresh < this.catalogRefreshLanded || !latest)
|
|
963
|
+
if (refresh < this.catalogRefreshLanded || !latest || this.disposed)
|
|
868
964
|
return this.state.catalog;
|
|
869
965
|
this.catalogRefreshLanded = refresh;
|
|
870
966
|
writeStorage(_MoonbaseStorefront.storefrontKey, JSON.stringify(latest));
|
|
@@ -878,8 +974,11 @@ var _MoonbaseStorefront = class _MoonbaseStorefront {
|
|
|
878
974
|
const consentedBefore = this.attributionConsent;
|
|
879
975
|
this.storeAllowsAttribution = latest.cookieConsentRequired === false;
|
|
880
976
|
this.attributionConsentChanged(consentedBefore);
|
|
881
|
-
|
|
977
|
+
this.setState(this.consentStatus(this.state.catalog.tracking));
|
|
978
|
+
if (typeof window !== "undefined") {
|
|
979
|
+
this.setState({ attributionConsentRequired: latest.cookieConsentRequired !== false });
|
|
882
980
|
window.dispatchEvent(new CustomEvent("moonbase-storefront-updated", { detail: { storefront: latest } }));
|
|
981
|
+
}
|
|
883
982
|
return latest;
|
|
884
983
|
}
|
|
885
984
|
/**
|
|
@@ -892,11 +991,15 @@ var _MoonbaseStorefront = class _MoonbaseStorefront {
|
|
|
892
991
|
this.refreshOrderAbortController = new AbortController();
|
|
893
992
|
const pushed = this.state.order;
|
|
894
993
|
clearTimeout(this.pushTimeout);
|
|
895
|
-
|
|
994
|
+
const push = () => {
|
|
995
|
+
this.pushTimeout = void 0;
|
|
996
|
+
this.pendingPush = void 0;
|
|
896
997
|
const latest = this.state.order;
|
|
897
998
|
const order = latest.id === pushed.id ? latest : pushed;
|
|
898
999
|
this.client.orders.pushContent(order, void 0, this.hasUtmOrReferrer ? this.state.utm : void 0, this.adSignals()).catch((err) => console.warn("Could not save the Moonbase cart", err));
|
|
899
|
-
}
|
|
1000
|
+
};
|
|
1001
|
+
this.pendingPush = push;
|
|
1002
|
+
this.pushTimeout = setTimeout(push, 500);
|
|
900
1003
|
}
|
|
901
1004
|
/**
|
|
902
1005
|
* Starts a new, empty cart, or with `keepItems` a new cart holding the same lines, under a new ID.
|
|
@@ -953,8 +1056,9 @@ var _MoonbaseStorefront = class _MoonbaseStorefront {
|
|
|
953
1056
|
}
|
|
954
1057
|
setState(patch) {
|
|
955
1058
|
const keys = Object.keys(patch);
|
|
956
|
-
if (keys.every((key) => Object.is(this.state[key], patch[key])))
|
|
1059
|
+
if (this.disposed || keys.every((key) => Object.is(this.state[key], patch[key])))
|
|
957
1060
|
return;
|
|
1061
|
+
const before = this.state;
|
|
958
1062
|
this.state = { ...this.state, ...patch };
|
|
959
1063
|
if (this.state.catalogLoaded)
|
|
960
1064
|
this.resolveCatalogLoaded();
|
|
@@ -962,6 +1066,8 @@ var _MoonbaseStorefront = class _MoonbaseStorefront {
|
|
|
962
1066
|
this.resolveUserLoaded();
|
|
963
1067
|
for (const listener of [...this.listeners])
|
|
964
1068
|
listener();
|
|
1069
|
+
if (this.state.user !== before.user)
|
|
1070
|
+
void this.reportAdConsentRefusal();
|
|
965
1071
|
}
|
|
966
1072
|
/**
|
|
967
1073
|
* The server refused to renew the buyer's session: they signed out of it elsewhere, changed their
|
|
@@ -1031,6 +1137,65 @@ var _MoonbaseStorefront = class _MoonbaseStorefront {
|
|
|
1031
1137
|
consentedUtm(utm) {
|
|
1032
1138
|
return this.attributionConsent ? utm : withoutClickIds(utm);
|
|
1033
1139
|
}
|
|
1140
|
+
/**
|
|
1141
|
+
* The visitor's Meta and Google Analytics browser IDs, and this page, for a store that connected either
|
|
1142
|
+
* in Moonbase, while the visitor's consent lets click IDs go. A browser that sends Global Privacy Control
|
|
1143
|
+
* gives only those of a platform the store set to ignore it. Read as each request is made, since the
|
|
1144
|
+
* vendors' tags write their cookies after the storefront starts.
|
|
1145
|
+
*/
|
|
1146
|
+
browserIdentifiers(landing) {
|
|
1147
|
+
var _a, _b, _c, _d, _e;
|
|
1148
|
+
const tracking = this.state.catalog.tracking;
|
|
1149
|
+
const meta = ((_b = (_a = tracking == null ? void 0 : tracking.meta) == null ? void 0 : _a.pixels.length) != null ? _b : 0) > 0 && !refusedByGlobalPrivacyControl(tracking == null ? void 0 : tracking.meta);
|
|
1150
|
+
const googleAnalytics = refusedByGlobalPrivacyControl(tracking == null ? void 0 : tracking.googleAnalytics) ? [] : (_d = (_c = tracking == null ? void 0 : tracking.googleAnalytics) == null ? void 0 : _c.streams.map((stream) => stream.id)) != null ? _d : [];
|
|
1151
|
+
if (!meta && googleAnalytics.length === 0 || !this.attributionConsent || typeof window === "undefined")
|
|
1152
|
+
return {};
|
|
1153
|
+
return {
|
|
1154
|
+
...readBrowserIdentifiers({
|
|
1155
|
+
cookies: readCookies(),
|
|
1156
|
+
meta,
|
|
1157
|
+
googleAnalytics,
|
|
1158
|
+
arrival: { fbclid: (_e = this.state.utm) == null ? void 0 : _e.fbclid, arrivedAt: landing == null ? void 0 : landing.arrivedAt }
|
|
1159
|
+
}),
|
|
1160
|
+
pageUrl: `${window.location.origin}${window.location.pathname}`
|
|
1161
|
+
};
|
|
1162
|
+
}
|
|
1163
|
+
consentStatus(tracking) {
|
|
1164
|
+
const answered = grantsClickIds(this.attributionConsentSetting) === false ? "refused" : this.attributionConsent ? "granted" : "unknown";
|
|
1165
|
+
return {
|
|
1166
|
+
attributionConsentStatus: globalPrivacyControlRefuses(tracking) ? "refused" : answered,
|
|
1167
|
+
attributionConsentStatusByPlatform: {
|
|
1168
|
+
meta: refusedByGlobalPrivacyControl(tracking == null ? void 0 : tracking.meta) ? "refused" : answered,
|
|
1169
|
+
googleAnalytics: refusedByGlobalPrivacyControl(tracking == null ? void 0 : tracking.googleAnalytics) ? "refused" : answered
|
|
1170
|
+
}
|
|
1171
|
+
};
|
|
1172
|
+
}
|
|
1173
|
+
/**
|
|
1174
|
+
* Tells Moonbase when the site reports that a signed-in customer refuses ad tracking, which renewals
|
|
1175
|
+
* and other events sent without a visit then follow. Once per tab for each customer and report, as a
|
|
1176
|
+
* site's banner often reports the same choice on every page.
|
|
1177
|
+
*/
|
|
1178
|
+
async reportAdConsentRefusal() {
|
|
1179
|
+
if (typeof window === "undefined")
|
|
1180
|
+
return;
|
|
1181
|
+
const user = this.state.user;
|
|
1182
|
+
const consent = this.attributionConsentSetting;
|
|
1183
|
+
const globalPrivacyControl = sendsGlobalPrivacyControl();
|
|
1184
|
+
if (!refusesAny(consent) && !globalPrivacyControl) {
|
|
1185
|
+
if (consent !== void 0)
|
|
1186
|
+
removeStorage(_MoonbaseStorefront.adConsentReportedKey, "session");
|
|
1187
|
+
return;
|
|
1188
|
+
}
|
|
1189
|
+
const reported = JSON.stringify({ user: user == null ? void 0 : user.id, consent, globalPrivacyControl });
|
|
1190
|
+
if (!user || readStorage(_MoonbaseStorefront.adConsentReportedKey, "session") === reported)
|
|
1191
|
+
return;
|
|
1192
|
+
writeStorage(_MoonbaseStorefront.adConsentReportedKey, reported, "session");
|
|
1193
|
+
try {
|
|
1194
|
+
await this.client.identity.reportAdConsent(this.adSignals());
|
|
1195
|
+
} catch (e) {
|
|
1196
|
+
removeStorage(_MoonbaseStorefront.adConsentReportedKey, "session");
|
|
1197
|
+
}
|
|
1198
|
+
}
|
|
1034
1199
|
handleStorageUpdate(event) {
|
|
1035
1200
|
var _a;
|
|
1036
1201
|
switch (event.key) {
|
|
@@ -1053,6 +1218,13 @@ var _MoonbaseStorefront = class _MoonbaseStorefront {
|
|
|
1053
1218
|
}
|
|
1054
1219
|
break;
|
|
1055
1220
|
}
|
|
1221
|
+
case _MoonbaseStorefront.attributionConsentKey: {
|
|
1222
|
+
const answer = _MoonbaseStorefront.parseAnswer(event.newValue);
|
|
1223
|
+
this.setState({ attributionConsentAnswer: answer });
|
|
1224
|
+
if (answer !== null)
|
|
1225
|
+
this.setAttributionConsent(answer);
|
|
1226
|
+
break;
|
|
1227
|
+
}
|
|
1056
1228
|
case _MoonbaseStorefront.arrivalKey: {
|
|
1057
1229
|
const landing = parseLanding(event.newValue);
|
|
1058
1230
|
if (landing)
|
|
@@ -1078,7 +1250,7 @@ var _MoonbaseStorefront = class _MoonbaseStorefront {
|
|
|
1078
1250
|
const refreshingId = this.state.order.id;
|
|
1079
1251
|
try {
|
|
1080
1252
|
const latest = await this.client.orders.get(refreshingId, { abort: this.refreshOrderAbortController.signal });
|
|
1081
|
-
if (this.state.order.id !== refreshingId || !latest)
|
|
1253
|
+
if (this.state.order.id !== refreshingId || !latest || this.disposed)
|
|
1082
1254
|
return;
|
|
1083
1255
|
if (latest.status !== OrderStatus3.Open) {
|
|
1084
1256
|
this.resetOrder();
|
|
@@ -1096,6 +1268,9 @@ var _MoonbaseStorefront = class _MoonbaseStorefront {
|
|
|
1096
1268
|
this.resetOrder();
|
|
1097
1269
|
}
|
|
1098
1270
|
}
|
|
1271
|
+
static parseAnswer(raw) {
|
|
1272
|
+
return raw === "true" ? true : raw === "false" ? false : null;
|
|
1273
|
+
}
|
|
1099
1274
|
static parseCachedUtm(raw) {
|
|
1100
1275
|
if (!raw)
|
|
1101
1276
|
return void 0;
|
|
@@ -1141,12 +1316,18 @@ _MoonbaseStorefront.sessionKey = "moonbase_session";
|
|
|
1141
1316
|
_MoonbaseStorefront.storefrontKey = "moonbase_storefront";
|
|
1142
1317
|
_MoonbaseStorefront.utmKey = "moonbase_utm";
|
|
1143
1318
|
_MoonbaseStorefront.arrivalKey = "moonbase_arrival";
|
|
1319
|
+
_MoonbaseStorefront.adConsentReportedKey = "moonbase_ad_consent_reported";
|
|
1320
|
+
_MoonbaseStorefront.attributionConsentKey = "moonbase_attribution_consent";
|
|
1144
1321
|
var MoonbaseStorefront = _MoonbaseStorefront;
|
|
1145
1322
|
|
|
1146
1323
|
// src/provider.ts
|
|
1147
1324
|
import { createContext, createElement, useContext, useEffect, useRef, useSyncExternalStore } from "react";
|
|
1148
1325
|
var StorefrontContext = createContext(null);
|
|
1149
1326
|
function StorefrontProvider({ storefront, children }) {
|
|
1327
|
+
useEffect(() => {
|
|
1328
|
+
const timeout = setTimeout(() => storefront.clearCheckoutReturn());
|
|
1329
|
+
return () => clearTimeout(timeout);
|
|
1330
|
+
}, [storefront]);
|
|
1150
1331
|
return createElement(StorefrontContext.Provider, { value: storefront }, children);
|
|
1151
1332
|
}
|
|
1152
1333
|
function useStorefront() {
|
|
@@ -1269,29 +1450,152 @@ function useCartOffer() {
|
|
|
1269
1450
|
return useMemo(() => cartOfferState(catalog, order), [catalog, order]);
|
|
1270
1451
|
}
|
|
1271
1452
|
|
|
1453
|
+
// src/hooks/useAccountData.ts
|
|
1454
|
+
import { AccountDataCache } from "@moonbase.sh/storefront-api";
|
|
1455
|
+
import { useCallback, useEffect as useEffect2, useMemo as useMemo2, useSyncExternalStore as useSyncExternalStore2 } from "react";
|
|
1456
|
+
var caches = /* @__PURE__ */ new WeakMap();
|
|
1457
|
+
function cacheOf(storefront) {
|
|
1458
|
+
var _a;
|
|
1459
|
+
let cache = caches.get(storefront);
|
|
1460
|
+
if (!cache) {
|
|
1461
|
+
const created = new AccountDataCache();
|
|
1462
|
+
let customer = (_a = storefront.getState().user) == null ? void 0 : _a.id;
|
|
1463
|
+
storefront.subscribe(() => {
|
|
1464
|
+
var _a2;
|
|
1465
|
+
const now = (_a2 = storefront.getState().user) == null ? void 0 : _a2.id;
|
|
1466
|
+
if (now !== customer) {
|
|
1467
|
+
customer = now;
|
|
1468
|
+
created.clear();
|
|
1469
|
+
}
|
|
1470
|
+
});
|
|
1471
|
+
caches.set(storefront, created);
|
|
1472
|
+
cache = created;
|
|
1473
|
+
}
|
|
1474
|
+
return cache;
|
|
1475
|
+
}
|
|
1476
|
+
var nothing = { data: null, loading: false, error: null, hasMore: false, next: null };
|
|
1477
|
+
async function noop() {
|
|
1478
|
+
}
|
|
1479
|
+
function useEntry(resource) {
|
|
1480
|
+
const storefront = useStorefront();
|
|
1481
|
+
const cache = cacheOf(storefront);
|
|
1482
|
+
const customer = useStorefrontState((state) => {
|
|
1483
|
+
var _a, _b;
|
|
1484
|
+
return (_b = (_a = state.user) == null ? void 0 : _a.id) != null ? _b : null;
|
|
1485
|
+
});
|
|
1486
|
+
const userLoaded = useStorefrontState((state) => state.userLoaded);
|
|
1487
|
+
const key = customer && resource !== null ? `${customer}
|
|
1488
|
+
${resource}` : null;
|
|
1489
|
+
const entry = useSyncExternalStore2(
|
|
1490
|
+
cache.subscribe,
|
|
1491
|
+
() => {
|
|
1492
|
+
var _a;
|
|
1493
|
+
return (_a = key ? cache.get(key) : void 0) != null ? _a : nothing;
|
|
1494
|
+
},
|
|
1495
|
+
() => nothing
|
|
1496
|
+
);
|
|
1497
|
+
const pending = resource !== null && (customer ? entry === nothing : !userLoaded);
|
|
1498
|
+
return { storefront, cache, key, entry, pending };
|
|
1499
|
+
}
|
|
1500
|
+
function useAccountItem(kind, id, fetch) {
|
|
1501
|
+
const resource = id == null ? null : `${kind}
|
|
1502
|
+
${id}`;
|
|
1503
|
+
const { storefront, cache, key, entry, pending } = useEntry(resource);
|
|
1504
|
+
const reload = useCallback(
|
|
1505
|
+
() => key && id != null ? cache.refresh(key, () => fetch(storefront.client, id)) : noop(),
|
|
1506
|
+
[cache, key, id, fetch, storefront]
|
|
1507
|
+
);
|
|
1508
|
+
useEffect2(() => {
|
|
1509
|
+
if (key)
|
|
1510
|
+
cache.ensure(key, reload);
|
|
1511
|
+
}, [cache, key, reload]);
|
|
1512
|
+
return useMemo2(() => ({
|
|
1513
|
+
data: entry.data,
|
|
1514
|
+
loading: entry.loading || pending,
|
|
1515
|
+
error: entry.error,
|
|
1516
|
+
reload
|
|
1517
|
+
}), [entry, pending, reload]);
|
|
1518
|
+
}
|
|
1519
|
+
function useAccountPages(kind, id, fetchPage) {
|
|
1520
|
+
const resource = id == null ? null : `${kind}
|
|
1521
|
+
${id}`;
|
|
1522
|
+
const { storefront, cache, key, entry, pending } = useEntry(resource);
|
|
1523
|
+
const reload = useCallback(
|
|
1524
|
+
() => key && id != null ? cache.refreshPages(key, (next) => fetchPage(storefront.client, id, next)) : noop(),
|
|
1525
|
+
[cache, key, id, fetchPage, storefront]
|
|
1526
|
+
);
|
|
1527
|
+
const loadMore = useCallback(
|
|
1528
|
+
() => key && id != null ? cache.loadMore(key, (next) => fetchPage(storefront.client, id, next)) : noop(),
|
|
1529
|
+
[cache, key, id, fetchPage, storefront]
|
|
1530
|
+
);
|
|
1531
|
+
useEffect2(() => {
|
|
1532
|
+
if (key)
|
|
1533
|
+
cache.ensure(key, reload);
|
|
1534
|
+
}, [cache, key, reload]);
|
|
1535
|
+
return useMemo2(() => ({
|
|
1536
|
+
data: entry.data,
|
|
1537
|
+
loading: entry.loading || pending,
|
|
1538
|
+
error: entry.error,
|
|
1539
|
+
hasMore: entry.hasMore,
|
|
1540
|
+
reload,
|
|
1541
|
+
loadMore
|
|
1542
|
+
}), [entry, pending, reload, loadMore]);
|
|
1543
|
+
}
|
|
1544
|
+
var ownedProducts = (client, _, next) => client.inventory.products.getOwned(next);
|
|
1545
|
+
var ownedProduct = (client, productId) => client.inventory.products.get(productId);
|
|
1546
|
+
var productActivations = (client, productId, next) => client.inventory.products.getActivations(productId, next);
|
|
1547
|
+
var productLicenses = (client, productId, next) => client.inventory.products.getLicenses(productId, next);
|
|
1548
|
+
var orderHistory = (client, _, next) => client.orders.getHistory(next);
|
|
1549
|
+
var subscriptions = (client, _, next) => client.inventory.subscriptions.get(next);
|
|
1550
|
+
var subscription = (client, subscriptionId) => client.inventory.subscriptions.getById(subscriptionId);
|
|
1551
|
+
function useOwnedProducts() {
|
|
1552
|
+
return useAccountPages("products", "", ownedProducts);
|
|
1553
|
+
}
|
|
1554
|
+
function useOwnedProduct(productId) {
|
|
1555
|
+
return useAccountItem("product", productId, ownedProduct);
|
|
1556
|
+
}
|
|
1557
|
+
function useProductActivations(productId) {
|
|
1558
|
+
return useAccountPages("product-activations", productId, productActivations);
|
|
1559
|
+
}
|
|
1560
|
+
function useProductLicenses(productId) {
|
|
1561
|
+
return useAccountPages("product-licenses", productId, productLicenses);
|
|
1562
|
+
}
|
|
1563
|
+
function useOrderHistory() {
|
|
1564
|
+
return useAccountPages("orders", "", orderHistory);
|
|
1565
|
+
}
|
|
1566
|
+
function useSubscriptions() {
|
|
1567
|
+
return useAccountPages("subscriptions", "", subscriptions);
|
|
1568
|
+
}
|
|
1569
|
+
function useSubscription(subscriptionId) {
|
|
1570
|
+
return useAccountItem("subscription", subscriptionId, subscription);
|
|
1571
|
+
}
|
|
1572
|
+
|
|
1272
1573
|
// src/hooks/useActivationRequest.ts
|
|
1273
1574
|
import { ActivationRequestStatus, NotAuthenticatedError as NotAuthenticatedError2 } from "@moonbase.sh/storefront-api";
|
|
1274
|
-
import { useCallback, useEffect as
|
|
1575
|
+
import { useCallback as useCallback2, useEffect as useEffect3, useRef as useRef2, useState } from "react";
|
|
1275
1576
|
var pollInterval = 1500;
|
|
1276
|
-
function
|
|
1277
|
-
|
|
1577
|
+
function errorMessage(err) {
|
|
1578
|
+
return err instanceof NotAuthenticatedError2 ? "You need to be signed in to do this" : err.message;
|
|
1579
|
+
}
|
|
1580
|
+
function useActivationRequest(token, options = {}) {
|
|
1581
|
+
var _a, _b, _c;
|
|
1278
1582
|
const storefront = useStorefront();
|
|
1279
1583
|
const userId = useStorefrontState((state) => {
|
|
1280
|
-
var _a2,
|
|
1281
|
-
return (
|
|
1584
|
+
var _a2, _b2;
|
|
1585
|
+
return (_b2 = (_a2 = state.user) == null ? void 0 : _a2.id) != null ? _b2 : null;
|
|
1282
1586
|
});
|
|
1283
1587
|
const [activationRequest, setActivationRequest] = useState(null);
|
|
1284
1588
|
const [fetched, setFetched] = useState(null);
|
|
1285
1589
|
const [fulfilling, setFulfilling] = useState(false);
|
|
1286
1590
|
const [completing, setCompleting] = useState(false);
|
|
1287
1591
|
const [error, setError] = useState(null);
|
|
1288
|
-
const adopt =
|
|
1592
|
+
const adopt = useCallback2((request) => {
|
|
1289
1593
|
setActivationRequest(request);
|
|
1290
1594
|
if (request.status === ActivationRequestStatus.Fulfilled)
|
|
1291
1595
|
setCompleting(true);
|
|
1292
1596
|
}, []);
|
|
1293
1597
|
const upToDate = !!token && (fetched == null ? void 0 : fetched.token) === token && (fetched.userId === userId || !!((_a = activationRequest == null ? void 0 : activationRequest.licenseEligibility) == null ? void 0 : _a.eligible));
|
|
1294
|
-
|
|
1598
|
+
useEffect3(() => {
|
|
1295
1599
|
if (!token || upToDate)
|
|
1296
1600
|
return;
|
|
1297
1601
|
let current = true;
|
|
@@ -1301,7 +1605,7 @@ function useActivationRequest(token) {
|
|
|
1301
1605
|
};
|
|
1302
1606
|
}, [storefront, token, userId, upToDate, adopt]);
|
|
1303
1607
|
const requestId = activationRequest == null ? void 0 : activationRequest.id;
|
|
1304
|
-
|
|
1608
|
+
useEffect3(() => {
|
|
1305
1609
|
if (!completing || !requestId)
|
|
1306
1610
|
return;
|
|
1307
1611
|
let current = true;
|
|
@@ -1328,9 +1632,10 @@ function useActivationRequest(token) {
|
|
|
1328
1632
|
clearTimeout(timeout);
|
|
1329
1633
|
};
|
|
1330
1634
|
}, [storefront, completing, requestId]);
|
|
1331
|
-
const fulfillLicense =
|
|
1635
|
+
const fulfillLicense = useCallback2(async () => {
|
|
1332
1636
|
if (!requestId)
|
|
1333
1637
|
return;
|
|
1638
|
+
setError(null);
|
|
1334
1639
|
setFulfilling(true);
|
|
1335
1640
|
try {
|
|
1336
1641
|
adopt(await storefront.client.activationRequests.fulfillLicense(requestId));
|
|
@@ -1339,9 +1644,10 @@ function useActivationRequest(token) {
|
|
|
1339
1644
|
setFulfilling(false);
|
|
1340
1645
|
}
|
|
1341
1646
|
}, [storefront, requestId, adopt]);
|
|
1342
|
-
const fulfillTrial =
|
|
1647
|
+
const fulfillTrial = useCallback2(async (newsletterOptIn = false) => {
|
|
1343
1648
|
if (!requestId)
|
|
1344
1649
|
return;
|
|
1650
|
+
setError(null);
|
|
1345
1651
|
setFulfilling(true);
|
|
1346
1652
|
try {
|
|
1347
1653
|
const user = storefront.getState().user;
|
|
@@ -1356,12 +1662,24 @@ function useActivationRequest(token) {
|
|
|
1356
1662
|
setCompleting(true);
|
|
1357
1663
|
} catch (err) {
|
|
1358
1664
|
console.warn("Could not fulfill trial: ", err);
|
|
1359
|
-
setError(err
|
|
1665
|
+
setError(errorMessage(err));
|
|
1360
1666
|
} finally {
|
|
1361
1667
|
setFulfilling(false);
|
|
1362
1668
|
}
|
|
1363
1669
|
}, [storefront, requestId, adopt]);
|
|
1364
|
-
const
|
|
1670
|
+
const autoFulfill = (_b = options.autoFulfill) != null ? _b : false;
|
|
1671
|
+
const autoFulfilled = useRef2(null);
|
|
1672
|
+
const eligible = (activationRequest == null ? void 0 : activationRequest.status) === ActivationRequestStatus.Requested && !!((_c = activationRequest.licenseEligibility) == null ? void 0 : _c.eligible);
|
|
1673
|
+
useEffect3(() => {
|
|
1674
|
+
if (!autoFulfill || !eligible || !requestId || autoFulfilled.current === requestId)
|
|
1675
|
+
return;
|
|
1676
|
+
autoFulfilled.current = requestId;
|
|
1677
|
+
fulfillLicense().catch((err) => {
|
|
1678
|
+
console.warn("Could not activate with the customer's license: ", err);
|
|
1679
|
+
setError(errorMessage(err));
|
|
1680
|
+
});
|
|
1681
|
+
}, [autoFulfill, eligible, requestId, fulfillLicense]);
|
|
1682
|
+
const cancel = useCallback2(async () => {
|
|
1365
1683
|
if (!activationRequest || activationRequest.status === ActivationRequestStatus.Cancelled)
|
|
1366
1684
|
return;
|
|
1367
1685
|
adopt(await storefront.client.activationRequests.cancel(activationRequest.id));
|
|
@@ -1379,23 +1697,31 @@ function useActivationRequest(token) {
|
|
|
1379
1697
|
};
|
|
1380
1698
|
}
|
|
1381
1699
|
|
|
1700
|
+
// src/hooks/useAttributionConsent.ts
|
|
1701
|
+
function useAttributionConsent() {
|
|
1702
|
+
const storefront = useStorefront();
|
|
1703
|
+
const required = useStorefrontState((state) => state.attributionConsentRequired);
|
|
1704
|
+
const answer = useStorefrontState((state) => state.attributionConsentAnswer);
|
|
1705
|
+
return { required, answer, setAnswer: storefront.answerAttributionConsent };
|
|
1706
|
+
}
|
|
1707
|
+
|
|
1382
1708
|
// src/hooks/useAuth.ts
|
|
1383
|
-
import { useMemo as
|
|
1709
|
+
import { useMemo as useMemo3 } from "react";
|
|
1384
1710
|
function useAuth() {
|
|
1385
1711
|
const storefront = useStorefront();
|
|
1386
1712
|
const user = useStorefrontState((state) => state.user);
|
|
1387
1713
|
const loaded = useStorefrontState((state) => state.userLoaded);
|
|
1388
|
-
return
|
|
1714
|
+
return useMemo3(() => ({ ...storefront.auth, user, loaded }), [storefront, user, loaded]);
|
|
1389
1715
|
}
|
|
1390
1716
|
|
|
1391
1717
|
// src/hooks/useCart.ts
|
|
1392
1718
|
import { CartUtils as CartUtils5, resolveDisplayCurrency as resolveDisplayCurrency5 } from "@moonbase.sh/storefront-api";
|
|
1393
|
-
import { useMemo as
|
|
1719
|
+
import { useMemo as useMemo4 } from "react";
|
|
1394
1720
|
function useCart() {
|
|
1395
1721
|
const storefront = useStorefront();
|
|
1396
1722
|
const catalog = useStorefrontState((state) => state.catalog);
|
|
1397
1723
|
const order = useStorefrontState((state) => state.order);
|
|
1398
|
-
return
|
|
1724
|
+
return useMemo4(() => {
|
|
1399
1725
|
const currency = resolveDisplayCurrency5(order, catalog);
|
|
1400
1726
|
const breakdown = CartUtils5.total(catalog, order, currency);
|
|
1401
1727
|
const items = enrichLineItems(catalog, order);
|
|
@@ -1410,19 +1736,25 @@ function useCart() {
|
|
|
1410
1736
|
};
|
|
1411
1737
|
}, [storefront, catalog, order]);
|
|
1412
1738
|
}
|
|
1739
|
+
function useCurrency() {
|
|
1740
|
+
return useStorefrontState((state) => resolveDisplayCurrency5(state.order, state.catalog));
|
|
1741
|
+
}
|
|
1742
|
+
function useCartActions() {
|
|
1743
|
+
return useStorefront().cart;
|
|
1744
|
+
}
|
|
1413
1745
|
|
|
1414
1746
|
// src/hooks/useCheckout.ts
|
|
1415
|
-
import { useCallback as
|
|
1747
|
+
import { useCallback as useCallback3 } from "react";
|
|
1416
1748
|
function useCheckout() {
|
|
1417
1749
|
const storefront = useStorefront();
|
|
1418
|
-
return
|
|
1750
|
+
return useCallback3((items, options) => storefront.checkout(items, options), [storefront]);
|
|
1419
1751
|
}
|
|
1420
1752
|
|
|
1421
1753
|
// src/hooks/useCommunicationPreferences.ts
|
|
1422
|
-
import { useMemo as
|
|
1754
|
+
import { useMemo as useMemo5 } from "react";
|
|
1423
1755
|
function useCommunicationPreferences() {
|
|
1424
1756
|
const storefront = useStorefront();
|
|
1425
|
-
return
|
|
1757
|
+
return useMemo5(() => {
|
|
1426
1758
|
const communications = storefront.client.communications;
|
|
1427
1759
|
return {
|
|
1428
1760
|
subscribe: (input) => communications.subscribe(input, storefront.adSignals()),
|
|
@@ -1435,14 +1767,14 @@ function useCommunicationPreferences() {
|
|
|
1435
1767
|
}
|
|
1436
1768
|
|
|
1437
1769
|
// src/hooks/useGroups.ts
|
|
1438
|
-
import { useMemo as
|
|
1770
|
+
import { useMemo as useMemo6 } from "react";
|
|
1439
1771
|
function useGroups() {
|
|
1440
1772
|
const storefront = useStorefront();
|
|
1441
|
-
return
|
|
1773
|
+
return useMemo6(() => ({ join: (groupId) => storefront.client.groups.join(groupId) }), [storefront]);
|
|
1442
1774
|
}
|
|
1443
1775
|
|
|
1444
1776
|
// src/hooks/useInventory.ts
|
|
1445
|
-
import { useMemo as
|
|
1777
|
+
import { useMemo as useMemo7 } from "react";
|
|
1446
1778
|
|
|
1447
1779
|
// src/inventory.ts
|
|
1448
1780
|
import { mountCheckout as mountCheckout4, mountSubscription as mountSubscription2, resolveRedirect as resolveRedirect3, withPriceFormat } from "@moonbase.sh/storefront-api";
|
|
@@ -1461,7 +1793,7 @@ function createInventory(storefront) {
|
|
|
1461
1793
|
downloadProduct: async (download) => {
|
|
1462
1794
|
if (!download.path)
|
|
1463
1795
|
throw new Error("Product is not downloadable");
|
|
1464
|
-
window.location.href = await inventory.products.getDownloadUrl(download.path, storefront.adSignals());
|
|
1796
|
+
window.location.href = await inventory.products.getDownloadUrl(download.path, storefront.adSignals(), storefront.hasUtmOrReferrer ? storefront.getState().utm : void 0);
|
|
1465
1797
|
},
|
|
1466
1798
|
getSubscriptions: (nextUrl) => inventory.subscriptions.get(nextUrl),
|
|
1467
1799
|
getSubscription: (subscriptionId) => inventory.subscriptions.getById(subscriptionId),
|
|
@@ -1482,13 +1814,13 @@ function createInventory(storefront) {
|
|
|
1482
1814
|
mountCheckout4(url);
|
|
1483
1815
|
return closed;
|
|
1484
1816
|
},
|
|
1485
|
-
updateSubscriptionPaymentMethod: async (
|
|
1486
|
-
const response = await inventory.subscriptions.getById(
|
|
1817
|
+
updateSubscriptionPaymentMethod: async (subscription2, options = {}) => {
|
|
1818
|
+
const response = await inventory.subscriptions.getById(subscription2.id, {
|
|
1487
1819
|
toUpdatePaymentMethod: true,
|
|
1488
1820
|
returnUrl: checkoutReturnUrl(options.returnUrl)
|
|
1489
1821
|
});
|
|
1490
1822
|
window.dispatchEvent(new CustomEvent("moonbase-subscription-payment-method-update-initiated", {
|
|
1491
|
-
detail: { subscription }
|
|
1823
|
+
detail: { subscription: subscription2 }
|
|
1492
1824
|
}));
|
|
1493
1825
|
const url = response.embeddedUpdatePaymentUrl;
|
|
1494
1826
|
if (!url)
|
|
@@ -1506,14 +1838,14 @@ function createInventory(storefront) {
|
|
|
1506
1838
|
// src/hooks/useInventory.ts
|
|
1507
1839
|
function useInventory() {
|
|
1508
1840
|
const storefront = useStorefront();
|
|
1509
|
-
return
|
|
1841
|
+
return useMemo7(() => createInventory(storefront), [storefront]);
|
|
1510
1842
|
}
|
|
1511
1843
|
|
|
1512
1844
|
// src/hooks/useOrders.ts
|
|
1513
|
-
import { useMemo as
|
|
1845
|
+
import { useMemo as useMemo8 } from "react";
|
|
1514
1846
|
function useOrders() {
|
|
1515
1847
|
const storefront = useStorefront();
|
|
1516
|
-
return
|
|
1848
|
+
return useMemo8(() => ({
|
|
1517
1849
|
getOrders: (nextUrl) => storefront.client.orders.getHistory(nextUrl),
|
|
1518
1850
|
getReceipt: (order) => storefront.client.orders.getReceipt(order.id),
|
|
1519
1851
|
downloadReceipt: async (order) => {
|
|
@@ -1530,12 +1862,31 @@ function useOrders() {
|
|
|
1530
1862
|
}), [storefront]);
|
|
1531
1863
|
}
|
|
1532
1864
|
|
|
1865
|
+
// src/hooks/usePrice.ts
|
|
1866
|
+
import { formatMoney } from "@moonbase.sh/storefront-api";
|
|
1867
|
+
import { useMemo as useMemo9 } from "react";
|
|
1868
|
+
function usePrice(variation, format) {
|
|
1869
|
+
const currency = useCurrency();
|
|
1870
|
+
return useMemo9(() => {
|
|
1871
|
+
const amount = variation == null ? void 0 : variation.price[currency];
|
|
1872
|
+
const original = (variation == null ? void 0 : variation.hasDiscount) ? variation.originalPrice[currency] : void 0;
|
|
1873
|
+
const saving = amount !== void 0 && original !== void 0 ? Number((original - amount).toFixed(6)) : void 0;
|
|
1874
|
+
return {
|
|
1875
|
+
currency,
|
|
1876
|
+
price: amount === void 0 ? void 0 : formatMoney(amount, currency, format),
|
|
1877
|
+
originalPrice: original === void 0 ? void 0 : formatMoney(original, currency, format),
|
|
1878
|
+
saving: saving === void 0 || saving <= 0 ? void 0 : formatMoney(saving, currency, format),
|
|
1879
|
+
amount
|
|
1880
|
+
};
|
|
1881
|
+
}, [variation, currency, format]);
|
|
1882
|
+
}
|
|
1883
|
+
|
|
1533
1884
|
// src/hooks/useReviews.ts
|
|
1534
|
-
import { useMemo as
|
|
1885
|
+
import { useMemo as useMemo10 } from "react";
|
|
1535
1886
|
function useReviews() {
|
|
1536
1887
|
const storefront = useStorefront();
|
|
1537
1888
|
const enabled = useStorefrontState((state) => !!state.catalog.reviewsEnabled);
|
|
1538
|
-
return
|
|
1889
|
+
return useMemo10(() => {
|
|
1539
1890
|
const reviews = storefront.client.reviews;
|
|
1540
1891
|
return {
|
|
1541
1892
|
enabled,
|
|
@@ -1550,11 +1901,11 @@ function useReviews() {
|
|
|
1550
1901
|
}
|
|
1551
1902
|
|
|
1552
1903
|
// src/hooks/useVendor.ts
|
|
1553
|
-
import { useEffect as
|
|
1904
|
+
import { useEffect as useEffect4, useState as useState2 } from "react";
|
|
1554
1905
|
function useVendor() {
|
|
1555
1906
|
const storefront = useStorefront();
|
|
1556
1907
|
const [state, setState] = useState2({ vendor: null, loading: true });
|
|
1557
|
-
|
|
1908
|
+
useEffect4(() => {
|
|
1558
1909
|
let current = true;
|
|
1559
1910
|
storefront.client.vendor.get().then((vendor) => current && setState({ vendor, loading: false })).catch(() => current && setState({ vendor: null, loading: false }));
|
|
1560
1911
|
return () => {
|
|
@@ -1566,10 +1917,10 @@ function useVendor() {
|
|
|
1566
1917
|
|
|
1567
1918
|
// src/hooks/useVoucher.ts
|
|
1568
1919
|
import { NotFoundError as NotFoundError2 } from "@moonbase.sh/storefront-api";
|
|
1569
|
-
import { useMemo as
|
|
1920
|
+
import { useMemo as useMemo11 } from "react";
|
|
1570
1921
|
function useVoucher() {
|
|
1571
1922
|
const storefront = useStorefront();
|
|
1572
|
-
return
|
|
1923
|
+
return useMemo11(() => {
|
|
1573
1924
|
const known = async (lookup) => {
|
|
1574
1925
|
try {
|
|
1575
1926
|
return await lookup;
|
|
@@ -1599,33 +1950,56 @@ function createStorefront(endpoint, options = {}) {
|
|
|
1599
1950
|
};
|
|
1600
1951
|
return new MoonbaseStorefront(configuration, new MoonbaseClient(configuration), { initialCatalog, autoApplyItemOffers });
|
|
1601
1952
|
}
|
|
1953
|
+
var browserStorefronts = /* @__PURE__ */ new Map();
|
|
1954
|
+
function getStorefront(endpoint, options = {}) {
|
|
1955
|
+
if (typeof window === "undefined")
|
|
1956
|
+
return createStorefront(endpoint, options);
|
|
1957
|
+
let storefront = browserStorefronts.get(endpoint);
|
|
1958
|
+
if (!storefront || storefront.disposed) {
|
|
1959
|
+
storefront = createStorefront(endpoint, options);
|
|
1960
|
+
browserStorefronts.set(endpoint, storefront);
|
|
1961
|
+
}
|
|
1962
|
+
return storefront;
|
|
1963
|
+
}
|
|
1602
1964
|
export {
|
|
1603
1965
|
MoonbaseStorefront,
|
|
1604
1966
|
StorefrontProvider,
|
|
1605
1967
|
createStorefront,
|
|
1968
|
+
getStorefront,
|
|
1606
1969
|
useActivationRequest,
|
|
1970
|
+
useAttributionConsent,
|
|
1607
1971
|
useAuth,
|
|
1608
1972
|
useBundle,
|
|
1609
1973
|
useBundles,
|
|
1610
1974
|
useCart,
|
|
1975
|
+
useCartActions,
|
|
1611
1976
|
useCartOffer,
|
|
1612
1977
|
useCatalog,
|
|
1613
1978
|
useCheckout,
|
|
1614
1979
|
useCommunicationPreferences,
|
|
1980
|
+
useCurrency,
|
|
1615
1981
|
useEligibleOffers,
|
|
1616
1982
|
useGroups,
|
|
1617
1983
|
useInventory,
|
|
1618
1984
|
useLockedOffers,
|
|
1619
1985
|
useOffer,
|
|
1620
1986
|
useOffers,
|
|
1987
|
+
useOrderHistory,
|
|
1621
1988
|
useOrders,
|
|
1989
|
+
useOwnedProduct,
|
|
1990
|
+
useOwnedProducts,
|
|
1991
|
+
usePrice,
|
|
1622
1992
|
useProduct,
|
|
1993
|
+
useProductActivations,
|
|
1994
|
+
useProductLicenses,
|
|
1623
1995
|
useProducts,
|
|
1624
1996
|
usePromotions,
|
|
1625
1997
|
useReviews,
|
|
1626
1998
|
useStorefront,
|
|
1627
1999
|
useStorefrontEvent,
|
|
1628
2000
|
useStorefrontState,
|
|
2001
|
+
useSubscription,
|
|
2002
|
+
useSubscriptions,
|
|
1629
2003
|
useVendor,
|
|
1630
2004
|
useVoucher
|
|
1631
2005
|
};
|