@clicktap/state 0.1.1 → 0.1.3

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/index.js ADDED
@@ -0,0 +1,729 @@
1
+ import _regeneratorRuntime from '@babel/runtime/regenerator';
2
+ import { createMachine, interpret, sendTo } from 'xstate';
3
+ import { assign } from '@xstate/immer';
4
+ import { jsx } from 'react/jsx-runtime';
5
+ import { createContext, useContext, cloneElement } from 'react';
6
+ import cookie from 'cookie';
7
+ import crypto from 'crypto';
8
+
9
+ /******************************************************************************
10
+ Copyright (c) Microsoft Corporation.
11
+
12
+ Permission to use, copy, modify, and/or distribute this software for any
13
+ purpose with or without fee is hereby granted.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
16
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
17
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
18
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
19
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
20
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
21
+ PERFORMANCE OF THIS SOFTWARE.
22
+ ***************************************************************************** */
23
+
24
+ function __awaiter(thisArg, _arguments, P, generator) {
25
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
26
+ return new (P || (P = Promise))(function (resolve, reject) {
27
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
28
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
29
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
30
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
31
+ });
32
+ }
33
+
34
+ function request(url, options) {
35
+ return __awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
36
+ var response;
37
+ return _regeneratorRuntime.wrap(function _callee$(_context) {
38
+ while (1) {
39
+ switch (_context.prev = _context.next) {
40
+ case 0:
41
+ _context.next = 2;
42
+ return fetch(url, options);
43
+
44
+ case 2:
45
+ response = _context.sent;
46
+
47
+ if (!(response.status === 200)) {
48
+ _context.next = 5;
49
+ break;
50
+ }
51
+
52
+ return _context.abrupt("return", response.json());
53
+
54
+ case 5:
55
+ return _context.abrupt("return", Promise.resolve({
56
+ message: response.statusText,
57
+ success: false
58
+ }));
59
+
60
+ case 6:
61
+ case "end":
62
+ return _context.stop();
63
+ }
64
+ }
65
+ }, _callee);
66
+ }));
67
+ }
68
+
69
+ var authMachine =
70
+ /** @xstate-layout N4IgpgJg5mDOIC5QEMCuAXAFgOjVsAdugJYDGyJBUAxBAPYFjbEEBudA1k3jj4SeUpQELdoOIMA2gAYAujNmJQABzqxiJBkpAAPRAEZ9Adn3YAbABYATAGYr1gKwWLDoxYA0IAJ6JbATmwADkCXPyszK0CzPyijAF84zx5cDEx+MgoWGjAAJxy6HOxlABsKADMCgFsUrBq0ogyhETY6cSk5BW1VdU0CbT0EfWkncysHYelba0DXTx8EBzNTCxDXPzMbaTCjQISk1OxUAj4G8SpaBiZRTm4Do5OBTKpmsUz2+TkutQ0JPqRdRAAWkMAUiNkCxjMO02+gsNjmvkCVmw+hsRnRjiM0hMexAyXuqXSZ2yeQKRVK6AqOWq+OOhNOT2E1zaBAUnX+3R+Wn+A2R0RsmwhRjMUKsRgcSIRCCR2Fs6MCYSmDn0wVxyRyYDKGtgmCyF0YzBaXDq2A1WrguuezLerI6nw5316-UQRhi2BiQ2kk2VkylUSCfnB9iWwSxYrVBzN2stJPyhRK5SqJqjFqyL1aNrZ9pUjt+zoQFn0Zmw0gs6Ks0n0gZMMSlwOx2ELTj80jMTisYvCEdqxToUBgEAA8hhqAAZQcAcQAkgA5dk5np5nm+KElsyBcWWQzhMZSizSQLYVzbhyB8F+Pz6bs4Xv9yBTghjyeDgCqABV5yBOU7l9KjDYghmQJhgVPwHArKw6yrYswKWf8HE2cYJWvbBbwHB9qB0WB0AobgynQXIAAozC9L0AEpqGSND71tRQHUXblQAGKJkVdZwqwlMIokCKCjGwYUYThf8SLRCwEkSEACDoCA4G0HgvgYv4mKBIs+LBIUoUCQT4W8IFIgsRtFmMMskMvKwUIeRosgUrklIBQZKwA1soWccJXU2PwpVEktjBmdcIhiZwUIJfAGSEGyf2UhBAQM0N1gPJYoVbHYoJCHyTCiLYYnXcMJPVTVo2s+jbPzawAkMSZwmxBUJX0KUxUPOV3LYy8tjEvKDmoocMAipcopsAJTzRNY0VhWEpVcaQSy0sVxjGFs0RQrqH16xj7MMOFGxWRYBXBRYeN0hA3XsINnBCfcLyvcSgA */
71
+ createMachine({
72
+ context: {
73
+ // id: '',
74
+ user: null,
75
+ accessToken: '',
76
+ refreshToken: '',
77
+
78
+ /**
79
+ * If unauthenticating fails, we need a way to ignore the refresh token on subsequent access token refresh.
80
+ * This would be set to true done/error of unauthenticate service, and false on done of authenticate service
81
+ */
82
+ ignoreRefreshToken: false,
83
+ endpoints: {
84
+ login: '',
85
+ logout: '',
86
+ refresh: '',
87
+ ssrRefresh: ''
88
+ }
89
+ },
90
+ predictableActionArguments: true,
91
+ id: 'auth',
92
+ initial: 'refreshing',
93
+ states: {
94
+ authenticating: {
95
+ invoke: {
96
+ src: 'authenticate',
97
+ onDone: [{
98
+ actions: 'setUserContext',
99
+ target: 'loggedIn'
100
+ }],
101
+ onError: [{
102
+ target: 'loggedOut'
103
+ }]
104
+ }
105
+ },
106
+ unauthenticating: {
107
+ invoke: {
108
+ src: 'unauthenticate',
109
+ onDone: [{
110
+ actions: 'unsetUserContext',
111
+ target: 'loggedOut'
112
+ }],
113
+ onError: [{
114
+ actions: ['unsetUserContext', 'setIgnoreRefreshToken'],
115
+ target: 'loggedOut'
116
+ }]
117
+ }
118
+ },
119
+ refreshing: {
120
+ invoke: {
121
+ src: 'refreshAccessToken',
122
+ onDone: [{
123
+ actions: ['unsetRefreshToken', 'setAccessToken'],
124
+ target: 'loggedIn'
125
+ }],
126
+ onError: [{
127
+ actions: 'unsetRefreshToken',
128
+ target: 'unauthenticating'
129
+ }]
130
+ }
131
+ },
132
+ loggedOut: {
133
+ on: {
134
+ LOGIN: {
135
+ target: 'authenticating'
136
+ }
137
+ }
138
+ },
139
+ loggedIn: {
140
+ after: {
141
+ '60000': {
142
+ cond: function cond() {
143
+ return typeof window !== 'undefined';
144
+ },
145
+ target: 'refreshing'
146
+ }
147
+ },
148
+ // entry: ['unsetUserContext'],
149
+ on: {
150
+ LOGOUT: {
151
+ target: 'unauthenticating'
152
+ }
153
+ }
154
+ }
155
+ }
156
+ }, {
157
+ actions: {
158
+ // setAccessToken: assign<UserContext, RefreshTokenEvent>((context, event) => {
159
+ // context.accessToken = event.accessToken;
160
+ // }),
161
+ setUserContext: assign(function (context, event) {
162
+ // event.type - done.invoke.auth.authenticating:invocation[0]
163
+ if (event.data.type !== 'AUTHENTICATE_SUCCESS') return; // context.user = event.data.user;
164
+
165
+ context.accessToken = event.data.accessToken;
166
+ context.ignoreRefreshToken = false;
167
+ }),
168
+ unsetUserContext: assign(function (context) {
169
+ var endpoints = context.endpoints;
170
+ Object.assign(context, authMachine.initialState.context);
171
+ context.endpoints = endpoints; // maintain endpoints passed through DI when machine created
172
+ // context = authMachine.initialState.context;
173
+ }),
174
+ setAccessToken: assign(function (context, event) {
175
+ if (event.data.type !== 'REFRESH_TOKEN_SUCCESS') return; // is this really necessary?
176
+
177
+ context.accessToken = event.data.accessToken;
178
+ context.ignoreRefreshToken = false;
179
+ }),
180
+ unsetRefreshToken: assign(function (context, event) {
181
+ if (event.data.type !== 'REFRESH_TOKEN_SUCCESS') return; // is this really necessary?
182
+
183
+ context.refreshToken = '';
184
+ }),
185
+ setIgnoreRefreshToken: assign(function (context) {
186
+ context.ignoreRefreshToken = true;
187
+ })
188
+ },
189
+ services: {
190
+ refreshAccessToken: function refreshAccessToken(context) {
191
+ return function () {
192
+ return __awaiter(void 0, void 0, void 0, /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
193
+ var response, data, _data;
194
+
195
+ return _regeneratorRuntime.wrap(function _callee2$(_context2) {
196
+ while (1) {
197
+ switch (_context2.prev = _context2.next) {
198
+ case 0:
199
+ if (!(typeof window === 'undefined')) {
200
+ _context2.next = 18;
201
+ break;
202
+ }
203
+
204
+ if (!(context.refreshToken === '')) {
205
+ _context2.next = 3;
206
+ break;
207
+ }
208
+
209
+ throw new Error('Unauthorized.');
210
+
211
+ case 3:
212
+ _context2.prev = 3;
213
+ _context2.next = 6;
214
+ return request(context.endpoints.ssrRefresh, {
215
+ method: 'POST',
216
+ mode: 'cors',
217
+ credentials: 'include',
218
+ headers: {
219
+ Cookie: "refresh_token=".concat(context.refreshToken),
220
+ 'Content-Type': 'application/json'
221
+ },
222
+ body: JSON.stringify({
223
+ grant_type: 'refresh_token' // client_id: 'default',
224
+ // client_secret: 'Password123!',
225
+ // scope: 'default',
226
+
227
+ }).toString()
228
+ });
229
+
230
+ case 6:
231
+ response = _context2.sent;
232
+ data = response;
233
+
234
+ if (!(typeof data.accessToken === 'undefined')) {
235
+ _context2.next = 10;
236
+ break;
237
+ }
238
+
239
+ throw new Error('Unauthorized.');
240
+
241
+ case 10:
242
+ return _context2.abrupt("return", {
243
+ type: 'REFRESH_TOKEN_SUCCESS',
244
+ accessToken: data.accessToken
245
+ });
246
+
247
+ case 13:
248
+ _context2.prev = 13;
249
+ _context2.t0 = _context2["catch"](3);
250
+ throw new Error('Could not complete refresh request (server)');
251
+
252
+ case 16:
253
+ _context2.next = 25;
254
+ break;
255
+
256
+ case 18:
257
+ _context2.next = 20;
258
+ return request(context.endpoints.refresh, {
259
+ method: 'POST',
260
+ mode: 'cors',
261
+ credentials: 'include',
262
+ headers: {
263
+ 'Content-Type': 'application/json'
264
+ },
265
+ body: JSON.stringify({
266
+ grant_type: 'refresh_token' // client_id: 'default',
267
+ // client_secret: 'Password123!',
268
+ // scope: 'default',
269
+
270
+ }).toString()
271
+ });
272
+
273
+ case 20:
274
+ response = _context2.sent;
275
+ _data = response;
276
+
277
+ if (!(typeof _data.accessToken === 'undefined')) {
278
+ _context2.next = 24;
279
+ break;
280
+ }
281
+
282
+ throw new Error('Unauthorized.');
283
+
284
+ case 24:
285
+ return _context2.abrupt("return", {
286
+ type: 'REFRESH_TOKEN_SUCCESS',
287
+ accessToken: _data.accessToken
288
+ });
289
+
290
+ case 25:
291
+ case "end":
292
+ return _context2.stop();
293
+ }
294
+ }
295
+ }, _callee2, null, [[3, 13]]);
296
+ }));
297
+ };
298
+ },
299
+ authenticate: function authenticate(context, event) {
300
+ return function () {
301
+ return __awaiter(void 0, void 0, void 0, /*#__PURE__*/_regeneratorRuntime.mark(function _callee3() {
302
+ var _a, _b, e, response, data;
303
+
304
+ return _regeneratorRuntime.wrap(function _callee3$(_context3) {
305
+ while (1) {
306
+ switch (_context3.prev = _context3.next) {
307
+ case 0:
308
+ e = event; // workaround to typescript not allowing casting in parameter
309
+
310
+ if (!(e.type !== 'LOGIN')) {
311
+ _context3.next = 3;
312
+ break;
313
+ }
314
+
315
+ throw new Error("Authenticate can only be called for \"LOGIN\" event type. Event caller: ".concat(String(e.type)));
316
+
317
+ case 3:
318
+ _context3.next = 5;
319
+ return request(context.endpoints.login, {
320
+ method: 'POST',
321
+ mode: 'cors',
322
+ credentials: 'include',
323
+ headers: {
324
+ 'Content-Type': 'application/json'
325
+ },
326
+ body: JSON.stringify({
327
+ username: e.username,
328
+ password: e.password // grant_type: 'password',
329
+ // client_id: 'default',
330
+ // client_secret: 'Password123!',
331
+ // scope: 'default',
332
+
333
+ }).toString()
334
+ });
335
+
336
+ case 5:
337
+ response = _context3.sent;
338
+
339
+ /** @todo do we need to handle different status codes here...like what if the service is down? */
340
+ data = response;
341
+
342
+ if (!(typeof data.accessToken === 'undefined')) {
343
+ _context3.next = 9;
344
+ break;
345
+ }
346
+
347
+ throw new Error((_b = (_a = data.message) === null || _a === void 0 ? void 0 : _a.toString()) !== null && _b !== void 0 ? _b : 'Sign in failed. Please try again.');
348
+
349
+ case 9:
350
+ return _context3.abrupt("return", {
351
+ type: 'AUTHENTICATE_SUCCESS',
352
+ // user: data.user,
353
+ accessToken: data.accessToken
354
+ });
355
+
356
+ case 10:
357
+ case "end":
358
+ return _context3.stop();
359
+ }
360
+ }
361
+ }, _callee3);
362
+ }));
363
+ };
364
+ },
365
+ unauthenticate: function unauthenticate(context) {
366
+ return function () {
367
+ return __awaiter(void 0, void 0, void 0, /*#__PURE__*/_regeneratorRuntime.mark(function _callee4() {
368
+ return _regeneratorRuntime.wrap(function _callee4$(_context4) {
369
+ while (1) {
370
+ switch (_context4.prev = _context4.next) {
371
+ case 0:
372
+ _context4.next = 2;
373
+ return fetch(context.endpoints.logout, {
374
+ method: 'POST',
375
+ mode: 'cors',
376
+ credentials: 'include' // headers: {
377
+ // 'Access-Control-Allow-Origin': 'https://middleware-clicktap.local-rmgmedia.com',
378
+ // 'Access-Control-Allow-Credentials': 'true',
379
+ // },
380
+
381
+ });
382
+
383
+ case 2:
384
+ return _context4.abrupt("return", {
385
+ type: 'UNAUTHENTICATE'
386
+ });
387
+
388
+ case 3:
389
+ case "end":
390
+ return _context4.stop();
391
+ }
392
+ }
393
+ }, _callee4);
394
+ }));
395
+ };
396
+ }
397
+ } // doesn't make sense in the case where you have an access token that is about to expire
398
+ // and the loggedIn state won't fire another refresh for 15 min
399
+ //
400
+ // guards: {
401
+ // checkIfLoggedIn: (context, _event) => {
402
+ // if (context.user) return true;
403
+ // },
404
+ // },
405
+
406
+ });
407
+
408
+ // import type { NextPageContext } from 'next';
409
+ // import { useRouter } from 'next/router';
410
+
411
+ var AuthContext = /*#__PURE__*/createContext({});
412
+ var useAuth = function useAuth() {
413
+ // console.log('useAuth');
414
+ // console.log(useContext(AuthContext).getSnapshot().context);
415
+ return useContext(AuthContext);
416
+ };
417
+ var useUser = function useUser() {
418
+ return useContext(AuthContext).getSnapshot().context.user;
419
+ };
420
+ /**
421
+ * this will run on the server side as part of app.getInitialProps
422
+ * @todo is it possible to share state from client to server? headers? cookies?
423
+ */
424
+
425
+ var getAuth = function getAuth(context, options // eslint-disable-next-line @typescript-eslint/require-await
426
+ ) {
427
+ return __awaiter(void 0, void 0, void 0, /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
428
+ var _a, authContext, cookies, authContextWithRefresh;
429
+
430
+ return _regeneratorRuntime.wrap(function _callee$(_context) {
431
+ while (1) {
432
+ switch (_context.prev = _context.next) {
433
+ case 0:
434
+ authContext = Object.assign(Object.assign({}, authMachine.initialState.context), {
435
+ endpoints: Object.assign({}, options.endpoints)
436
+ });
437
+
438
+ if (!(typeof window !== 'undefined')) {
439
+ _context.next = 4;
440
+ break;
441
+ }
442
+
443
+ // eslint-disable-next-line no-console
444
+ console.warn('App.getInitialProps::getAuth should not be run on the frontend. You are probably missing getServerSideProps in your page.');
445
+ return _context.abrupt("return", interpret(authMachine.withContext(authContext), {
446
+ devTools: options.devTools
447
+ }).start());
448
+
449
+ case 4:
450
+ cookies = cookie.parse(((_a = context === null || context === void 0 ? void 0 : context.req) === null || _a === void 0 ? void 0 : _a.headers) && context.req.headers.cookie ? context.req.headers.cookie : '');
451
+ authContextWithRefresh = Object.assign(authContext, {
452
+ refreshToken: cookies['refresh_token']
453
+ });
454
+
455
+ if (!(typeof cookies['refresh_token'] !== 'undefined')) {
456
+ _context.next = 8;
457
+ break;
458
+ }
459
+
460
+ return _context.abrupt("return", interpret(authMachine.withContext(authContextWithRefresh), {
461
+ devTools: options.devTools
462
+ }).start());
463
+
464
+ case 8:
465
+ return _context.abrupt("return", interpret(authMachine.withContext(authContext), {
466
+ devTools: options.devTools
467
+ }).start());
468
+
469
+ case 9:
470
+ case "end":
471
+ return _context.stop();
472
+ }
473
+ }
474
+ }, _callee);
475
+ }));
476
+ };
477
+ function AuthProvider(_ref) {
478
+ var children = _ref.children,
479
+ service = _ref.service;
480
+ // console.log('AuthProvider::state');
481
+ // console.log(state.context);
482
+ // let authService = interpret(authMachine, { devTools: options.devTools }).start(state);
483
+ // console.log('AuthProvider::authService');
484
+ // console.log(authService.getSnapshot().context);
485
+ return jsx(AuthContext.Provider, Object.assign({
486
+ value: service
487
+ }, {
488
+ children: children
489
+ }));
490
+ }
491
+
492
+ var timerMachine =
493
+ /** @xstate-layout N4IgpgJg5mDOIC5QBcCWBbMAnAdABwEMBXWSAYgCUBRAZQFUBZKgfQBUBJJigbQAYBdRKDwB7WKjQiAdkJAAPRAFoAjADYATDmUBOXQA5tAZmV69AFgDsq1QBoQAT0SHehnAFYAvh7tpMuLERSUqhSUGQcAMIA0nyCSCCi4pIy8QoIJqo4qnoWbnaOCGZmmaq8Zbyqyi5uym6qXj4Y2DgBQSFhAAoAgnQ0LBxcsbKJEqjSsmlVbjimFmZ6agZmbsbq+U4u7g0gvs2twaFkQ-EjyRNObrxZOXkOiGbKFjjq5WbqFurFvNpmXt4gUhEEDgsl2WGGYlG41SSnUVS0um0BmMpks1nW6Tcmk8-zB+GIpAgEKSYxSoDSvAxn142zx+3axKhZPkiEpdwQhjmW1xTVwAGMROg8AAbMDISCMs4whBsgpuOp-DxAA */
494
+ createMachine({
495
+ id: 'timer',
496
+ initial: 'running',
497
+ context: {
498
+ elapsed: 0,
499
+ duration: 3000,
500
+ interval: 100,
501
+ intervalId: null
502
+ },
503
+ states: {
504
+ paused: {
505
+ on: {
506
+ // START_TIMER: {
507
+ // target: 'running',
508
+ // cond: (context) => context.elapsed < context.duration,
509
+ // },
510
+ RESUME_TIMER: {
511
+ target: 'running',
512
+ cond: function cond(context) {
513
+ return context.elapsed < context.duration;
514
+ }
515
+ }
516
+ }
517
+ },
518
+ running: {
519
+ invoke: {
520
+ src: function src(context) {
521
+ return function (send) {
522
+ // eslint-disable-next-line no-console
523
+ // console.log('context.interval: ', context.interval);
524
+ var interval = setInterval(function () {
525
+ send('TICK');
526
+ }, context.interval);
527
+ return function () {
528
+ clearInterval(interval);
529
+ };
530
+ };
531
+ }
532
+ },
533
+ always: [{
534
+ target: 'completed',
535
+ cond: function cond(context) {
536
+ return context.elapsed >= context.duration;
537
+ }
538
+ }],
539
+ on: {
540
+ TICK: {
541
+ actions: assign(function (context, event) {
542
+ if (event.type !== 'TICK') return;
543
+ context.elapsed += context.interval; // eslint-disable-next-line no-console
544
+ // console.log(
545
+ // 'elapsed: %d | interval: %d',
546
+ // context.elapsed,
547
+ // context.interval
548
+ // );
549
+ })
550
+ },
551
+ PAUSE_TIMER: {
552
+ target: 'paused'
553
+ }
554
+ }
555
+ },
556
+ completed: {
557
+ type: 'final'
558
+ }
559
+ }
560
+ });
561
+
562
+ var toastMachine =
563
+ /** @xstate-layout N4IgpgJg5mDOIC5QBcD2BDWyB0BLCANmAMQCCAIuQPoCSAKgKICyA2gAwC6ioADqrLmS5UAO24gAHogCMAZgCs2AGzS2SgBxKA7AE4lO2ToAsAGhABPRAFoj6gL52zaTDnQBjIQDcSEUWDwinqgA1v5CALZgAE7sXEggfAJCouJSCEpsOspaOQay0kqyAExKZpYIReps2LY6mmxG+kUKWkYOThhY2O5eJBTU9Myx4omCwmLxabKF2NKN8kpFbPls8gpl1vLqykXyasaNhdJFWu0gzl09uN7EAErMAPIAagy0jKycI-xjKZOIi9sDPI9EZgYV1LItBsEIYlNg9NIdGxKki1jkzhdXB5riQAMoMOi0cgAGQYw3io2SE1AaTkWS0RQOSKWEK0c2hzWkNUWJy0Skah1sGM6WN6dwYuIAqkxXnQaDLbuTeN8qakZPIuXylhktAppgUitDilpsJpQeo9KsVOoCsKXN1sTcAAqkSX4qhyhVKhIq8ZqirSbaZeTyLRbVnqVqyaHSaQm45mtirKo6Eo6O2XCAQXAiKDEfGEmgksmfCm+340xC2bCh3XGrTLfR8+TQqxFJbYTIoow6HStBshjOuLM5vMFqikADCcpe3spfr+MKMmsZbDmFvU6jBRtW8I3IcMBT7Q+wUTA4VQnlH+YJRNJc-L1MkiHURiKps0DIWKhyIZjehqfc1h0I8GyUE8zwvK9cxvQkpxnEs4mVJIF0rBBU1kWZVw1WtgSMUwLHVIxsEqIxjVDIF5CFM4RFQCA4HETEvhQitnwQKxYxNOoDFjbQ2RtfRW1kfJO1THjA1sVRZBPfAiGYn4nzSKx9D3cS+MDI9-zhWot2A0C1BPK5vHk1VF1kIwuOZPICmKYEtMAuoDxAptlkMkdcxM1C2MMYjKhKXUlH5DUimkVsijImsxMMcLpjDUMIPPS9R081i0i3OE10afI42XcKW0ImFgRrYE5FBWx1F2cyHAcIA */
564
+ createMachine({
565
+ context: {
566
+ items: [],
567
+ order: 'desc',
568
+ duration: 0,
569
+ activeItem: null
570
+ },
571
+ predictableActionArguments: true,
572
+ id: 'toast',
573
+ initial: 'idle',
574
+ states: {
575
+ idle: {
576
+ on: {
577
+ ADD_ITEM: {
578
+ target: 'adding'
579
+ }
580
+ }
581
+ },
582
+ active: {
583
+ invoke: {
584
+ id: 'timer',
585
+ src: timerMachine,
586
+ onDone: {
587
+ target: 'removing',
588
+ cond: 'itemHasTimeout'
589
+ },
590
+ data: Object.assign(timerMachine.context, {
591
+ duration: function duration(context) {
592
+ var _a; // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-return
593
+
594
+
595
+ return (_a = context.items[context.items.length - 1].duration) !== null && _a !== void 0 ? _a : context.duration;
596
+ }
597
+ })
598
+ },
599
+ on: {
600
+ ADD_ITEM: {
601
+ // actions: sendTo('timer', 'PAUSE_TIMER'),
602
+ target: 'adding'
603
+ },
604
+ REMOVE_ITEM: {
605
+ target: 'removing'
606
+ },
607
+ SET_IDLE: {
608
+ target: 'idle'
609
+ },
610
+ RESUME_TIMER: {
611
+ actions: sendTo('timer', 'RESUME_TIMER') // cond: (context) => context.duration > 0,
612
+
613
+ },
614
+ PAUSE_TIMER: {
615
+ actions: sendTo('timer', 'PAUSE_TIMER') // cond: (context) => context.duration > 0,
616
+
617
+ }
618
+ } // after: {
619
+ // ITEM_TIMEOUT: {
620
+ // target: 'removing',
621
+ // cond: 'itemHasTimeout',
622
+ // },
623
+ // },
624
+
625
+ },
626
+ adding: {
627
+ entry: ['addItem'],
628
+ on: {
629
+ SET_IDLE: {
630
+ target: 'idle'
631
+ },
632
+ SET_ACTIVE: {
633
+ target: 'active'
634
+ }
635
+ }
636
+ },
637
+ removing: {
638
+ entry: 'removeItem',
639
+ on: {
640
+ SET_IDLE: {
641
+ target: 'idle'
642
+ },
643
+ SET_ACTIVE: {
644
+ target: 'active'
645
+ }
646
+ }
647
+ }
648
+ }
649
+ }, {
650
+ actions: {
651
+ addItem: assign(function (context, event) {
652
+ var _a, _b;
653
+
654
+ if (event.type !== 'ADD_ITEM') return;
655
+ var id = "notification-".concat(crypto.randomBytes(16).toString('hex'));
656
+ var duration = (_a = event.duration) !== null && _a !== void 0 ? _a : context.duration; // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
657
+
658
+ var notification = /*#__PURE__*/cloneElement(event.item, Object.assign({
659
+ duration: duration,
660
+ id: id,
661
+ key: (_b = event.item.key) !== null && _b !== void 0 ? _b : id
662
+ }, event.item.props));
663
+ var item = {
664
+ duration: duration,
665
+ id: id,
666
+ element: notification
667
+ };
668
+
669
+ if (context.order === 'desc') {
670
+ context.items.push(item);
671
+ } else {
672
+ context.items.unshift(item);
673
+ }
674
+
675
+ context.activeItem = id;
676
+ }),
677
+ removeItem: assign(function (context, event) {
678
+ if (event.type !== 'REMOVE_ITEM' && event.type !== 'xstate.after(ITEM_TIMEOUT)#toast.active' && event.type !== 'done.invoke.timer') return;
679
+
680
+ if (context.order === 'desc') {
681
+ context.items.pop();
682
+ context.activeItem = context.items.length > 0 ? context.items[context.items.length - 1].id : null;
683
+ } else {
684
+ context.items.shift();
685
+ context.activeItem = context.items.length > 0 ? context.items[0].id : null;
686
+ }
687
+ }) // startTimer: sendTo('timer', { type: 'START' }),
688
+
689
+ },
690
+ delays: {
691
+ ITEM_TIMEOUT: function ITEM_TIMEOUT(context) {
692
+ var _a; // eslint-disable-next-line @typescript-eslint/no-unsafe-return
693
+
694
+
695
+ return (// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
696
+ (_a = context.items[context.items.length - 1].duration) !== null && _a !== void 0 ? _a : context.duration
697
+ );
698
+ }
699
+ },
700
+ guards: {
701
+ itemHasTimeout: function itemHasTimeout(context) {
702
+ return (// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
703
+ context.items[context.items.length - 1].duration > 0 || context.duration > 0
704
+ );
705
+ }
706
+ }
707
+ });
708
+
709
+ var ToastContext = /*#__PURE__*/createContext({});
710
+ var useToast = function useToast() {
711
+ return useContext(ToastContext);
712
+ };
713
+ function ToastProvider(_ref) {
714
+ var children = _ref.children,
715
+ service = _ref.service;
716
+ // const toastService = interpret(toastMachine, {
717
+ // devTools: options.devTools,
718
+ // }).start(state);
719
+ // toastService.subscribe((s) => {
720
+ // setState(s);
721
+ // });
722
+ return jsx(ToastContext.Provider, Object.assign({
723
+ value: service
724
+ }, {
725
+ children: children
726
+ }));
727
+ }
728
+
729
+ export { AuthContext, AuthProvider, ToastContext, ToastProvider, authMachine, getAuth, timerMachine, toastMachine, useAuth, useToast, useUser };