@frontegg/react-hooks 7.0.0-alpha.1 → 7.0.0-alpha.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.
Files changed (141) hide show
  1. package/FronteggProvider/FronteggProvider.d.ts +12 -0
  2. package/FronteggProvider/FronteggProvider.js +140 -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 +73 -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 +1 -0
  79. package/common/index.js +3 -3
  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 +148 -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 +77 -67
  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 +2 -2
  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 +3 -3
  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
@@ -1,77 +1,109 @@
1
- import { bindActionCreators, subscriptionActions, subscriptionsStoreName } from '@frontegg/redux-store';
2
- import { useDispatch, useSelector } from '../FronteggStoreContext';
3
- import { shallowEqual } from 'react-redux';
4
- import { useMemo } from 'react';
5
- function getSubscriptionState(state) {
6
- return state[subscriptionsStoreName];
1
+ import _extends from "@babel/runtime/helpers/esm/extends";
2
+ import { useStore } from '../FronteggStoreContext';
3
+ import { useSnapshot } from '../useSnapshot';
4
+ function getSubscriptionsState() {
5
+ // eslint-disable-next-line react-hooks/rules-of-hooks
6
+ return useStore().store.subscriptions;
7
7
  }
8
8
  export const usePaymentProvider = () => {
9
- return useSelector(state => getSubscriptionState(state).config, shallowEqual);
9
+ const {
10
+ config
11
+ } = getSubscriptionsState();
12
+ return useSnapshot(config);
10
13
  };
11
14
  export const usePaymentProviderActions = () => {
12
- const dispatch = useDispatch();
13
- return useMemo(() => bindActionCreators(subscriptionActions.config, dispatch), [dispatch]);
15
+ return useStore().stateActions.subscriptions.config;
14
16
  };
15
17
  export const usePlans = () => {
16
- return useSelector(state => getSubscriptionState(state).plans, shallowEqual);
18
+ const {
19
+ plans
20
+ } = getSubscriptionsState();
21
+ return useSnapshot(plans);
17
22
  };
18
23
  export const usePlansActions = () => {
19
- const dispatch = useDispatch();
20
- return useMemo(() => bindActionCreators(subscriptionActions.plans, dispatch), [dispatch]);
24
+ return useStore().stateActions.subscriptions.plans;
21
25
  };
22
26
  export const useCheckout = () => {
23
- return useSelector(state => getSubscriptionState(state).checkout, shallowEqual);
27
+ const {
28
+ checkout
29
+ } = getSubscriptionsState();
30
+ return useSnapshot(checkout);
24
31
  };
25
32
  export const useCheckoutActions = () => {
26
- const dispatch = useDispatch();
27
- return useMemo(() => bindActionCreators(subscriptionActions.checkout, dispatch), [dispatch]);
33
+ return useStore().stateActions.subscriptions.checkout;
28
34
  };
29
35
  const useBillingActions = billingKey => {
30
- const dispatch = useDispatch();
31
- return useMemo(() => bindActionCreators(subscriptionActions.billing[billingKey], dispatch), [billingKey, dispatch]);
32
- };
33
- export const useBillingInformation = () => useSelector(state => getSubscriptionState(state).billing.information, shallowEqual);
34
- export const useBillingInformationActions = () => useBillingActions('information');
35
- export const usePaymentMethod = () => useSelector(state => getSubscriptionState(state).billing.paymentMethod, shallowEqual);
36
- export const usePaymentMethodActions = () => useBillingActions('paymentMethod');
37
- export const useInvoices = () => useSelector(state => getSubscriptionState(state).billing.invoices, shallowEqual);
38
- export const useInvoicesActions = () => useBillingActions('invoices');
39
- export const useStripeState = () => {
40
- return useSelector(state => getSubscriptionState(state).stripe, shallowEqual);
36
+ return useStore().stateActions.subscriptions.billing[billingKey];
41
37
  };
42
- export const useStripeActions = () => {
43
- const dispatch = useDispatch();
44
- return useMemo(() => bindActionCreators(subscriptionActions.stripe, dispatch), [dispatch]);
38
+ export const useBillingInformation = () => {
39
+ const {
40
+ billing
41
+ } = getSubscriptionsState();
42
+ return useSnapshot(billing.information);
43
+ };
44
+ export const useBillingInformationActions = () => {
45
+ return useBillingActions('information');
46
+ };
47
+ export const usePaymentMethod = () => {
48
+ const {
49
+ billing
50
+ } = getSubscriptionsState();
51
+ return useSnapshot(billing.paymentMethod);
52
+ };
53
+ export const usePaymentMethodActions = () => {
54
+ return useBillingActions('paymentMethod');
55
+ };
56
+ export const useInvoices = () => {
57
+ const {
58
+ billing
59
+ } = getSubscriptionsState();
60
+ return useSnapshot(billing.invoices);
61
+ };
62
+ export const useInvoicesActions = () => {
63
+ return useBillingActions('invoices');
45
64
  };
46
65
  export const useSubscriptionCancellation = () => {
47
- return useSelector(state => getSubscriptionState(state).billing.subscription.cancellation, shallowEqual);
66
+ const {
67
+ billing
68
+ } = getSubscriptionsState();
69
+ const {
70
+ cancellation
71
+ } = useSnapshot(billing.subscription);
72
+ return cancellation;
48
73
  };
49
74
  export const useSubscriptionRenewal = () => {
50
- return useSelector(state => getSubscriptionState(state).billing.subscription.renewal, shallowEqual);
75
+ const {
76
+ billing
77
+ } = getSubscriptionsState();
78
+ const {
79
+ renewal
80
+ } = useSnapshot(billing.subscription);
81
+ return renewal;
51
82
  };
52
83
  export const useSubscription = () => {
53
- return useSelector(state => {
54
- const {
55
- billing: {
56
- subscription
57
- }
58
- } = getSubscriptionState(state);
59
- return subscription;
60
- }, shallowEqual);
84
+ const {
85
+ billing
86
+ } = getSubscriptionsState();
87
+ return useSnapshot(billing.subscription);
61
88
  };
62
89
  export const useSubscriptionActions = () => {
63
- const dispatch = useDispatch();
64
- return useMemo(() => bindActionCreators(subscriptionActions.billing.subscription, dispatch), [dispatch]);
90
+ const actions = useStore().stateActions.subscriptions.billing.subscription;
91
+ return _extends({}, actions, {
92
+ load: actions.loadSubscription
93
+ });
94
+ };
95
+ export const useStripeState = () => {
96
+ const {
97
+ stripe
98
+ } = getSubscriptionsState();
99
+ return useSnapshot(stripe);
100
+ };
101
+ export const useStripeActions = () => {
102
+ return useStore().stateActions.subscriptions.stripe;
65
103
  };
66
104
  export const useVendorPublicConfig = () => {
67
- return useSelector(state => {
68
- const {
69
- vendorPublicConfig
70
- } = getSubscriptionState(state);
71
- return vendorPublicConfig;
72
- }, shallowEqual);
105
+ return useSnapshot(useStore().store.subscriptions.vendorPublicConfig);
73
106
  };
74
107
  export const useVendorPublicConfigActions = () => {
75
- const dispatch = useDispatch();
76
- return useMemo(() => bindActionCreators(subscriptionActions.vendorPublicConfig, dispatch), [dispatch]);
108
+ return useStore().stateActions.subscriptions.vendorPublicConfig;
77
109
  };
@@ -0,0 +1,338 @@
1
+ /* eslint-disable react-hooks/rules-of-hooks */
2
+ // noinspection JSUnusedLocalSymbols
3
+
4
+ import { useAcceptInvitationActions, useAcceptInvitationState, useActivateAccountActions, useActivateAccountState, useApiTokensActions, useApiTokensState, useAuditsActions, useAuditsState, useAuth, useAuthActions, useConnectivityActions, useConnectivityState, useForgotPasswordActions, useForgotPasswordState, useLoginActions, useLoginState, useMfaActions, useMfaState, useProfileActions, useProfileState, useResetPhoneNumberActions, useResetPhoneNumberState, useRolesActions, useRolesState, useSecurityPolicyActions, useSecurityPolicyState, useSessionsActions, useSessionsPolicyActions, useSessionsPolicyState, useSessionsState, useSignUpActions, useSignUpState, useSocialLoginActions, useSocialLoginState, useSSOActions, useSSOState, useTenantsActions, useTenantsState, useVendorActions, useVendorState } from '../';
5
+
6
+ /**
7
+ * useAuth hook typescript tests
8
+ */
9
+ {
10
+ const {
11
+ isAuthenticated,
12
+ isLoading
13
+ } = useAuth();
14
+ const isLoadingDirect = useAuth(state => state.isLoading);
15
+ isLoadingDirect.valueOf();
16
+ const {
17
+ user
18
+ } = useAuth(state => ({
19
+ user: state.user
20
+ }));
21
+ const {
22
+ loginUrl
23
+ } = useAuth(state => state.routes);
24
+ const {
25
+ loadProfile,
26
+ setLoginState
27
+ } = useAuthActions();
28
+ }
29
+
30
+ /**
31
+ * useAcceptInvitationState hook typescript tests
32
+ */
33
+ {
34
+ const {
35
+ step
36
+ } = useAcceptInvitationState();
37
+ const errorDirect = useAcceptInvitationState(state => state.error);
38
+ errorDirect == null ? void 0 : errorDirect.substring(0);
39
+ const {
40
+ acceptInvitation
41
+ } = useAcceptInvitationActions();
42
+ }
43
+
44
+ /**
45
+ * useActivateAccountState hook typescript tests
46
+ */
47
+ {
48
+ const {
49
+ step
50
+ } = useActivateAccountState();
51
+ const loadingDirect = useActivateAccountState(state => state.loading);
52
+ loadingDirect.valueOf();
53
+ const {
54
+ activateAccount
55
+ } = useActivateAccountActions();
56
+ }
57
+
58
+ /**
59
+ * useApiTokensState hook typescript tests
60
+ */
61
+ {
62
+ const {
63
+ roles
64
+ } = useApiTokensState();
65
+ const {
66
+ loaders: loadingDirect
67
+ } = useApiTokensState();
68
+ const m = loadingDirect.LOAD_API_TOKENS;
69
+ const {
70
+ loadUserApiTokens
71
+ } = useApiTokensActions();
72
+ }
73
+
74
+ /**
75
+ * useForgotPasswordState hook typescript tests
76
+ */
77
+ {
78
+ const {
79
+ step
80
+ } = useForgotPasswordState();
81
+ const {
82
+ email: emailDirect
83
+ } = useForgotPasswordState();
84
+ emailDirect.substring(0);
85
+ const {
86
+ forgotPassword
87
+ } = useForgotPasswordActions();
88
+ }
89
+
90
+ /**
91
+ * useMfaState hook typescript tests
92
+ */
93
+ {
94
+ const {
95
+ step
96
+ } = useMfaState();
97
+ const loadingDirect = useMfaState(state => state.loading);
98
+ loadingDirect.valueOf();
99
+ const {
100
+ enrollMfa
101
+ } = useMfaActions();
102
+ }
103
+
104
+ /**
105
+ * useLoginState hook typescript tests
106
+ */
107
+ {
108
+ const {
109
+ step
110
+ } = useLoginState();
111
+ const {
112
+ loading: loadingDirect
113
+ } = useLoginState();
114
+ loadingDirect.valueOf();
115
+ const {
116
+ logout
117
+ } = useLoginActions();
118
+ }
119
+
120
+ /**
121
+ * useLoginState hook typescript tests
122
+ */
123
+ {
124
+ const {
125
+ profile
126
+ } = useProfileState();
127
+ const {
128
+ loading: loadingDirect
129
+ } = useProfileState();
130
+ loadingDirect.valueOf();
131
+ const {
132
+ loadProfile
133
+ } = useProfileActions();
134
+ }
135
+
136
+ /**
137
+ * useResetPhoneNumberState hook typescript tests
138
+ */
139
+ {
140
+ const {
141
+ step
142
+ } = useResetPhoneNumberState();
143
+ const {
144
+ loading: loadingDirect
145
+ } = useResetPhoneNumberState();
146
+ loadingDirect.valueOf();
147
+ const {
148
+ resetPhoneNumber
149
+ } = useResetPhoneNumberActions();
150
+ }
151
+
152
+ /**
153
+ * useRolesState hook typescript tests
154
+ */
155
+ {
156
+ var _loadingDirect$loadRo;
157
+ const {
158
+ saving
159
+ } = useRolesState();
160
+ const {
161
+ loaders: loadingDirect
162
+ } = useRolesState();
163
+ loadingDirect == null ? void 0 : (_loadingDirect$loadRo = loadingDirect.loadRoles) == null ? void 0 : _loadingDirect$loadRo.valueOf();
164
+ const {
165
+ loadRolesAndPermissions
166
+ } = useRolesActions();
167
+ }
168
+
169
+ /**
170
+ * useSecurityPolicyState hook typescript tests
171
+ */
172
+ {
173
+ const {
174
+ lockoutPolicy
175
+ } = useSecurityPolicyState();
176
+ const captchaPolicy = useSecurityPolicyState(state => state.captchaPolicy);
177
+ captchaPolicy.loading.valueOf();
178
+ const {
179
+ loadPublicSecurityPolicy
180
+ } = useSecurityPolicyActions();
181
+ }
182
+
183
+ /**
184
+ * useSessionsState hook typescript tests
185
+ */
186
+ {
187
+ const {
188
+ sessions
189
+ } = useSessionsState();
190
+ const {
191
+ loading: loadingDirect
192
+ } = useSessionsState();
193
+ loadingDirect.valueOf();
194
+ const {
195
+ deleteUserSession
196
+ } = useSessionsActions();
197
+ }
198
+
199
+ /**
200
+ * useSessionsPolicyState hook typescript tests
201
+ */
202
+ {
203
+ const {
204
+ configurations
205
+ } = useSessionsPolicyState();
206
+ const {
207
+ loading: loadingDirect
208
+ } = useSessionsPolicyState();
209
+ loadingDirect.valueOf();
210
+ const {
211
+ resetSessionsPolicyState
212
+ } = useSessionsPolicyActions();
213
+ }
214
+
215
+ /**
216
+ * useSSOState hook typescript tests
217
+ */
218
+ {
219
+ const {
220
+ authorizationRoles
221
+ } = useSSOState();
222
+ const {
223
+ loading: loadingDirect
224
+ } = useSSOState();
225
+ loadingDirect.valueOf();
226
+ const {
227
+ loadSSOConfigurationsV2
228
+ } = useSSOActions();
229
+ }
230
+
231
+ /**
232
+ * useSignUpState hook typescript tests
233
+ */
234
+ {
235
+ const {
236
+ stage
237
+ } = useSignUpState();
238
+ const loadingDirect = useSignUpState(state => state.loading);
239
+ loadingDirect.valueOf();
240
+ const {
241
+ setSignUpState
242
+ } = useSignUpActions();
243
+ }
244
+
245
+ /**
246
+ * useSocialLoginState hook typescript tests
247
+ */
248
+ {
249
+ const {
250
+ socialLoginsConfig
251
+ } = useSocialLoginState();
252
+ const loadingDirect = useSocialLoginState(state => state.loading);
253
+ loadingDirect.valueOf();
254
+ const {
255
+ loadSocialLoginsConfiguration
256
+ } = useSocialLoginActions();
257
+ }
258
+
259
+ /**
260
+ * useSocialLoginState hook typescript tests
261
+ */
262
+ {
263
+ const {
264
+ tenants
265
+ } = useTenantsState();
266
+ const {
267
+ loading: loadingDirect
268
+ } = useTenantsState();
269
+ loadingDirect.valueOf();
270
+ const {
271
+ loadTenants
272
+ } = useTenantsActions();
273
+ }
274
+
275
+ /**
276
+ * useVendorState hook typescript tests
277
+ */
278
+ {
279
+ const {
280
+ loading
281
+ } = useVendorState();
282
+ const {
283
+ loading: isLoadingDirect
284
+ } = useVendorState();
285
+ isLoadingDirect.valueOf();
286
+ const {
287
+ whiteLabelMode
288
+ } = useVendorState(state => ({
289
+ whiteLabelMode: state.whiteLabelMode
290
+ }));
291
+ const {
292
+ loadVendorPublicInfo
293
+ } = useVendorActions();
294
+ }
295
+
296
+ /**
297
+ * useConnectivityState hook typescript tests
298
+ */
299
+ {
300
+ const {
301
+ isLoading,
302
+ error
303
+ } = useConnectivityState();
304
+ const {
305
+ processIds
306
+ } = useConnectivityState();
307
+ processIds.map(id => id);
308
+ const {
309
+ email = []
310
+ } = useConnectivityState();
311
+ email.length;
312
+ const {
313
+ loadScope,
314
+ loadDataAction
315
+ } = useConnectivityActions();
316
+ }
317
+
318
+ /**
319
+ * useAuditsState hook typescript tests
320
+ */
321
+ {
322
+ const {
323
+ auditLogsState
324
+ } = useAuditsState();
325
+ const {
326
+ columns
327
+ } = useAuditsState(state => state.auditsMetadataState);
328
+ const {
329
+ loading
330
+ } = useAuditsState(state => ({
331
+ loading: state.auditLogsState.loading
332
+ }));
333
+ loading.valueOf();
334
+ const x = columns == null ? void 0 : columns.length;
335
+ const {
336
+ loadAuditLogs
337
+ } = useAuditsActions();
338
+ }
@@ -0,0 +1,78 @@
1
+ import { Snapshot } from '@frontegg/redux-store';
2
+ declare type Options = {
3
+ sync?: boolean;
4
+ };
5
+ /**
6
+ * useSnapshot
7
+ *
8
+ * Create a local snapshot that catches changes. This hook actually returns a wrapped snapshot in a proxy for
9
+ * render optimization instead of a plain object compared to `snapshot()` method.
10
+ * Rule of thumb: read from snapshots, mutate the source.
11
+ * The component will only re-render when the parts of the state you access have changed, it is render-optimized.
12
+ *
13
+ * @example A
14
+ * function Counter() {
15
+ * const snap = useSnapshot(state)
16
+ * return (
17
+ * <div>
18
+ * {snap.count}
19
+ * <button onClick={() => ++state.count}>+1</button>
20
+ * </div>
21
+ * )
22
+ * }
23
+ *
24
+ * [Notes]
25
+ * Every object inside your proxy also becomes a proxy (if you don't use "ref"), so you can also use them to create
26
+ * the local snapshot as seen on example B.
27
+ *
28
+ * @example B
29
+ * function ProfileName() {
30
+ * const snap = useSnapshot(state.profile)
31
+ * return (
32
+ * <div>
33
+ * {snap.name}
34
+ * </div>
35
+ * )
36
+ * }
37
+ *
38
+ * Beware that you still can replace the child proxy with something else so it will break your snapshot. You can see
39
+ * above what happens with the original proxy when you replace the child proxy.
40
+ *
41
+ * > console.log(state)
42
+ * { profile: { name: "valtio" } }
43
+ * > childState = state.profile
44
+ * > console.log(childState)
45
+ * { name: "valtio" }
46
+ * > state.profile.name = "react"
47
+ * > console.log(childState)
48
+ * { name: "react" }
49
+ * > state.profile = { name: "new name" }
50
+ * > console.log(childState)
51
+ * { name: "react" }
52
+ * > console.log(state)
53
+ * { profile: { name: "new name" } }
54
+ *
55
+ * `useSnapshot()` depends on the original reference of the child proxy so if you replace it with a new one, the component
56
+ * that is subscribed to the old proxy won't receive new updates because it is still subscribed to the old one.
57
+ *
58
+ * In this case we recommend the example C or D. On both examples you don't need to worry with re-render,
59
+ * because it is render-optimized.
60
+ *
61
+ * @example C
62
+ * const snap = useSnapshot(state)
63
+ * return (
64
+ * <div>
65
+ * {snap.profile.name}
66
+ * </div>
67
+ * )
68
+ *
69
+ * @example D
70
+ * const { profile } = useSnapshot(state)
71
+ * return (
72
+ * <div>
73
+ * {profile.name}
74
+ * </div>
75
+ * )
76
+ */
77
+ export declare function useSnapshot<T extends object>(proxyObject: T, options?: Options): Snapshot<T>;
78
+ export {};
@@ -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
+ }