@frontegg/react-hooks 7.0.0-alpha.1 → 7.0.0-alpha.10

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 (141) hide show
  1. package/FronteggProvider/FronteggProvider.d.ts +12 -0
  2. package/FronteggProvider/FronteggProvider.js +141 -0
  3. package/FronteggProvider/index.d.ts +1 -12
  4. package/FronteggProvider/index.js +1 -133
  5. package/FronteggStoreContext/FronteggStoreContext.d.ts +9 -0
  6. package/FronteggStoreContext/FronteggStoreContext.js +16 -0
  7. package/FronteggStoreContext/index.d.ts +1 -13
  8. package/FronteggStoreContext/index.js +1 -14
  9. package/audits/auditLogs.d.ts +2 -4
  10. package/audits/auditLogs.js +8 -6
  11. package/audits/auditsMetadata.d.ts +2 -4
  12. package/audits/auditsMetadata.js +8 -6
  13. package/audits/hooks.d.ts +0 -8
  14. package/audits/hooks.js +6 -30
  15. package/auth/acceptInvitation.js +10 -5
  16. package/auth/activateAccount.js +10 -4
  17. package/auth/{MSP/allAccounts.d.ts → allAccounts.d.ts} +2 -4
  18. package/auth/allAccounts.js +20 -0
  19. package/auth/apiTokens.d.ts +0 -2
  20. package/auth/apiTokens.js +8 -6
  21. package/auth/applications.d.ts +3 -0
  22. package/auth/applications.js +9 -0
  23. package/auth/customLogin.d.ts +2 -5
  24. package/auth/customLogin.js +10 -11
  25. package/auth/entitlements.d.ts +32 -13
  26. package/auth/entitlements.js +103 -21
  27. package/auth/forgotPassword.d.ts +0 -2
  28. package/auth/forgotPassword.js +8 -6
  29. package/auth/groups.d.ts +0 -4
  30. package/auth/groups.js +12 -13
  31. package/auth/hooks.d.ts +44 -12
  32. package/auth/hooks.js +77 -62
  33. package/auth/impersonate.d.ts +0 -2
  34. package/auth/impersonate.js +7 -6
  35. package/auth/index.d.ts +16 -16
  36. package/auth/index.js +16 -16
  37. package/auth/login.d.ts +1 -3
  38. package/auth/login.js +7 -6
  39. package/auth/mfa.d.ts +1 -1
  40. package/auth/mfa.js +13 -4
  41. package/auth/passkeys.d.ts +1 -4
  42. package/auth/passkeys.js +7 -6
  43. package/auth/profile.d.ts +2 -4
  44. package/auth/profile.js +9 -8
  45. package/auth/provisioning.d.ts +1 -3
  46. package/auth/provisioning.js +7 -6
  47. package/auth/resetPhoneNumber.d.ts +1 -3
  48. package/auth/resetPhoneNumber.js +7 -6
  49. package/auth/roles.d.ts +1 -3
  50. package/auth/roles.js +7 -6
  51. package/auth/security/index.d.ts +4 -0
  52. package/auth/security/index.js +4 -0
  53. package/auth/security/restrictions.d.ts +0 -2
  54. package/auth/security/restrictions.js +10 -10
  55. package/auth/security/securityCenter.d.ts +1 -4
  56. package/auth/security/securityCenter.js +8 -6
  57. package/auth/security/securityPolicy.d.ts +5 -11
  58. package/auth/security/securityPolicy.js +19 -30
  59. package/auth/security/sessionsPolicy.d.ts +1 -3
  60. package/auth/security/sessionsPolicy.js +8 -6
  61. package/auth/sessions.d.ts +1 -3
  62. package/auth/sessions.js +7 -6
  63. package/auth/signup.d.ts +1 -1
  64. package/auth/signup.js +13 -4
  65. package/auth/sms.d.ts +3 -0
  66. package/auth/sms.js +8 -0
  67. package/auth/socialLogin.d.ts +1 -1
  68. package/auth/socialLogin.js +10 -4
  69. package/auth/sso.d.ts +1 -1
  70. package/auth/sso.js +10 -4
  71. package/auth/stepUp.d.ts +16 -0
  72. package/auth/stepUp.js +51 -0
  73. package/auth/team.d.ts +9 -1
  74. package/auth/team.js +22 -4
  75. package/auth/tenants.d.ts +1 -3
  76. package/auth/tenants.js +8 -6
  77. package/common/CustomComponentHolder.js +3 -1
  78. package/common/index.d.ts +5 -0
  79. package/common/index.js +11 -4
  80. package/connectivity/hooks.d.ts +2 -5
  81. package/connectivity/hooks.js +6 -9
  82. package/index.d.ts +2 -2
  83. package/index.js +3 -3
  84. package/node/FronteggProvider/FronteggProvider.js +149 -0
  85. package/node/FronteggProvider/index.js +9 -134
  86. package/node/FronteggStoreContext/FronteggStoreContext.js +28 -0
  87. package/node/FronteggStoreContext/index.js +11 -35
  88. package/node/audits/auditLogs.js +8 -6
  89. package/node/audits/auditsMetadata.js +8 -6
  90. package/node/audits/hooks.js +7 -34
  91. package/node/auth/acceptInvitation.js +7 -5
  92. package/node/auth/activateAccount.js +7 -4
  93. package/node/auth/allAccounts.js +27 -0
  94. package/node/auth/apiTokens.js +8 -6
  95. package/node/auth/applications.js +17 -0
  96. package/node/auth/customLogin.js +11 -12
  97. package/node/auth/entitlements.js +106 -22
  98. package/node/auth/forgotPassword.js +8 -6
  99. package/node/auth/groups.js +12 -12
  100. package/node/auth/hooks.js +80 -66
  101. package/node/auth/impersonate.js +7 -6
  102. package/node/auth/index.js +107 -93
  103. package/node/auth/login.js +7 -6
  104. package/node/auth/mfa.js +10 -4
  105. package/node/auth/passkeys.js +7 -6
  106. package/node/auth/profile.js +10 -9
  107. package/node/auth/provisioning.js +7 -6
  108. package/node/auth/resetPhoneNumber.js +7 -6
  109. package/node/auth/roles.js +7 -6
  110. package/node/auth/security/index.js +49 -0
  111. package/node/auth/security/restrictions.js +10 -10
  112. package/node/auth/security/securityCenter.js +8 -6
  113. package/node/auth/security/securityPolicy.js +16 -30
  114. package/node/auth/security/sessionsPolicy.js +8 -6
  115. package/node/auth/sessions.js +7 -6
  116. package/node/auth/signup.js +10 -4
  117. package/node/auth/sms.js +16 -0
  118. package/node/auth/socialLogin.js +7 -4
  119. package/node/auth/sso.js +7 -4
  120. package/node/auth/stepUp.js +62 -0
  121. package/node/auth/team.js +25 -4
  122. package/node/auth/tenants.js +8 -6
  123. package/node/common/CustomComponentHolder.js +3 -1
  124. package/node/common/index.js +10 -3
  125. package/node/connectivity/hooks.js +5 -8
  126. package/node/index.js +16 -16
  127. package/node/subscriptions/hooks.js +84 -51
  128. package/node/tests/hooks.tsc-test.js +339 -0
  129. package/node/useSnapshot/index.js +137 -0
  130. package/node/vendor/hooks.js +5 -6
  131. package/package.json +4 -5
  132. package/subscriptions/hooks.d.ts +32 -10
  133. package/subscriptions/hooks.js +81 -49
  134. package/tests/hooks.tsc-test.js +338 -0
  135. package/useSnapshot/index.d.ts +78 -0
  136. package/useSnapshot/index.js +128 -0
  137. package/useSnapshot/package.json +6 -0
  138. package/vendor/hooks.d.ts +0 -1
  139. package/vendor/hooks.js +9 -7
  140. package/auth/MSP/allAccounts.js +0 -15
  141. package/node/auth/MSP/allAccounts.js +0 -24
