@ic-reactor/react 2.0.0-alpha.0 → 2.0.0

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 (40) hide show
  1. package/dist/context/actor/create.js +28 -27
  2. package/dist/context/actor/hooks/useActorInterface.d.ts +1 -1
  3. package/dist/context/actor/hooks/useActorInterface.js +1 -2
  4. package/dist/context/actor/hooks/useMethod.d.ts +3 -3
  5. package/dist/context/actor/hooks/useMethod.js +4 -5
  6. package/dist/context/actor/hooks/useQueryCall.d.ts +5 -5
  7. package/dist/context/actor/hooks/useQueryCall.js +4 -5
  8. package/dist/context/actor/hooks/useUpdateCall.d.ts +6 -6
  9. package/dist/context/actor/hooks/useUpdateCall.js +5 -6
  10. package/dist/context/actor/hooks/useVisitMethod.d.ts +1 -1
  11. package/dist/context/actor/hooks/useVisitMethod.js +1 -2
  12. package/dist/context/actor/hooks/useVisitService.js +1 -2
  13. package/dist/context/actor/provider.d.ts +22 -1
  14. package/dist/context/actor/provider.js +23 -1
  15. package/dist/context/actor/types.d.ts +6 -2
  16. package/dist/context/adapter/create.d.ts +2 -2
  17. package/dist/context/adapter/create.js +24 -32
  18. package/dist/context/adapter/index.d.ts +1 -2
  19. package/dist/context/adapter/provider.d.ts +0 -1
  20. package/dist/context/adapter/types.d.ts +33 -6
  21. package/dist/context/agent/create.js +11 -19
  22. package/dist/context/agent/hooks/useAuth.d.ts +1 -1
  23. package/dist/context/agent/index.d.ts +2 -1
  24. package/dist/context/agent/index.js +2 -1
  25. package/dist/context/agent/provider.d.ts +0 -1
  26. package/dist/createReactor.js +7 -2
  27. package/dist/helpers/actorHooks.js +111 -59
  28. package/dist/helpers/authHooks.js +46 -51
  29. package/dist/helpers/extractActorContext.d.ts +1 -1
  30. package/dist/helpers/extractActorContext.js +2 -3
  31. package/dist/helpers/types.d.ts +138 -20
  32. package/dist/hooks/index.d.ts +1 -0
  33. package/dist/hooks/index.js +1 -0
  34. package/dist/hooks/types.d.ts +9 -4
  35. package/dist/hooks/useActor.js +37 -38
  36. package/dist/hooks/useActorManager.d.ts +68 -0
  37. package/dist/hooks/useActorManager.js +75 -0
  38. package/dist/index.js +17 -7
  39. package/dist/types.d.ts +1 -2
  40. 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 = void 0;
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 } = config, contextOptions = __rest(config, ["disableAuthenticateOnMount"]);
85
+ const { disableAuthenticateOnMount: defaultDisable, ...contextOptions } = config;
97
86
  const AgentContext = react_1.default.createContext(null);
