@ic-reactor/react 1.3.0 → 1.3.2

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.
@@ -123,7 +123,7 @@ const actorHooks = (actorManager) => {
123
123
  throw new Error("Args required");
124
124
  }
125
125
  attributes.validate((args.args || []));
126
- formRequired = false;
126
+ formRequired = args.refetchOnMount === false ? true : false;
127
127
  }
128
128
  catch (error) {
129
129
  refetchOnMount = false;
@@ -0,0 +1,21 @@
1
+ /// <reference types="react" />
2
+ import { CandidAdapter } from "@ic-reactor/core/dist/classes";
3
+ import { AgentContext } from "../../types";
4
+ export interface UseCandidAdapterParams {
5
+ agentContext?: React.Context<AgentContext | null>;
6
+ didjsCanisterId?: string;
7
+ }
8
+ /**
9
+ * Accesses the `AgentManager` instance for managing agent configurations and state.
10
+ *
11
+ * @example
12
+ *```jsx
13
+ * function AgentManagerComponent() {
14
+ * const agentManager = useAgentManager();
15
+ *
16
+ * // Use agentManager for managing agent configurations, etc.
17
+ * return <div>Agent Manager ready.</div>;
18
+ * }
19
+ *```
20
+ */
21
+ export declare const useCandidAdapter: (config: UseCandidAdapterParams) => CandidAdapter | undefined;
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useCandidAdapter = void 0;
4
+ const useAgentManager_1 = require("./useAgentManager");
5
+ const react_1 = require("react");
6
+ const core_1 = require("@ic-reactor/core");
7
+ /**
8
+ * Accesses the `AgentManager` instance for managing agent configurations and state.
9
+ *
10
+ * @example
11
+ *```jsx
12
+ * function AgentManagerComponent() {
13
+ * const agentManager = useAgentManager();
14
+ *
15
+ * // Use agentManager for managing agent configurations, etc.
16
+ * return <div>Agent Manager ready.</div>;
17
+ * }
18
+ *```
19
+ */
20
+ const useCandidAdapter = (config) => {
21
+ const [candidAdapter, setCandidAdapter] = (0, react_1.useState)();
22
+ const agentManager = (0, useAgentManager_1.useAgentManager)(config.agentContext);
23
+ (0, react_1.useEffect)(() => {
24
+ const agent = agentManager.getAgent();
25
+ try {
26
+ const candidManager = (0, core_1.createCandidAdapter)({
27
+ agent,
28
+ didjsCanisterId: config.didjsCanisterId,
29
+ });
30
+ setCandidAdapter(candidManager);
31
+ }
32
+ catch (error) {
33
+ // eslint-disable-next-line no-console
34
+ console.error("Error creating CandidAdapter", error);
35
+ }
36
+ }, [agentManager, config.didjsCanisterId]);
37
+ return candidAdapter;
38
+ };
39
+ exports.useCandidAdapter = useCandidAdapter;
@@ -1,4 +1,4 @@
1
- import { type BaseActor } from "../types";
1
+ import type { BaseActor } from "../types";
2
2
  import type { UseActorParameters, UseActorReturn } from "./types";
3
3
  /**
4
4
  * A comprehensive hook that manages both the fetching of Candid interfaces
@@ -26,6 +26,7 @@ const react_1 = require("react");
26
26
  const useAgentManager_1 = require("./agent/useAgentManager");
27
27
  const helpers_1 = require("../helpers");
28
28
  const agent_1 = require("./agent");
29
+ const useCandidAdapter_1 = require("./agent/useCandidAdapter");
29
30
  /**
30
31
  * A comprehensive hook that manages both the fetching of Candid interfaces
31
32
  * and the initialization of actor stores for Internet Computer (IC) canisters.
@@ -93,27 +94,31 @@ const useActor = (config) => {
93
94
  if (!canisterId) {
94
95
  throw new Error("canisterId is required");
95
96
  }
96
- const [actorManager, setActorManager] = (0, react_1.useState)();
97
+ const [actorManager, setActorManager] = (0, react_1.useState)(null);
98
+ (0, react_1.useEffect)(() => {
99
+ if ((actorManager === null || actorManager === void 0 ? void 0 : actorManager.canisterId) !== canisterId) {
100
+ setActorManager(null);
101
+ }
102
+ return actorManager === null || actorManager === void 0 ? void 0 : actorManager.cleanup();
103
+ }, [canisterId, actorManager]);
97
104
  const [{ fetching, fetchError }, setState] = (0, react_1.useState)({
98
105
  fetching: false,
99
106
  fetchError: null,
100
107
  });
101
- const agentManager = (0, useAgentManager_1.useAgentManager)(agentContext);
108
+ const candidAdapter = (0, useCandidAdapter_1.useCandidAdapter)({
109
+ agentContext,
110
+ didjsCanisterId,
111
+ });
102
112
  const authenticating = (0, agent_1.useAuthState)().authenticating;
103
113
  const fetchCandid = (0, react_1.useCallback)(() => __awaiter(void 0, void 0, void 0, function* () {
104
- if (fetching || authenticating)
114
+ if (fetching || authenticating || !candidAdapter)
105
115
  return;
106
116
  setState({
107
117
  fetching: true,
108
118
  fetchError: null,
109
119
  });
110
- const agent = agentManager.getAgent();
111
120
  try {
112
- const candidManager = (0, core_1.createCandidAdapter)({
113
- agent,
114
- didjsCanisterId,
115
- });
116
- const { idlFactory } = yield candidManager.getCandidDefinition(canisterId);
121
+ const { idlFactory } = yield candidAdapter.getCandidDefinition(canisterId);
117
122
  setState({
118
123
  fetching: false,
119
124
  fetchError: null,
@@ -128,32 +133,33 @@ const useActor = (config) => {
128
133
  fetching: false,
129
134
  });
130
135
  }
131
- }), [canisterId, authenticating, didjsCanisterId]);
136
+ }), [canisterId, candidAdapter, authenticating, didjsCanisterId]);
137
+ const agentManager = (0, useAgentManager_1.useAgentManager)(agentContext);
138
+ const initialActorManager = (0, react_1.useCallback)((idlFactory) => {
139
+ if (authenticating)
140
+ return;
141
+ const actorManager = (0, core_1.createActorManager)(Object.assign({ agentManager,
142
+ idlFactory,
143
+ canisterId }, actorConfig));
144
+ setActorManager(actorManager);
145
+ }, [canisterId, agentManager, authenticating]);
132
146
  (0, react_1.useEffect)(() => {
133
147
  if (maybeIdlFactory) {
134
- const actorManager = (0, core_1.createActorManager)(Object.assign({ agentManager, idlFactory: maybeIdlFactory, canisterId }, actorConfig));
135
- setActorManager(() => actorManager);
148
+ initialActorManager(maybeIdlFactory);
136
149
  }
137
150
  else {
138
- setActorManager(undefined);
139
151
  fetchCandid().then((idlFactory) => {
140
152
  if (!idlFactory)
141
153
  return;
142
- const actorManager = (0, core_1.createActorManager)(Object.assign({ agentManager,
143
- idlFactory,
144
- canisterId }, actorConfig));
145
- setActorManager(() => actorManager);
154
+ initialActorManager(idlFactory);
146
155
  });
147
156
  }
148
- return actorManager === null || actorManager === void 0 ? void 0 : actorManager.cleanup();
149
- }, [fetchCandid, maybeIdlFactory, canisterId, agentManager]);
157
+ }, [fetchCandid, maybeIdlFactory, initialActorManager]);
150
158
  const hooks = (0, react_1.useMemo)(() => {
151
- if (fetching)
152
- return null;
153
159
  if (!actorManager)
154
160
  return null;
155
161
  return (0, helpers_1.actorHooks)(actorManager);
156
- }, [canisterId, authenticating, actorManager, fetching]);
162
+ }, [actorManager]);
157
163
  return { hooks, authenticating, fetching, fetchError };
158
164
  };
159
165
  exports.useActor = useActor;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ic-reactor/react",
3
- "version": "1.3.0",
3
+ "version": "1.3.2",
4
4
  "description": "A React library for interacting with Internet Computer canisters",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -35,7 +35,7 @@
35
35
  "node": ">=10"
36
36
  },
37
37
  "dependencies": {
38
- "@ic-reactor/core": "^1.3.0",
38
+ "@ic-reactor/core": "^1.3.1",
39
39
  "zustand-utils": "^1.3"
40
40
  },
41
41
  "peerDependencies": {
@@ -47,5 +47,5 @@
47
47
  "react": ">=16.8",
48
48
  "zustand": "4.5"
49
49
  },
50
- "gitHead": "25043c2d1a78b97e327995abaf13835ac511e5d6"
50
+ "gitHead": "02ebf0dfbd01792ded00b91a69b4196aab018285"
51
51
  }