@zerohash-sdk/profile-react 0.3.2 → 0.5.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.
Files changed (2) hide show
  1. package/dist/index.js +225 -26
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -1,39 +1,238 @@
1
- import h, { useRef as m, useEffect as f } from "react";
2
- const x = {
1
+ import { jsxs as W, jsx as g } from "react/jsx-runtime";
2
+ import A, { useEffect as B, useState as T, useCallback as j, useRef as M } from "react";
3
+ const y = {
4
+ evergreen: "#00381D",
5
+ green: "#CCFFD0",
6
+ offBlack: "#1C1B1C",
7
+ grayLight: "#EBEBE9"
8
+ }, D = ({ onRetry: e }) => W("div", { role: "alert", style: {
9
+ display: "flex",
10
+ flexDirection: "column",
11
+ alignItems: "center",
12
+ gap: "8px",
13
+ maxWidth: "360px",
14
+ margin: "0 auto",
15
+ padding: "32px 24px",
16
+ textAlign: "center",
17
+ fontFamily: "inherit",
18
+ color: y.offBlack,
19
+ background: "#FFFFFF",
20
+ border: `1px solid ${y.grayLight}`,
21
+ borderRadius: "12px"
22
+ }, children: [g("svg", { width: "32", height: "32", viewBox: "0 0 24 24", fill: "none", "aria-hidden": "true", children: g("path", { d: "M12 9v4m0 4h.01M10.29 3.86L1.82 18a2 2 0 001.71 3h16.94a2 2 0 001.71-3L13.71 3.86a2 2 0 00-3.42 0z", stroke: y.evergreen, strokeWidth: "1.5", strokeLinecap: "round", strokeLinejoin: "round" }) }), g("p", { style: { margin: 0, fontSize: "16px", fontWeight: 600 }, children: "Something went wrong" }), g("p", { style: { margin: 0, fontSize: "14px", opacity: 0.75 }, children: "We couldn't load this content. Please check your internet connection and try again." }), g("button", { type: "button", onClick: e, style: {
23
+ marginTop: "8px",
24
+ padding: "8px 20px",
25
+ fontSize: "14px",
26
+ fontWeight: 600,
27
+ fontFamily: "inherit",
28
+ color: y.green,
29
+ background: y.evergreen,
30
+ border: "none",
31
+ borderRadius: "8px",
32
+ cursor: "pointer"
33
+ }, children: "Try again" })] }), P = (e, t) => {
34
+ B(() => {
35
+ const o = e.current;
36
+ if (o)
37
+ for (const [r, n] of Object.entries(t))
38
+ n !== void 0 && (o[r] = n);
39
+ });
40
+ }, V = {
41
+ dev: "https://grafana-faro-collector.dev.0hash.com/collect",
42
+ cert: "https://grafana-faro-collector.cert.zerohash.com/collect",
43
+ prod: "https://grafana-faro-collector.zerohash.com/collect",
44
+ "eu-cert": "https://grafana-faro-collector.cert.zerohash.eu/collect",
45
+ "eu-prod": "https://grafana-faro-collector.zerohash.eu/collect",
46
+ sandbox: "https://grafana-faro-collector.cert.zerohash.com/collect",
47
+ production: "https://grafana-faro-collector.zerohash.com/collect"
48
+ }, J = (e) => {
49
+ if (!e || typeof e != "string")
50
+ return null;
51
+ const t = e.split(".");
52
+ if (t.length < 2)
53
+ return null;
54
+ try {
55
+ const o = t[1].replace(/-/g, "+").replace(/_/g, "/"), r = o + "===".slice(0, (4 - o.length % 4) % 4), n = typeof atob < "u" ? atob(r) : Buffer.from(r, "base64").toString("utf-8"), c = JSON.parse(n)?.payload?.region;
56
+ if (typeof c != "string")
57
+ return null;
58
+ const a = c.toLowerCase();
59
+ return a === "us" || a === "eu" ? a : null;
60
+ } catch {
61
+ return null;
62
+ }
63
+ }, I = (e, t) => J(t) !== "eu" ? e : e === "cert" ? "eu-cert" : e === "prod" ? "eu-prod" : e, $ = (e) => {
64
+ if (!e || typeof e != "string")
65
+ return {};
66
+ const t = e.split(".");
67
+ if (t.length < 2)
68
+ return {};
69
+ try {
70
+ const o = t[1].replace(/-/g, "+").replace(/_/g, "/"), r = o + "===".slice(0, (4 - o.length % 4) % 4), n = typeof atob < "u" ? atob(r) : Buffer.from(r, "base64").toString("utf-8"), i = JSON.parse(n), c = i?.payload ?? {}, a = (s) => typeof s == "string" && s.length > 0 ? s : void 0;
71
+ return {
72
+ participantCode: a(c.participant_code),
73
+ platformName: a(c.platform_name),
74
+ platformCode: a(i.platform_code),
75
+ region: a(c.region)
76
+ };
77
+ } catch {
78
+ return {};
79
+ }
80
+ }, L = (e, t, o) => {
81
+ const r = I(t, o);
82
+ return e[r] ?? e[t] ?? e.prod;
83
+ }, K = (e, t, o) => {
84
+ const r = I(t, o);
85
+ return e[r] ?? e[t];
86
+ }, C = () => {
87
+ }, H = () => {
88
+ const e = new Uint8Array(8);
89
+ return globalThis.crypto.getRandomValues(e), Array.from(e, (t) => t.toString(16).padStart(2, "0")).join("");
90
+ }, G = (e, t) => {
91
+ const o = H(), r = globalThis.__ZH_WEB_SDK_VERSION__, n = {};
92
+ e.claims.participantCode && (n.participant_code = e.claims.participantCode), e.claims.platformName && (n.platform_name = e.claims.platformName), e.claims.platformCode && (n.platform_code = e.claims.platformCode), e.claims.region && (n.region = e.claims.region), r && (n.zh_web_sdk_version = r);
93
+ const i = {
94
+ meta: {
95
+ app: { name: e.appName, version: t ?? "unknown", environment: e.env },
96
+ session: { id: o, attributes: n },
97
+ browser: typeof navigator < "u" ? { userAgent: navigator.userAgent } : void 0,
98
+ page: typeof window < "u" ? { url: window.location.origin } : void 0
99
+ },
100
+ logs: [
101
+ {
102
+ message: `Failed to load the script for ${e.webComponentTag} from ${e.env} environment.`,
103
+ level: "error",
104
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
105
+ context: {
106
+ web_component: e.webComponentTag,
107
+ script_url: e.scriptUrl,
108
+ reason: e.reason,
109
+ tried_fallback: String(e.triedFallback),
110
+ elapsed_ms: String(e.elapsedMs)
111
+ }
112
+ }
113
+ ]
114
+ };
115
+ return { sessionId: o, payload: i };
116
+ }, Z = (e, t, o) => {
117
+ if (!e || typeof fetch > "u")
118
+ return;
119
+ const { sessionId: r, payload: n } = G(t, o);
120
+ try {
121
+ fetch(e, {
122
+ method: "POST",
123
+ headers: { "Content-Type": "application/json", "x-faro-session-id": r },
124
+ body: JSON.stringify(n),
125
+ keepalive: !0
126
+ }).catch(() => {
127
+ });
128
+ } catch {
129
+ }
130
+ }, q = (e) => {
131
+ const { webComponentTag: t, appName: o, appVersion: r, scriptUrls: n, fallbackScriptUrls: i, collectorUrls: c, env: a, jwt: s, timeoutMs: f = 15e3, onLoad: h, onError: b } = e;
132
+ if (typeof document > "u")
133
+ return C;
134
+ const u = `${t}-script-${a}`;
135
+ if (customElements.get(t) || document.getElementById(u))
136
+ return C;
137
+ const R = $(s), S = e.report ?? ((d) => Z(c && K(c, a, s), d, r));
138
+ let p = !1, O = 0, x;
139
+ const _ = () => {
140
+ x !== void 0 && clearTimeout(x);
141
+ }, v = (d, m, l) => {
142
+ if (p)
143
+ return;
144
+ const U = Math.round(performance.now() - O), N = {
145
+ webComponentTag: t,
146
+ appName: o,
147
+ env: a,
148
+ scriptUrl: m,
149
+ reason: d,
150
+ triedFallback: l,
151
+ elapsedMs: U,
152
+ claims: R
153
+ };
154
+ S(N);
155
+ const w = l ? void 0 : L(i ?? {}, a, s);
156
+ if (w && w !== m) {
157
+ k(w, !0);
158
+ return;
159
+ }
160
+ p = !0, _(), document.getElementById(u)?.remove(), b?.(N);
161
+ }, k = (d, m) => {
162
+ _(), O = performance.now();
163
+ const l = document.createElement("script");
164
+ l.id = u, l.src = d, l.type = "module", l.async = !0, l.onload = () => {
165
+ setTimeout(() => {
166
+ p || (customElements.get(t) ? (p = !0, _(), h?.()) : v("not-defined", d, m));
167
+ }, 0);
168
+ }, l.onerror = () => v("network", d, m), x = setTimeout(() => v("timeout", d, m), f), document.getElementById(u)?.remove(), document.head.appendChild(l);
169
+ }, E = L(n, a, s);
170
+ return E ? (k(E, !1), () => {
171
+ p = !0, _();
172
+ }) : C;
173
+ };
174
+ var F;
175
+ (function(e) {
176
+ e.NETWORK_ERROR = "network_error", e.AUTH_ERROR = "auth_error", e.NOT_FOUND_ERROR = "not_found_error", e.VALIDATION_ERROR = "validation_error", e.SERVER_ERROR = "server_error", e.CLIENT_ERROR = "client_error", e.UNKNOWN_ERROR = "unknown_error";
177
+ })(F || (F = {}));
178
+ const Q = (e) => {
179
+ const { webComponentTag: t, appName: o, appVersion: r, scriptUrls: n, fallbackScriptUrls: i, collectorUrls: c, env: a, jwt: s, timeoutMs: f } = e, [h, b] = T(!1), [u, R] = T(0);
180
+ B(() => (b(!1), q({
181
+ webComponentTag: t,
182
+ appName: o ?? t,
183
+ appVersion: r,
184
+ scriptUrls: n,
185
+ fallbackScriptUrls: i,
186
+ collectorUrls: c ?? V,
187
+ env: a,
188
+ jwt: s,
189
+ timeoutMs: f,
190
+ onError: () => b(!0)
191
+ })), [
192
+ t,
193
+ o,
194
+ r,
195
+ n,
196
+ i,
197
+ c,
198
+ a,
199
+ s,
200
+ f,
201
+ u
202
+ ]);
203
+ const S = j(() => R((p) => p + 1), []);
204
+ return { loadFailed: h, retry: S };
205
+ }, X = {
3
206
  local: "http://localhost:5173/profile-web/index.js",
4
207
  dev: "https://connect-sdk.dev.0hash.com/profile-web/index.js",
5
208
  cert: "https://sdk.sandbox.connect.xyz/profile-web/index.js",
6
209
  prod: "https://sdk.connect.xyz/profile-web/index.js"
7
- }, b = "zerohash-profile-script", a = "zerohash-profile", y = (c = "prod") => x[c], R = ({
8
- jwt: c,
9
- env: r = "prod",
210
+ }, z = "zerohash-profile", te = ({
211
+ jwt: e,
212
+ env: t = "prod",
10
213
  theme: o,
11
- onError: s,
214
+ onError: r,
12
215
  onClose: n,
13
216
  onLoaded: i,
14
- onEvent: p,
15
- ...u
217
+ onEvent: c,
218
+ ...a
16
219
  }) => {
17
- const d = m(null);
18
- return f(() => {
19
- const e = d.current;
20
- e && (o !== void 0 && (e.theme = o), s && (e.onError = s), n && (e.onClose = n), i && (e.onLoaded = i), p && (e.onEvent = p));
21
- }, [o, s, n, i, p]), f(() => {
22
- const e = y(r), l = `${b}-${r}`;
23
- if (document.getElementById(l))
24
- return;
25
- const t = document.createElement("script");
26
- t.id = l, t.src = e, t.type = "module", t.async = !0, t.onerror = () => {
27
- console.error(`Failed to load the script for ${a} from ${r} environment.`);
28
- }, document.head.appendChild(t);
29
- }, [r]), h.createElement(a, {
30
- ref: d,
31
- jwt: c,
32
- env: r,
220
+ const s = M(null);
221
+ P(s, { theme: o, onError: r, onClose: n, onLoaded: i, onEvent: c });
222
+ const { loadFailed: f, retry: h } = Q({
223
+ webComponentTag: z,
224
+ scriptUrls: X,
225
+ env: t,
226
+ jwt: e
227
+ });
228
+ return f ? /* @__PURE__ */ g(D, { onRetry: h }) : A.createElement(z, {
229
+ ref: s,
230
+ jwt: e,
231
+ env: t,
33
232
  theme: o,
34
- ...u
233
+ ...a
35
234
  });
36
235
  };
37
236
  export {
38
- R as Profile
237
+ te as Profile
39
238
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zerohash-sdk/profile-react",
3
- "version": "0.3.2",
3
+ "version": "0.5.0",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",