@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.
- package/FronteggProvider/FronteggProvider.d.ts +12 -0
- package/FronteggProvider/FronteggProvider.js +141 -0
- package/FronteggProvider/index.d.ts +1 -12
- package/FronteggProvider/index.js +1 -133
- package/FronteggStoreContext/FronteggStoreContext.d.ts +9 -0
- package/FronteggStoreContext/FronteggStoreContext.js +16 -0
- package/FronteggStoreContext/index.d.ts +1 -13
- package/FronteggStoreContext/index.js +1 -14
- package/audits/auditLogs.d.ts +2 -4
- package/audits/auditLogs.js +8 -6
- package/audits/auditsMetadata.d.ts +2 -4
- package/audits/auditsMetadata.js +8 -6
- package/audits/hooks.d.ts +0 -8
- package/audits/hooks.js +6 -30
- package/auth/acceptInvitation.js +10 -5
- package/auth/activateAccount.js +10 -4
- package/auth/{MSP/allAccounts.d.ts → allAccounts.d.ts} +2 -4
- package/auth/allAccounts.js +20 -0
- package/auth/apiTokens.d.ts +0 -2
- package/auth/apiTokens.js +8 -6
- package/auth/applications.d.ts +3 -0
- package/auth/applications.js +9 -0
- package/auth/customLogin.d.ts +2 -5
- package/auth/customLogin.js +10 -11
- package/auth/entitlements.d.ts +32 -13
- package/auth/entitlements.js +103 -21
- package/auth/forgotPassword.d.ts +0 -2
- package/auth/forgotPassword.js +8 -6
- package/auth/groups.d.ts +0 -4
- package/auth/groups.js +12 -13
- package/auth/hooks.d.ts +44 -12
- package/auth/hooks.js +77 -62
- package/auth/impersonate.d.ts +0 -2
- package/auth/impersonate.js +7 -6
- package/auth/index.d.ts +16 -16
- package/auth/index.js +16 -16
- package/auth/login.d.ts +1 -3
- package/auth/login.js +7 -6
- package/auth/mfa.d.ts +1 -1
- package/auth/mfa.js +13 -4
- package/auth/passkeys.d.ts +1 -4
- package/auth/passkeys.js +7 -6
- package/auth/profile.d.ts +2 -4
- package/auth/profile.js +9 -8
- package/auth/provisioning.d.ts +1 -3
- package/auth/provisioning.js +7 -6
- package/auth/resetPhoneNumber.d.ts +1 -3
- package/auth/resetPhoneNumber.js +7 -6
- package/auth/roles.d.ts +1 -3
- package/auth/roles.js +7 -6
- package/auth/security/index.d.ts +4 -0
- package/auth/security/index.js +4 -0
- package/auth/security/restrictions.d.ts +0 -2
- package/auth/security/restrictions.js +10 -10
- package/auth/security/securityCenter.d.ts +1 -4
- package/auth/security/securityCenter.js +8 -6
- package/auth/security/securityPolicy.d.ts +5 -11
- package/auth/security/securityPolicy.js +19 -30
- package/auth/security/sessionsPolicy.d.ts +1 -3
- package/auth/security/sessionsPolicy.js +8 -6
- package/auth/sessions.d.ts +1 -3
- package/auth/sessions.js +7 -6
- package/auth/signup.d.ts +1 -1
- package/auth/signup.js +13 -4
- package/auth/sms.d.ts +3 -0
- package/auth/sms.js +8 -0
- package/auth/socialLogin.d.ts +1 -1
- package/auth/socialLogin.js +10 -4
- package/auth/sso.d.ts +1 -1
- package/auth/sso.js +10 -4
- package/auth/stepUp.d.ts +16 -0
- package/auth/stepUp.js +51 -0
- package/auth/team.d.ts +9 -1
- package/auth/team.js +22 -4
- package/auth/tenants.d.ts +1 -3
- package/auth/tenants.js +8 -6
- package/common/CustomComponentHolder.js +3 -1
- package/common/index.d.ts +5 -0
- package/common/index.js +11 -4
- package/connectivity/hooks.d.ts +2 -5
- package/connectivity/hooks.js +6 -9
- package/index.d.ts +2 -2
- package/index.js +3 -3
- package/node/FronteggProvider/FronteggProvider.js +149 -0
- package/node/FronteggProvider/index.js +9 -134
- package/node/FronteggStoreContext/FronteggStoreContext.js +28 -0
- package/node/FronteggStoreContext/index.js +11 -35
- package/node/audits/auditLogs.js +8 -6
- package/node/audits/auditsMetadata.js +8 -6
- package/node/audits/hooks.js +7 -34
- package/node/auth/acceptInvitation.js +7 -5
- package/node/auth/activateAccount.js +7 -4
- package/node/auth/allAccounts.js +27 -0
- package/node/auth/apiTokens.js +8 -6
- package/node/auth/applications.js +17 -0
- package/node/auth/customLogin.js +11 -12
- package/node/auth/entitlements.js +106 -22
- package/node/auth/forgotPassword.js +8 -6
- package/node/auth/groups.js +12 -12
- package/node/auth/hooks.js +80 -66
- package/node/auth/impersonate.js +7 -6
- package/node/auth/index.js +107 -93
- package/node/auth/login.js +7 -6
- package/node/auth/mfa.js +10 -4
- package/node/auth/passkeys.js +7 -6
- package/node/auth/profile.js +10 -9
- package/node/auth/provisioning.js +7 -6
- package/node/auth/resetPhoneNumber.js +7 -6
- package/node/auth/roles.js +7 -6
- package/node/auth/security/index.js +49 -0
- package/node/auth/security/restrictions.js +10 -10
- package/node/auth/security/securityCenter.js +8 -6
- package/node/auth/security/securityPolicy.js +16 -30
- package/node/auth/security/sessionsPolicy.js +8 -6
- package/node/auth/sessions.js +7 -6
- package/node/auth/signup.js +10 -4
- package/node/auth/sms.js +16 -0
- package/node/auth/socialLogin.js +7 -4
- package/node/auth/sso.js +7 -4
- package/node/auth/stepUp.js +62 -0
- package/node/auth/team.js +25 -4
- package/node/auth/tenants.js +8 -6
- package/node/common/CustomComponentHolder.js +3 -1
- package/node/common/index.js +10 -3
- package/node/connectivity/hooks.js +5 -8
- package/node/index.js +16 -16
- package/node/subscriptions/hooks.js +84 -51
- package/node/tests/hooks.tsc-test.js +339 -0
- package/node/useSnapshot/index.js +137 -0
- package/node/vendor/hooks.js +5 -6
- package/package.json +4 -5
- package/subscriptions/hooks.d.ts +32 -10
- package/subscriptions/hooks.js +81 -49
- package/tests/hooks.tsc-test.js +338 -0
- package/useSnapshot/index.d.ts +78 -0
- package/useSnapshot/index.js +128 -0
- package/useSnapshot/package.json +6 -0
- package/vendor/hooks.d.ts +0 -1
- package/vendor/hooks.js +9 -7
- package/auth/MSP/allAccounts.js +0 -15
- 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
|
+
}
|
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 {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
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
|
-
|
|
9
|
+
const state = useStore().store.vendor;
|
|
10
|
+
return stateMapper(useSnapshot(state));
|
|
8
11
|
}
|
|
9
12
|
export const useVendorActions = () => {
|
|
10
|
-
|
|
11
|
-
return useMemo(() => bindActionCreators(vendorActions, dispatch), [vendorActions]);
|
|
13
|
+
return useStore().stateActions.vendor;
|
|
12
14
|
};
|
package/auth/MSP/allAccounts.js
DELETED
|
@@ -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;
|