@ic-reactor/react 1.0.5 → 1.0.7
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/README.md +7 -8
- package/dist/context/actor.d.ts +64 -0
- package/dist/context/actor.js +117 -0
- package/dist/context/agent.d.ts +74 -0
- package/dist/context/agent.js +128 -0
- package/dist/{provider → context}/types.d.ts +2 -1
- package/dist/createReactor.d.ts +49 -0
- package/dist/createReactor.js +64 -0
- package/dist/helpers/authHooks.js +14 -18
- package/dist/helpers/extractActorContext.js +0 -82
- package/dist/helpers/extractAgentContext.d.ts +1 -1
- package/dist/helpers/extractAgentContext.js +3 -81
- package/dist/helpers/types.d.ts +15 -15
- package/dist/hooks/actor/hooks.d.ts +1 -0
- package/dist/hooks/actor/hooks.js +5 -0
- package/dist/hooks/actor/index.d.ts +4 -0
- package/dist/{provider/types.js → hooks/actor/index.js} +4 -1
- package/dist/hooks/actor/useActorState.d.ts +21 -0
- package/dist/hooks/actor/useActorState.js +25 -0
- package/dist/hooks/actor/useQueryCall.d.ts +28 -0
- package/dist/hooks/actor/useQueryCall.js +34 -0
- package/dist/hooks/actor/useUpdateCall.d.ts +29 -0
- package/dist/hooks/actor/useUpdateCall.js +35 -0
- package/dist/hooks/actor/useVisitMethod.d.ts +8 -0
- package/dist/hooks/actor/useVisitMethod.js +14 -0
- package/dist/hooks/agent/hooks.d.ts +1 -0
- package/dist/hooks/agent/hooks.js +5 -0
- package/dist/hooks/agent/index.d.ts +6 -0
- package/dist/hooks/agent/index.js +22 -0
- package/dist/hooks/agent/useAgent.d.ts +14 -0
- package/dist/hooks/agent/useAgent.js +18 -0
- package/dist/hooks/agent/useAgentManager.d.ts +15 -0
- package/dist/hooks/agent/useAgentManager.js +18 -0
- package/dist/hooks/agent/useAgentState.d.ts +21 -0
- package/dist/hooks/agent/useAgentState.js +25 -0
- package/dist/hooks/agent/useAuth.d.ts +57 -0
- package/dist/hooks/agent/useAuth.js +61 -0
- package/dist/hooks/agent/useAuthState.d.ts +19 -0
- package/dist/hooks/agent/useAuthState.js +23 -0
- package/dist/hooks/agent/useUserPrincipal.d.ts +17 -0
- package/dist/hooks/agent/useUserPrincipal.js +21 -0
- package/dist/hooks/index.d.ts +3 -0
- package/dist/{provider/hooks → hooks}/index.js +2 -0
- package/dist/{provider/hooks → hooks}/types.d.ts +2 -2
- package/dist/hooks/types.js +2 -0
- package/dist/{provider/hooks → hooks}/useActor.d.ts +6 -4
- package/dist/{provider/hooks → hooks}/useActor.js +14 -16
- package/dist/index.d.ts +6 -2
- package/dist/index.js +6 -2
- package/dist/provider/actor.d.ts +18 -56
- package/dist/provider/actor.js +19 -107
- package/dist/provider/agent.d.ts +20 -62
- package/dist/provider/agent.js +21 -113
- package/dist/types.d.ts +2 -1
- package/dist/types.js +2 -1
- package/package.json +3 -3
- package/dist/main.d.ts +0 -38
- package/dist/main.js +0 -53
- package/dist/provider/ActorProvider.d.ts +0 -27
- package/dist/provider/ActorProvider.js +0 -30
- package/dist/provider/AgentProvider.d.ts +0 -30
- package/dist/provider/AgentProvider.js +0 -33
- package/dist/provider/hooks/index.d.ts +0 -1
- package/dist/provider/index.d.ts +0 -8
- package/dist/provider/index.js +0 -25
- /package/dist/{provider/hooks → context}/types.js +0 -0
package/README.md
CHANGED
|
@@ -36,12 +36,11 @@ import { createReactor } from "@ic-reactor/react"
|
|
|
36
36
|
|
|
37
37
|
type Actor = typeof actor
|
|
38
38
|
|
|
39
|
-
export const { useActorStore,
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
})
|
|
39
|
+
export const { useActorStore, useAuth, useQueryCall } = createReactor<Actor>({
|
|
40
|
+
canisterId,
|
|
41
|
+
idlFactory,
|
|
42
|
+
host: "https://localhost:4943",
|
|
43
|
+
})
|
|
45
44
|
```
|
|
46
45
|
|
|
47
46
|
Then, use the `useQueryCall` hook to call your canister method:
|
|
@@ -82,7 +81,7 @@ export default Balance
|
|
|
82
81
|
|
|
83
82
|
```jsx
|
|
84
83
|
// Login.tsx
|
|
85
|
-
import {
|
|
84
|
+
import { useAuth } from "./actor"
|
|
86
85
|
|
|
87
86
|
const Login = () => {
|
|
88
87
|
const {
|
|
@@ -93,7 +92,7 @@ const Login = () => {
|
|
|
93
92
|
identity,
|
|
94
93
|
authenticating,
|
|
95
94
|
authenticated,
|
|
96
|
-
} =
|
|
95
|
+
} = useAuth()
|
|
97
96
|
|
|
98
97
|
return (
|
|
99
98
|
<div>
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { BaseActor } from "../types";
|
|
2
|
+
import { CreateActorContextParameters, CreateActorContextReturnType } from "./types";
|
|
3
|
+
/**
|
|
4
|
+
* Creates a React context specifically designed for managing the state and interactions with an actor on the Internet Computer (IC) blockchain.
|
|
5
|
+
* This context facilitates the dynamic creation and management of IC actors within React applications, leveraging the provided configuration options.
|
|
6
|
+
*
|
|
7
|
+
* @param reactorParameters A partial configuration object for the actor context, allowing customization and specification of actor-related settings.
|
|
8
|
+
* - `canisterId`: The default Canister ID to be used if not overridden in the `ActorProvider` component.
|
|
9
|
+
* - Other configurations can include properties related to the actor's interaction, such as agent options or authentication requirements.
|
|
10
|
+
*
|
|
11
|
+
* @returns An object containing the `ActorProvider` component and various hooks for interacting with the actor.
|
|
12
|
+
* - `ActorProvider`: A context provider component that allows child components to access and interact with the configured actor.
|
|
13
|
+
* - Hooks: Custom hooks derived from the actor context, facilitating interactions like querying or updating the actor's state.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```jsx
|
|
17
|
+
* import React from 'react';
|
|
18
|
+
* import { createActorContext } from '@ic-reactor/react';
|
|
19
|
+
* import { backend, canisterId, idlFactory } from './declarations/candid'; // Assuming 'declarations/candid' is where your actor interface is defined.
|
|
20
|
+
*
|
|
21
|
+
* // Initialize the actor context with configuration options
|
|
22
|
+
* const { ActorProvider, useActorState, useQueryCall, useUpdateCall } = createActorContext<typeof backend>({
|
|
23
|
+
* canisterId,
|
|
24
|
+
* idlFactory, // Optional
|
|
25
|
+
* });
|
|
26
|
+
*
|
|
27
|
+
* // A sample component that utilizes the actor context
|
|
28
|
+
* const App = () => (
|
|
29
|
+
* <AgentProvider>
|
|
30
|
+
* <ActorProvider>
|
|
31
|
+
* <div>
|
|
32
|
+
* <h1>IC Actor Interaction Example</h1>
|
|
33
|
+
* <ActorComponent />
|
|
34
|
+
* </div>
|
|
35
|
+
* </ActorProvider>
|
|
36
|
+
* </AgentProvider>
|
|
37
|
+
* );
|
|
38
|
+
*
|
|
39
|
+
* export default App;
|
|
40
|
+
*
|
|
41
|
+
* // A sample component that uses the actor hooks
|
|
42
|
+
* const ActorComponent = () => {
|
|
43
|
+
* const { data, loading, error } = useQueryCall({
|
|
44
|
+
* functionName: 'backendMethodName',
|
|
45
|
+
* args: [],
|
|
46
|
+
* refetchInterval: 10000,
|
|
47
|
+
* refetchOnMount: true,
|
|
48
|
+
* });
|
|
49
|
+
|
|
50
|
+
* return (
|
|
51
|
+
* <div>
|
|
52
|
+
* {loading && <p>Loading...</p>}
|
|
53
|
+
* {error && <p>Error: {error.message}</p>}
|
|
54
|
+
* {data && <p>Actor data: {data}</p>}
|
|
55
|
+
* </div>
|
|
56
|
+
* );
|
|
57
|
+
* };
|
|
58
|
+
* ```
|
|
59
|
+
*
|
|
60
|
+
* This function streamlines the process of setting up a context for IC actor interactions within a React app, making it easier
|
|
61
|
+
* to manage actor state and perform actions such as queries or updates. It abstracts away the complexities involved in directly
|
|
62
|
+
* managing IC agents and actors, providing a simple, declarative API for developers.
|
|
63
|
+
*/
|
|
64
|
+
export declare function createActorContext<A = BaseActor>(config?: CreateActorContextParameters): CreateActorContextReturnType<A>;
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
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 __rest = (this && this.__rest) || function (s, e) {
|
|
26
|
+
var t = {};
|
|
27
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
28
|
+
t[p] = s[p];
|
|
29
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
30
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
31
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
32
|
+
t[p[i]] = s[p[i]];
|
|
33
|
+
}
|
|
34
|
+
return t;
|
|
35
|
+
};
|
|
36
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
37
|
+
exports.createActorContext = void 0;
|
|
38
|
+
const react_1 = __importStar(require("react"));
|
|
39
|
+
const useActor_1 = require("../hooks/useActor");
|
|
40
|
+
const extractActorContext_1 = require("../helpers/extractActorContext");
|
|
41
|
+
/**
|
|
42
|
+
* Creates a React context specifically designed for managing the state and interactions with an actor on the Internet Computer (IC) blockchain.
|
|
43
|
+
* This context facilitates the dynamic creation and management of IC actors within React applications, leveraging the provided configuration options.
|
|
44
|
+
*
|
|
45
|
+
* @param reactorParameters A partial configuration object for the actor context, allowing customization and specification of actor-related settings.
|
|
46
|
+
* - `canisterId`: The default Canister ID to be used if not overridden in the `ActorProvider` component.
|
|
47
|
+
* - Other configurations can include properties related to the actor's interaction, such as agent options or authentication requirements.
|
|
48
|
+
*
|
|
49
|
+
* @returns An object containing the `ActorProvider` component and various hooks for interacting with the actor.
|
|
50
|
+
* - `ActorProvider`: A context provider component that allows child components to access and interact with the configured actor.
|
|
51
|
+
* - Hooks: Custom hooks derived from the actor context, facilitating interactions like querying or updating the actor's state.
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```jsx
|
|
55
|
+
* import React from 'react';
|
|
56
|
+
* import { createActorContext } from '@ic-reactor/react';
|
|
57
|
+
* import { backend, canisterId, idlFactory } from './declarations/candid'; // Assuming 'declarations/candid' is where your actor interface is defined.
|
|
58
|
+
*
|
|
59
|
+
* // Initialize the actor context with configuration options
|
|
60
|
+
* const { ActorProvider, useActorState, useQueryCall, useUpdateCall } = createActorContext<typeof backend>({
|
|
61
|
+
* canisterId,
|
|
62
|
+
* idlFactory, // Optional
|
|
63
|
+
* });
|
|
64
|
+
*
|
|
65
|
+
* // A sample component that utilizes the actor context
|
|
66
|
+
* const App = () => (
|
|
67
|
+
* <AgentProvider>
|
|
68
|
+
* <ActorProvider>
|
|
69
|
+
* <div>
|
|
70
|
+
* <h1>IC Actor Interaction Example</h1>
|
|
71
|
+
* <ActorComponent />
|
|
72
|
+
* </div>
|
|
73
|
+
* </ActorProvider>
|
|
74
|
+
* </AgentProvider>
|
|
75
|
+
* );
|
|
76
|
+
*
|
|
77
|
+
* export default App;
|
|
78
|
+
*
|
|
79
|
+
* // A sample component that uses the actor hooks
|
|
80
|
+
* const ActorComponent = () => {
|
|
81
|
+
* const { data, loading, error } = useQueryCall({
|
|
82
|
+
* functionName: 'backendMethodName',
|
|
83
|
+
* args: [],
|
|
84
|
+
* refetchInterval: 10000,
|
|
85
|
+
* refetchOnMount: true,
|
|
86
|
+
* });
|
|
87
|
+
|
|
88
|
+
* return (
|
|
89
|
+
* <div>
|
|
90
|
+
* {loading && <p>Loading...</p>}
|
|
91
|
+
* {error && <p>Error: {error.message}</p>}
|
|
92
|
+
* {data && <p>Actor data: {data}</p>}
|
|
93
|
+
* </div>
|
|
94
|
+
* );
|
|
95
|
+
* };
|
|
96
|
+
* ```
|
|
97
|
+
*
|
|
98
|
+
* This function streamlines the process of setting up a context for IC actor interactions within a React app, making it easier
|
|
99
|
+
* to manage actor state and perform actions such as queries or updates. It abstracts away the complexities involved in directly
|
|
100
|
+
* managing IC agents and actors, providing a simple, declarative API for developers.
|
|
101
|
+
*/
|
|
102
|
+
function createActorContext(config = {}) {
|
|
103
|
+
const { canisterId: defaultCanisterId } = config, defaultConfig = __rest(config, ["canisterId"]);
|
|
104
|
+
const ActorContext = (0, react_1.createContext)(null);
|
|
105
|
+
const ActorProvider = (_a) => {
|
|
106
|
+
var { children, canisterId = defaultCanisterId, loadingComponent = react_1.default.createElement("div", null, "Fetching canister...") } = _a, restConfig = __rest(_a, ["children", "canisterId", "loadingComponent"]);
|
|
107
|
+
if (!canisterId) {
|
|
108
|
+
throw new Error("canisterId is required");
|
|
109
|
+
}
|
|
110
|
+
const config = (0, react_1.useMemo)(() => (Object.assign(Object.assign({}, defaultConfig), restConfig)), [defaultConfig, restConfig]);
|
|
111
|
+
const { fetchError, fetching, hooks } = (0, useActor_1.useActor)(Object.assign({ canisterId }, config));
|
|
112
|
+
return (react_1.default.createElement(ActorContext.Provider, { value: hooks }, fetching || hooks === null ? fetchError !== null && fetchError !== void 0 ? fetchError : loadingComponent : children));
|
|
113
|
+
};
|
|
114
|
+
ActorProvider.displayName = "ActorProvider";
|
|
115
|
+
return Object.assign({ ActorProvider }, (0, extractActorContext_1.extractActorContext)(ActorContext));
|
|
116
|
+
}
|
|
117
|
+
exports.createActorContext = createActorContext;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type { CreateAgentContextReturnType, CreateAgentCotextParameters } from "./types";
|
|
2
|
+
/**
|
|
3
|
+
* Creates a React context for managing IC agent and authentication states, providing hooks for interacting with the IC blockchain.
|
|
4
|
+
* This function initializes an `AgentContext` with a set of utilities and hooks based on the provided agent configuration.
|
|
5
|
+
*
|
|
6
|
+
* @param config A partial configuration object {@link CreateAgentCotextParameters}, allowing customization of the agent's behavior.
|
|
7
|
+
*
|
|
8
|
+
* @returns
|
|
9
|
+
* An object containing the {@link AgentProvider} component and various hooks for interacting with the agent and authentication state.
|
|
10
|
+
* The {@link AgentProvider} component is a React context provider that should wrap your app or components needing access to agent functionalities.
|
|
11
|
+
*
|
|
12
|
+
* Usage:
|
|
13
|
+
* - {@link AgentProvider}: React component to provide agent context to your application.
|
|
14
|
+
* - {@link useAgent}, {@link useAuth}, {@link useAuthState},
|
|
15
|
+
* {@link useAgentState}, {@link useAgentManager}, {@link useUserPrincipal}:
|
|
16
|
+
* Hooks extracted from the created context for managing agent and authentication state within components.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* agent.ts
|
|
20
|
+
* ```tsx
|
|
21
|
+
* import { createAgentContext } from "@ic-reactor/react";
|
|
22
|
+
* import { CreateAgentCotextParameters } from "@ic-reactor/react/dist/types";
|
|
23
|
+
*
|
|
24
|
+
* // Optional: Define custom agent configuration
|
|
25
|
+
* const agentConfig: CreateAgentCotextParameters = {
|
|
26
|
+
* host: "https://localhost:8000",
|
|
27
|
+
* // or
|
|
28
|
+
* // isLocalEnv: true,
|
|
29
|
+
* // port: 8000,
|
|
30
|
+
* };
|
|
31
|
+
*
|
|
32
|
+
* export const {
|
|
33
|
+
* AgentProvider,
|
|
34
|
+
* useAgent,
|
|
35
|
+
* useAuth,
|
|
36
|
+
* useAuthState,
|
|
37
|
+
* useAgentState,
|
|
38
|
+
* useAgentManager,
|
|
39
|
+
* useUserPrincipal,
|
|
40
|
+
* } = createAgentContext(agentConfig);
|
|
41
|
+
*
|
|
42
|
+
* // Now you can use the returned hooks in your React components
|
|
43
|
+
*```
|
|
44
|
+
* App.tsx
|
|
45
|
+
* ```tsx
|
|
46
|
+
* import React from 'react';
|
|
47
|
+
* import { AgentProvider } from './agent';
|
|
48
|
+
*
|
|
49
|
+
* const App = () => (
|
|
50
|
+
* <AgentProvider>
|
|
51
|
+
* <Login />
|
|
52
|
+
* <YourActor />
|
|
53
|
+
* </AgentProvider>
|
|
54
|
+
* );
|
|
55
|
+
*
|
|
56
|
+
* const Login = () => {
|
|
57
|
+
* const { login } = useAuth()
|
|
58
|
+
* const principal = useUserPrincipal()
|
|
59
|
+
*
|
|
60
|
+
* return (
|
|
61
|
+
* <div>
|
|
62
|
+
* <button onClick={() => login()}>Login</button>
|
|
63
|
+
* <p>User: {principal?.toText()}</p>
|
|
64
|
+
* </div>
|
|
65
|
+
* )
|
|
66
|
+
* };
|
|
67
|
+
*
|
|
68
|
+
* ```
|
|
69
|
+
*
|
|
70
|
+
* This setup allows you to use the agent and authentication hooks within
|
|
71
|
+
* the components wrapped by {@link AgentProvider}, facilitating interaction
|
|
72
|
+
* with the Internet Computer blockchain.
|
|
73
|
+
*/
|
|
74
|
+
export declare const createAgentContext: (config?: CreateAgentCotextParameters) => CreateAgentContextReturnType;
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
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 __rest = (this && this.__rest) || function (s, e) {
|
|
26
|
+
var t = {};
|
|
27
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
28
|
+
t[p] = s[p];
|
|
29
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
30
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
31
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
32
|
+
t[p[i]] = s[p[i]];
|
|
33
|
+
}
|
|
34
|
+
return t;
|
|
35
|
+
};
|
|
36
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
37
|
+
exports.createAgentContext = void 0;
|
|
38
|
+
const react_1 = __importStar(require("react"));
|
|
39
|
+
const core_1 = require("@ic-reactor/core");
|
|
40
|
+
const agentHooks_1 = require("../helpers/agentHooks");
|
|
41
|
+
const authHooks_1 = require("../helpers/authHooks");
|
|
42
|
+
const extractAgentContext_1 = require("../helpers/extractAgentContext");
|
|
43
|
+
/**
|
|
44
|
+
* Creates a React context for managing IC agent and authentication states, providing hooks for interacting with the IC blockchain.
|
|
45
|
+
* This function initializes an `AgentContext` with a set of utilities and hooks based on the provided agent configuration.
|
|
46
|
+
*
|
|
47
|
+
* @param config A partial configuration object {@link CreateAgentCotextParameters}, allowing customization of the agent's behavior.
|
|
48
|
+
*
|
|
49
|
+
* @returns
|
|
50
|
+
* An object containing the {@link AgentProvider} component and various hooks for interacting with the agent and authentication state.
|
|
51
|
+
* The {@link AgentProvider} component is a React context provider that should wrap your app or components needing access to agent functionalities.
|
|
52
|
+
*
|
|
53
|
+
* Usage:
|
|
54
|
+
* - {@link AgentProvider}: React component to provide agent context to your application.
|
|
55
|
+
* - {@link useAgent}, {@link useAuth}, {@link useAuthState},
|
|
56
|
+
* {@link useAgentState}, {@link useAgentManager}, {@link useUserPrincipal}:
|
|
57
|
+
* Hooks extracted from the created context for managing agent and authentication state within components.
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* agent.ts
|
|
61
|
+
* ```tsx
|
|
62
|
+
* import { createAgentContext } from "@ic-reactor/react";
|
|
63
|
+
* import { CreateAgentCotextParameters } from "@ic-reactor/react/dist/types";
|
|
64
|
+
*
|
|
65
|
+
* // Optional: Define custom agent configuration
|
|
66
|
+
* const agentConfig: CreateAgentCotextParameters = {
|
|
67
|
+
* host: "https://localhost:8000",
|
|
68
|
+
* // or
|
|
69
|
+
* // isLocalEnv: true,
|
|
70
|
+
* // port: 8000,
|
|
71
|
+
* };
|
|
72
|
+
*
|
|
73
|
+
* export const {
|
|
74
|
+
* AgentProvider,
|
|
75
|
+
* useAgent,
|
|
76
|
+
* useAuth,
|
|
77
|
+
* useAuthState,
|
|
78
|
+
* useAgentState,
|
|
79
|
+
* useAgentManager,
|
|
80
|
+
* useUserPrincipal,
|
|
81
|
+
* } = createAgentContext(agentConfig);
|
|
82
|
+
*
|
|
83
|
+
* // Now you can use the returned hooks in your React components
|
|
84
|
+
*```
|
|
85
|
+
* App.tsx
|
|
86
|
+
* ```tsx
|
|
87
|
+
* import React from 'react';
|
|
88
|
+
* import { AgentProvider } from './agent';
|
|
89
|
+
*
|
|
90
|
+
* const App = () => (
|
|
91
|
+
* <AgentProvider>
|
|
92
|
+
* <Login />
|
|
93
|
+
* <YourActor />
|
|
94
|
+
* </AgentProvider>
|
|
95
|
+
* );
|
|
96
|
+
*
|
|
97
|
+
* const Login = () => {
|
|
98
|
+
* const { login } = useAuth()
|
|
99
|
+
* const principal = useUserPrincipal()
|
|
100
|
+
*
|
|
101
|
+
* return (
|
|
102
|
+
* <div>
|
|
103
|
+
* <button onClick={() => login()}>Login</button>
|
|
104
|
+
* <p>User: {principal?.toText()}</p>
|
|
105
|
+
* </div>
|
|
106
|
+
* )
|
|
107
|
+
* };
|
|
108
|
+
*
|
|
109
|
+
* ```
|
|
110
|
+
*
|
|
111
|
+
* This setup allows you to use the agent and authentication hooks within
|
|
112
|
+
* the components wrapped by {@link AgentProvider}, facilitating interaction
|
|
113
|
+
* with the Internet Computer blockchain.
|
|
114
|
+
*/
|
|
115
|
+
const createAgentContext = (config = {}) => {
|
|
116
|
+
const AgentContext = (0, react_1.createContext)(null);
|
|
117
|
+
const AgentProvider = (_a) => {
|
|
118
|
+
var { children, agentManager: mybeAgentManager } = _a, options = __rest(_a, ["children", "agentManager"]);
|
|
119
|
+
const hooks = (0, react_1.useMemo)(() => {
|
|
120
|
+
const agentManager = mybeAgentManager !== null && mybeAgentManager !== void 0 ? mybeAgentManager : (0, core_1.createAgentManager)(Object.assign(Object.assign({}, options), config));
|
|
121
|
+
return Object.assign(Object.assign(Object.assign({}, (0, agentHooks_1.agentHooks)(agentManager)), (0, authHooks_1.authHooks)(agentManager)), { agentManager });
|
|
122
|
+
}, [options]);
|
|
123
|
+
return (react_1.default.createElement(AgentContext.Provider, { value: hooks }, children));
|
|
124
|
+
};
|
|
125
|
+
AgentProvider.displayName = "AgentProvider";
|
|
126
|
+
return Object.assign({ AgentProvider }, (0, extractAgentContext_1.extractAgentContext)(AgentContext));
|
|
127
|
+
};
|
|
128
|
+
exports.createAgentContext = createAgentContext;
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import type { IDL, ActorHooksReturnType, AgentHooksReturnType, AuthHooksReturnType, BaseActor, AgentManager, ActorManagerParameters, AgentManagerParameters } from "../types";
|
|
2
2
|
import type { PropsWithChildren } from "react";
|
|
3
|
-
export * from "./hooks/types";
|
|
4
3
|
export interface AgentContext extends AgentHooksReturnType, AuthHooksReturnType {
|
|
5
4
|
agentManager: AgentManager;
|
|
6
5
|
}
|
|
6
|
+
export interface CreateAgentCotextParameters extends AgentManagerParameters {
|
|
7
|
+
}
|
|
7
8
|
export interface CreateAgentContextReturnType extends AgentHooksReturnType, AuthHooksReturnType {
|
|
8
9
|
useAgentManager: (agentContext?: React.Context<AgentContext | null>) => AgentManager;
|
|
9
10
|
AgentProvider: React.FC<AgentProviderProps>;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { BaseActor, CreateReactorParameters, CreateReactorReturnType } from "./types";
|
|
2
|
+
/**
|
|
3
|
+
* Initializes and configures the reactor environment for interacting with the Internet Computer (IC) blockchain within a React application.
|
|
4
|
+
* It encapsulates the creation of actors, authentication, and agent management, offering a streamlined interface for blockchain interactions.
|
|
5
|
+
*
|
|
6
|
+
* @param config Configuration {@link CreateReactorParameters} for the reactor, including:
|
|
7
|
+
* - `withProcessEnv` (optional): Specifies whether to use process environment variables to determine if the environment is local or development. Defaults to false.
|
|
8
|
+
* - `isLocalEnv` (optional): Indicates if the current environment is local or development, influencing the agent and actor behavior. Useful for testing or development.
|
|
9
|
+
* - `port` (optional): Port number for the local or development environment.
|
|
10
|
+
*
|
|
11
|
+
* @returns An object containing {@link CreateReactorReturnType} hooks and utilities:
|
|
12
|
+
* - {@link getAgent} - Returns the current agent instance.
|
|
13
|
+
* - {@link getVisitFunction} - Returns the visit function for the actor.
|
|
14
|
+
* - {@link useQueryCall} - A hook for querying actor methods.
|
|
15
|
+
* - {@link useUpdateCall} - A hook for updating actor methods.
|
|
16
|
+
* - {@link useAuth} - A hook for managing authentication and user principal.
|
|
17
|
+
* - {@link useActor} - A hook for managing actors and their methods.
|
|
18
|
+
* - {@link useActorManager} - A hook for managing actor manager and its methods.
|
|
19
|
+
* - {@link useAgentManager} - A hook for managing agent manager and its methods.
|
|
20
|
+
* - {@link initialize} - A function to initialize the actor manager if not initialized.
|
|
21
|
+
* - {@link useActorState} - A hook for managing actor state.
|
|
22
|
+
* - {@link useAgent} - A hook for managing agent and its methods.
|
|
23
|
+
* - {@link useAuthState} - A hook for managing authentication state.
|
|
24
|
+
* - {@link useAgentState} - A hook for managing agent state.
|
|
25
|
+
* - {@link useUserPrincipal} - A hook for managing user principal.
|
|
26
|
+
* - {@link useVisitMethod} - A hook for visiting actor methods.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```typescript
|
|
30
|
+
* import { createReactor } from "@ic-reactor/react";
|
|
31
|
+
* import type { CreateReactorParameters } from "@ic-reactor/react/dist/types";
|
|
32
|
+
* import { canisterId, idlFactory, yourActor } from "./declaration/yourActor"
|
|
33
|
+
*
|
|
34
|
+
* const config: CreateReactorParameters = {
|
|
35
|
+
* canisterId,
|
|
36
|
+
* idlFactory,
|
|
37
|
+
* host: "https://localhost:8000", // IC network host |
|
|
38
|
+
* isLocalEnv: true, // Set true for local network | one of these
|
|
39
|
+
* withProcessEnv: true, // Use process.env to determine host |
|
|
40
|
+
* port: 8000, // Port number for local network |
|
|
41
|
+
* };
|
|
42
|
+
*
|
|
43
|
+
* export type YourActor = typeof yourActor;
|
|
44
|
+
*
|
|
45
|
+
* export const { useAuth, useQueryCall, useUpdateCall } = createReactor<YourActor>(config);
|
|
46
|
+
* // Now you can use the returned hooks in your React components
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
export declare const createReactor: <A = BaseActor>(config: CreateReactorParameters) => CreateReactorReturnType<A>;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createReactor = void 0;
|
|
4
|
+
const core_1 = require("@ic-reactor/core");
|
|
5
|
+
const helpers_1 = require("./helpers");
|
|
6
|
+
/**
|
|
7
|
+
* Initializes and configures the reactor environment for interacting with the Internet Computer (IC) blockchain within a React application.
|
|
8
|
+
* It encapsulates the creation of actors, authentication, and agent management, offering a streamlined interface for blockchain interactions.
|
|
9
|
+
*
|
|
10
|
+
* @param config Configuration {@link CreateReactorParameters} for the reactor, including:
|
|
11
|
+
* - `withProcessEnv` (optional): Specifies whether to use process environment variables to determine if the environment is local or development. Defaults to false.
|
|
12
|
+
* - `isLocalEnv` (optional): Indicates if the current environment is local or development, influencing the agent and actor behavior. Useful for testing or development.
|
|
13
|
+
* - `port` (optional): Port number for the local or development environment.
|
|
14
|
+
*
|
|
15
|
+
* @returns An object containing {@link CreateReactorReturnType} hooks and utilities:
|
|
16
|
+
* - {@link getAgent} - Returns the current agent instance.
|
|
17
|
+
* - {@link getVisitFunction} - Returns the visit function for the actor.
|
|
18
|
+
* - {@link useQueryCall} - A hook for querying actor methods.
|
|
19
|
+
* - {@link useUpdateCall} - A hook for updating actor methods.
|
|
20
|
+
* - {@link useAuth} - A hook for managing authentication and user principal.
|
|
21
|
+
* - {@link useActor} - A hook for managing actors and their methods.
|
|
22
|
+
* - {@link useActorManager} - A hook for managing actor manager and its methods.
|
|
23
|
+
* - {@link useAgentManager} - A hook for managing agent manager and its methods.
|
|
24
|
+
* - {@link initialize} - A function to initialize the actor manager if not initialized.
|
|
25
|
+
* - {@link useActorState} - A hook for managing actor state.
|
|
26
|
+
* - {@link useAgent} - A hook for managing agent and its methods.
|
|
27
|
+
* - {@link useAuthState} - A hook for managing authentication state.
|
|
28
|
+
* - {@link useAgentState} - A hook for managing agent state.
|
|
29
|
+
* - {@link useUserPrincipal} - A hook for managing user principal.
|
|
30
|
+
* - {@link useVisitMethod} - A hook for visiting actor methods.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```typescript
|
|
34
|
+
* import { createReactor } from "@ic-reactor/react";
|
|
35
|
+
* import type { CreateReactorParameters } from "@ic-reactor/react/dist/types";
|
|
36
|
+
* import { canisterId, idlFactory, yourActor } from "./declaration/yourActor"
|
|
37
|
+
*
|
|
38
|
+
* const config: CreateReactorParameters = {
|
|
39
|
+
* canisterId,
|
|
40
|
+
* idlFactory,
|
|
41
|
+
* host: "https://localhost:8000", // IC network host |
|
|
42
|
+
* isLocalEnv: true, // Set true for local network | one of these
|
|
43
|
+
* withProcessEnv: true, // Use process.env to determine host |
|
|
44
|
+
* port: 8000, // Port number for local network |
|
|
45
|
+
* };
|
|
46
|
+
*
|
|
47
|
+
* export type YourActor = typeof yourActor;
|
|
48
|
+
*
|
|
49
|
+
* export const { useAuth, useQueryCall, useUpdateCall } = createReactor<YourActor>(config);
|
|
50
|
+
* // Now you can use the returned hooks in your React components
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
const createReactor = (config) => {
|
|
54
|
+
const actorManager = (0, core_1.createReactorStore)(config);
|
|
55
|
+
const getVisitFunction = () => {
|
|
56
|
+
return actorManager.visitFunction;
|
|
57
|
+
};
|
|
58
|
+
const getAgent = () => {
|
|
59
|
+
return actorManager.agentManager.getAgent();
|
|
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));
|
|
63
|
+
};
|
|
64
|
+
exports.createReactor = createReactor;
|
|
@@ -12,17 +12,17 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
12
12
|
exports.authHooks = void 0;
|
|
13
13
|
const zustand_1 = require("zustand");
|
|
14
14
|
const react_1 = require("react");
|
|
15
|
-
const
|
|
15
|
+
const utils_1 = require("@ic-reactor/core/dist/utils");
|
|
16
16
|
const authHooks = (agentManager) => {
|
|
17
|
-
const { authenticate: authenticator, authStore, isLocalEnv } = agentManager;
|
|
17
|
+
const { authenticate: authenticator, getAuth, authStore, isLocalEnv, } = agentManager;
|
|
18
18
|
const useAuthState = () => (0, zustand_1.useStore)(authStore);
|
|
19
19
|
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(); };
|
|
20
|
-
const
|
|
20
|
+
const useAuth = ({ onAuthentication, onAuthenticationSuccess, onAuthenticationFailure, onLogin, onLoginSuccess, onLoginError, onLoggedOut, } = {}) => {
|
|
21
21
|
const [loginState, setLoginState] = (0, react_1.useState)({
|
|
22
22
|
loading: false,
|
|
23
23
|
error: null,
|
|
24
24
|
});
|
|
25
|
-
const {
|
|
25
|
+
const { authenticated, authenticating, error, identity } = useAuthState();
|
|
26
26
|
const authenticate = (0, react_1.useCallback)(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
27
27
|
const authenticatePromise = new Promise((resolve, reject) => {
|
|
28
28
|
authenticator()
|
|
@@ -47,12 +47,13 @@ const authHooks = (agentManager) => {
|
|
|
47
47
|
setLoginState({ loading: true, error: null });
|
|
48
48
|
const loginPromise = new Promise((resolve, reject) => {
|
|
49
49
|
try {
|
|
50
|
+
const authClient = getAuth();
|
|
50
51
|
if (!authClient) {
|
|
51
52
|
throw new Error("Auth client not initialized");
|
|
52
53
|
}
|
|
53
54
|
authClient.login(Object.assign(Object.assign({ identityProvider: isLocalEnv
|
|
54
|
-
?
|
|
55
|
-
:
|
|
55
|
+
? utils_1.LOCAL_INTERNET_IDENTITY_PROVIDER
|
|
56
|
+
: utils_1.IC_INTERNET_IDENTITY_PROVIDER }, options), { onSuccess: () => {
|
|
56
57
|
authenticate()
|
|
57
58
|
.then((identity) => {
|
|
58
59
|
var _a;
|
|
@@ -60,6 +61,7 @@ const authHooks = (agentManager) => {
|
|
|
60
61
|
(_a = options === null || options === void 0 ? void 0 : options.onSuccess) === null || _a === void 0 ? void 0 : _a.call(options);
|
|
61
62
|
onLoginSuccess === null || onLoginSuccess === void 0 ? void 0 : onLoginSuccess(principal);
|
|
62
63
|
resolve(principal);
|
|
64
|
+
setLoginState({ loading: false, error: null });
|
|
63
65
|
})
|
|
64
66
|
.catch((e) => {
|
|
65
67
|
const error = e;
|
|
@@ -84,29 +86,23 @@ const authHooks = (agentManager) => {
|
|
|
84
86
|
}
|
|
85
87
|
});
|
|
86
88
|
onLogin === null || onLogin === void 0 ? void 0 : onLogin(() => loginPromise);
|
|
87
|
-
}), [
|
|
88
|
-
authClient,
|
|
89
|
-
onLogin,
|
|
90
|
-
onLoginSuccess,
|
|
91
|
-
onLoginError,
|
|
92
|
-
isLocalEnv,
|
|
93
|
-
authenticate,
|
|
94
|
-
]);
|
|
89
|
+
}), [onLogin, onLoginSuccess, onLoginError, isLocalEnv, authenticate]);
|
|
95
90
|
const logout = (0, react_1.useCallback)((options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
91
|
+
const authClient = getAuth();
|
|
96
92
|
if (!authClient) {
|
|
97
93
|
throw new Error("Auth client not initialized");
|
|
98
94
|
}
|
|
99
95
|
yield authClient.logout(options);
|
|
100
96
|
yield authenticate();
|
|
101
97
|
onLoggedOut === null || onLoggedOut === void 0 ? void 0 : onLoggedOut();
|
|
102
|
-
}), [
|
|
98
|
+
}), [onLoggedOut]);
|
|
103
99
|
(0, react_1.useEffect)(() => {
|
|
100
|
+
const authClient = getAuth();
|
|
104
101
|
if (!authClient && !authenticating) {
|
|
105
102
|
authenticate();
|
|
106
103
|
}
|
|
107
|
-
}, [
|
|
104
|
+
}, [authenticating]);
|
|
108
105
|
return {
|
|
109
|
-
authClient,
|
|
110
106
|
authenticated,
|
|
111
107
|
authenticating,
|
|
112
108
|
identity,
|
|
@@ -121,7 +117,7 @@ const authHooks = (agentManager) => {
|
|
|
121
117
|
return {
|
|
122
118
|
useUserPrincipal,
|
|
123
119
|
useAuthState,
|
|
124
|
-
|
|
120
|
+
useAuth,
|
|
125
121
|
};
|
|
126
122
|
};
|
|
127
123
|
exports.authHooks = authHooks;
|