analytica.click 0.0.236 → 0.0.241

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -130,6 +130,24 @@ declare module 'analytica.click/ui-npm/src/components/Feedback/index' {
130
130
  import { IFeedbackWidget } from 'analytica.click/common-ui/src/types/index';
131
131
  export const Feedback: (props: IFeedbackWidget) => JSX.Element;
132
132
 
133
+ }
134
+ declare module 'analytica.click/ui-npm/src/components/index' {
135
+ export * from 'analytica.click/ui-npm/src/components/AnalyticaConfig/index';
136
+ export * from 'analytica.click/ui-npm/src/components/ErrorBoundary/index';
137
+ export * from 'analytica.click/ui-npm/src/components/ExternalComponent/index';
138
+ export * from 'analytica.click/ui-npm/src/components/Feedback/index';
139
+ export * from 'analytica.click/ui-npm/src/components/UserProvider/index';
140
+ export { DashboardAuthValidation } from 'analytica.click/ui-npm/src/components/UserProvider/DashboardAuthValidation';
141
+
142
+ }
143
+ declare module 'analytica.click/ui-npm/src/components/UserProvider/cognito' {
144
+ import { ICognitoAuth } from 'ag-common/dist/ui/helpers/cognito';
145
+ import { User, AwsCreds } from 'ag-common/dist/ui/helpers/jwt';
146
+ export function getAwsCreds({ user, config, }: {
147
+ user?: User;
148
+ config: ICognitoAuth;
149
+ }): Promise<AwsCreds | undefined>;
150
+
133
151
  }
134
152
  declare module 'analytica.click/ui-npm/src/components/UserProvider/DashboardAuthValidation' {
135
153
  /// <reference types="react" />
@@ -160,15 +178,6 @@ declare module 'analytica.click/ui-npm/src/components/UserProvider/DashboardAuth
160
178
  openApiDisabled: boolean;
161
179
  };
162
180
 
163
- }
164
- declare module 'analytica.click/ui-npm/src/components/UserProvider/cognito' {
165
- import { ICognitoAuth } from 'ag-common/dist/ui/helpers/cognito';
166
- import { User, AwsCreds } from 'ag-common/dist/ui/helpers/jwt';
167
- export function getAwsCreds({ user, config, }: {
168
- user?: User;
169
- config: ICognitoAuth;
170
- }): Promise<AwsCreds | undefined>;
171
-
172
181
  }
173
182
  declare module 'analytica.click/ui-npm/src/components/UserProvider/getTokensFromCode' {
174
183
  import { User } from 'ag-common/dist/ui/helpers/jwt';
@@ -217,13 +226,12 @@ declare module 'analytica.click/ui-npm/src/components/UserProvider/refreshToken'
217
226
  }): Promise<User | undefined>;
218
227
 
219
228
  }
220
- declare module 'analytica.click/ui-npm/src/components/index' {
221
- export * from 'analytica.click/ui-npm/src/components/AnalyticaConfig/index';
222
- export * from 'analytica.click/ui-npm/src/components/ErrorBoundary/index';
223
- export * from 'analytica.click/ui-npm/src/components/ExternalComponent/index';
224
- export * from 'analytica.click/ui-npm/src/components/Feedback/index';
225
- export * from 'analytica.click/ui-npm/src/components/UserProvider/index';
226
- export { DashboardAuthValidation } from 'analytica.click/ui-npm/src/components/UserProvider/DashboardAuthValidation';
229
+ declare module 'analytica.click/ui-npm/src/components/UserProvider/useGranularHook' {
230
+ import { DependencyList, EffectCallback } from 'react';
231
+ type HookWithDependencies<C, R> = (callback: C, deps: DependencyList) => R;
232
+ export const useGranularHook: <T extends HookWithDependencies<C, ReturnType<T>>, C>(hook: T, callback: C, primaryDeps: DependencyList, secondaryDeps: DependencyList) => ReturnType<T>;
233
+ export const useGranularEffect: (effect: EffectCallback, primaryDeps: DependencyList, secondaryDeps: DependencyList) => void;
234
+ export {};
227
235
 
228
236
  }
229
237
  declare module 'analytica.click/ui-npm/src/helpers/errorTrack' {
package/dist/index.js CHANGED
@@ -1310,13 +1310,45 @@ function getTokensFromCode(_0) {
1310
1310
  });
1311
1311
  }
