@rift-finance/react 0.1.2 → 0.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.
@@ -1,31 +1,31 @@
1
- import { jsx as T, jsxs as te } from "react/jsx-runtime";
2
- import { createContext as re, useMemo as O, useState as p, useRef as z, useCallback as y, useEffect as w, useContext as ne } from "react";
3
- let c = null, I = !1, E = [], x = null;
4
- const d = /* @__PURE__ */ new Map();
5
- function G() {
1
+ import { jsx as P, jsxs as te } from "react/jsx-runtime";
2
+ import { createContext as re, useMemo as j, useState as w, useRef as G, useCallback as E, useEffect as y, useContext as ne } from "react";
3
+ let d = null, C = !1, M = [], T = null;
4
+ const u = /* @__PURE__ */ new Map();
5
+ function X() {
6
6
  return `${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 9)}`;
7
7
  }
8
- function J(e) {
8
+ function Q(e) {
9
9
  if (typeof document > "u")
10
10
  return Promise.reject(new Error("Cannot mount refresh iframe outside the browser"));
11
- if (c && I) return Promise.resolve();
12
- if (x = new URL(e.widgetUrl).origin, !c) {
13
- c = document.createElement("iframe"), c.setAttribute("aria-hidden", "true"), c.setAttribute("tabindex", "-1"), c.title = "Rift session refresh", c.style.cssText = "position:absolute;width:1px;height:1px;border:0;opacity:0;pointer-events:none;left:-9999px;top:-9999px;";
14
- const s = new URLSearchParams({
11
+ if (d && C) return Promise.resolve();
12
+ if (T = new URL(e.widgetUrl).origin, !d) {
13
+ d = document.createElement("iframe"), d.setAttribute("aria-hidden", "true"), d.setAttribute("tabindex", "-1"), d.title = "Rift session refresh", d.style.cssText = "position:absolute;width:1px;height:1px;border:0;opacity:0;pointer-events:none;left:-9999px;top:-9999px;";
14
+ const i = new URLSearchParams({
15
15
  key: e.apiKey,
16
16
  headless: "1"
17
17
  });
18
- c.src = `${e.widgetUrl.replace(/\/$/, "")}/?${s.toString()}`, document.body.appendChild(c), window.addEventListener("message", (i) => {
19
- if (i.origin !== x) return;
20
- const t = i.data;
18
+ d.src = `${e.widgetUrl.replace(/\/$/, "")}/?${i.toString()}`, document.body.appendChild(d), window.addEventListener("message", (o) => {
19
+ if (o.origin !== T) return;
20
+ const t = o.data;
21
21
  if (!(!t || typeof t != "object" || typeof t.type != "string") && t.type.startsWith("rift:")) {
22
22
  if (t.type === "rift:ready") {
23
- I = !0, E.forEach((n) => n()), E = [];
23
+ C = !0, M.forEach((n) => n()), M = [];
24
24
  return;
25
25
  }
26
26
  if (t.type === "rift:refresh-result") {
27
- const n = d.get(t.requestId);
28
- n && (d.delete(t.requestId), n.resolve({
27
+ const n = u.get(t.requestId);
28
+ n && (u.delete(t.requestId), n.resolve({
29
29
  accessToken: t.accessToken,
30
30
  expiresAt: t.expiresAt,
31
31
  expiresIn: t.expiresIn
@@ -33,62 +33,62 @@ function J(e) {
33
33
  return;
34
34
  }
35
35
  if (t.type === "rift:refresh-error") {
36
- const n = d.get(t.requestId);
37
- n && (d.delete(t.requestId), n.reject(new Error(t.message || "Refresh failed")));
36
+ const n = u.get(t.requestId);
37
+ n && (u.delete(t.requestId), n.reject(new Error(t.message || "Refresh failed")));
38
38
  return;
39
39
  }
40
40
  if (t.type === "rift:logout-result") {
41
- const n = d.get(t.requestId);
42
- n && (d.delete(t.requestId), n.resolve({ accessToken: "", expiresAt: "", expiresIn: 0 }));
41
+ const n = u.get(t.requestId);
42
+ n && (u.delete(t.requestId), n.resolve({ accessToken: "", expiresAt: "", expiresIn: 0 }));
43
43
  }
44
44
  }
45
45
  });
46
46
  }
47
- return I ? Promise.resolve() : new Promise((s) => {
48
- E.push(s), setTimeout(() => {
49
- if (!I) {
50
- const i = E.shift();
51
- i && i();
47
+ return C ? Promise.resolve() : new Promise((i) => {
48
+ M.push(i), setTimeout(() => {
49
+ if (!C) {
50
+ const o = M.shift();
51
+ o && o();
52
52
  }
53
53
  }, 8e3);
54
54
  });
55
55
  }
56
- async function Y(e) {
57
- if (await J(e), !c?.contentWindow || !x)
56
+ async function J(e) {
57
+ if (await Q(e), !d?.contentWindow || !T)
58
58
  throw new Error("Refresh iframe is not available");
59
- const s = G();
60
- return new Promise((i, t) => {
61
- d.set(s, { resolve: i, reject: t }), c.contentWindow.postMessage(
62
- { type: "rift:refresh-request", requestId: s },
63
- x
59
+ const i = X();
60
+ return new Promise((o, t) => {
61
+ u.set(i, { resolve: o, reject: t }), d.contentWindow.postMessage(
62
+ { type: "rift:refresh-request", requestId: i },
63
+ T
64
64
  ), setTimeout(() => {
65
- const n = d.get(s);
66
- n && (d.delete(s), n.reject(new Error("Refresh timed out")));
65
+ const n = u.get(i);
66
+ n && (u.delete(i), n.reject(new Error("Refresh timed out")));
67
67
  }, 1e4);
68
68
  });
69
69
  }
70
70
  async function se(e) {
71
71
  try {
72
- if (await J(e), !c?.contentWindow || !x) return;
73
- const s = G();
74
- await new Promise((i) => {
75
- d.set(s, {
76
- resolve: () => i(),
77
- reject: () => i()
72
+ if (await Q(e), !d?.contentWindow || !T) return;
73
+ const i = X();
74
+ await new Promise((o) => {
75
+ u.set(i, {
76
+ resolve: () => o(),
77
+ reject: () => o()
78
78
  // logout is idempotent — never reject
79
- }), c.contentWindow.postMessage(
80
- { type: "rift:logout-request", requestId: s },
81
- x
79
+ }), d.contentWindow.postMessage(
80
+ { type: "rift:logout-request", requestId: i },
81
+ T
82
82
  ), setTimeout(() => {
83
- d.delete(s), i();
83
+ u.delete(i), o();
84
84
  }, 5e3);
85
85
  });
86
86
  } catch {
87
87
  }
88
88
  }
89
- const ie = "https://widget.riftfi.xyz", P = "rift:identity", oe = 60, B = re(null);
90
- function Q() {
91
- const e = ne(B);
89
+ const ie = "https://widget.riftfi.xyz", $ = "rift:identity", oe = 60, V = re(null);
90
+ function Z() {
91
+ const e = ne(V);
92
92
  if (!e)
93
93
  throw new Error(
94
94
  "[@rift/react] useRift() / <RiftAuth> must be used inside <RiftProvider>"
@@ -98,7 +98,7 @@ function Q() {
98
98
  function ae() {
99
99
  if (typeof window > "u") return null;
100
100
  try {
101
- const e = localStorage.getItem(P);
101
+ const e = localStorage.getItem($);
102
102
  return e ? JSON.parse(e) : null;
103
103
  } catch {
104
104
  return null;
@@ -107,28 +107,28 @@ function ae() {
107
107
  function ce(e) {
108
108
  if (!(typeof window > "u"))
109
109
  try {
110
- e ? localStorage.setItem(P, JSON.stringify(e)) : localStorage.removeItem(P);
110
+ e ? localStorage.setItem($, JSON.stringify(e)) : localStorage.removeItem($);
111
111
  } catch {
112
112
  }
113
113
  }
114
- function fe({
114
+ function pe({
115
115
  apiKey: e,
116
- widgetUrl: s,
117
- children: i,
116
+ widgetUrl: i,
117
+ children: o,
118
118
  autoOpen: t = !1,
119
119
  persist: n = !0
120
120
  }) {
121
- const o = s || ie, h = O(() => {
121
+ const a = i || ie, p = j(() => {
122
122
  try {
123
- return new URL(o).origin;
123
+ return new URL(a).origin;
124
124
  } catch {
125
- return o;
125
+ return a;
126
126
  }
127
- }, [o]), [l, v] = p(null), [g, b] = p(!1), [k, V] = p("signin"), [_, S] = p(!1), [W, q] = p(null), [j, X] = p(540), [C, Z] = p(0), L = z(null);
128
- L.current = l;
129
- const R = z(null), f = y(
127
+ }, [a]), [b, W] = w(null), [f, k] = w(!1), [A, q] = w("signin"), [x, I] = w(!1), [v, L] = w(null), [U, _] = w(540), [S, O] = w(0), R = G(null);
128
+ R.current = b;
129
+ const m = G(null), l = E(
130
130
  (r) => {
131
- v(r), n && ce(
131
+ W(r), n && ce(
132
132
  r ? {
133
133
  user: r.user,
134
134
  address: r.address,
@@ -138,191 +138,211 @@ function fe({
138
138
  },
139
139
  [n]
140
140
  );
141
- w(() => {
141
+ y(() => {
142
142
  if (!n) return;
143
143
  const r = ae();
144
144
  if (!r) return;
145
- let u = !0;
145
+ let c = !0;
146
146
  return (async () => {
147
147
  try {
148
- const a = await Y({
148
+ const s = await J({
149
149
  apiKey: e,
150
- widgetUrl: o
150
+ widgetUrl: a
151
151
  });
152
- if (!u) return;
153
- f({
152
+ if (!c) return;
153
+ l({
154
154
  user: r.user,
155
155
  address: r.address,
156
156
  btcAddress: r.btcAddress,
157
- accessToken: a.accessToken,
158
- expiresAt: a.expiresAt
157
+ accessToken: s.accessToken,
158
+ expiresAt: s.expiresAt
159
159
  });
160
160
  } catch {
161
- if (!u) return;
162
- f(null);
161
+ if (!c) return;
162
+ l(null);
163
163
  }
164
164
  })(), () => {
165
- u = !1;
165
+ c = !1;
166
166
  };
167
167
  }, []);
168
- const U = y((r) => {
169
- V(r?.mode || "signin"), q(null), S(!1), Z((u) => u + 1), b(!0);
170
- }, []), A = y(() => {
171
- b(!1), S(!1);
172
- }, []), M = y(async () => {
173
- await se({ apiKey: e, widgetUrl: o }), f(null);
174
- }, [e, o, f]), $ = y(async () => {
175
- const r = L.current;
168
+ const F = E((r) => {
169
+ q(r?.mode || "signin"), L(null), I(!1), O((c) => c + 1), k(!0);
170
+ }, []), D = E(() => {
171
+ k(!1), I(!1);
172
+ }, []), H = E(async () => {
173
+ await se({ apiKey: e, widgetUrl: a }), l(null);
174
+ }, [e, a, l]), B = E(async () => {
175
+ const r = R.current;
176
176
  if (!r) throw new Error("Not signed in");
177
- const u = r.expiresAt ? new Date(r.expiresAt).getTime() : null, a = Date.now();
178
- return !(!u || u - a < oe * 1e3) && r.accessToken ? r.accessToken : (R.current || (R.current = (async () => {
177
+ const c = r.expiresAt ? new Date(r.expiresAt).getTime() : null, s = Date.now();
178
+ return !(!c || c - s < oe * 1e3) && r.accessToken ? r.accessToken : (m.current || (m.current = (async () => {
179
179
  try {
180
- const m = await Y({
180
+ const h = await J({
181
181
  apiKey: e,
182
- widgetUrl: o
183
- }), N = L.current;
184
- if (!N) throw new Error("Signed out during refresh");
182
+ widgetUrl: a
183
+ }), Y = R.current;
184
+ if (!Y) throw new Error("Signed out during refresh");
185
185
  const ee = {
186
- ...N,
187
- accessToken: m.accessToken,
188
- expiresAt: m.expiresAt
186
+ ...Y,
187
+ accessToken: h.accessToken,
188
+ expiresAt: h.expiresAt
189
189
  };
190
- return f(ee), m.accessToken;
191
- } catch (m) {
192
- throw f(null), m instanceof Error ? m : new Error(String(m));
190
+ return l(ee), h.accessToken;
191
+ } catch (h) {
192
+ throw l(null), h instanceof Error ? h : new Error(String(h));
193
193
  } finally {
194
- R.current = null;
194
+ m.current = null;
195
195
  }
196
- })()), R.current);
197
- }, [e, o, f]);
198
- w(() => {
196
+ })()), m.current);
197
+ }, [e, a, l]);
198
+ y(() => {
199
199
  if (typeof window > "u") return;
200
- const r = (u) => {
201
- if (u.origin !== h) return;
202
- const a = u.data;
203
- if (!(!a || typeof a != "object" || typeof a.type != "string") && a.type.startsWith("rift:"))
204
- switch (a.type) {
200
+ const r = (c) => {
201
+ if (c.origin !== p) return;
202
+ const s = c.data;
203
+ if (!(!s || typeof s != "object" || typeof s.type != "string") && s.type.startsWith("rift:"))
204
+ switch (s.type) {
205
205
  case "rift:ready":
206
- g && S(!0);
206
+ f && I(!0);
207
207
  break;
208
208
  case "rift:close":
209
- A();
209
+ D();
210
210
  break;
211
211
  case "rift:resize":
212
- X(Math.max(360, Math.min(820, a.height + 8)));
212
+ _(Math.max(360, Math.min(820, s.height + 8)));
213
213
  break;
214
214
  case "rift:signin-success": {
215
- const H = {
216
- user: a.user,
217
- address: a.address,
218
- btcAddress: a.btcAddress,
219
- accessToken: a.accessToken,
220
- expiresAt: a.expiresAt
215
+ const g = {
216
+ user: s.user,
217
+ address: s.address,
218
+ btcAddress: s.btcAddress,
219
+ accessToken: s.accessToken,
220
+ expiresAt: s.expiresAt
221
221
  };
222
- f(H), b(!1);
222
+ l(g), k(!1);
223
223
  break;
224
224
  }
225
225
  case "rift:signin-error":
226
- q(a.message);
226
+ L(s.message);
227
227
  break;
228
228
  }
229
229
  };
230
230
  return window.addEventListener("message", r), () => window.removeEventListener("message", r);
231
- }, [h, A, f, g]), w(() => {
232
- if (!(typeof document > "u") && g) {
231
+ }, [p, D, l, f]), y(() => {
232
+ if (!(typeof document > "u") && f) {
233
233
  const r = document.documentElement.style.overflow;
234
234
  return document.documentElement.style.overflow = "hidden", () => {
235
235
  document.documentElement.style.overflow = r;
236
236
  };
237
237
  }
238
- }, [g]), w(() => {
239
- t && !l && U();
238
+ }, [f]), y(() => {
239
+ t && !b && F();
240
240
  }, []);
241
- const D = O(() => {
241
+ const N = j(() => {
242
242
  const r = new URLSearchParams({
243
243
  key: e,
244
- mode: k,
244
+ mode: A,
245
245
  origin: typeof window < "u" ? window.location.origin : "",
246
- t: String(C)
246
+ t: String(S)
247
247
  });
248
- return `${o.replace(/\/$/, "")}/?${r.toString()}`;
249
- }, [e, k, C, o]), F = y(() => {
250
- }, []), K = O(
248
+ if (typeof document < "u") {
249
+ const c = document.documentElement, s = c.getAttribute("data-theme");
250
+ let g = null;
251
+ s === "dark" || s === "light" ? g = s : (c.classList.contains("dark") || document.body?.classList.contains("dark") || window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches) && (g = "dark"), g && r.set("theme", g);
252
+ }
253
+ return `${a.replace(/\/$/, "")}/?${r.toString()}`;
254
+ }, [e, A, S, a]), z = E(() => {
255
+ }, []), K = j(
251
256
  () => ({
252
257
  apiKey: e,
253
- widgetUrl: o,
254
- user: l,
255
- isOpen: g,
256
- isReady: _,
257
- error: W,
258
- open: U,
259
- close: A,
260
- signOut: M,
261
- getAccessToken: $,
262
- _iframeSrc: D,
263
- _iframeHeight: j,
264
- _onIframeLoad: F
258
+ widgetUrl: a,
259
+ user: b,
260
+ isOpen: f,
261
+ isReady: x,
262
+ error: v,
263
+ open: F,
264
+ close: D,
265
+ signOut: H,
266
+ getAccessToken: B,
267
+ _iframeSrc: N,
268
+ _iframeHeight: U,
269
+ _onIframeLoad: z
265
270
  }),
266
271
  [
267
272
  e,
268
- o,
269
- l,
270
- g,
271
- _,
272
- W,
273
- U,
274
- A,
275
- M,
276
- $,
273
+ a,
274
+ b,
275
+ f,
276
+ x,
277
+ v,
278
+ F,
277
279
  D,
278
- j,
279
- F
280
+ H,
281
+ B,
282
+ N,
283
+ U,
284
+ z
280
285
  ]
281
286
  );
282
- return /* @__PURE__ */ T(B.Provider, { value: K, children: i });
287
+ return /* @__PURE__ */ P(V.Provider, { value: K, children: o });
283
288
  }
284
- function le({ onSuccess: e, onError: s, onClose: i }) {
289
+ const de = "rgba(15,15,20,0.55)", ue = 6, fe = 480, le = 18;
290
+ function he({
291
+ onSuccess: e,
292
+ onError: i,
293
+ onClose: o,
294
+ maxHeight: t,
295
+ maxWidth: n = fe,
296
+ radius: a = le,
297
+ backdrop: p,
298
+ backdropStyle: b,
299
+ iframeStyle: W
300
+ }) {
285
301
  const {
286
- isOpen: t,
287
- isReady: n,
288
- error: o,
289
- close: h,
290
- user: l,
291
- _iframeSrc: v,
292
- _iframeHeight: g,
293
- _onIframeLoad: b
294
- } = Q();
295
- return w(() => {
296
- l && e && e(l);
297
- }, [l, e]), w(() => {
298
- o && s && s(o);
299
- }, [o, s]), w(() => {
300
- !t && i && i();
301
- }, [t]), t ? /* @__PURE__ */ te(
302
+ isOpen: f,
303
+ isReady: k,
304
+ error: A,
305
+ close: q,
306
+ user: x,
307
+ _iframeSrc: I,
308
+ _iframeHeight: v,
309
+ _onIframeLoad: L
310
+ } = Z();
311
+ if (y(() => {
312
+ x && e && e(x);
313
+ }, [x, e]), y(() => {
314
+ A && i && i(A);
315
+ }, [A, i]), y(() => {
316
+ !f && o && o();
317
+ }, [f]), !f) return null;
318
+ const U = p?.color ?? de, _ = p?.blur ?? ue, S = _ > 0 ? `blur(${_}px)` : void 0;
319
+ let O = v, R;
320
+ return typeof t == "number" ? O = Math.min(v, t) : typeof t == "string" && (R = t), /* @__PURE__ */ te(
302
321
  "div",
303
322
  {
304
323
  role: "dialog",
305
324
  "aria-modal": "true",
306
325
  "aria-label": "Sign in",
307
- onClick: (k) => {
308
- k.target === k.currentTarget && h();
326
+ onClick: (m) => {
327
+ m.target === m.currentTarget && q();
309
328
  },
310
329
  style: {
311
330
  position: "fixed",
312
331
  inset: 0,
313
332
  zIndex: 2147483646,
314
- background: "rgba(15,15,20,0.55)",
315
- backdropFilter: "blur(6px)",
316
- WebkitBackdropFilter: "blur(6px)",
333
+ background: U,
334
+ backdropFilter: S,
335
+ WebkitBackdropFilter: S,
317
336
  display: "flex",
318
337
  alignItems: "center",
319
338
  justifyContent: "center",
320
339
  padding: 16,
321
- animation: "rift-fade 180ms ease-out"
340
+ animation: "rift-fade 180ms ease-out",
341
+ ...b
322
342
  },
323
343
  children: [
324
- /* @__PURE__ */ T("style", { children: "@keyframes rift-fade { from { opacity: 0 } to { opacity: 1 } }" }),
325
- !n && /* @__PURE__ */ T(
344
+ /* @__PURE__ */ P("style", { children: "@keyframes rift-fade { from { opacity: 0 } to { opacity: 1 } }" }),
345
+ !k && /* @__PURE__ */ P(
326
346
  "div",
327
347
  {
328
348
  "aria-hidden": !0,
@@ -335,11 +355,11 @@ function le({ onSuccess: e, onError: s, onClose: i }) {
335
355
  children: "Loading sign-in…"
336
356
  }
337
357
  ),
338
- /* @__PURE__ */ T(
358
+ /* @__PURE__ */ P(
339
359
  "iframe",
340
360
  {
341
- src: v,
342
- onLoad: b,
361
+ src: I,
362
+ onLoad: L,
343
363
  title: "Rift sign-in",
344
364
  allow: "publickey-credentials-get; identity-credentials-get",
345
365
  style: {
@@ -347,35 +367,37 @@ function le({ onSuccess: e, onError: s, onClose: i }) {
347
367
  background: "transparent",
348
368
  colorScheme: "light",
349
369
  width: "100%",
350
- maxWidth: 480,
351
- height: g,
352
- borderRadius: 18,
370
+ maxWidth: n,
371
+ height: O,
372
+ maxHeight: R,
373
+ borderRadius: a,
353
374
  boxShadow: "0 24px 60px -12px rgba(0,0,0,0.35)",
354
375
  transition: "height 200ms ease",
355
- opacity: n ? 1 : 0
376
+ opacity: k ? 1 : 0,
377
+ ...W
356
378
  }
357
379
  }
358
380
  )
359
381
  ]
360
382
  }
361
- ) : null;
383
+ );
362
384
  }
363
- function ge() {
364
- const { user: e, isOpen: s, open: i, close: t, signOut: n, getAccessToken: o, error: h } = Q();
385
+ function we() {
386
+ const { user: e, isOpen: i, open: o, close: t, signOut: n, getAccessToken: a, error: p } = Z();
365
387
  return {
366
388
  user: e,
367
389
  isAuthenticated: !!e,
368
- isOpen: s,
369
- open: i,
390
+ isOpen: i,
391
+ open: o,
370
392
  close: t,
371
393
  signOut: n,
372
- getAccessToken: o,
373
- error: h
394
+ getAccessToken: a,
395
+ error: p
374
396
  };
375
397
  }
376
398
  export {
377
- le as RiftAuth,
378
- fe as RiftProvider,
379
- ge as useRift
399
+ he as RiftAuth,
400
+ pe as RiftProvider,
401
+ we as useRift
380
402
  };
381
403
  //# sourceMappingURL=rift-react.js.map