@@ -0,0 +1,128 @@
1
+ /// <reference types="react/experimental" />
2
+
3
+ import React, { useCallback, useDebugValue, useEffect, useMemo, useRef } from 'react';
4
+ import { affectedToPathList, createProxy as createProxyToCompare, isChanged } from 'proxy-compare';
5
+
6
+ // @ts-ignore
7
+ import useSyncExternalStoreExports from 'use-sync-external-store/shim';
8
+ import { snapshot, subscribe } from '@frontegg/redux-store';
9
+ const {
10
+ useSyncExternalStore
11
+ } = useSyncExternalStoreExports;
12
+ const useAffectedDebugValue = (state, affected) => {
13
+ const pathList = useRef();
14
+ useEffect(() => {
15
+ pathList.current = affectedToPathList(state, affected, true);
16
+ });
17
+ useDebugValue(pathList.current);
18
+ };
19
+
20
+ // This is required only for performance.
21
+ // Ref: https://github.com/pmndrs/valtio/issues/519
22
+ const targetCache = new WeakMap();
23
+ /**
24
+ * useSnapshot
25
+ *
26
+ * Create a local snapshot that catches changes. This hook actually returns a wrapped snapshot in a proxy for
27
+ * render optimization instead of a plain object compared to `snapshot()` method.
28
+ * Rule of thumb: read from snapshots, mutate the source.
29
+ * The component will only re-render when the parts of the state you access have changed, it is render-optimized.
30
+ *
31
+ * @example A
32
+ * function Counter() {
33
+ * const snap = useSnapshot(state)
34
+ * return (
35
+ * <div>
36
+ * {snap.count}
37
+ * <button onClick={() => ++state.count}>+1</button>
38
+ * </div>
39
+ * )
40
+ * }
41
+ *
42
+ * [Notes]
43
+ * Every object inside your proxy also becomes a proxy (if you don't use "ref"), so you can also use them to create
44
+ * the local snapshot as seen on example B.
45
+ *
46
+ * @example B
47
+ * function ProfileName() {
48
+ * const snap = useSnapshot(state.profile)
49
+ * return (
50
+ * <div>
51
+ * {snap.name}
52
+ * </div>
53
+ * )
54
+ * }
55
+ *
56
+ * Beware that you still can replace the child proxy with something else so it will break your snapshot. You can see
57
+ * above what happens with the original proxy when you replace the child proxy.
58
+ *
59
+ * > console.log(state)
60
+ * { profile: { name: "valtio" } }
61
+ * > childState = state.profile
62
+ * > console.log(childState)
63
+ * { name: "valtio" }
64
+ * > state.profile.name = "react"
65
+ * > console.log(childState)
66
+ * { name: "react" }
67
+ * > state.profile = { name: "new name" }
68
+ * > console.log(childState)
69
+ * { name: "react" }
70
+ * > console.log(state)
71
+ * { profile: { name: "new name" } }
72
+ *
73
+ * `useSnapshot()` depends on the original reference of the child proxy so if you replace it with a new one, the component
74
+ * that is subscribed to the old proxy won't receive new updates because it is still subscribed to the old one.
75
+ *
76
+ * In this case we recommend the example C or D. On both examples you don't need to worry with re-render,
77
+ * because it is render-optimized.
78
+ *
79
+ * @example C
80
+ * const snap = useSnapshot(state)
81
+ * return (
82
+ * <div>
83
+ * {snap.profile.name}
84
+ * </div>
85
+ * )
86
+ *
87
+ * @example D
88
+ * const { profile } = useSnapshot(state)
89
+ * return (
90
+ * <div>
91
+ * {profile.name}
92
+ * </div>
93
+ * )
94
+ */
95
+ export function useSnapshot(proxyObject, options) {
96
+ const notifyInSync = options == null ? void 0 : options.sync;
97
+ const lastSnapshot = useRef();
98
+ const lastAffected = useRef();
99
+ let inRender = true;
100
+ const currSnapshot = useSyncExternalStore(useCallback(callback => {
101
+ const unsub = subscribe(proxyObject, callback, notifyInSync);
102
+ callback(); // Note: do we really need this?
103
+ return unsub;
104
+ }, [proxyObject, notifyInSync]), () => {
105
+ const nextSnapshot = snapshot(proxyObject, React.use);
106
+ try {
107
+ if (!inRender && lastSnapshot.current && lastAffected.current && !isChanged(lastSnapshot.current, nextSnapshot, lastAffected.current, new WeakMap())) {
108
+ // not changed
109
+ return lastSnapshot.current;
110
+ }
111
+ } catch (e) {
112
+ // ignore if a promise or something is thrown
113
+ }
114
+ return nextSnapshot;
115
+ }, () => snapshot(proxyObject, React.use));
116
+ inRender = false;
117
+ const currAffected = new WeakMap();
118
+ useEffect(() => {
119
+ lastSnapshot.current = currSnapshot;
120
+ lastAffected.current = currAffected;
121
+ });
122
+ // if (import.meta.env?.MODE !== 'production') {
123
+ // // eslint-disable-next-line react-hooks/rules-of-hooks
124
+ // useAffectedDebugValue(currSnapshot, currAffected)
125
+ // }
126
+ const proxyCache = useMemo(() => new WeakMap(), []); // per-hook proxyCache
127
+ return createProxyToCompare(currSnapshot, currAffected, proxyCache, targetCache);
128
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "sideEffects": false,
3
+ "module": "./index.js",
4
+ "main": "../node/useSnapshot/index.js",
5
+ "types": "./index.d.ts"
6
+ }
package/vendor/hooks.d.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  import { VendorActions, VendorState } from '@frontegg/redux-store';
2
2
  export declare type VendorStateMapper<S> = (state: VendorState) => S;
