@ic-reactor/react 1.3.1 → 1.3.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.
@@ -4,3 +4,4 @@ export * from "./useAgentManager";
4
4
  export * from "./useAuth";
5
5
  export * from "./useAuthState";
6
6
  export * from "./useUserPrincipal";
7
+ export * from "./useCandidAdapter";
@@ -20,3 +20,4 @@ __exportStar(require("./useAgentManager"), exports);
20
20
  __exportStar(require("./useAuth"), exports);
21
21
  __exportStar(require("./useAuthState"), exports);
22
22
  __exportStar(require("./useUserPrincipal"), exports);
23
+ __exportStar(require("./useCandidAdapter"), exports);
@@ -0,0 +1,30 @@
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 `CandidAdapter` to download the actor's Candid interface.
10
+ *
11
+ * @param config - `UseCandidAdapterParams` The configuration object.
12
+ * @returns The `CandidAdapter` instance.
13
+ *
14
+ * @example
15
+ * ```jsx
16
+ * function CandidAdapterComponent() {
17
+ * const candidAdapter = useCandidAdapter();
18
+ *
19
+ * const getActor = async () => {
20
+ * const { idlFactory } = await candidAdapter.getCandidDefinition(canisterId)
21
+ * console.log(idlFactory)
22
+ * }
23
+ *
24
+ * return (
25
+ * <button onClick={getActor}>Get Actor</button>
26
+ * );
27
+ * }
28
+ *```
29
+ */
30
+ export declare const useCandidAdapter: (config: UseCandidAdapterParams) => CandidAdapter | undefined;
@@ -0,0 +1,48 @@
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 `CandidAdapter` to download the actor's Candid interface.
9
+ *
10
+ * @param config - `UseCandidAdapterParams` The configuration object.
11
+ * @returns The `CandidAdapter` instance.
12
+ *
13
+ * @example
14
+ * ```jsx
15
+ * function CandidAdapterComponent() {
16
+ * const candidAdapter = useCandidAdapter();
17
+ *
18
+ * const getActor = async () => {
19
+ * const { idlFactory } = await candidAdapter.getCandidDefinition(canisterId)
20
+ * console.log(idlFactory)
21
+ * }
22
+ *
23
+ * return (
24
+ * <button onClick={getActor}>Get Actor</button>
25
+ * );
26
+ * }
27
+ *```
28
+ */
29
+ const useCandidAdapter = (config) => {
30
+ const [candidAdapter, setCandidAdapter] = (0, react_1.useState)();
31
+ const agentManager = (0, useAgentManager_1.useAgentManager)(config.agentContext);
32
+ (0, react_1.useEffect)(() => {
33
+ const agent = agentManager.getAgent();
34
+ try {
35
+ const candidManager = (0, core_1.createCandidAdapter)({
36
+ agent,
37
+ didjsCanisterId: config.didjsCanisterId,
38
+ });
39
+ setCandidAdapter(candidManager);
40
+ }
41
+ catch (error) {
42
+ // eslint-disable-next-line no-console
43
+ console.error("Error creating CandidAdapter", error);
44
+ }
45
+ }, [agentManager, config.didjsCanisterId]);
46
+ return candidAdapter;
47
+ };
48
+ 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,28 +133,27 @@ 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);
132
138
  const initialActorManager = (0, react_1.useCallback)((idlFactory) => {
133
139
  if (authenticating)
134
140
  return;
135
141
  const actorManager = (0, core_1.createActorManager)(Object.assign({ agentManager,
136
142
  idlFactory,
137
143
  canisterId }, actorConfig));
138
- setActorManager(() => actorManager);
144
+ setActorManager(actorManager);
139
145
  }, [canisterId, agentManager, authenticating]);
140
146
  (0, react_1.useEffect)(() => {
141
147
  if (maybeIdlFactory) {
142
148
  initialActorManager(maybeIdlFactory);
143
149
  }
144
150
  else {
145
- setActorManager(undefined);
146
151
  fetchCandid().then((idlFactory) => {
147
152
  if (!idlFactory)
148
153
  return;
149
154
  initialActorManager(idlFactory);
150
155
  });
151
156
  }
152
- return actorManager === null || actorManager === void 0 ? void 0 : actorManager.cleanup();
153
157
  }, [fetchCandid, maybeIdlFactory, initialActorManager]);
154
158
  const hooks = (0, react_1.useMemo)(() => {
155
159
  if (!actorManager)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ic-reactor/react",
3
- "version": "1.3.1",
3
+ "version": "1.3.3",
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",
@@ -47,5 +47,5 @@
47
47
  "react": ">=16.8",
48
48
  "zustand": "4.5"
49
49
  },
50
- "gitHead": "05130e2581edfafd6390cf04ba618cb084bafc31"
50
+ "gitHead": "a471daafc519bd06902e773058748241c1f0331b"
51
51
  }