@monetize.software/sdk-react 3.0.0-alpha.7 → 3.0.0-alpha.9

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