@monetize.software/sdk-react 3.0.0-alpha.1 → 3.0.0-alpha.11

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