1312
1312
 
1313
+ // src/components/UserProvider/useGranularHook.ts
1314
+ var import_react7 = require("react");
1315
+ var useGranularHook = (hook, callback, primaryDeps, secondaryDeps) => {
1316
+ const ref = (0, import_react7.useRef)();
1317
+ if (!ref.current || !primaryDeps.every((w, i) => {
1318
+ var _a;
1319
+ return Object.is(w, (_a = ref.current) == null ? void 0 : _a[i]);
1320
+ }))
1321
+ ref.current = [...primaryDeps, ...secondaryDeps];
1322
+ return hook(callback, ref.current);
1323
+ };
1324
+ var useGranularEffect = (effect, primaryDeps, secondaryDeps) => useGranularHook(import_react7.useEffect, effect, primaryDeps, secondaryDeps);
1325
+
1313
1326
  // src/components/UserProvider/index.tsx
1314
1327
  var import_log9 = require("ag-common/dist/common/helpers/log");
1315
- var import_react7 = __toESM(require("react"));
1328
+ var import_react8 = __toESM(require("react"));
1316
1329
  var import_useLocalStorage = require("ag-common/dist/ui/helpers/useLocalStorage");
1317
1330
  var import_cookie = require("ag-common/dist/ui/helpers/cookie");
1318
1331
  var import_dom3 = __toESM(require_dom());
