@monetize.software/sdk-react 3.0.0-alpha.2 → 3.0.0-alpha.20

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/dist/index.js CHANGED
@@ -1,237 +1,319 @@
1
1
  "use client";
2
- import { jsx as i, Fragment as P } from "react/jsx-runtime";
3
- import { createContext as A, useState as p, useEffect as u, useContext as C, useCallback as f, useSyncExternalStore as T, useRef as _, forwardRef as B } from "react";
4
- import { PaywallUI as O } from "@monetize.software/sdk";
5
- const S = A(null);
6
- S.displayName = "PaywallContext";
7
- const k = A(!1);
8
- k.displayName = "PaywallProviderMarker";
9
- function j(e) {
10
- const n = "instance" in e ? e.instance : void 0, t = "options" in e ? e.options : void 0, [r, l] = p(
11
- n ?? null
2
+ import { jsx as d, Fragment as P } from "react/jsx-runtime";
3
+ import { createContext as B, useState as g, useEffect as p, useContext as T, useCallback as y, useSyncExternalStore as C, useRef as m, forwardRef as R } from "react";
4
+ import { PaywallUI as N } from "@monetize.software/sdk";
5
+ const x = B(null);
6
+ x.displayName = "PaywallContext";
7
+ const O = B(!1);
8
+ O.displayName = "PaywallProviderMarker";
9
+ function q(n) {
10
+ const e = "instance" in n ? n.instance : void 0, t = "options" in n ? n.options : void 0, [r, s] = g(
11
+ e ?? null
12
12
  );
13
- return u(() => {
14
- if (n) {
15
- l(n);
13
+ return p(() => {
14
+ if (e) {
15
+ s(e);
16
16
  return;
17
17
  }
18
18
  if (!t) return;
19
- const a = new O(t);
20
- return l(a), () => {
21
- a.destroy(), l(null);
19
+ const l = new N(t);
20
+ return s(l), () => {
21
+ l.destroy(), s(null);
22
22
  };
23
- }, [n]), /* @__PURE__ */ i(k.Provider, { value: !0, children: /* @__PURE__ */ i(S.Provider, { value: r, children: e.children }) });
23
+ }, [e]), /* @__PURE__ */ d(O.Provider, { value: !0, children: /* @__PURE__ */ d(x.Provider, { value: r, children: n.children }) });
24
24
  }
25
- function o() {
26
- const e = C(k), n = C(S);
27
- if (!e)
25
+ function c() {
26
+ const n = T(O), e = T(x);
27
+ if (!n)
28
28
  throw new Error(
29
29
  "[sdk-react] usePaywall() called outside <PaywallProvider>. Wrap your tree with <PaywallProvider options={...}> or pass an externally-created instance via <PaywallProvider instance={paywall}>."
30
30
  );
31
- return n;
31
+ return e;
32
32
  }
33
- const m = { open: !1, view: null, error: null };
34
- function D() {
35
- const e = o(), n = f(
36
- (r) => e ? e.onStateChange(r, { immediate: "none" }) : () => {
33
+ const _ = {
34
+ open: !1,
35
+ view: null,
36
+ error: null,
37
+ processing: !1
38
+ };
39
+ function G() {
40
+ const n = c(), e = y(
41
+ (r) => n ? n.onStateChange(r, { immediate: "none" }) : () => {
37
42
  },
38
- [e]
39
- ), t = f(() => e ? e.getState() : m, [e]);
40
- return T(n, t, () => m);
43
+ [n]
44
+ ), t = y(() => n ? n.getState() : _, [n]);
45
+ return C(e, t, () => _);
41
46
  }
42
- function F() {
43
- const e = o(), n = f(
44
- (r) => e ? e.on("userChange", () => r()) : () => {
47
+ const S = { status: "loading", user: null, session: null }, b = { status: "guest", user: null, session: null };
48
+ function z() {
49
+ const n = c(), e = m(S), t = y(
50
+ (s) => {
51
+ if (!n) return () => {
52
+ };
53
+ const l = n.on("userChange", () => s()), a = n.auth ? n.on("authChange", () => s()) : null;
54
+ return () => {
55
+ l(), a?.();
56
+ };
45
57
  },
46
- [e]
47
- ), t = f(() => e ? e.billing.getCachedUser() : null, [e]);
48
- return T(n, t, R);
58
+ [n]
59
+ ), r = y(() => {
60
+ if (!n)
61
+ return e.current = S, S;
62
+ const s = n.billing.getCachedUser();
63
+ if (n.auth) {
64
+ const l = n.auth.getCachedSession();
65
+ if (!l)
66
+ return e.current = b, b;
67
+ const a = e.current;
68
+ if (a.status === "signed_in" && a.user === s && a.session === l)
69
+ return a;
70
+ const o = { status: "signed_in", user: s, session: l };
71
+ return e.current = o, o;
72
+ }
73
+ if (s) {
74
+ const l = e.current;
75
+ if (l.status === "signed_in" && l.user === s && l.session === null)
76
+ return l;
77
+ const a = {
78
+ status: "signed_in",
79
+ user: s,
80
+ session: null
81
+ };
82
+ return e.current = a, a;
83
+ }
84
+ return e.current = b, b;
85
+ }, [n]);
86
+ return C(t, r, F);
49
87
  }
50
- function R() {
51
- return null;
88
+ function F() {
89
+ return S;
52
90
  }
53
- function H(e, n) {
54
- const t = o(), r = _(n);
55
- r.current = n, u(() => {
91
+ function J(n, e) {
92
+ const t = c(), r = m(e);
93
+ r.current = e, p(() => {
56
94
  if (t)
57
- return t.on(e, (l) => {
58
- r.current(l);
95
+ return t.on(n, (s) => {
96
+ r.current(s);
59
97
  });
60
- }, [t, e]);
98
+ }, [t, n]);
61
99
  }
62
- const x = { status: "loading", result: null };
63
- function N(e = {}) {
64
- const n = o(), [t, r] = p(x), l = e.skipTrial === !0, a = e.skipVisibility === !0;
65
- return u(() => {
66
- if (!n) {
67
- r(x);
100
+ const E = { status: "loading", result: null };
101
+ function D(n = {}) {
102
+ const e = c(), [t, r] = g(E), s = n.skipTrial === !0, l = n.skipVisibility === !0;
103
+ return p(() => {
104
+ if (!e) {
105
+ r(E);
68
106
  return;
69
107
  }
70
- const c = new AbortController();
71
- let d = !1;
72
- const s = () => {
73
- n.getAccess({ skipTrial: l, skipVisibility: a, signal: c.signal }).then((g) => {
74
- d || c.signal.aborted || r({ status: "ready", result: g });
108
+ const a = new AbortController();
109
+ let o = !1;
110
+ const u = () => {
111
+ e.getAccess({ skipTrial: s, skipVisibility: l, signal: a.signal }).then((h) => {
112
+ o || a.signal.aborted || r({ status: "ready", result: h });
75
113
  }).catch(() => {
76
114
  });
77
115
  };
78
- s();
79
- const y = n.on("userChange", s), b = n.on("purchase_completed", s);
116
+ u();
117
+ const i = e.on("userChange", u), f = e.on("purchase_completed", u);
80
118
  return () => {
81
- d = !0, c.abort(), y(), b();
119
+ o = !0, a.abort(), i(), f();
82
120
  };
83
- }, [n, l, a]), t;
121
+ }, [e, s, l]), t;
84
122
  }
85
- function L() {
86
- const e = o(), [n, t] = p(() => ({
87
- prices: e?.getCachedPrices() ?? null,
123
+ function K() {
124
+ const n = c(), [e, t] = g(() => ({
125
+ prices: n?.getCachedPrices() ?? null,
88
126
  loading: !0,
89
127
  error: null
90
128
  }));
91
- return u(() => {
92
- if (!e) {
129
+ return p(() => {
130
+ if (!n) {
93
131
  t({ prices: null, loading: !0, error: null });
94
132
  return;
95
133
  }
96
- const r = e.getCachedPrices();
134
+ const r = n.getCachedPrices();
97
135
  t({ prices: r, loading: r === null, error: null });
98
- const l = new AbortController();
99
- let a = !1;
136
+ const s = new AbortController();
137
+ let l = !1;
100
138
  (() => {
101
- e.getPrices({ signal: l.signal }).then((s) => {
102
- a || t({ prices: s, loading: !1, error: null });
103
- }).catch((s) => {
104
- a || l.signal.aborted || t((y) => ({
105
- prices: y.prices,
139
+ n.getPrices({ signal: s.signal }).then((u) => {
140
+ l || t({ prices: u, loading: !1, error: null });
141
+ }).catch((u) => {
142
+ l || s.signal.aborted || t((i) => ({
143
+ prices: i.prices,
106
144
  loading: !1,
107
- error: s instanceof Error ? s : new Error(String(s))
145
+ error: u instanceof Error ? u : new Error(String(u))
108
146
  }));
109
147
  });
110
148
  })();
111
- const d = e.on("ready", () => {
112
- const s = e.getCachedPrices();
113
- s && t({ prices: s, loading: !1, error: null });
149
+ const o = n.on("ready", () => {
150
+ const u = n.getCachedPrices();
151
+ u && t({ prices: u, loading: !1, error: null });
114
152
  });
115
153
  return () => {
116
- a = !0, l.abort(), d();
154
+ l = !0, s.abort(), o();
117
155
  };
118
- }, [e]), n;
156
+ }, [n]), e;
119
157
  }
120
- function W() {
121
- const e = o(), [n, t] = p(
122
- () => e?.getTrialStatus() ?? null
123
- ), r = f(() => {
158
+ function Q(n) {
159
+ const e = c(), [t, r] = g(
160
+ () => e ? e.getOfferForPrice(n) : null
161
+ ), s = m(n);
162
+ return s.current = n, p(() => {
124
163
  if (!e) {
164
+ r(null);
165
+ return;
166
+ }
167
+ let l = !1;
168
+ const a = () => {
169
+ if (l) return;
170
+ const f = e.getOfferForPrice(s.current);
171
+ return r(f), f;
172
+ }, o = a(), u = e.on("ready", () => a());
173
+ let i = null;
174
+ return o && o.remainingMs !== null && (i = setInterval(() => {
175
+ const f = a();
176
+ (!f || f.remainingMs === null || f.remainingMs <= 0) && (i && clearInterval(i), i = null);
177
+ }, 1e3)), () => {
178
+ l = !0, u(), i && clearInterval(i);
179
+ };
180
+ }, [e, n]), t;
181
+ }
182
+ function X() {
183
+ const n = c(), e = y(
184
+ (r) => n ? n.on("ready", () => r()) : () => {
185
+ },
186
+ [n]
187
+ ), t = y(() => n ? n.getCachedOffers() : null, [n]);
188
+ return C(e, t, L);
189
+ }
190
+ function L() {
191
+ return null;
192
+ }
193
+ function Y() {
194
+ const n = c(), [e, t] = g(
195
+ () => n?.getTrialStatus() ?? null
196
+ ), r = y(() => {
197
+ if (!n) {
125
198
  t(null);
126
199
  return;
127
200
  }
128
- t(e.getTrialStatus());
129
- }, [e]);
130
- return u(() => {
131
- if (!e) {
201
+ t(n.getTrialStatus());
202
+ }, [n]);
203
+ return p(() => {
204
+ if (!n) {
132
205
  t(null);
133
206
  return;
134
207
  }
135
208
  r();
136
- const l = e.on("trial_blocked", r), a = e.on("trial_expired", r);
209
+ const s = n.on("trial_blocked", r), l = n.on("trial_expired", r);
137
210
  return () => {
138
- l(), a();
211
+ s(), l();
139
212
  };
140
- }, [e, r]), n;
213
+ }, [n, r]), e;
141
214
  }
142
- function q() {
143
- const e = o(), [n, t] = p(
144
- () => e?.getVisibility() ?? null
145
- ), r = f(() => {
146
- if (!e) {
215
+ function Z() {
216
+ const n = c(), [e, t] = g(
217
+ () => n?.getVisibility() ?? null
218
+ ), r = y(() => {
219
+ if (!n) {
147
220
  t(null);
148
221
  return;
149
222
  }
150
- t(e.getVisibility());
151
- }, [e]);
152
- return u(() => {
153
- if (!e) {
223
+ t(n.getVisibility());
224
+ }, [n]);
225
+ return p(() => {
226
+ if (!n) {
154
227
  t(null);
155
228
  return;
156
229
  }
157
230
  r();
158
- const l = e.on("ready", r), a = e.on("visibility_blocked", r);
231
+ const s = n.on("ready", r), l = n.on("visibility_blocked", r);
159
232
  return () => {
160
- l(), a();
233
+ s(), l();
161
234
  };
162
- }, [e, r]), n;
235
+ }, [n, r]), e;
163
236
  }
164
- function z(e) {
165
- const n = o(), t = N(), r = t.status === "ready" && t.result.access === "blocked", l = e.openOnBlocked === !0 && r;
166
- if (u(() => {
167
- l && n && n.open();
168
- }, [l, n]), t.status === "loading")
169
- return /* @__PURE__ */ i(P, { children: e.loading ?? null });
237
+ function nn(n) {
238
+ const e = c(), t = D(), r = t.status === "ready" && t.result.access === "blocked", s = n.openOnBlocked === !0 && r;
239
+ if (p(() => {
240
+ s && e && e.open();
241
+ }, [s, e]), t.status === "loading")
242
+ return /* @__PURE__ */ d(P, { children: n.loading ?? null });
170
243
  if (t.result.access === "granted")
171
- return /* @__PURE__ */ i(P, { children: e.children });
172
- const a = e.fallback;
173
- return typeof a == "function" ? /* @__PURE__ */ i(P, { children: a({
244
+ return /* @__PURE__ */ d(P, { children: n.children });
245
+ const l = n.fallback;
246
+ return typeof l == "function" ? /* @__PURE__ */ d(P, { children: l({
174
247
  result: t.result,
175
- open: () => n?.open()
176
- }) }) : /* @__PURE__ */ i(P, { children: a ?? null });
248
+ open: () => e?.open()
249
+ }) }) : /* @__PURE__ */ d(P, { children: l ?? null });
177
250
  }
178
- const U = B(
179
- function(n, t) {
180
- const r = o(), {
251
+ const j = R(
252
+ function(e, t) {
253
+ const r = c(), s = G(), {
181
254
  mode: l = "paywall",
182
- identity: a,
183
- renew: c,
184
- skipTrial: d,
185
- skipVisibility: s,
186
- render: y,
187
- onClick: b,
188
- disabled: g,
189
- ...E
190
- } = n, h = r !== null, w = { identity: a, renew: c, skipTrial: d, skipVisibility: s }, v = () => {
191
- if (r)
255
+ priceId: a,
256
+ identity: o,
257
+ renew: u,
258
+ skipTrial: i,
259
+ skipVisibility: f,
260
+ render: h,
261
+ onClick: V,
262
+ disabled: I,
263
+ ...U
264
+ } = e, v = r !== null, k = !!a && s.processing, w = { identity: o, renew: u, skipTrial: i, skipVisibility: f }, A = () => {
265
+ if (r) {
266
+ if (a) {
267
+ r.checkout(a, w);
268
+ return;
269
+ }
192
270
  switch (l) {
193
271
  case "support":
194
272
  r.openSupport(w);
195
273
  return;
196
274
  case "auth":
197
- r.openAuth(w);
275
+ case "signin":
276
+ r.openSignin(w);
198
277
  return;
199
- case "anon":
200
- r.openAnonGate(w);
278
+ case "signup":
279
+ r.openSignup(w);
201
280
  return;
202
281
  default:
203
282
  r.open(w);
204
283
  }
284
+ }
205
285
  };
206
- return y ? y({ open: v, ready: h }) : /* @__PURE__ */ i(
286
+ return h ? h({ open: A, ready: v, processing: k }) : /* @__PURE__ */ d(
207
287
  "button",
208
288
  {
209
289
  ref: t,
210
290
  type: "button",
211
- disabled: g || !h,
212
- "aria-busy": h ? void 0 : !0,
213
- onClick: (V) => {
214
- v(), b?.(V);
291
+ disabled: I || !v || k,
292
+ "aria-busy": !v || k ? !0 : void 0,
293
+ onClick: (M) => {
294
+ A(), V?.(M);
215
295
  },
216
- ...E
296
+ ...U
217
297
  }
218
298
  );
219
299
  }
220
- ), J = B(function(n, t) {
221
- return /* @__PURE__ */ i(U, { ...n, mode: "support", ref: t });
300
+ ), en = R(function(e, t) {
301
+ return /* @__PURE__ */ d(j, { ...e, mode: "support", ref: t });
222
302
  });
223
303
  export {
224
- U as PaywallButton,
225
- z as PaywallGate,
226
- j as PaywallProvider,
227
- J as PaywallSupportButton,
228
- o as usePaywall,
229
- N as usePaywallAccess,
230
- H as usePaywallEvent,
231
- L as usePaywallPrices,
232
- D as usePaywallState,
233
- W as usePaywallTrial,
234
- F as usePaywallUser,
235
- q as usePaywallVisibility
304
+ j as PaywallButton,
305
+ nn as PaywallGate,
306
+ q as PaywallProvider,
307
+ en as PaywallSupportButton,
308
+ c as usePaywall,
309
+ D as usePaywallAccess,
310
+ J as usePaywallEvent,
311
+ Q as usePaywallOffer,
312
+ X as usePaywallOffers,
313
+ K as usePaywallPrices,
314
+ G as usePaywallState,
315
+ Y as usePaywallTrial,
316
+ z as usePaywallUser,
317
+ Z as usePaywallVisibility
236
318
  };
237
319
  //# sourceMappingURL=index.js.map