@ic-reactor/react 2.0.0-alpha.0 → 2.0.1
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/context/actor/create.js +28 -27
- package/dist/context/actor/hooks/useActorInterface.d.ts +1 -1
- package/dist/context/actor/hooks/useActorInterface.js +1 -2
- package/dist/context/actor/hooks/useMethod.d.ts +3 -3
- package/dist/context/actor/hooks/useMethod.js +4 -5
- package/dist/context/actor/hooks/useQueryCall.d.ts +5 -5
- package/dist/context/actor/hooks/useQueryCall.js +4 -5
- package/dist/context/actor/hooks/useUpdateCall.d.ts +6 -6
- package/dist/context/actor/hooks/useUpdateCall.js +5 -6
- package/dist/context/actor/hooks/useVisitMethod.d.ts +1 -1
- package/dist/context/actor/hooks/useVisitMethod.js +1 -2
- package/dist/context/actor/hooks/useVisitService.js +1 -2
- package/dist/context/actor/provider.d.ts +22 -1
- package/dist/context/actor/provider.js +23 -1
- package/dist/context/actor/types.d.ts +6 -2
- package/dist/context/adapter/create.d.ts +2 -2
- package/dist/context/adapter/create.js +24 -32
- package/dist/context/adapter/index.d.ts +1 -2
- package/dist/context/adapter/provider.d.ts +0 -1
- package/dist/context/adapter/types.d.ts +33 -6
- package/dist/context/agent/create.js +11 -19
- package/dist/context/agent/hooks/useAuth.d.ts +1 -1
- package/dist/context/agent/index.d.ts +2 -1
- package/dist/context/agent/index.js +2 -1
- package/dist/context/agent/provider.d.ts +0 -1
- package/dist/createReactor.js +7 -2
- package/dist/helpers/actorHooks.js +111 -59
- package/dist/helpers/authHooks.js +46 -51
- package/dist/helpers/extractActorContext.d.ts +1 -1
- package/dist/helpers/extractActorContext.js +2 -3
- package/dist/helpers/types.d.ts +138 -20
- package/dist/hooks/index.d.ts +1 -0
- package/dist/hooks/index.js +1 -0
- package/dist/hooks/types.d.ts +9 -4
- package/dist/hooks/useActor.js +37 -38
- package/dist/hooks/useActorManager.d.ts +68 -0
- package/dist/hooks/useActorManager.js +75 -0
- package/dist/index.js +17 -7
- package/dist/types.d.ts +1 -2
- package/package.json +17 -16
|
@@ -1,20 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
|
3
|
-
var t = {};
|
|
4
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
5
|
-
t[p] = s[p];
|
|
6
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
7
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
8
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
9
|
-
t[p[i]] = s[p[i]];
|
|
10
|
-
}
|
|
11
|
-
return t;
|
|
12
|
-
};
|
|
13
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
14
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
15
4
|
};
|
|
16
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.createAgentContext =
|
|
6
|
+
exports.createAgentContext = createAgentContext;
|
|
18
7
|
const react_1 = __importDefault(require("react"));
|
|
19
8
|
const core_1 = require("@ic-reactor/core");
|
|
20
9
|
const agentHooks_1 = require("../../helpers/agentHooks");
|
|
@@ -93,20 +82,23 @@ const extractAgentContext_1 = require("../../helpers/extractAgentContext");
|
|
|
93
82
|
* with the Internet Computer blockchain.
|
|
94
83
|
*/
|
|
95
84
|
function createAgentContext(config = {}) {
|
|
96
|
-
const { disableAuthenticateOnMount: defaultDisable
|
|
85
|
+
const { disableAuthenticateOnMount: defaultDisable, ...contextOptions } = config;
|
|
97
86
|
const AgentContext = react_1.default.createContext(null);
|
|
98
|
-
const AgentProvider = (
|
|
99
|
-
var { children, agentManager: mybeAgentManager, disableAuthenticateOnMount = defaultDisable !== null && defaultDisable !== void 0 ? defaultDisable : false } = _a, options = __rest(_a, ["children", "agentManager", "disableAuthenticateOnMount"]);
|
|
87
|
+
const AgentProvider = ({ children, agentManager: mybeAgentManager, disableAuthenticateOnMount = defaultDisable ?? false, ...options }) => {
|
|
100
88
|
const hooks = react_1.default.useMemo(() => {
|
|
101
|
-
const agentManager = mybeAgentManager
|
|
89
|
+
const agentManager = mybeAgentManager ??
|
|
90
|
+
(0, core_1.createAgentManager)({ ...options, ...contextOptions });
|
|
102
91
|
if (!disableAuthenticateOnMount) {
|
|
103
92
|
agentManager.authenticate();
|
|
104
93
|
}
|
|
105
|
-
return
|
|
94
|
+
return {
|
|
95
|
+
...(0, agentHooks_1.agentHooks)(agentManager),
|
|
96
|
+
...(0, authHooks_1.authHooks)(agentManager),
|
|
97
|
+
agentManager,
|
|
98
|
+
};
|
|
106
99
|
}, []);
|
|
107
100
|
return (react_1.default.createElement(AgentContext.Provider, { value: hooks }, children));
|
|
108
101
|
};
|
|
109
102
|
AgentProvider.displayName = "AgentProvider";
|
|
110
|
-
return
|
|
103
|
+
return { AgentContext, AgentProvider, ...(0, extractAgentContext_1.extractAgentContext)(AgentContext) };
|
|
111
104
|
}
|
|
112
|
-
exports.createAgentContext = createAgentContext;
|
|
@@ -54,4 +54,4 @@
|
|
|
54
54
|
*
|
|
55
55
|
* This hook simplifies integrating authentication flows into your IC application, providing hooks for various stages of the authentication process.
|
|
56
56
|
*/
|
|
57
|
-
export declare const useAuth: (options?: import("../../../types").UseAuthParameters
|
|
57
|
+
export declare const useAuth: (options?: import("../../../types").UseAuthParameters) => import("../../../types").UseAuthReturnType;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
import { createAgentContext } from "./create";
|
|
2
|
+
export { createAgentContext };
|
|
2
3
|
/** @ignore */
|
|
3
4
|
export declare const AgentHooks: import("./types").CreateAgentContextReturnType;
|
|
4
5
|
export declare const AgentContext: import("react").Context<import("./types").AgentContext | null>;
|
|
@@ -14,8 +14,9 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.AgentContext = exports.AgentHooks = void 0;
|
|
17
|
+
exports.AgentContext = exports.AgentHooks = exports.createAgentContext = void 0;
|
|
18
18
|
const create_1 = require("./create");
|
|
19
|
+
Object.defineProperty(exports, "createAgentContext", { enumerable: true, get: function () { return create_1.createAgentContext; } });
|
|
19
20
|
/** @ignore */
|
|
20
21
|
exports.AgentHooks = (0, create_1.createAgentContext)();
|
|
21
22
|
exports.AgentContext = exports.AgentHooks.AgentContext;
|
package/dist/createReactor.js
CHANGED
|
@@ -58,7 +58,12 @@ const createReactor = (config) => {
|
|
|
58
58
|
const getAgent = () => {
|
|
59
59
|
return actorManager.agentManager.getAgent();
|
|
60
60
|
};
|
|
61
|
-
return
|
|
62
|
-
|
|
61
|
+
return {
|
|
62
|
+
getAgent,
|
|
63
|
+
getVisitFunction,
|
|
64
|
+
...(0, helpers_1.actorHooks)(actorManager),
|
|
65
|
+
...(0, helpers_1.authHooks)(actorManager.agentManager),
|
|
66
|
+
...(0, helpers_1.agentHooks)(actorManager.agentManager),
|
|
67
|
+
};
|
|
63
68
|
};
|
|
64
69
|
exports.createReactor = createReactor;
|
|
@@ -15,33 +15,23 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
|
|
|
15
15
|
}) : function(o, v) {
|
|
16
16
|
o["default"] = v;
|
|
17
17
|
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
return
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
var t = {};
|
|
36
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
37
|
-
t[p] = s[p];
|
|
38
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
39
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
40
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
41
|
-
t[p[i]] = s[p[i]];
|
|
42
|
-
}
|
|
43
|
-
return t;
|
|
44
|
-
};
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
45
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
46
36
|
exports.actorHooks = void 0;
|
|
47
37
|
const React = __importStar(require("react"));
|
|
@@ -52,6 +42,7 @@ const DEFAULT_STATE = {
|
|
|
52
42
|
data: undefined,
|
|
53
43
|
error: undefined,
|
|
54
44
|
loading: false,
|
|
45
|
+
isLoading: false,
|
|
55
46
|
};
|
|
56
47
|
/**
|
|
57
48
|
* Provides a set of React hooks designed for interacting with actors in an Internet Computer (IC) project using the React framework and Zustand for state management.
|
|
@@ -78,13 +69,15 @@ const actorHooks = (actorManager) => {
|
|
|
78
69
|
name: state.name,
|
|
79
70
|
error: state.error,
|
|
80
71
|
version: state.version,
|
|
81
|
-
initialized: state.
|
|
82
|
-
|
|
72
|
+
initialized: state.isInitialized,
|
|
73
|
+
isInitialized: state.isInitialized,
|
|
74
|
+
initializing: state.isInitializing,
|
|
75
|
+
isInitializing: state.isInitializing,
|
|
83
76
|
canisterId,
|
|
84
77
|
}));
|
|
85
78
|
};
|
|
86
79
|
const useMethodState = (functionName, requestKey) => {
|
|
87
|
-
const state = useActorStore((state) =>
|
|
80
|
+
const state = useActorStore((state) => state.methodState[functionName]?.[requestKey]);
|
|
88
81
|
const setSharedState = React.useCallback((newState) => {
|
|
89
82
|
updateMethodState(functionName, requestKey, newState);
|
|
90
83
|
}, [functionName, requestKey]);
|
|
@@ -113,43 +106,81 @@ const actorHooks = (actorManager) => {
|
|
|
113
106
|
return visitFunction[functionName];
|
|
114
107
|
}, [functionName]);
|
|
115
108
|
};
|
|
116
|
-
const useSharedCall = (
|
|
117
|
-
var { args = [], functionName, throwOnError = false, onError, onLoading, onSuccess } = _a, options = __rest(_a, ["args", "functionName", "throwOnError", "onError", "onLoading", "onSuccess"]);
|
|
109
|
+
const useSharedCall = ({ args = [], functionName, throwOnError = false, onError, onLoading, onSuccess, onSuccessResult, onErrorResult, ...options }) => {
|
|
118
110
|
const requestKey = React.useMemo(() => (0, utils_1.generateRequestHash)(args), [args]);
|
|
119
111
|
const [sharedState, setSharedState] = useMethodState(functionName, requestKey);
|
|
120
|
-
const
|
|
121
|
-
const
|
|
122
|
-
|
|
123
|
-
|
|
112
|
+
const latestDataRef = React.useRef(null);
|
|
113
|
+
const reset = React.useCallback(() => {
|
|
114
|
+
updateMethodState(functionName, requestKey, DEFAULT_STATE);
|
|
115
|
+
latestDataRef.current = null;
|
|
116
|
+
}, [functionName, requestKey]);
|
|
117
|
+
const call = React.useCallback(async (eventOrReplaceArgs) => {
|
|
118
|
+
setSharedState({ error: undefined, loading: true, isLoading: true });
|
|
119
|
+
onLoading?.(true);
|
|
124
120
|
try {
|
|
125
121
|
const replaceArgs = eventOrReplaceArgs instanceof Array ? eventOrReplaceArgs : args;
|
|
126
|
-
const data =
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
122
|
+
const data = await callMethodWithOptions(options)(functionName, ...(replaceArgs ?? args));
|
|
123
|
+
latestDataRef.current = data;
|
|
124
|
+
setSharedState({
|
|
125
|
+
data,
|
|
126
|
+
error: undefined,
|
|
127
|
+
loading: false,
|
|
128
|
+
isLoading: false,
|
|
129
|
+
});
|
|
130
|
+
onLoading?.(false);
|
|
131
|
+
onSuccess?.(data);
|
|
132
|
+
const { isOk, value, error } = (0, utils_1.createCompiledResult)(data);
|
|
133
|
+
if (isOk) {
|
|
134
|
+
onSuccessResult?.(value);
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
onErrorResult?.(error);
|
|
138
|
+
if (throwOnError) {
|
|
139
|
+
throw error;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
130
142
|
return data;
|
|
131
143
|
}
|
|
132
144
|
catch (error) {
|
|
133
145
|
// eslint-disable-next-line no-console
|
|
134
146
|
console.error(`Error calling method ${functionName}:`, error);
|
|
147
|
+
latestDataRef.current = null;
|
|
135
148
|
setSharedState({
|
|
136
149
|
error: error,
|
|
137
150
|
loading: false,
|
|
151
|
+
isLoading: false,
|
|
138
152
|
});
|
|
139
|
-
onError
|
|
140
|
-
onLoading
|
|
153
|
+
onError?.(error);
|
|
154
|
+
onLoading?.(false);
|
|
141
155
|
if (throwOnError)
|
|
142
156
|
throw error;
|
|
157
|
+
return undefined;
|
|
143
158
|
}
|
|
144
|
-
}
|
|
145
|
-
|
|
159
|
+
}, [
|
|
160
|
+
args,
|
|
161
|
+
functionName,
|
|
162
|
+
options,
|
|
163
|
+
onError,
|
|
164
|
+
onLoading,
|
|
165
|
+
onSuccess,
|
|
166
|
+
onSuccessResult,
|
|
167
|
+
throwOnError,
|
|
168
|
+
]);
|
|
169
|
+
const compileResult = () => {
|
|
170
|
+
return (0, utils_1.createCompiledResult)(latestDataRef.current || sharedState?.data);
|
|
171
|
+
};
|
|
172
|
+
return {
|
|
173
|
+
call,
|
|
174
|
+
reset,
|
|
175
|
+
compileResult,
|
|
176
|
+
requestKey,
|
|
177
|
+
...sharedState,
|
|
178
|
+
};
|
|
146
179
|
};
|
|
147
|
-
const useQueryCall = (
|
|
148
|
-
|
|
149
|
-
const
|
|
150
|
-
const intervalId = React.useRef();
|
|
180
|
+
const useQueryCall = ({ refetchOnMount = true, refetchInterval = false, ...rest }) => {
|
|
181
|
+
const { call, requestKey, ...state } = useSharedCall(rest);
|
|
182
|
+
const intervalId = React.useRef(null);
|
|
151
183
|
React.useEffect(() => {
|
|
152
|
-
var _a;
|
|
153
184
|
if (refetchInterval) {
|
|
154
185
|
intervalId.current = setInterval(call, refetchInterval);
|
|
155
186
|
}
|
|
@@ -157,11 +188,25 @@ const actorHooks = (actorManager) => {
|
|
|
157
188
|
call();
|
|
158
189
|
}
|
|
159
190
|
else if (refetchOnMount && state.data !== undefined) {
|
|
160
|
-
|
|
191
|
+
rest.onSuccess?.(state.data);
|
|
192
|
+
const { isOk, value, error } = (0, utils_1.createCompiledResult)(state.data);
|
|
193
|
+
if (isOk) {
|
|
194
|
+
rest.onSuccessResult?.(value);
|
|
195
|
+
}
|
|
196
|
+
else {
|
|
197
|
+
rest.onErrorResult?.(error);
|
|
198
|
+
}
|
|
161
199
|
}
|
|
162
|
-
return () =>
|
|
163
|
-
|
|
164
|
-
|
|
200
|
+
return () => {
|
|
201
|
+
if (intervalId.current) {
|
|
202
|
+
clearInterval(intervalId.current);
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
}, [refetchInterval, refetchOnMount, requestKey]);
|
|
206
|
+
const refetch = () => {
|
|
207
|
+
call();
|
|
208
|
+
};
|
|
209
|
+
return { call, refetch, intervalId, requestKey, ...state };
|
|
165
210
|
};
|
|
166
211
|
const useUpdateCall = useSharedCall;
|
|
167
212
|
const useMethod = (params) => {
|
|
@@ -195,21 +240,28 @@ const actorHooks = (actorManager) => {
|
|
|
195
240
|
}, [attributes]);
|
|
196
241
|
let refetchOnMount = params.refetchOnMount;
|
|
197
242
|
let refetchInterval = params.refetchInterval;
|
|
198
|
-
let
|
|
243
|
+
let isFormRequired = true;
|
|
199
244
|
switch (attributes.type) {
|
|
200
245
|
case "query":
|
|
201
246
|
if (validateArgs(params.args)) {
|
|
202
|
-
|
|
247
|
+
isFormRequired = params.refetchOnMount === false ? true : false;
|
|
203
248
|
}
|
|
204
249
|
else {
|
|
205
250
|
refetchOnMount = false;
|
|
206
251
|
refetchInterval = false;
|
|
207
252
|
}
|
|
208
|
-
return
|
|
209
|
-
|
|
210
|
-
|
|
253
|
+
return {
|
|
254
|
+
visit,
|
|
255
|
+
validateArgs,
|
|
256
|
+
...useQueryCall({
|
|
257
|
+
...params,
|
|
258
|
+
refetchOnMount,
|
|
259
|
+
refetchInterval,
|
|
260
|
+
}),
|
|
261
|
+
isFormRequired,
|
|
262
|
+
};
|
|
211
263
|
case "update":
|
|
212
|
-
return
|
|
264
|
+
return { visit, validateArgs, ...useUpdateCall(params), isFormRequired };
|
|
213
265
|
default:
|
|
214
266
|
throw new Error(`Method type ${attributes.type} not found`);
|
|
215
267
|
}
|
|
@@ -1,13 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
4
|
};
|
|
@@ -19,100 +10,104 @@ const utils_1 = require("@ic-reactor/core/dist/utils");
|
|
|
19
10
|
const authHooks = (agentManager) => {
|
|
20
11
|
const { authenticate: authenticator, getIsLocal, getAuth, authStore, } = agentManager;
|
|
21
12
|
const useAuthState = () => (0, zustand_1.useStore)(authStore);
|
|
22
|
-
const useUserPrincipal = () =>
|
|
13
|
+
const useUserPrincipal = () => useAuthState()?.identity?.getPrincipal();
|
|
23
14
|
const useAuth = ({ onAuthentication, onAuthenticationSuccess, onAuthenticationFailure, onLogin, onLoginSuccess, onLoginError, onLoggedOut, } = {}) => {
|
|
24
|
-
const network = react_1.default.useRef("ic");
|
|
25
15
|
const [loginState, setLoginState] = react_1.default.useState({
|
|
26
16
|
loading: false,
|
|
17
|
+
isLoading: false,
|
|
27
18
|
error: undefined,
|
|
28
19
|
});
|
|
29
|
-
const {
|
|
30
|
-
const authenticate = react_1.default.useCallback(() =>
|
|
20
|
+
const { isAuthenticated, isAuthenticating, error, identity } = useAuthState();
|
|
21
|
+
const authenticate = react_1.default.useCallback(async () => {
|
|
31
22
|
const authenticatePromise = new Promise((resolve, reject) => {
|
|
32
23
|
authenticator()
|
|
33
24
|
.then((identity) => {
|
|
34
|
-
onAuthenticationSuccess
|
|
25
|
+
onAuthenticationSuccess?.(identity);
|
|
35
26
|
resolve(identity);
|
|
36
27
|
})
|
|
37
28
|
.catch((e) => {
|
|
38
|
-
onAuthenticationFailure
|
|
29
|
+
onAuthenticationFailure?.(e);
|
|
39
30
|
reject(e);
|
|
40
31
|
});
|
|
41
32
|
});
|
|
42
|
-
onAuthentication
|
|
33
|
+
onAuthentication?.(() => authenticatePromise);
|
|
43
34
|
return authenticatePromise;
|
|
44
|
-
}
|
|
35
|
+
}, [
|
|
45
36
|
authenticator,
|
|
46
37
|
onAuthentication,
|
|
47
38
|
onAuthenticationSuccess,
|
|
48
39
|
onAuthenticationFailure,
|
|
49
40
|
]);
|
|
50
|
-
const login = react_1.default.useCallback((options) =>
|
|
51
|
-
setLoginState({ loading: true, error: undefined });
|
|
41
|
+
const login = react_1.default.useCallback(async (options) => {
|
|
42
|
+
setLoginState({ loading: true, isLoading: true, error: undefined });
|
|
52
43
|
const loginPromise = new Promise((resolve, reject) => {
|
|
53
44
|
try {
|
|
54
45
|
const authClient = getAuth();
|
|
55
46
|
if (!authClient) {
|
|
56
47
|
throw new Error("Auth client not initialized");
|
|
57
48
|
}
|
|
58
|
-
authClient.login(
|
|
49
|
+
authClient.login({
|
|
50
|
+
identityProvider: getIsLocal()
|
|
59
51
|
? utils_1.LOCAL_INTERNET_IDENTITY_PROVIDER
|
|
60
|
-
: utils_1.IC_INTERNET_IDENTITY_PROVIDER
|
|
52
|
+
: utils_1.IC_INTERNET_IDENTITY_PROVIDER,
|
|
53
|
+
...options,
|
|
54
|
+
onSuccess: (msg) => {
|
|
61
55
|
authenticate()
|
|
62
56
|
.then((identity) => {
|
|
63
|
-
var _a;
|
|
64
57
|
const principal = identity.getPrincipal();
|
|
65
|
-
|
|
66
|
-
onLoginSuccess
|
|
58
|
+
options?.onSuccess?.(msg);
|
|
59
|
+
onLoginSuccess?.(principal);
|
|
67
60
|
resolve(principal);
|
|
68
|
-
setLoginState({
|
|
61
|
+
setLoginState({
|
|
62
|
+
loading: false,
|
|
63
|
+
isLoading: false,
|
|
64
|
+
error: undefined,
|
|
65
|
+
});
|
|
69
66
|
})
|
|
70
67
|
.catch((error) => {
|
|
71
|
-
setLoginState({ loading: false, error });
|
|
72
|
-
onLoginError
|
|
68
|
+
setLoginState({ loading: false, isLoading: false, error });
|
|
69
|
+
onLoginError?.(error);
|
|
73
70
|
reject(error);
|
|
74
71
|
});
|
|
75
|
-
},
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
setLoginState({ loading: false, error });
|
|
79
|
-
onLoginError
|
|
72
|
+
},
|
|
73
|
+
onError: (error) => {
|
|
74
|
+
options?.onError?.(error);
|
|
75
|
+
setLoginState({ loading: false, isLoading: false, error });
|
|
76
|
+
onLoginError?.(error);
|
|
80
77
|
reject(error);
|
|
81
|
-
}
|
|
78
|
+
},
|
|
79
|
+
});
|
|
82
80
|
}
|
|
83
81
|
catch (e) {
|
|
84
82
|
const error = e;
|
|
85
|
-
setLoginState({ loading: false, error });
|
|
86
|
-
onLoginError
|
|
83
|
+
setLoginState({ loading: false, isLoading: false, error });
|
|
84
|
+
onLoginError?.(error);
|
|
87
85
|
reject(error);
|
|
88
86
|
}
|
|
89
87
|
});
|
|
90
|
-
onLogin
|
|
91
|
-
}
|
|
92
|
-
const logout = react_1.default.useCallback((options) =>
|
|
88
|
+
onLogin?.(() => loginPromise);
|
|
89
|
+
}, [onLogin, onLoginSuccess, onLoginError, authenticate]);
|
|
90
|
+
const logout = react_1.default.useCallback(async (options) => {
|
|
93
91
|
const authClient = getAuth();
|
|
94
92
|
if (!authClient) {
|
|
95
93
|
throw new Error("Auth client not initialized");
|
|
96
94
|
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
onLoggedOut
|
|
100
|
-
}
|
|
101
|
-
react_1.default.useEffect(() => agentManager.subscribeAgentState((state) => {
|
|
102
|
-
if (network.current !== state.network) {
|
|
103
|
-
network.current = state.network;
|
|
104
|
-
authenticate();
|
|
105
|
-
}
|
|
106
|
-
}), []);
|
|
95
|
+
await authClient.logout(options);
|
|
96
|
+
await authenticate();
|
|
97
|
+
onLoggedOut?.();
|
|
98
|
+
}, [onLoggedOut]);
|
|
107
99
|
return {
|
|
108
|
-
|
|
109
|
-
|
|
100
|
+
isAuthenticated,
|
|
101
|
+
isAuthenticating,
|
|
102
|
+
authenticated: isAuthenticated,
|
|
103
|
+
authenticating: isAuthenticating,
|
|
110
104
|
identity,
|
|
111
105
|
error,
|
|
112
106
|
login,
|
|
113
107
|
logout,
|
|
114
108
|
authenticate,
|
|
115
|
-
loginLoading: loginState.
|
|
109
|
+
loginLoading: loginState.isLoading,
|
|
110
|
+
isLoginLoading: loginState.isLoading,
|
|
116
111
|
loginError: loginState.error,
|
|
117
112
|
};
|
|
118
113
|
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import React from "react";
|
|
2
2
|
import type { BaseActor } from "../types";
|
|
3
3
|
import type { CreateActorContextReturnType, CreateActorContextType } from "../context/actor/types";
|
|
4
|
-
export declare function extractActorContext<A = BaseActor>(actorContext: React.Context<CreateActorContextType<A> | null>): Omit<CreateActorContextReturnType<A>, "ActorProvider" | "ActorHookProvider">;
|
|
4
|
+
export declare function extractActorContext<A = BaseActor>(actorContext: React.Context<CreateActorContextType<A> | null>): Omit<CreateActorContextReturnType<A>, "ActorProvider" | "ActorHookProvider" | "ActorManagerProvider">;
|
|
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.extractActorContext =
|
|
6
|
+
exports.extractActorContext = extractActorContext;
|
|
7
7
|
const react_1 = __importDefault(require("react"));
|
|
8
8
|
function extractActorContext(actorContext) {
|
|
9
9
|
const useActorContext = () => {
|
|
@@ -26,7 +26,7 @@ function extractActorContext(actorContext) {
|
|
|
26
26
|
const useVisitMethod = (functionName) => useActorContext().useVisitMethod(functionName);
|
|
27
27
|
const useVisitService = () => useActorContext().useVisitService();
|
|
28
28
|
const useActorInterface = () => useActorContext().useActorInterface();
|
|
29
|
-
const useInitializeActor = () =>
|
|
29
|
+
const useInitializeActor = () => useActorContext().useInitializeActor?.();
|
|
30
30
|
return {
|
|
31
31
|
useActorStore,
|
|
32
32
|
useActorState,
|
|
@@ -42,4 +42,3 @@ function extractActorContext(actorContext) {
|
|
|
42
42
|
initialize,
|
|
43
43
|
};
|
|
44
44
|
}
|
|
45
|
-
exports.extractActorContext = extractActorContext;
|