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