3
- export declare type StateHookFunction<T> = (() => T) & (<S extends object>(mapper: (state: T) => S) => S);
4
3
  export declare function useVendorState(): VendorState;
5
4
  export declare function useVendorState<S>(stateMapper: VendorStateMapper<S>): S;
6
5
  export declare const useVendorActions: () => VendorActions;
package/vendor/hooks.js CHANGED
@@ -1,12 +1,14 @@
1
- import { useMemo } from 'react';
2
- import { bindActionCreators, vendorActions, vendorStoreName } from '@frontegg/redux-store';
3
- import { shallowEqual } from 'react-redux';
4
- import { useSelector, useDispatch } from '../FronteggStoreContext';
1
+ import { useStore } from '../FronteggStoreContext';
2
+ import { useSnapshot } from '../useSnapshot';
3
+
4
+ // TODO: remove useless stateMapper, valtio already does this
5
+
5
6
  const defaultMapper = state => state;
7
+ // @deprecated use useVendorState() instead
6
8
  export function useVendorState(stateMapper = defaultMapper) {
7
- return useSelector(state => stateMapper(state[vendorStoreName]), shallowEqual);
9
+ const state = useStore().store.vendor;
10
+ return stateMapper(useSnapshot(state));
8
11
  }
9
12
  export const useVendorActions = () => {
10
- const dispatch = useDispatch();
11
- return useMemo(() => bindActionCreators(vendorActions, dispatch), [vendorActions]);
13
+ return useStore().stateActions.vendor;
12
14
  };