1319
- var CognitoAuthContext = import_react7.default.createContext(void 0);
1332
+ var CognitoAuthContext = import_react8.default.createContext(void 0);
1333
+ var getStatefulUrl = ({
1334
+ redirectUrl,
1335
+ qs
1336
+ }) => {
1337
+ let url = redirectUrl || "/";
1338
+ if (qs == null ? void 0 : qs.state) {
1339
+ try {
1340
+ if (!url.includes("?")) {
1341
+ url += "?";
1342
+ } else {
1343
+ url += "&";
1344
+ }
1345
+ url += `state=${qs.state}`;
1346
+ } catch (e) {
1347
+ (0, import_log9.warn)("bad state passed in");
1348
+ }
1349
+ }
1350
+ return url;
1351
+ };
1320
1352
  var CognitoAuthProvider = ({
1321
1353
  config,
1322
1354
  children,
@@ -1327,7 +1359,7 @@ var CognitoAuthProvider = ({
1327
1359
  cookieDocument
1328
1360
  }) => {
1329
1361
  var _a, _b;
1330
- const [error2, setError] = (0, import_react7.useState)();
1362
+ const [error2, setError] = (0, import_react8.useState)();
1331
1363
  const [idTokenRaw, setIdToken] = (0, import_cookie.useCookieString)({
1332
1364
  name: "id_token",
1333
1365
  cookieDocument: typeof window === "undefined" ? cookieDocument : void 0,
@@ -1335,17 +1367,20 @@ var CognitoAuthProvider = ({
1335
1367
  });
1336
1368
  let idToken = idTokenRaw;
1337
1369
  if (idToken && !decodeIdToken(idToken)) {
1338
- (0, import_log9.warn)(`bad token, logging out`);
1370
+ (0, import_log9.warn)(`bad token, wiping`);
1339
1371
  (0, import_cookie.wipeCookies)("id_token");
1340
- void goToPageUrl({ url: location.origin, login: false });
1341
1372
  idToken = "";
1342
1373
  }
1343
1374
  let defUser;
1344
1375
  if (idToken && !(0, import_useLocalStorage.getLocalStorageItem)("user", void 0)) {
1345
1376
  defUser = getUserFromIdToken(idToken);
1346
1377
  }
1347
- const [user, setUser] = (0, import_useLocalStorage.UseLocalStorage)("user", defUser);
1348
- const [loading, setLoading] = (0, import_react7.useState)(false);
1378
+ const [user, setUserRaw] = (0, import_useLocalStorage.UseLocalStorage)("user", defUser);
1379
+ const setUser = (value2) => {
1380
+ setUserRaw(value2);
1381
+ return;
1382
+ };
1383
+ const [loading, setLoading] = (0, import_react8.useState)(false);
1349
1384
  const qs = (0, import_dom3.extractQs)({
1350
1385
  search: typeof window === "undefined" ? "" : window.location.search,
1351
1386
  sanatise: false
@@ -1360,8 +1395,7 @@ var CognitoAuthProvider = ({
1360
1395
  yield goToPageUrl({ url: location.origin, login: false });
1361
1396
  setLoading(false);
1362
1397
  });
1363
- const logoutCopy = logout;
1364
- (0, import_react7.useEffect)(() => {
1398
+ (0, import_react8.useEffect)(() => {
1365
1399
  var _a2;
1366
1400
  const newT = (_a2 = user == null ? void 0 : user.jwt) == null ? void 0 : _a2.id_token;
1367
1401
  if (!!newT && newT !== idToken) {
@@ -1387,29 +1421,33 @@ var CognitoAuthProvider = ({
1387
1421
  login: true
1388
1422
  });
1389
1423
  });
1390
- (0, import_react7.useEffect)(() => {
1424
+ useGranularEffect(() => {
1391
1425
  function run() {
1392
1426
  return __async(this, null, function* () {
1393
- var _a2, _b2, _c;
1427
+ var _a2, _b2, _c, _d;
1394
1428
  const hasQs = !!(qs == null ? void 0 : qs.code) || !!(qs == null ? void 0 : qs.error);
1395
1429
  const expired = jwtExpired(user == null ? void 0 : user.jwt);
1396
1430
  const onlyJwt = !(user == null ? void 0 : user.userId) && !!(user == null ? void 0 : user.jwt);
1397
- if (!hasQs && !expired && !onlyJwt) {
1431
+ if (loading || error2 || ((_a2 = user == null ? void 0 : user.idJwt) == null ? void 0 : _a2.email) && !hasQs && !expired) {
1398
1432
  return;
1399
1433
  }
1400
1434
  try {
1401
1435
  setLoading(true);
1402
- if (hasQs && !expired) {
1403
- yield goToPageUrl({ url: redirectUrl, login: true });
1404
- } else if (hasQs) {
1405
- if (qs == null ? void 0 : qs.error) {
1406
- const newerror = {
1407
- message: (qs == null ? void 0 : qs.error_description) || "auth error"
1408
- };
1409
- if (JSON.stringify(newerror || {}) !== JSON.stringify(error2 || {})) {
1410
- setError(newerror);
1411
- }
1412
- } else if (qs == null ? void 0 : qs.code) {
1436
+ if (qs == null ? void 0 : qs.error) {
1437
+ const newerror = {
1438
+ message: (qs == null ? void 0 : qs.error_description) || "auth error"
1439
+ };
1440
+ if (JSON.stringify(newerror || {}) !== JSON.stringify(error2 || {})) {
1441
+ setError(newerror);
1442
+ }
1443
+ return;
1444
+ }
1445
+ if ((qs == null ? void 0 : qs.code) && !expired) {
1446
+ yield goToPageUrl({ url: redirectUrl, login: false });
1447
+ return;
1448
+ }
1449
+ if (qs == null ? void 0 : qs.code) {
1450
+ if (expired) {
1413
1451
  yield getTokensFromCode({
1414
1452
  code: qs.code,
1415
1453
  redirectUrl,
@@ -1417,31 +1455,24 @@ var CognitoAuthProvider = ({
1417
1455
  setUser
1418
1456
  });
1419
1457
  }
1420
- const newPath = "";
1421
- let url = newPath || redirectUrl || "/";
1422
- if (qs == null ? void 0 : qs.state) {
1423
- try {
1424
- if (!url.includes("?")) {
1425
- url += "?";
1426
- } else {
1427
- url += "&";
1428
- }
1429
- url += `state=${qs.state}`;
1430
- } catch (e) {
1431
- (0, import_log9.warn)("bad state passed in");
1432
- }
1433
- }
1434
- yield goToPageUrl({ url, login: true });
1435
- } else if (expired && ((_a2 = user == null ? void 0 : user.jwt) == null ? void 0 : _a2.refresh_token)) {
1458
+ yield goToPageUrl({
1459
+ url: getStatefulUrl({ redirectUrl, qs }),
1460
+ login: false
1461
+ });
1462
+ return;
1463
+ }
1464
+ if (expired && ((_b2 = user == null ? void 0 : user.jwt) == null ? void 0 : _b2.refresh_token)) {
1436
1465
  yield refreshToken({
1437
1466
  setUser,
1438
1467
  setError,
1439
1468
  user,
1440
1469
  config,
1441
- logout: logoutCopy
1470
+ logout
1442
1471
  });
1443
- } else if (onlyJwt) {
1444
- const token = (_c = (_b2 = user == null ? void 0 : user.jwt) == null ? void 0 : _b2.id_token) == null ? void 0 : _c.substr(user.jwt.id_token.indexOf(" ") + 1);
1472
+ return;
1473
+ }
1474
+ if (onlyJwt) {
1475
+ const token = (_d = (_c = user == null ? void 0 : user.jwt) == null ? void 0 : _c.id_token) == null ? void 0 : _d.substr(user.jwt.id_token.indexOf(" ") + 1);
1445
1476
  if (!user || !token) {
1446
1477
  (0, import_log9.info)("logging out no token");
1447
1478
  setUser(void 0);
@@ -1458,21 +1489,19 @@ var CognitoAuthProvider = ({
1458
1489
  }
1459
1490
  });
1460
1491
  }
1461
- if (!loading && !error2) {
1462
- run();
1463
- }
1464
- }, [
1492
+ void run();
1493
+ }, [user == null ? void 0 : user.jwt, qs], [
1465
1494
  config,
1466
1495
  error2,
1467
1496
  goToPageUrl,
1468
- logoutCopy,
1497
+ logout,
1469
1498
  loading,
1470
1499
  qs,
1471
1500
  redirectUrl,
1472
1501
  setUser,
1473
1502
  user
1474
1503
  ]);
1475
- (0, import_react7.useEffect)(() => {
1504
+ (0, import_react8.useEffect)(() => {
1476
1505
  var _a2;
1477
1506
  if (error2) {
1478
1507
  (0, import_log9.error)(JSON.stringify(error2));
@@ -1497,7 +1526,7 @@ var CognitoAuthProvider = ({
1497
1526
  error: error2,
1498
1527
  user
1499
1528
  };
1500
- return /* @__PURE__ */ import_react7.default.createElement(CognitoAuthContext.Provider, {
1529
+ return /* @__PURE__ */ import_react8.default.createElement(CognitoAuthContext.Provider, {
1501
1530
  value
1502
1531
  }, children);
1503
1532
  };
@@ -1506,7 +1535,8 @@ var CognitoAuthProvider = ({
1506
1535
  var import_log10 = require("ag-common/dist/common/helpers/log");
1507
1536
  var import_string = require("ag-common/dist/common/helpers/string");
1508
1537
  var import_Loader = require("ag-common/dist/ui/components/Loader");
1509
- var import_react8 = __toESM(require("react"));
1538
+ var import_useQueryString = require("ag-common/dist/ui/helpers/useQueryString");
1539
+ var import_react9 = __toESM(require("react"));
1510
1540
  var DashboardAuthValidation = ({
1511
1541
  rc: { request, pushPath },
1512
1542
  cac: {
@@ -1520,7 +1550,7 @@ var DashboardAuthValidation = ({
1520
1550
  getUnauthedPage,
1521
1551
  addToast
1522
1552
  }) => {
1523
- (0, import_react8.useEffect)(() => {
1553
+ useGranularEffect(() => {
1524
1554
  if (authError) {
1525
1555
  const emailOption = authError.message.includes("email") ? ` We require the use of your email for the functionality of this app.
1526
1556
  You can update your settings at https://www.facebook.com/settings?tab=applications` : "";
@@ -1530,28 +1560,29 @@ var DashboardAuthValidation = ({
1530
1560
  (0, import_log10.error)(`auth error:`, JSON.stringify(authError, null, 2));
1531
1561
  void logout();
1532
1562
  }
1533
- }, [authError]);
1563
+ }, [authError], [authError, addToast, logout]);
1534
1564
  const forceLogin = Object.keys(request.url.query || {}).includes("login");
1535
1565
  if (forceLogin) {
1536
1566
  if (!isAuthenticated) {
1537
1567
  void loginWithRedirect(request.url.query.state);
1538
- return { render: /* @__PURE__ */ import_react8.default.createElement(import_react8.default.Fragment, null), openApiDisabled: true };
1568
+ return { render: /* @__PURE__ */ import_react9.default.createElement(import_react9.default.Fragment, null), openApiDisabled: true };
1539
1569
  }
1540
1570
  if (isAuthenticated) {
1541
1571
  void pushPath(getDashboardPath());
1542
- return { render: /* @__PURE__ */ import_react8.default.createElement(import_react8.default.Fragment, null), openApiDisabled: true };
1572
+ return { render: /* @__PURE__ */ import_react9.default.createElement(import_react9.default.Fragment, null), openApiDisabled: true };
1543
1573
  }
1544
1574
  }
1545
1575
  if (request.url.query.state && isAuthenticated) {
1546
1576
  const decoded = JSON.parse((0, import_string.fromBase64)(request.url.query.state));
1547
1577
  if (decoded.redirect) {
1548
1578
  void pushPath(decoded.redirect);
1549
- return { render: /* @__PURE__ */ import_react8.default.createElement(import_react8.default.Fragment, null), openApiDisabled: true };
1579
+ return { render: /* @__PURE__ */ import_react9.default.createElement(import_react9.default.Fragment, null), openApiDisabled: true };
1550
1580
  }
1551
1581
  }
1552
- if (authLoading) {
1582
+ const serverAuthLoading = import_useQueryString.isServer && !authError && !authLoading && !isAuthenticated && Object.keys(request.url.query || {}).includes("code");
1583
+ if (authLoading || serverAuthLoading) {
1553
1584
  return {
1554
- render: /* @__PURE__ */ import_react8.default.createElement(import_Loader.Loader, {
1585
+ render: /* @__PURE__ */ import_react9.default.createElement(import_Loader.Loader, {
1555
1586
  name: "authload"
1556
1587
  }),
1557
1588
  openApiDisabled: true
@@ -1559,7 +1590,7 @@ var DashboardAuthValidation = ({
1559
1590
  }
1560
1591
  if (!isAuthenticated && !authLoading && !authError && getUnauthedPage) {
1561
1592
  void pushPath(getUnauthedPage());
1562
- return { render: /* @__PURE__ */ import_react8.default.createElement(import_react8.default.Fragment, null), openApiDisabled: true };
1593
+ return { render: /* @__PURE__ */ import_react9.default.createElement(import_react9.default.Fragment, null), openApiDisabled: true };
1563
1594
  }
1564
1595
  const openApiDisabled = !isAuthenticated || authLoading || !!authError;
1565
1596
  return { openApiDisabled };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "analytica.click",
3
- "version": "0.0.236",
3
+ "version": "0.0.241",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "typings": "dist/index.d.ts",
@@ -9,7 +9,7 @@
9
9
  "scripts": {
10
10
  "format": "npx eslint --ext .ts,.tsx ./src --fix",
11
11
  "lint": "tsc && npx eslint --ext .ts,.tsx ./src",
12
- "build-direct": "cross-env NODE_ENV=prod ts-node esbuild",
12
+ "build-direct": "rimraf .build && rimraf dist && cross-env NODE_ENV=prod ts-node esbuild",
13
13
  "build": "yarn lint && yarn build-direct",
14
14
  "start": "cross-env NODE_ENV=dev nodemon -e ts,tsx -w ./src --exec 'ts-node esbuild'"
15
15
  },
@@ -30,6 +30,7 @@
30
30
  "nodemon": "2.x",
31
31
  "react": "18.x",
32
32
  "react-dom": "18.x",
33
+ "rimraf": "3.x",
33
34
  "styled-components": "5.x",
34
35
  "ts-node": "10.x",
35
36
  "typescript": "4.x"