98
- const AgentProvider = (_a) => {
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 !== null && mybeAgentManager !== void 0 ? mybeAgentManager : (0, core_1.createAgentManager)(Object.assign(Object.assign({}, options), contextOptions));
89
+ const agentManager = mybeAgentManager ??
90
+ (0, core_1.createAgentManager)({ ...options, ...contextOptions });
102
91
  if (!disableAuthenticateOnMount) {
103
92
  agentManager.authenticate();
104
93
  }
105
- return Object.assign(Object.assign(Object.assign({}, (0, agentHooks_1.agentHooks)(agentManager)), (0, authHooks_1.authHooks)(agentManager)), { agentManager });
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 Object.assign({ AgentContext, AgentProvider }, (0, extractAgentContext_1.extractAgentContext)(AgentContext));
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 | undefined) => import("../../../types").UseAuthReturnType;
57
+ export declare const useAuth: (options?: import("../../../types").UseAuthParameters) => import("../../../types").UseAuthReturnType;
@@ -1,4 +1,5 @@
1
- /// <reference types="react" />
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;
@@ -1,4 +1,3 @@
1
- /// <reference types="react" />
2
1
  /**
3
2
  * `AgentProvider` is a React functional component that serves as a context provider for IC agent and authentication hooks.
4
3
  * It enables any child components to access and use the agent and authentication functionalities seamlessly.
@@ -58,7 +58,12 @@ const createReactor = (config) => {
58
58
  const getAgent = () => {
59
59
  return actorManager.agentManager.getAgent();
60
60
  };
61
- return Object.assign(Object.assign(Object.assign({ getAgent,
62
- getVisitFunction }, (0, helpers_1.actorHooks)(actorManager)), (0, helpers_1.authHooks)(actorManager.agentManager)), (0, helpers_1.agentHooks)(actorManager.agentManager));
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 (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
- return new (P || (P = Promise))(function (resolve, reject) {
28
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
- step((generator = generator.apply(thisArg, _arguments || [])).next());
32
- });
33
- };
34
- var __rest = (this && this.__rest) || function (s, e) {
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.initialized,
82
- initializing: state.initializing,
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) => { var _a; return (_a = state.methodState[functionName]) === null || _a === void 0 ? void 0 : _a[requestKey]; });
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 = (_a) => {
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 reset = React.useCallback(() => updateMethodState(functionName, requestKey, DEFAULT_STATE), [functionName, requestKey]);
121
- const call = React.useCallback((eventOrReplaceArgs) => __awaiter(void 0, void 0, void 0, function* () {
122
- setSharedState({ error: undefined, loading: true });
123
- onLoading === null || onLoading === void 0 ? void 0 : onLoading(true);
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 = yield callMethodWithOptions(options)(functionName, ...(replaceArgs !== null && replaceArgs !== void 0 ? replaceArgs : args));
127
- setSharedState({ data, error: undefined, loading: false });
128
- onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(data);
129
- onLoading === null || onLoading === void 0 ? void 0 : onLoading(false);
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 === null || onError === void 0 ? void 0 : onError(error);
140
- onLoading === null || onLoading === void 0 ? void 0 : onLoading(false);
153
+ onError?.(error);
154
+ onLoading?.(false);
141
155
  if (throwOnError)
142
156
  throw error;
157
+ return undefined;
143
158
  }
144
- }), [args, functionName, options, onError, onLoading, onSuccess, throwOnError]);
145
- return Object.assign({ call, reset, requestKey }, sharedState);
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 = (_a) => {
148
- var { refetchOnMount = true, refetchInterval = false } = _a, rest = __rest(_a, ["refetchOnMount", "refetchInterval"]);
149
- const _b = useSharedCall(rest), { call } = _b, state = __rest(_b, ["call"]);
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
- (_a = rest.onSuccess) === null || _a === void 0 ? void 0 : _a.call(rest, state.data);
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 () => clearInterval(intervalId.current);
163
- }, [refetchInterval, refetchOnMount]);
164
- return Object.assign({ call, intervalId }, state);
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 formRequired = true;
243
+ let isFormRequired = true;
199
244
  switch (attributes.type) {
200
245
  case "query":
201
246
  if (validateArgs(params.args)) {
202
- formRequired = params.refetchOnMount === false ? true : false;
247
+ isFormRequired = params.refetchOnMount === false ? true : false;
203
248
  }
204
249
  else {
205
250
  refetchOnMount = false;
206
251
  refetchInterval = false;
207
252
  }
208
- return Object.assign(Object.assign({ visit,
209
- validateArgs }, useQueryCall(Object.assign(Object.assign({}, params), { refetchOnMount,
210
- refetchInterval }))), { formRequired });
253
+ return {
254
+ visit,
255
+ validateArgs,
256
+ ...useQueryCall({
257
+ ...params,
258
+ refetchOnMount,
259
+ refetchInterval,
260
+ }),
261
+ isFormRequired,
262
+ };
211
263
  case "update":
212
- return Object.assign(Object.assign({ visit, validateArgs }, useUpdateCall(params)), { formRequired });
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 = () => { var _a, _b; return (_b = (_a = useAuthState()) === null || _a === void 0 ? void 0 : _a.identity) === null || _b === void 0 ? void 0 : _b.getPrincipal(); };
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 { authenticated, authenticating, error, identity } = useAuthState();
30
- const authenticate = react_1.default.useCallback(() => __awaiter(void 0, void 0, void 0, function* () {
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 === null || onAuthenticationSuccess === void 0 ? void 0 : onAuthenticationSuccess(identity);
25
+ onAuthenticationSuccess?.(identity);
35
26
  resolve(identity);
36
27
  })
37
28
  .catch((e) => {
38
- onAuthenticationFailure === null || onAuthenticationFailure === void 0 ? void 0 : onAuthenticationFailure(e);
29
+ onAuthenticationFailure?.(e);
39
30
  reject(e);
40
31
  });
41
32
  });
42
- onAuthentication === null || onAuthentication === void 0 ? void 0 : onAuthentication(() => authenticatePromise);
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) => __awaiter(void 0, void 0, void 0, function* () {
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(Object.assign(Object.assign({ identityProvider: getIsLocal()
49
+ authClient.login({
50
+ identityProvider: getIsLocal()
59
51
  ? utils_1.LOCAL_INTERNET_IDENTITY_PROVIDER
60
- : utils_1.IC_INTERNET_IDENTITY_PROVIDER }, options), { onSuccess: (msg) => {
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
- (_a = options === null || options === void 0 ? void 0 : options.onSuccess) === null || _a === void 0 ? void 0 : _a.call(options, msg);
66
- onLoginSuccess === null || onLoginSuccess === void 0 ? void 0 : onLoginSuccess(principal);
58
+ options?.onSuccess?.(msg);
59
+ onLoginSuccess?.(principal);
67
60
  resolve(principal);
68
- setLoginState({ loading: false, error: undefined });
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 === null || onLoginError === void 0 ? void 0 : onLoginError(error);
68
+ setLoginState({ loading: false, isLoading: false, error });
69
+ onLoginError?.(error);
73
70
  reject(error);
74
71
  });
75
- }, onError: (error) => {
76
- var _a;
77
- (_a = options === null || options === void 0 ? void 0 : options.onError) === null || _a === void 0 ? void 0 : _a.call(options, error);
78
- setLoginState({ loading: false, error });
79
- onLoginError === null || onLoginError === void 0 ? void 0 : onLoginError(error);
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 === null || onLoginError === void 0 ? void 0 : onLoginError(error);
83
+ setLoginState({ loading: false, isLoading: false, error });
84
+ onLoginError?.(error);
87
85
  reject(error);
88
86
  }
89
87
  });
90
- onLogin === null || onLogin === void 0 ? void 0 : onLogin(() => loginPromise);
91
- }), [onLogin, onLoginSuccess, onLoginError, authenticate]);
92
- const logout = react_1.default.useCallback((options) => __awaiter(void 0, void 0, void 0, function* () {
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
- yield authClient.logout(options);
98
- yield authenticate();
99
- onLoggedOut === null || onLoggedOut === void 0 ? void 0 : onLoggedOut();
100
- }), [onLoggedOut]);
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
- authenticated,
109
- authenticating,
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.loading,
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 = void 0;
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 = () => { var _a, _b; return (_b = (_a = useActorContext()).useInitializeActor) === null || _b === void 0 ? void 0 : _b.call(_a); };
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;