@@ -1,15 +0,0 @@
1
- import { allAccountsActions, allAccountsReducers, allAccountsDialogsReducers } from '@frontegg/redux-store';
2
- import { reducerActionsGenerator, stateHookGenerator } from '../hooks';
3
- const defaultMapper = state => state;
4
- export function useAllAccountsState(stateMapper = defaultMapper) {
5
- return stateHookGenerator(stateMapper, 'allAccountsState');
6
- }
7
- export const useAllAccountsActions = () => reducerActionsGenerator(allAccountsActions, allAccountsReducers);
8
-
9
- //dialogs
10
-
11
- const defaultDialogsMapper = state => state;
12
- export function useAllAccountsDialogsState(stateMapper = defaultDialogsMapper) {
13
- return stateHookGenerator(stateMapper, 'allAccountsDialogsState');
14
- }
15
- export const useAllAccountsDialogsActions = () => reducerActionsGenerator({}, allAccountsDialogsReducers);
@@ -1,24 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.useAllAccountsDialogsActions = exports.useAllAccountsActions = void 0;
7
- exports.useAllAccountsDialogsState = useAllAccountsDialogsState;
8
- exports.useAllAccountsState = useAllAccountsState;
9
- var _reduxStore = require("@frontegg/redux-store");
10
- var _hooks = require("../hooks");
11
- const defaultMapper = state => state;
12
- function useAllAccountsState(stateMapper = defaultMapper) {
13
- return (0, _hooks.stateHookGenerator)(stateMapper, 'allAccountsState');
14
- }
15
- const useAllAccountsActions = () => (0, _hooks.reducerActionsGenerator)(_reduxStore.allAccountsActions, _reduxStore.allAccountsReducers);
16
-
17
- //dialogs
18
- exports.useAllAccountsActions = useAllAccountsActions;
19
- const defaultDialogsMapper = state => state;
20
- function useAllAccountsDialogsState(stateMapper = defaultDialogsMapper) {
21
- return (0, _hooks.stateHookGenerator)(stateMapper, 'allAccountsDialogsState');
22
- }
23
- const useAllAccountsDialogsActions = () => (0, _hooks.reducerActionsGenerator)({}, _reduxStore.allAccountsDialogsReducers);
24
- exports.useAllAccountsDialogsActions = useAllAccountsDialogsActions;