@ic-reactor/react 1.15.0 → 1.15.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 +9 -1
- package/dist/context/actor/provider.d.ts +22 -0
- package/dist/context/actor/provider.js +23 -1
- package/dist/context/actor/types.d.ts +6 -1
- package/dist/helpers/extractActorContext.d.ts +1 -1
- package/dist/hooks/index.d.ts +1 -0
- package/dist/hooks/index.js +1 -0
- package/dist/hooks/types.d.ts +7 -1
- package/dist/hooks/useActorManager.d.ts +68 -0
- package/dist/hooks/useActorManager.js +75 -0
- package/package.json +2 -2
|
@@ -18,6 +18,7 @@ exports.createActorContext = createActorContext;
|
|
|
18
18
|
const react_1 = __importDefault(require("react"));
|
|
19
19
|
const useActor_1 = require("../../hooks/useActor");
|
|
20
20
|
const extractActorContext_1 = require("../../helpers/extractActorContext");
|
|
21
|
+
const useActorManager_1 = require("../../hooks/useActorManager");
|
|
21
22
|
/**
|
|
22
23
|
* Creates a React context specifically designed for managing the state and interactions with an actor on the Internet Computer (IC) blockchain.
|
|
23
24
|
* This context facilitates the dynamic creation and management of IC actors within React applications, leveraging the provided configuration options.
|
|
@@ -109,6 +110,13 @@ function createActorContext(contextConfig = {}) {
|
|
|
109
110
|
const ActorHookProvider = ({ hooks, children, }) => {
|
|
110
111
|
return (react_1.default.createElement(ActorContext.Provider, { value: hooks }, children));
|
|
111
112
|
};
|
|
113
|
+
const ActorManagerProvider = ({ actorManager, children, }) => {
|
|
114
|
+
const { hooks } = (0, useActorManager_1.useActorManager)({
|
|
115
|
+
actorManager,
|
|
116
|
+
});
|
|
117
|
+
return (react_1.default.createElement(ActorContext.Provider, { value: hooks }, children));
|
|
118
|
+
};
|
|
112
119
|
return Object.assign({ ActorProvider,
|
|
113
|
-
ActorHookProvider
|
|
120
|
+
ActorHookProvider,
|
|
121
|
+
ActorManagerProvider }, (0, extractActorContext_1.extractActorContext)(ActorContext));
|
|
114
122
|
}
|
|
@@ -47,3 +47,25 @@ export declare const ActorProvider: import("react").FC<import("./types").ActorPr
|
|
|
47
47
|
* This setup ensures that `YourComponent` and any of its children can interact with the specified IC actor hooks through the context provided by `ActorHookProvider`.
|
|
48
48
|
*/
|
|
49
49
|
export declare const ActorHookProvider: import("react").FC<import("./types").ActorHookProviderProps<import("@ic-reactor/core/dist/types").BaseActor>>;
|
|
50
|
+
/**
|
|
51
|
+
* `ActorManagerProvider` is a React functional component that serves as a context provider for IC actor managers within a React application.
|
|
52
|
+
* It wraps child components, providing them access to actor-specific manager functionalities based on the provided actor manager and configuration.
|
|
53
|
+
*
|
|
54
|
+
* Props:
|
|
55
|
+
* - `actorManager`: ActorManager - The actor manager object containing the actor manager functionalities.
|
|
56
|
+
* - `children`: React Node - The child components that will have access to the actor manager context.
|
|
57
|
+
*
|
|
58
|
+
* Behavior:
|
|
59
|
+
* - Validates the presence of the `actorManager`. Throws an error if it is missing, ensuring that the actor manager is always available for actor operations.
|
|
60
|
+
* - Utilizes `useMemo` to memoize the `actorManager`, optimizing for performance by avoiding unnecessary recalculations.
|
|
61
|
+
* - Renders the child components once the `actorManager` is available, effectively providing them access to the actor manager context.
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* ```jsx
|
|
65
|
+
* <ActorManagerProvider actorManager={yourActorManager}>
|
|
66
|
+
* <YourComponent />
|
|
67
|
+
* </ActorManagerProvider>
|
|
68
|
+
* ```
|
|
69
|
+
* This setup ensures that `YourComponent` and any of its children can interact with the specified IC actor manager through the context provided by `ActorManagerProvider`.
|
|
70
|
+
*/
|
|
71
|
+
export declare const ActorManagerProvider: import("react").FC<import("./types").ActorManagerProviderProps<import("@ic-reactor/core/dist/types").BaseActor>>;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ActorHookProvider = exports.ActorProvider = void 0;
|
|
3
|
+
exports.ActorManagerProvider = exports.ActorHookProvider = exports.ActorProvider = void 0;
|
|
4
4
|
const _1 = require(".");
|
|
5
5
|
/**
|
|
6
6
|
* `ActorProvider` is a React functional component that serves as a context provider for IC actor interactions within a React application.
|
|
@@ -51,3 +51,25 @@ exports.ActorProvider = _1.ActorHooks.ActorProvider;
|
|
|
51
51
|
* This setup ensures that `YourComponent` and any of its children can interact with the specified IC actor hooks through the context provided by `ActorHookProvider`.
|
|
52
52
|
*/
|
|
53
53
|
exports.ActorHookProvider = _1.ActorHooks.ActorHookProvider;
|
|
54
|
+
/**
|
|
55
|
+
* `ActorManagerProvider` is a React functional component that serves as a context provider for IC actor managers within a React application.
|
|
56
|
+
* It wraps child components, providing them access to actor-specific manager functionalities based on the provided actor manager and configuration.
|
|
57
|
+
*
|
|
58
|
+
* Props:
|
|
59
|
+
* - `actorManager`: ActorManager - The actor manager object containing the actor manager functionalities.
|
|
60
|
+
* - `children`: React Node - The child components that will have access to the actor manager context.
|
|
61
|
+
*
|
|
62
|
+
* Behavior:
|
|
63
|
+
* - Validates the presence of the `actorManager`. Throws an error if it is missing, ensuring that the actor manager is always available for actor operations.
|
|
64
|
+
* - Utilizes `useMemo` to memoize the `actorManager`, optimizing for performance by avoiding unnecessary recalculations.
|
|
65
|
+
* - Renders the child components once the `actorManager` is available, effectively providing them access to the actor manager context.
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* ```jsx
|
|
69
|
+
* <ActorManagerProvider actorManager={yourActorManager}>
|
|
70
|
+
* <YourComponent />
|
|
71
|
+
* </ActorManagerProvider>
|
|
72
|
+
* ```
|
|
73
|
+
* This setup ensures that `YourComponent` and any of its children can interact with the specified IC actor manager through the context provided by `ActorManagerProvider`.
|
|
74
|
+
*/
|
|
75
|
+
exports.ActorManagerProvider = _1.ActorHooks.ActorManagerProvider;
|
|
@@ -1,16 +1,21 @@
|
|
|
1
|
-
import type { IDL, ActorHooksReturnType, BaseActor, ActorManagerParameters, CanisterId, InitializeActor } from "../../types";
|
|
1
|
+
import type { IDL, ActorHooksReturnType, BaseActor, ActorManagerParameters, CanisterId, InitializeActor, ActorManager } from "../../types";
|
|
2
2
|
export interface CreateActorContextType<A = BaseActor> extends ActorHooksReturnType<A> {
|
|
3
3
|
useInitializeActor?: () => InitializeActor;
|
|
4
4
|
}
|
|
5
5
|
export interface CreateActorContextReturnType<A = BaseActor> extends ActorHooksReturnType<A> {
|
|
6
6
|
ActorProvider: React.FC<ActorProviderProps>;
|
|
7
7
|
ActorHookProvider: React.FC<ActorHookProviderProps<A>>;
|
|
8
|
+
ActorManagerProvider: React.FC<ActorManagerProviderProps<A>>;
|
|
8
9
|
useInitializeActor: () => InitializeActor;
|
|
9
10
|
}
|
|
10
11
|
export interface ActorHookProviderProps<A> {
|
|
11
12
|
hooks: ActorHooksReturnType<A>;
|
|
12
13
|
children?: React.ReactNode;
|
|
13
14
|
}
|
|
15
|
+
export interface ActorManagerProviderProps<A> {
|
|
16
|
+
children?: React.ReactNode | undefined;
|
|
17
|
+
actorManager: ActorManager<A>;
|
|
18
|
+
}
|
|
14
19
|
export interface ActorProviderProps extends CreateActorContextParameters {
|
|
15
20
|
children?: React.ReactNode | undefined;
|
|
16
21
|
candidString?: string;
|
|
@@ -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">;
|
package/dist/hooks/index.d.ts
CHANGED
package/dist/hooks/index.js
CHANGED
package/dist/hooks/types.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { IDL, ActorHooksReturnType, ActorManagerParameters, BaseActor, CanisterId } from "../types";
|
|
1
|
+
import { IDL, ActorHooksReturnType, ActorManagerParameters, BaseActor, CanisterId, ActorManager } from "../types";
|
|
2
2
|
export type ActorReConfigParameters = Omit<ActorManagerParameters, "idlFactory" | "canisterId" | "agentManager">;
|
|
3
3
|
export interface UseActorParameters extends ActorReConfigParameters {
|
|
4
4
|
candidString?: string;
|
|
@@ -6,6 +6,9 @@ export interface UseActorParameters extends ActorReConfigParameters {
|
|
|
6
6
|
idlFactory?: IDL.InterfaceFactory;
|
|
7
7
|
disableAutoFetch?: boolean;
|
|
8
8
|
}
|
|
9
|
+
export interface UseActorManagerParameters<A> extends ActorReConfigParameters {
|
|
10
|
+
actorManager: ActorManager<A>;
|
|
11
|
+
}
|
|
9
12
|
export interface UseActorReturn<A = BaseActor> {
|
|
10
13
|
hooks: ActorHooksReturnType<A> | null;
|
|
11
14
|
fetching: boolean;
|
|
@@ -13,4 +16,7 @@ export interface UseActorReturn<A = BaseActor> {
|
|
|
13
16
|
authenticating: boolean;
|
|
14
17
|
initializeActor: InitializeActor;
|
|
15
18
|
}
|
|
19
|
+
export interface UseActorManagerReturn<A = BaseActor> {
|
|
20
|
+
hooks: ActorHooksReturnType<A>;
|
|
21
|
+
}
|
|
16
22
|
export type InitializeActor = (idlFactory: IDL.InterfaceFactory, actorReConfig?: ActorReConfigParameters) => void;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { BaseActor } from "../types";
|
|
2
|
+
import type { UseActorManagerParameters, UseActorManagerReturn } from "./types";
|
|
3
|
+
/**
|
|
4
|
+
* A comprehensive hook that manages both the fetching of Candid interfaces
|
|
5
|
+
* and the initialization of actor stores for Internet Computer (IC) canisters.
|
|
6
|
+
* It simplifies the process of interacting with canisters by encapsulating
|
|
7
|
+
* the logic for Candid retrieval and actor store management.
|
|
8
|
+
*
|
|
9
|
+
* You can use react context to share the actor hooks across your application.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```tsx
|
|
13
|
+
* import { AgentProvider, extractActorHooks, useActorManager } from "@ic-reactor/react"
|
|
14
|
+
* import { createContext } from "react"
|
|
15
|
+
* import type { ActorHooks } from "@ic-reactor/react/dist/types"
|
|
16
|
+
* // With this import, you can have type safety for the actor's interface.
|
|
17
|
+
* // You can get it from the `.did.d.ts` file generated by the DFX tool.
|
|
18
|
+
* // or from dashboard https://dashboard.internetcomputer.org/canisters/<canister-id>
|
|
19
|
+
* import type { Ledger } from "../declarations/ledger"
|
|
20
|
+
*
|
|
21
|
+
* const ActorContext = createContext<ActorHooks<Ledger> | null>(null)
|
|
22
|
+
*
|
|
23
|
+
* export const { useQueryCall, useUpdateCall } = extractActorHooks(ActorContext)
|
|
24
|
+
*
|
|
25
|
+
* const actorManager = createActorManager<Ledger>({
|
|
26
|
+
* canisterId: "ryjl3-tyaaa-aaaaa-aaaba-cai",
|
|
27
|
+
* idlFactory: idlFactory
|
|
28
|
+
* })
|
|
29
|
+
*
|
|
30
|
+
* const LedgerActor = ({ children }) => {
|
|
31
|
+
* const { hooks } = useActorManager<Ledger>({
|
|
32
|
+
* actorManager
|
|
33
|
+
* })
|
|
34
|
+
*
|
|
35
|
+
* return (
|
|
36
|
+
* <ActorContext.Provider value={hooks}>
|
|
37
|
+
* <h2>IC Canister Interaction</h2>
|
|
38
|
+
* </ActorContext.Provider>
|
|
39
|
+
* )
|
|
40
|
+
* }
|
|
41
|
+
* // later in the code
|
|
42
|
+
* const CanisterName = () => {
|
|
43
|
+
* const { data } = useQueryCall({
|
|
44
|
+
* functionName: "name",
|
|
45
|
+
* })
|
|
46
|
+
*
|
|
47
|
+
* return (
|
|
48
|
+
* <div>
|
|
49
|
+
* <h3>Query Call</h3>
|
|
50
|
+
* <p>Result: {JSON.stringify(data)}</p>
|
|
51
|
+
* </div>
|
|
52
|
+
* )
|
|
53
|
+
* }
|
|
54
|
+
*
|
|
55
|
+
* const App = () => (
|
|
56
|
+
* <AgentProvider withDevtools>
|
|
57
|
+
* <CandidAdapterProvider>
|
|
58
|
+
* <LedgerActor>
|
|
59
|
+
* <CanisterName />
|
|
60
|
+
* </LedgerActor>
|
|
61
|
+
* </CandidAdapterProvider>
|
|
62
|
+
* </AgentProvider>
|
|
63
|
+
* )
|
|
64
|
+
*
|
|
65
|
+
* export default App
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
export declare const useActorManager: <A = BaseActor>(config: UseActorManagerParameters<A>) => UseActorManagerReturn<A>;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useActorManager = void 0;
|
|
4
|
+
const helpers_1 = require("../helpers");
|
|
5
|
+
/**
|
|
6
|
+
* A comprehensive hook that manages both the fetching of Candid interfaces
|
|
7
|
+
* and the initialization of actor stores for Internet Computer (IC) canisters.
|
|
8
|
+
* It simplifies the process of interacting with canisters by encapsulating
|
|
9
|
+
* the logic for Candid retrieval and actor store management.
|
|
10
|
+
*
|
|
11
|
+
* You can use react context to share the actor hooks across your application.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```tsx
|
|
15
|
+
* import { AgentProvider, extractActorHooks, useActorManager } from "@ic-reactor/react"
|
|
16
|
+
* import { createContext } from "react"
|
|
17
|
+
* import type { ActorHooks } from "@ic-reactor/react/dist/types"
|
|
18
|
+
* // With this import, you can have type safety for the actor's interface.
|
|
19
|
+
* // You can get it from the `.did.d.ts` file generated by the DFX tool.
|
|
20
|
+
* // or from dashboard https://dashboard.internetcomputer.org/canisters/<canister-id>
|
|
21
|
+
* import type { Ledger } from "../declarations/ledger"
|
|
22
|
+
*
|
|
23
|
+
* const ActorContext = createContext<ActorHooks<Ledger> | null>(null)
|
|
24
|
+
*
|
|
25
|
+
* export const { useQueryCall, useUpdateCall } = extractActorHooks(ActorContext)
|
|
26
|
+
*
|
|
27
|
+
* const actorManager = createActorManager<Ledger>({
|
|
28
|
+
* canisterId: "ryjl3-tyaaa-aaaaa-aaaba-cai",
|
|
29
|
+
* idlFactory: idlFactory
|
|
30
|
+
* })
|
|
31
|
+
*
|
|
32
|
+
* const LedgerActor = ({ children }) => {
|
|
33
|
+
* const { hooks } = useActorManager<Ledger>({
|
|
34
|
+
* actorManager
|
|
35
|
+
* })
|
|
36
|
+
*
|
|
37
|
+
* return (
|
|
38
|
+
* <ActorContext.Provider value={hooks}>
|
|
39
|
+
* <h2>IC Canister Interaction</h2>
|
|
40
|
+
* </ActorContext.Provider>
|
|
41
|
+
* )
|
|
42
|
+
* }
|
|
43
|
+
* // later in the code
|
|
44
|
+
* const CanisterName = () => {
|
|
45
|
+
* const { data } = useQueryCall({
|
|
46
|
+
* functionName: "name",
|
|
47
|
+
* })
|
|
48
|
+
*
|
|
49
|
+
* return (
|
|
50
|
+
* <div>
|
|
51
|
+
* <h3>Query Call</h3>
|
|
52
|
+
* <p>Result: {JSON.stringify(data)}</p>
|
|
53
|
+
* </div>
|
|
54
|
+
* )
|
|
55
|
+
* }
|
|
56
|
+
*
|
|
57
|
+
* const App = () => (
|
|
58
|
+
* <AgentProvider withDevtools>
|
|
59
|
+
* <CandidAdapterProvider>
|
|
60
|
+
* <LedgerActor>
|
|
61
|
+
* <CanisterName />
|
|
62
|
+
* </LedgerActor>
|
|
63
|
+
* </CandidAdapterProvider>
|
|
64
|
+
* </AgentProvider>
|
|
65
|
+
* )
|
|
66
|
+
*
|
|
67
|
+
* export default App
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
const useActorManager = (config) => {
|
|
71
|
+
const { actorManager } = config;
|
|
72
|
+
const hooks = (0, helpers_1.actorHooks)(actorManager);
|
|
73
|
+
return { hooks };
|
|
74
|
+
};
|
|
75
|
+
exports.useActorManager = useActorManager;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ic-reactor/react",
|
|
3
|
-
"version": "1.15.
|
|
3
|
+
"version": "1.15.1",
|
|
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",
|
|
@@ -48,7 +48,7 @@
|
|
|
48
48
|
"react": ">=16.8",
|
|
49
49
|
"zustand": ">=5.0.0"
|
|
50
50
|
},
|
|
51
|
-
"gitHead": "
|
|
51
|
+
"gitHead": "7105d3df4b003c1e4232396054cf7b217888ccee",
|
|
52
52
|
"devDependencies": {
|
|
53
53
|
"@dfinity/agent": ">=2.1",
|
|
54
54
|
"@dfinity/auth-client": ">=2.1",
|