@compilot/react-sdk 2.0.48-dev
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/compilot-react-sdk.cjs.d.ts +2 -0
- package/dist/compilot-react-sdk.cjs.dev.js +1021 -0
- package/dist/compilot-react-sdk.cjs.js +7 -0
- package/dist/compilot-react-sdk.cjs.prod.js +1021 -0
- package/dist/compilot-react-sdk.esm.js +1006 -0
- package/dist/declarations/src/configuration/ComPilotProvider.d.ts +67 -0
- package/dist/declarations/src/configuration/ComPilotProvider.d.ts.map +1 -0
- package/dist/declarations/src/hooks/index.d.ts +6 -0
- package/dist/declarations/src/hooks/index.d.ts.map +1 -0
- package/dist/declarations/src/hooks/useCustomerStatus.d.ts +42 -0
- package/dist/declarations/src/hooks/useCustomerStatus.d.ts.map +1 -0
- package/dist/declarations/src/hooks/useDisconnect.d.ts +28 -0
- package/dist/declarations/src/hooks/useDisconnect.d.ts.map +1 -0
- package/dist/declarations/src/hooks/useGetTxAuthDataSignature.d.ts +168 -0
- package/dist/declarations/src/hooks/useGetTxAuthDataSignature.d.ts.map +1 -0
- package/dist/declarations/src/hooks/useIsAuthenticated.d.ts +10 -0
- package/dist/declarations/src/hooks/useIsAuthenticated.d.ts.map +1 -0
- package/dist/declarations/src/hooks/useOpenWidget.d.ts +51 -0
- package/dist/declarations/src/hooks/useOpenWidget.d.ts.map +1 -0
- package/dist/declarations/src/index.d.ts +4 -0
- package/dist/declarations/src/index.d.ts.map +1 -0
- package/dist/declarations/src/utils/useAsyncMutationState.d.ts +43 -0
- package/dist/declarations/src/utils/useAsyncMutationState.d.ts.map +1 -0
- package/dist/declarations/src/utils/useAsyncQueryState.d.ts +36 -0
- package/dist/declarations/src/utils/useAsyncQueryState.d.ts.map +1 -0
- package/dist/package.json +63 -0
- package/package.json +62 -0
- package/readme.md +15 -0
|
@@ -0,0 +1,1006 @@
|
|
|
1
|
+
import { useEffect, useContext, createContext, useState, useCallback } from 'react';
|
|
2
|
+
import { load, openWidget, isOpen, watchWidgetVisibleState, getTxAuthDataSignature, isAuthenticated, watchIsAuthenticated, watchCustomerStatus, getCustomerStatus, disconnect } from '@compilot/web-sdk';
|
|
3
|
+
export * from '@compilot/web-sdk';
|
|
4
|
+
import { jsx, Fragment } from 'react/jsx-runtime';
|
|
5
|
+
|
|
6
|
+
var AutoLoader = function AutoLoader(_ref) {
|
|
7
|
+
var autoLoad = _ref.autoLoad,
|
|
8
|
+
children = _ref.children;
|
|
9
|
+
var config = useComPilotConfig();
|
|
10
|
+
useEffect(function () {
|
|
11
|
+
if (autoLoad) {
|
|
12
|
+
void load(config);
|
|
13
|
+
}
|
|
14
|
+
}, [autoLoad]);
|
|
15
|
+
return /*#__PURE__*/jsx(Fragment, {
|
|
16
|
+
children: children
|
|
17
|
+
});
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
var ComPilotContext = /*#__PURE__*/createContext(null);
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The props type of {@link ComPilotProvider}.
|
|
24
|
+
*
|
|
25
|
+
* @category PropTypes
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* ComPilotProvider is a React Context Provider that provides the ComPilot configuration to the rest of the application.
|
|
30
|
+
*
|
|
31
|
+
* @param props - The props of the ComPilotProvider component.
|
|
32
|
+
*
|
|
33
|
+
* @category Component
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
*
|
|
37
|
+
* Basic
|
|
38
|
+
* ```tsx
|
|
39
|
+
* import { ComPilotProvider } from "@compilot/react-sdk";
|
|
40
|
+
* import { config } from "./config";
|
|
41
|
+
*
|
|
42
|
+
* const App = () => {
|
|
43
|
+
* return (
|
|
44
|
+
* <ComPilotProvider config={config}>
|
|
45
|
+
* <MyApp />
|
|
46
|
+
* </ComPilotProvider>
|
|
47
|
+
* );
|
|
48
|
+
* };
|
|
49
|
+
* ```
|
|
50
|
+
*
|
|
51
|
+
* With AutoLoader disabled
|
|
52
|
+
* ```tsx
|
|
53
|
+
* import { ComPilotProvider } from "@compilot/react-sdk";
|
|
54
|
+
* import { config } from "./config";
|
|
55
|
+
*
|
|
56
|
+
* const App = () => {
|
|
57
|
+
* return (
|
|
58
|
+
* <ComPilotProvider config={config} autoLoad={false}>
|
|
59
|
+
* <MyApp />
|
|
60
|
+
* </ComPilotProvider>
|
|
61
|
+
* );
|
|
62
|
+
* };
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
var ComPilotProvider = function ComPilotProvider(_ref) {
|
|
66
|
+
var config = _ref.config,
|
|
67
|
+
_ref$autoLoad = _ref.autoLoad,
|
|
68
|
+
autoLoad = _ref$autoLoad === void 0 ? true : _ref$autoLoad,
|
|
69
|
+
children = _ref.children;
|
|
70
|
+
return /*#__PURE__*/jsx(ComPilotContext.Provider, {
|
|
71
|
+
value: config,
|
|
72
|
+
children: /*#__PURE__*/jsx(AutoLoader, {
|
|
73
|
+
autoLoad: autoLoad,
|
|
74
|
+
children: children
|
|
75
|
+
})
|
|
76
|
+
});
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* A hook to access the ComPilot configuration object.
|
|
81
|
+
* @private This hook is intended for internal use only.
|
|
82
|
+
*/
|
|
83
|
+
var useComPilotConfig = function useComPilotConfig() {
|
|
84
|
+
var config = useContext(ComPilotContext);
|
|
85
|
+
if (!config) {
|
|
86
|
+
throw new Error("No ComPilot config found");
|
|
87
|
+
}
|
|
88
|
+
return config;
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
function _toPrimitive(t, r) {
|
|
92
|
+
if ("object" != typeof t || !t) return t;
|
|
93
|
+
var e = t[Symbol.toPrimitive];
|
|
94
|
+
if (void 0 !== e) {
|
|
95
|
+
var i = e.call(t, r || "default");
|
|
96
|
+
if ("object" != typeof i) return i;
|
|
97
|
+
throw new TypeError("@@toPrimitive must return a primitive value.");
|
|
98
|
+
}
|
|
99
|
+
return ("string" === r ? String : Number)(t);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function _toPropertyKey(t) {
|
|
103
|
+
var i = _toPrimitive(t, "string");
|
|
104
|
+
return "symbol" == typeof i ? i : i + "";
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function _defineProperty(e, r, t) {
|
|
108
|
+
return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
|
|
109
|
+
value: t,
|
|
110
|
+
enumerable: !0,
|
|
111
|
+
configurable: !0,
|
|
112
|
+
writable: !0
|
|
113
|
+
}) : e[r] = t, e;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function ownKeys(e, r) {
|
|
117
|
+
var t = Object.keys(e);
|
|
118
|
+
if (Object.getOwnPropertySymbols) {
|
|
119
|
+
var o = Object.getOwnPropertySymbols(e);
|
|
120
|
+
r && (o = o.filter(function (r) {
|
|
121
|
+
return Object.getOwnPropertyDescriptor(e, r).enumerable;
|
|
122
|
+
})), t.push.apply(t, o);
|
|
123
|
+
}
|
|
124
|
+
return t;
|
|
125
|
+
}
|
|
126
|
+
function _objectSpread2(e) {
|
|
127
|
+
for (var r = 1; r < arguments.length; r++) {
|
|
128
|
+
var t = null != arguments[r] ? arguments[r] : {};
|
|
129
|
+
r % 2 ? ownKeys(Object(t), !0).forEach(function (r) {
|
|
130
|
+
_defineProperty(e, r, t[r]);
|
|
131
|
+
}) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) {
|
|
132
|
+
Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r));
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
return e;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function _regeneratorRuntime() {
|
|
139
|
+
_regeneratorRuntime = function () {
|
|
140
|
+
return e;
|
|
141
|
+
};
|
|
142
|
+
var t,
|
|
143
|
+
e = {},
|
|
144
|
+
r = Object.prototype,
|
|
145
|
+
n = r.hasOwnProperty,
|
|
146
|
+
o = Object.defineProperty || function (t, e, r) {
|
|
147
|
+
t[e] = r.value;
|
|
148
|
+
},
|
|
149
|
+
i = "function" == typeof Symbol ? Symbol : {},
|
|
150
|
+
a = i.iterator || "@@iterator",
|
|
151
|
+
c = i.asyncIterator || "@@asyncIterator",
|
|
152
|
+
u = i.toStringTag || "@@toStringTag";
|
|
153
|
+
function define(t, e, r) {
|
|
154
|
+
return Object.defineProperty(t, e, {
|
|
155
|
+
value: r,
|
|
156
|
+
enumerable: !0,
|
|
157
|
+
configurable: !0,
|
|
158
|
+
writable: !0
|
|
159
|
+
}), t[e];
|
|
160
|
+
}
|
|
161
|
+
try {
|
|
162
|
+
define({}, "");
|
|
163
|
+
} catch (t) {
|
|
164
|
+
define = function (t, e, r) {
|
|
165
|
+
return t[e] = r;
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
function wrap(t, e, r, n) {
|
|
169
|
+
var i = e && e.prototype instanceof Generator ? e : Generator,
|
|
170
|
+
a = Object.create(i.prototype),
|
|
171
|
+
c = new Context(n || []);
|
|
172
|
+
return o(a, "_invoke", {
|
|
173
|
+
value: makeInvokeMethod(t, r, c)
|
|
174
|
+
}), a;
|
|
175
|
+
}
|
|
176
|
+
function tryCatch(t, e, r) {
|
|
177
|
+
try {
|
|
178
|
+
return {
|
|
179
|
+
type: "normal",
|
|
180
|
+
arg: t.call(e, r)
|
|
181
|
+
};
|
|
182
|
+
} catch (t) {
|
|
183
|
+
return {
|
|
184
|
+
type: "throw",
|
|
185
|
+
arg: t
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
e.wrap = wrap;
|
|
190
|
+
var h = "suspendedStart",
|
|
191
|
+
l = "suspendedYield",
|
|
192
|
+
f = "executing",
|
|
193
|
+
s = "completed",
|
|
194
|
+
y = {};
|
|
195
|
+
function Generator() {}
|
|
196
|
+
function GeneratorFunction() {}
|
|
197
|
+
function GeneratorFunctionPrototype() {}
|
|
198
|
+
var p = {};
|
|
199
|
+
define(p, a, function () {
|
|
200
|
+
return this;
|
|
201
|
+
});
|
|
202
|
+
var d = Object.getPrototypeOf,
|
|
203
|
+
v = d && d(d(values([])));
|
|
204
|
+
v && v !== r && n.call(v, a) && (p = v);
|
|
205
|
+
var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p);
|
|
206
|
+
function defineIteratorMethods(t) {
|
|
207
|
+
["next", "throw", "return"].forEach(function (e) {
|
|
208
|
+
define(t, e, function (t) {
|
|
209
|
+
return this._invoke(e, t);
|
|
210
|
+
});
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
function AsyncIterator(t, e) {
|
|
214
|
+
function invoke(r, o, i, a) {
|
|
215
|
+
var c = tryCatch(t[r], t, o);
|
|
216
|
+
if ("throw" !== c.type) {
|
|
217
|
+
var u = c.arg,
|
|
218
|
+
h = u.value;
|
|
219
|
+
return h && "object" == typeof h && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) {
|
|
220
|
+
invoke("next", t, i, a);
|
|
221
|
+
}, function (t) {
|
|
222
|
+
invoke("throw", t, i, a);
|
|
223
|
+
}) : e.resolve(h).then(function (t) {
|
|
224
|
+
u.value = t, i(u);
|
|
225
|
+
}, function (t) {
|
|
226
|
+
return invoke("throw", t, i, a);
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
a(c.arg);
|
|
230
|
+
}
|
|
231
|
+
var r;
|
|
232
|
+
o(this, "_invoke", {
|
|
233
|
+
value: function (t, n) {
|
|
234
|
+
function callInvokeWithMethodAndArg() {
|
|
235
|
+
return new e(function (e, r) {
|
|
236
|
+
invoke(t, n, e, r);
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg();
|
|
240
|
+
}
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
function makeInvokeMethod(e, r, n) {
|
|
244
|
+
var o = h;
|
|
245
|
+
return function (i, a) {
|
|
246
|
+
if (o === f) throw Error("Generator is already running");
|
|
247
|
+
if (o === s) {
|
|
248
|
+
if ("throw" === i) throw a;
|
|
249
|
+
return {
|
|
250
|
+
value: t,
|
|
251
|
+
done: !0
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
for (n.method = i, n.arg = a;;) {
|
|
255
|
+
var c = n.delegate;
|
|
256
|
+
if (c) {
|
|
257
|
+
var u = maybeInvokeDelegate(c, n);
|
|
258
|
+
if (u) {
|
|
259
|
+
if (u === y) continue;
|
|
260
|
+
return u;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) {
|
|
264
|
+
if (o === h) throw o = s, n.arg;
|
|
265
|
+
n.dispatchException(n.arg);
|
|
266
|
+
} else "return" === n.method && n.abrupt("return", n.arg);
|
|
267
|
+
o = f;
|
|
268
|
+
var p = tryCatch(e, r, n);
|
|
269
|
+
if ("normal" === p.type) {
|
|
270
|
+
if (o = n.done ? s : l, p.arg === y) continue;
|
|
271
|
+
return {
|
|
272
|
+
value: p.arg,
|
|
273
|
+
done: n.done
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
"throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg);
|
|
277
|
+
}
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
function maybeInvokeDelegate(e, r) {
|
|
281
|
+
var n = r.method,
|
|
282
|
+
o = e.iterator[n];
|
|
283
|
+
if (o === t) return r.delegate = null, "throw" === n && e.iterator.return && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y;
|
|
284
|
+
var i = tryCatch(o, e.iterator, r.arg);
|
|
285
|
+
if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y;
|
|
286
|
+
var a = i.arg;
|
|
287
|
+
return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y);
|
|
288
|
+
}
|
|
289
|
+
function pushTryEntry(t) {
|
|
290
|
+
var e = {
|
|
291
|
+
tryLoc: t[0]
|
|
292
|
+
};
|
|
293
|
+
1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e);
|
|
294
|
+
}
|
|
295
|
+
function resetTryEntry(t) {
|
|
296
|
+
var e = t.completion || {};
|
|
297
|
+
e.type = "normal", delete e.arg, t.completion = e;
|
|
298
|
+
}
|
|
299
|
+
function Context(t) {
|
|
300
|
+
this.tryEntries = [{
|
|
301
|
+
tryLoc: "root"
|
|
302
|
+
}], t.forEach(pushTryEntry, this), this.reset(!0);
|
|
303
|
+
}
|
|
304
|
+
function values(e) {
|
|
305
|
+
if (e || "" === e) {
|
|
306
|
+
var r = e[a];
|
|
307
|
+
if (r) return r.call(e);
|
|
308
|
+
if ("function" == typeof e.next) return e;
|
|
309
|
+
if (!isNaN(e.length)) {
|
|
310
|
+
var o = -1,
|
|
311
|
+
i = function next() {
|
|
312
|
+
for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next;
|
|
313
|
+
return next.value = t, next.done = !0, next;
|
|
314
|
+
};
|
|
315
|
+
return i.next = i;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
throw new TypeError(typeof e + " is not iterable");
|
|
319
|
+
}
|
|
320
|
+
return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", {
|
|
321
|
+
value: GeneratorFunctionPrototype,
|
|
322
|
+
configurable: !0
|
|
323
|
+
}), o(GeneratorFunctionPrototype, "constructor", {
|
|
324
|
+
value: GeneratorFunction,
|
|
325
|
+
configurable: !0
|
|
326
|
+
}), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) {
|
|
327
|
+
var e = "function" == typeof t && t.constructor;
|
|
328
|
+
return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name));
|
|
329
|
+
}, e.mark = function (t) {
|
|
330
|
+
return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t;
|
|
331
|
+
}, e.awrap = function (t) {
|
|
332
|
+
return {
|
|
333
|
+
__await: t
|
|
334
|
+
};
|
|
335
|
+
}, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () {
|
|
336
|
+
return this;
|
|
337
|
+
}), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) {
|
|
338
|
+
void 0 === i && (i = Promise);
|
|
339
|
+
var a = new AsyncIterator(wrap(t, r, n, o), i);
|
|
340
|
+
return e.isGeneratorFunction(r) ? a : a.next().then(function (t) {
|
|
341
|
+
return t.done ? t.value : a.next();
|
|
342
|
+
});
|
|
343
|
+
}, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () {
|
|
344
|
+
return this;
|
|
345
|
+
}), define(g, "toString", function () {
|
|
346
|
+
return "[object Generator]";
|
|
347
|
+
}), e.keys = function (t) {
|
|
348
|
+
var e = Object(t),
|
|
349
|
+
r = [];
|
|
350
|
+
for (var n in e) r.push(n);
|
|
351
|
+
return r.reverse(), function next() {
|
|
352
|
+
for (; r.length;) {
|
|
353
|
+
var t = r.pop();
|
|
354
|
+
if (t in e) return next.value = t, next.done = !1, next;
|
|
355
|
+
}
|
|
356
|
+
return next.done = !0, next;
|
|
357
|
+
};
|
|
358
|
+
}, e.values = values, Context.prototype = {
|
|
359
|
+
constructor: Context,
|
|
360
|
+
reset: function (e) {
|
|
361
|
+
if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t);
|
|
362
|
+
},
|
|
363
|
+
stop: function () {
|
|
364
|
+
this.done = !0;
|
|
365
|
+
var t = this.tryEntries[0].completion;
|
|
366
|
+
if ("throw" === t.type) throw t.arg;
|
|
367
|
+
return this.rval;
|
|
368
|
+
},
|
|
369
|
+
dispatchException: function (e) {
|
|
370
|
+
if (this.done) throw e;
|
|
371
|
+
var r = this;
|
|
372
|
+
function handle(n, o) {
|
|
373
|
+
return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o;
|
|
374
|
+
}
|
|
375
|
+
for (var o = this.tryEntries.length - 1; o >= 0; --o) {
|
|
376
|
+
var i = this.tryEntries[o],
|
|
377
|
+
a = i.completion;
|
|
378
|
+
if ("root" === i.tryLoc) return handle("end");
|
|
379
|
+
if (i.tryLoc <= this.prev) {
|
|
380
|
+
var c = n.call(i, "catchLoc"),
|
|
381
|
+
u = n.call(i, "finallyLoc");
|
|
382
|
+
if (c && u) {
|
|
383
|
+
if (this.prev < i.catchLoc) return handle(i.catchLoc, !0);
|
|
384
|
+
if (this.prev < i.finallyLoc) return handle(i.finallyLoc);
|
|
385
|
+
} else if (c) {
|
|
386
|
+
if (this.prev < i.catchLoc) return handle(i.catchLoc, !0);
|
|
387
|
+
} else {
|
|
388
|
+
if (!u) throw Error("try statement without catch or finally");
|
|
389
|
+
if (this.prev < i.finallyLoc) return handle(i.finallyLoc);
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
},
|
|
394
|
+
abrupt: function (t, e) {
|
|
395
|
+
for (var r = this.tryEntries.length - 1; r >= 0; --r) {
|
|
396
|
+
var o = this.tryEntries[r];
|
|
397
|
+
if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) {
|
|
398
|
+
var i = o;
|
|
399
|
+
break;
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null);
|
|
403
|
+
var a = i ? i.completion : {};
|
|
404
|
+
return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a);
|
|
405
|
+
},
|
|
406
|
+
complete: function (t, e) {
|
|
407
|
+
if ("throw" === t.type) throw t.arg;
|
|
408
|
+
return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y;
|
|
409
|
+
},
|
|
410
|
+
finish: function (t) {
|
|
411
|
+
for (var e = this.tryEntries.length - 1; e >= 0; --e) {
|
|
412
|
+
var r = this.tryEntries[e];
|
|
413
|
+
if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y;
|
|
414
|
+
}
|
|
415
|
+
},
|
|
416
|
+
catch: function (t) {
|
|
417
|
+
for (var e = this.tryEntries.length - 1; e >= 0; --e) {
|
|
418
|
+
var r = this.tryEntries[e];
|
|
419
|
+
if (r.tryLoc === t) {
|
|
420
|
+
var n = r.completion;
|
|
421
|
+
if ("throw" === n.type) {
|
|
422
|
+
var o = n.arg;
|
|
423
|
+
resetTryEntry(r);
|
|
424
|
+
}
|
|
425
|
+
return o;
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
throw Error("illegal catch attempt");
|
|
429
|
+
},
|
|
430
|
+
delegateYield: function (e, r, n) {
|
|
431
|
+
return this.delegate = {
|
|
432
|
+
iterator: values(e),
|
|
433
|
+
resultName: r,
|
|
434
|
+
nextLoc: n
|
|
435
|
+
}, "next" === this.method && (this.arg = t), y;
|
|
436
|
+
}
|
|
437
|
+
}, e;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
function asyncGeneratorStep(n, t, e, r, o, a, c) {
|
|
441
|
+
try {
|
|
442
|
+
var i = n[a](c),
|
|
443
|
+
u = i.value;
|
|
444
|
+
} catch (n) {
|
|
445
|
+
return void e(n);
|
|
446
|
+
}
|
|
447
|
+
i.done ? t(u) : Promise.resolve(u).then(r, o);
|
|
448
|
+
}
|
|
449
|
+
function _asyncToGenerator(n) {
|
|
450
|
+
return function () {
|
|
451
|
+
var t = this,
|
|
452
|
+
e = arguments;
|
|
453
|
+
return new Promise(function (r, o) {
|
|
454
|
+
var a = n.apply(t, e);
|
|
455
|
+
function _next(n) {
|
|
456
|
+
asyncGeneratorStep(a, r, o, _next, _throw, "next", n);
|
|
457
|
+
}
|
|
458
|
+
function _throw(n) {
|
|
459
|
+
asyncGeneratorStep(a, r, o, _next, _throw, "throw", n);
|
|
460
|
+
}
|
|
461
|
+
_next(void 0);
|
|
462
|
+
});
|
|
463
|
+
};
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
function _arrayWithHoles(r) {
|
|
467
|
+
if (Array.isArray(r)) return r;
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
function _iterableToArrayLimit(r, l) {
|
|
471
|
+
var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
|
|
472
|
+
if (null != t) {
|
|
473
|
+
var e,
|
|
474
|
+
n,
|
|
475
|
+
i,
|
|
476
|
+
u,
|
|
477
|
+
a = [],
|
|
478
|
+
f = !0,
|
|
479
|
+
o = !1;
|
|
480
|
+
try {
|
|
481
|
+
if (i = (t = t.call(r)).next, 0 === l) {
|
|
482
|
+
if (Object(t) !== t) return;
|
|
483
|
+
f = !1;
|
|
484
|
+
} else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0);
|
|
485
|
+
} catch (r) {
|
|
486
|
+
o = !0, n = r;
|
|
487
|
+
} finally {
|
|
488
|
+
try {
|
|
489
|
+
if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return;
|
|
490
|
+
} finally {
|
|
491
|
+
if (o) throw n;
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
return a;
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
function _arrayLikeToArray(r, a) {
|
|
499
|
+
(null == a || a > r.length) && (a = r.length);
|
|
500
|
+
for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e];
|
|
501
|
+
return n;
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
function _unsupportedIterableToArray(r, a) {
|
|
505
|
+
if (r) {
|
|
506
|
+
if ("string" == typeof r) return _arrayLikeToArray(r, a);
|
|
507
|
+
var t = {}.toString.call(r).slice(8, -1);
|
|
508
|
+
return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0;
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
function _nonIterableRest() {
|
|
513
|
+
throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
function _slicedToArray(r, e) {
|
|
517
|
+
return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest();
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
521
|
+
|
|
522
|
+
var useAsyncMutationState = function useAsyncMutationState(_ref) {
|
|
523
|
+
var mutationFn = _ref.mutationFn;
|
|
524
|
+
var _useState = useState({
|
|
525
|
+
data: undefined,
|
|
526
|
+
isIdle: true,
|
|
527
|
+
isPending: false,
|
|
528
|
+
isError: false,
|
|
529
|
+
isSuccess: false,
|
|
530
|
+
error: null
|
|
531
|
+
}),
|
|
532
|
+
_useState2 = _slicedToArray(_useState, 2),
|
|
533
|
+
state = _useState2[0],
|
|
534
|
+
setState = _useState2[1];
|
|
535
|
+
var mutateAsync = useCallback(/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
|
|
536
|
+
var result,
|
|
537
|
+
_args = arguments;
|
|
538
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
539
|
+
while (1) switch (_context.prev = _context.next) {
|
|
540
|
+
case 0:
|
|
541
|
+
setState({
|
|
542
|
+
data: state.data,
|
|
543
|
+
isIdle: false,
|
|
544
|
+
isPending: true,
|
|
545
|
+
isError: false,
|
|
546
|
+
isSuccess: false,
|
|
547
|
+
error: null
|
|
548
|
+
});
|
|
549
|
+
_context.prev = 1;
|
|
550
|
+
_context.next = 4;
|
|
551
|
+
return mutationFn.apply(void 0, _args);
|
|
552
|
+
case 4:
|
|
553
|
+
result = _context.sent;
|
|
554
|
+
setState({
|
|
555
|
+
data: result,
|
|
556
|
+
isIdle: false,
|
|
557
|
+
isPending: false,
|
|
558
|
+
isError: false,
|
|
559
|
+
isSuccess: true,
|
|
560
|
+
error: null
|
|
561
|
+
});
|
|
562
|
+
|
|
563
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
564
|
+
return _context.abrupt("return", result);
|
|
565
|
+
case 9:
|
|
566
|
+
_context.prev = 9;
|
|
567
|
+
_context.t0 = _context["catch"](1);
|
|
568
|
+
setState({
|
|
569
|
+
data: state.data,
|
|
570
|
+
isIdle: false,
|
|
571
|
+
isPending: false,
|
|
572
|
+
isError: true,
|
|
573
|
+
isSuccess: false,
|
|
574
|
+
error: _context.t0 instanceof Error ? _context.t0 : new Error(String(_context.t0))
|
|
575
|
+
});
|
|
576
|
+
throw _context.t0;
|
|
577
|
+
case 13:
|
|
578
|
+
case "end":
|
|
579
|
+
return _context.stop();
|
|
580
|
+
}
|
|
581
|
+
}, _callee, null, [[1, 9]]);
|
|
582
|
+
})), [setState, mutationFn]);
|
|
583
|
+
return _objectSpread2(_objectSpread2({}, state), {}, {
|
|
584
|
+
mutateAsync: mutateAsync
|
|
585
|
+
});
|
|
586
|
+
};
|
|
587
|
+
|
|
588
|
+
var useAsyncQueryState = function useAsyncQueryState(defaultValue) {
|
|
589
|
+
var _useState = useState(defaultValue === undefined ? {
|
|
590
|
+
isLoading: false,
|
|
591
|
+
isError: false,
|
|
592
|
+
isSuccess: false,
|
|
593
|
+
error: null,
|
|
594
|
+
data: undefined
|
|
595
|
+
} : {
|
|
596
|
+
isLoading: false,
|
|
597
|
+
isError: false,
|
|
598
|
+
isSuccess: true,
|
|
599
|
+
error: null,
|
|
600
|
+
data: defaultValue
|
|
601
|
+
}),
|
|
602
|
+
_useState2 = _slicedToArray(_useState, 2),
|
|
603
|
+
state = _useState2[0],
|
|
604
|
+
setState = _useState2[1];
|
|
605
|
+
var setResult = useCallback(function (result) {
|
|
606
|
+
setState({
|
|
607
|
+
isLoading: false,
|
|
608
|
+
isError: false,
|
|
609
|
+
isSuccess: true,
|
|
610
|
+
error: null,
|
|
611
|
+
data: result
|
|
612
|
+
});
|
|
613
|
+
}, [setState]);
|
|
614
|
+
var setError = useCallback(function (error) {
|
|
615
|
+
setState({
|
|
616
|
+
isLoading: false,
|
|
617
|
+
isError: true,
|
|
618
|
+
isSuccess: false,
|
|
619
|
+
error: error,
|
|
620
|
+
data: undefined
|
|
621
|
+
});
|
|
622
|
+
}, [setState]);
|
|
623
|
+
var startLoading = useCallback(function () {
|
|
624
|
+
setState({
|
|
625
|
+
isLoading: true,
|
|
626
|
+
isError: false,
|
|
627
|
+
isSuccess: false,
|
|
628
|
+
error: null,
|
|
629
|
+
data: undefined
|
|
630
|
+
});
|
|
631
|
+
}, [setState]);
|
|
632
|
+
return {
|
|
633
|
+
state: state,
|
|
634
|
+
setResult: setResult,
|
|
635
|
+
setError: setError,
|
|
636
|
+
startLoading: startLoading
|
|
637
|
+
};
|
|
638
|
+
};
|
|
639
|
+
|
|
640
|
+
/**
|
|
641
|
+
* A hook that returns a function that opens the ComPilot ID widget.
|
|
642
|
+
*
|
|
643
|
+
* @param loginParams - The login parameters to use when opening the widget.
|
|
644
|
+
* @returns The async mutation state of the widget opening.
|
|
645
|
+
*
|
|
646
|
+
* @category Hook
|
|
647
|
+
*/
|
|
648
|
+
var useOpenWidget = function useOpenWidget(loginParams) {
|
|
649
|
+
var config = useComPilotConfig();
|
|
650
|
+
var mutationFn = useCallback(function () {
|
|
651
|
+
return openWidget(config, loginParams);
|
|
652
|
+
}, [config]);
|
|
653
|
+
var mutationState = useAsyncMutationState({
|
|
654
|
+
mutationFn: mutationFn
|
|
655
|
+
});
|
|
656
|
+
|
|
657
|
+
// but open state can change without us mutating it
|
|
658
|
+
// that's the only case for now so we don't need another kind of mutation state
|
|
659
|
+
var _useAsyncQueryState = useAsyncQueryState(isOpen(config)),
|
|
660
|
+
setResult = _useAsyncQueryState.setResult,
|
|
661
|
+
startLoading = _useAsyncQueryState.startLoading,
|
|
662
|
+
queryState = _useAsyncQueryState.state;
|
|
663
|
+
useEffect(function () {
|
|
664
|
+
var unsubscribe = watchWidgetVisibleState(config, {
|
|
665
|
+
onChange: function onChange(isVisible) {
|
|
666
|
+
setResult(isVisible);
|
|
667
|
+
},
|
|
668
|
+
onIsLoadingChange: function onIsLoadingChange(isLoading) {
|
|
669
|
+
if (isLoading) {
|
|
670
|
+
startLoading();
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
});
|
|
674
|
+
return unsubscribe;
|
|
675
|
+
}, [config, setResult, startLoading]);
|
|
676
|
+
return _objectSpread2(_objectSpread2({}, queryState.data === undefined ? mutationState : {
|
|
677
|
+
error: queryState.error,
|
|
678
|
+
isPending: queryState.isLoading,
|
|
679
|
+
isSuccess: queryState.isSuccess,
|
|
680
|
+
isError: queryState.isError,
|
|
681
|
+
isIdle: !queryState.isError && !queryState.isLoading && !queryState.isSuccess,
|
|
682
|
+
data: queryState.data
|
|
683
|
+
}), {}, {
|
|
684
|
+
mutateAsync: mutationState.mutateAsync
|
|
685
|
+
});
|
|
686
|
+
};
|
|
687
|
+
|
|
688
|
+
/**
|
|
689
|
+
* A callback that returns a transaction authorization data signature.
|
|
690
|
+
*
|
|
691
|
+
* @category Callback
|
|
692
|
+
*/
|
|
693
|
+
|
|
694
|
+
/**
|
|
695
|
+
* A hook that returns a function that returns a transaction authorization data signature.
|
|
696
|
+
*
|
|
697
|
+
* @returns A function that returns a transaction authorization data signature.
|
|
698
|
+
*
|
|
699
|
+
* @category Hook
|
|
700
|
+
*
|
|
701
|
+
* @example
|
|
702
|
+
*
|
|
703
|
+
* ### EIP-155 Transaction Wagmi Example
|
|
704
|
+
* ```tsx
|
|
705
|
+
* import { useGetTxAuthDataSignature } from "@compilot/react-sdk";
|
|
706
|
+
* import { waitForTransactionReceipt } from "viem/actions";
|
|
707
|
+
* import { encodeFunctionData } from "viem";
|
|
708
|
+
* import { usePublicClient, useWalletClient } from "wagmi";
|
|
709
|
+
*
|
|
710
|
+
* const MyComponent = () => {
|
|
711
|
+
*
|
|
712
|
+
* const { mutateAsync: getTxAuthDataSignature } = useGetTxAuthDataSignature();
|
|
713
|
+
* const walletClient = useWalletClient();
|
|
714
|
+
* const publicClient = usePublicClient();
|
|
715
|
+
*
|
|
716
|
+
* const gatedNftMint = async () => {
|
|
717
|
+
*
|
|
718
|
+
* // Get signature for the given parameters
|
|
719
|
+
* const signatureResponse = await getTxAuthDataSignature({
|
|
720
|
+
* namespace: "eip155",
|
|
721
|
+
* contractAbi,
|
|
722
|
+
* contractAddress,
|
|
723
|
+
* functionName: "mintNFTGated",
|
|
724
|
+
* args: [account.address],
|
|
725
|
+
* userAddress: account.address,
|
|
726
|
+
* chainId: EvmChainId.parse(chainId),
|
|
727
|
+
* });
|
|
728
|
+
*
|
|
729
|
+
* if (!signatureResponse.isAuthorized) {
|
|
730
|
+
* throw new Error("User is not authorized");
|
|
731
|
+
* }
|
|
732
|
+
*
|
|
733
|
+
* // Mint Gated Nft with signature
|
|
734
|
+
* const unsignedTx = encodeFunctionData({
|
|
735
|
+
* abi: contractAbi,
|
|
736
|
+
* functionName: "mintNFTGated",
|
|
737
|
+
* args: [account.address],
|
|
738
|
+
* });
|
|
739
|
+
*
|
|
740
|
+
* // Build the raw transaction data with the signature
|
|
741
|
+
* const txData = (unsignedTx + signatureResponse.payload) as `0x${string}`;
|
|
742
|
+
*
|
|
743
|
+
* // try to mint nft
|
|
744
|
+
* const tx = await walletClient.data.sendTransaction({
|
|
745
|
+
* to: contractAddress,
|
|
746
|
+
* data: txData,
|
|
747
|
+
* });
|
|
748
|
+
*
|
|
749
|
+
* const receipt = await waitForTransactionReceipt(publicClient, {
|
|
750
|
+
* hash: tx,
|
|
751
|
+
* });
|
|
752
|
+
*
|
|
753
|
+
* return receipt;
|
|
754
|
+
* };
|
|
755
|
+
*
|
|
756
|
+
* return <button onClick={gatedNftMint}>Mint Gated NFT</button>;
|
|
757
|
+
* };
|
|
758
|
+
* ```
|
|
759
|
+
*
|
|
760
|
+
* ### Tezos Transaction Example
|
|
761
|
+
*
|
|
762
|
+
* ```tsx
|
|
763
|
+
* import { useGetTxAuthDataSignature } from "@compilot/react-sdk";
|
|
764
|
+
* import { packDataBytes, Parser } from "@taquito/michel-codec";
|
|
765
|
+
* import type { MichelsonData, MichelsonType } from "@taquito/michel-codec";
|
|
766
|
+
* import { RpcClient } from "@taquito/rpc";
|
|
767
|
+
*
|
|
768
|
+
* const MyComponent = () => {
|
|
769
|
+
* const { mutateAsync: getTxAuthDataSignature } = useGetTxAuthDataSignature();
|
|
770
|
+
* const wallet = useWallet();
|
|
771
|
+
*
|
|
772
|
+
* const signAndSend = async () => {
|
|
773
|
+
*
|
|
774
|
+
* // prepare the mint function call
|
|
775
|
+
* const storage: any = await claimerContract.storage();
|
|
776
|
+
* const lastAssetId = storage.siggated_extension.extension.lastMinted.toNumber() as number;
|
|
777
|
+
* const functionCallArgs = {
|
|
778
|
+
* owner: userAddress,
|
|
779
|
+
* token_id: (lastAssetId + 1).toString(), //"1",
|
|
780
|
+
* };
|
|
781
|
+
* const functionCallArgsBytes = convertMint(
|
|
782
|
+
* functionCallArgs.owner,
|
|
783
|
+
* functionCallArgs.token_id,
|
|
784
|
+
* );
|
|
785
|
+
*
|
|
786
|
+
* // Get signature for the given parameters
|
|
787
|
+
* const signatureResponse = await getTxAuthDataSignature({
|
|
788
|
+
* namespace: "tezos",
|
|
789
|
+
* contractAddress: "KT1JN7a2es4Ne8SuePZU7YrHKG49hfgCCyBK",
|
|
790
|
+
* functionName: "%mint_gated%",
|
|
791
|
+
* args: functionCallArgsBytes,
|
|
792
|
+
* chainID: TezosChainId.parse(currentChainId),
|
|
793
|
+
* userAddress: userAddress as TezosImplicitAddress,
|
|
794
|
+
* });
|
|
795
|
+
*
|
|
796
|
+
* // Check if the user is authorized
|
|
797
|
+
* if (!signatureResponse.isAuthorized) {
|
|
798
|
+
* return {
|
|
799
|
+
* signatureResponse: {
|
|
800
|
+
* isAuthorized: false,
|
|
801
|
+
* signature: "None",
|
|
802
|
+
* },
|
|
803
|
+
* };
|
|
804
|
+
* }
|
|
805
|
+
*
|
|
806
|
+
* // Mint Gated Nft with signature
|
|
807
|
+
* const op = await claimerContract.methodsObject
|
|
808
|
+
* .mint_gated({
|
|
809
|
+
* userAddress,
|
|
810
|
+
* expirationBlock: signatureResponse.blockExpiration,
|
|
811
|
+
* functionName,
|
|
812
|
+
* functionArgs: functionCallArgsBytes,
|
|
813
|
+
* signerPublicKey: COMPILOT_SIGNER_PK,
|
|
814
|
+
* signature: signatureResponse.signature,
|
|
815
|
+
* })
|
|
816
|
+
* .send();
|
|
817
|
+
* await op.confirmation(2);
|
|
818
|
+
* };
|
|
819
|
+
* return <button onClick={signAndSend}>Sign and Send</button>;
|
|
820
|
+
* };
|
|
821
|
+
*
|
|
822
|
+
*
|
|
823
|
+
* // Helper function to convert mint function to bytes
|
|
824
|
+
* function convertMint(owner_str: string, token_id: string) {
|
|
825
|
+
* const data = `(Pair "${owner_str}" ${token_id})`;
|
|
826
|
+
* const type = `(pair address nat)`;
|
|
827
|
+
* const p = new Parser();
|
|
828
|
+
* const dataJSON = p.parseMichelineExpression(data);
|
|
829
|
+
* const typeJSON = p.parseMichelineExpression(type);
|
|
830
|
+
* const packed = packDataBytes(
|
|
831
|
+
* dataJSON as MichelsonData,
|
|
832
|
+
* typeJSON as MichelsonType,
|
|
833
|
+
* );
|
|
834
|
+
* return packed.bytes;
|
|
835
|
+
* }
|
|
836
|
+
*
|
|
837
|
+
* // Tezos signer public key
|
|
838
|
+
* const COMPILOT_SIGNER_PK = "edpkurPsQ8eUApnLUJ9ZPDvu98E8VNj4KtJa1aZr16Cr5ow5VHKnz4";
|
|
839
|
+
* const client = new RpcClient("https://rpc.ghostnet.teztnets.com/");
|
|
840
|
+
* ```
|
|
841
|
+
*
|
|
842
|
+
*/
|
|
843
|
+
var useGetTxAuthDataSignature = function useGetTxAuthDataSignature() {
|
|
844
|
+
var config = useComPilotConfig();
|
|
845
|
+
var mutationFn = useCallback(/*#__PURE__*/function () {
|
|
846
|
+
var _ref = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(params) {
|
|
847
|
+
var res;
|
|
848
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
849
|
+
while (1) switch (_context.prev = _context.next) {
|
|
850
|
+
case 0:
|
|
851
|
+
_context.next = 2;
|
|
852
|
+
return getTxAuthDataSignature(config, params);
|
|
853
|
+
case 2:
|
|
854
|
+
res = _context.sent;
|
|
855
|
+
return _context.abrupt("return", res);
|
|
856
|
+
case 4:
|
|
857
|
+
case "end":
|
|
858
|
+
return _context.stop();
|
|
859
|
+
}
|
|
860
|
+
}, _callee);
|
|
861
|
+
}));
|
|
862
|
+
return function (_x) {
|
|
863
|
+
return _ref.apply(this, arguments);
|
|
864
|
+
};
|
|
865
|
+
}(), [config._internal.widgetStateStore, config._internal.identifier]);
|
|
866
|
+
return useAsyncMutationState({
|
|
867
|
+
mutationFn: mutationFn
|
|
868
|
+
});
|
|
869
|
+
};
|
|
870
|
+
|
|
871
|
+
/**
|
|
872
|
+
* A hook that returns whether the user is authenticated.
|
|
873
|
+
*
|
|
874
|
+
* @returns Whether the user is authenticated or not.
|
|
875
|
+
*
|
|
876
|
+
* @category Hook
|
|
877
|
+
*/
|
|
878
|
+
var useIsAuthenticated = function useIsAuthenticated() {
|
|
879
|
+
var config = useComPilotConfig();
|
|
880
|
+
var _useAsyncQueryState = useAsyncQueryState(isAuthenticated(config)),
|
|
881
|
+
state = _useAsyncQueryState.state,
|
|
882
|
+
setResult = _useAsyncQueryState.setResult,
|
|
883
|
+
startLoading = _useAsyncQueryState.startLoading;
|
|
884
|
+
useEffect(function () {
|
|
885
|
+
var unsubscribe = watchIsAuthenticated(config, {
|
|
886
|
+
onIsAuthenticatedChange: function onIsAuthenticatedChange(isAuthenticated) {
|
|
887
|
+
setResult(isAuthenticated);
|
|
888
|
+
},
|
|
889
|
+
onIsLoadingChange: function onIsLoadingChange(isLoading) {
|
|
890
|
+
if (isLoading) {
|
|
891
|
+
startLoading();
|
|
892
|
+
}
|
|
893
|
+
}
|
|
894
|
+
});
|
|
895
|
+
return function () {
|
|
896
|
+
unsubscribe();
|
|
897
|
+
};
|
|
898
|
+
}, [config._internal.widgetStateStore, config._internal.identifier]);
|
|
899
|
+
return state;
|
|
900
|
+
};
|
|
901
|
+
|
|
902
|
+
/**
|
|
903
|
+
* The parameters of the {@link useCustomerStatus} hook.
|
|
904
|
+
*
|
|
905
|
+
* @category Parameter Types
|
|
906
|
+
*/
|
|
907
|
+
|
|
908
|
+
/**
|
|
909
|
+
* A hook that returns the current customer status.
|
|
910
|
+
*
|
|
911
|
+
* @param params {@link UseCustomerStatusParams}
|
|
912
|
+
*
|
|
913
|
+
* @returns The async query state of the customer status.
|
|
914
|
+
*
|
|
915
|
+
* @category Hook
|
|
916
|
+
*
|
|
917
|
+
* @example
|
|
918
|
+
* ```tsx
|
|
919
|
+
* import { useCustomerStatus } from "@compilot/react-sdk";
|
|
920
|
+
*
|
|
921
|
+
* const MyComponent = () => {
|
|
922
|
+
* const { data: status } = useCustomerStatus();
|
|
923
|
+
* const isVerified = status === "Active";
|
|
924
|
+
*
|
|
925
|
+
* return <div>User is verified: {isVerified}</div>;
|
|
926
|
+
* };
|
|
927
|
+
* ```
|
|
928
|
+
*
|
|
929
|
+
* Output:
|
|
930
|
+
* ```tsx
|
|
931
|
+
* <div>User is verified: true</div>
|
|
932
|
+
* ```
|
|
933
|
+
*/
|
|
934
|
+
var useCustomerStatus = function useCustomerStatus() {
|
|
935
|
+
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {
|
|
936
|
+
refreshInterval: 10000
|
|
937
|
+
},
|
|
938
|
+
refreshInterval = _ref.refreshInterval;
|
|
939
|
+
var config = useComPilotConfig();
|
|
940
|
+
var _useAsyncQueryState = useAsyncQueryState(),
|
|
941
|
+
state = _useAsyncQueryState.state,
|
|
942
|
+
setError = _useAsyncQueryState.setError,
|
|
943
|
+
setResult = _useAsyncQueryState.setResult,
|
|
944
|
+
startLoading = _useAsyncQueryState.startLoading;
|
|
945
|
+
useEffect(function () {
|
|
946
|
+
var unsubscribe = watchCustomerStatus(config, {
|
|
947
|
+
onChange: function onChange(status) {
|
|
948
|
+
setResult(status);
|
|
949
|
+
}
|
|
950
|
+
});
|
|
951
|
+
return function () {
|
|
952
|
+
unsubscribe();
|
|
953
|
+
};
|
|
954
|
+
}, [config._internal.externalEventEmitter, config._internal.identifier]);
|
|
955
|
+
useEffect(function () {
|
|
956
|
+
startLoading();
|
|
957
|
+
var interval = setInterval(function () {
|
|
958
|
+
getCustomerStatus(config).then(function (status) {
|
|
959
|
+
setResult(status);
|
|
960
|
+
})["catch"](function (error) {
|
|
961
|
+
setError(error instanceof Error ? error : new Error(String(error)));
|
|
962
|
+
});
|
|
963
|
+
}, refreshInterval);
|
|
964
|
+
return function () {
|
|
965
|
+
clearInterval(interval);
|
|
966
|
+
};
|
|
967
|
+
}, [config._internal.identifier, refreshInterval, config, setError, setResult, startLoading]);
|
|
968
|
+
return state;
|
|
969
|
+
};
|
|
970
|
+
|
|
971
|
+
/**
|
|
972
|
+
* A hook that returns a function that disconnects the ComPilot SDK.
|
|
973
|
+
*
|
|
974
|
+
* @returns The async mutation state of the disconnect function.
|
|
975
|
+
*
|
|
976
|
+
* @category Hook
|
|
977
|
+
*
|
|
978
|
+
* @example
|
|
979
|
+
* ```tsx
|
|
980
|
+
* import { useDisconnect } from "@compilot/react-sdk";
|
|
981
|
+
*
|
|
982
|
+
* const Logout = () => {
|
|
983
|
+
* const { mutateAsync: disconnect, isLoading } = useDisconnect();
|
|
984
|
+
*
|
|
985
|
+
* return (
|
|
986
|
+
* <button
|
|
987
|
+
* disabled={isLoading}
|
|
988
|
+
* onClick={disconnect}
|
|
989
|
+
* >
|
|
990
|
+
* Logout
|
|
991
|
+
* </button>;
|
|
992
|
+
* );
|
|
993
|
+
* };
|
|
994
|
+
* ```
|
|
995
|
+
*/
|
|
996
|
+
var useDisconnect = function useDisconnect() {
|
|
997
|
+
var config = useComPilotConfig();
|
|
998
|
+
var mutationFn = useCallback(function () {
|
|
999
|
+
return disconnect(config);
|
|
1000
|
+
}, [config]);
|
|
1001
|
+
return useAsyncMutationState({
|
|
1002
|
+
mutationFn: mutationFn
|
|
1003
|
+
});
|
|
1004
|
+
};
|
|
1005
|
+
|
|
1006
|
+
export { ComPilotProvider, useComPilotConfig, useCustomerStatus, useDisconnect, useGetTxAuthDataSignature, useIsAuthenticated, useOpenWidget };
|