@ic-reactor/react 1.7.4 → 1.7.6

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.
@@ -70,15 +70,20 @@ const DEFAULT_STATE = {
70
70
  */
71
71
  const actorHooks = (actorManager) => {
72
72
  const { actorStore, canisterId, visitFunction, methodAttributes, updateMethodState, extractMethodAttributes, extractInterface, callMethodWithOptions, initialize, } = actorManager;
73
- const useActorState = () => (0, zustand_1.useStore)(actorStore, (0, shallow_1.useShallow)((state) => ({
74
- name: state.name,
75
- error: state.error,
76
- initialized: state.initialized,
77
- initializing: state.initializing,
78
- canisterId,
79
- })));
73
+ const useActorStore = (selector = (s) => s) => {
74
+ return (0, zustand_1.useStore)(actorStore, (0, shallow_1.useShallow)(selector));
75
+ };
76
+ const useActorState = () => {
77
+ return useActorStore((state) => ({
78
+ name: state.name,
79
+ error: state.error,
80
+ initialized: state.initialized,
81
+ initializing: state.initializing,
82
+ canisterId,
83
+ }));
84
+ };
80
85
  const useMethodState = (functionName, requestKey) => {
81
- const state = (0, zustand_1.useStore)(actorStore, (0, shallow_1.useShallow)((state) => { var _a; return (_a = state.methodState[functionName]) === null || _a === void 0 ? void 0 : _a[requestKey]; })) || DEFAULT_STATE;
86
+ const state = useActorStore((state) => { var _a; return (_a = state.methodState[functionName]) === null || _a === void 0 ? void 0 : _a[requestKey]; });
82
87
  const setSharedState = React.useCallback((newState) => {
83
88
  updateMethodState(functionName, requestKey, newState);
84
89
  }, [functionName, requestKey]);
@@ -207,6 +212,7 @@ const actorHooks = (actorManager) => {
207
212
  };
208
213
  return {
209
214
  initialize,
215
+ useActorStore,
210
216
  useMethodAttributes,
211
217
  useMethodNames,
212
218
  useMethod,
@@ -16,6 +16,9 @@ function extractActorContext(actorContext) {
16
16
  const initialize = () => useActorContext().initialize();
17
17
  const useMethodNames = () => useActorContext().useMethodNames();
18
18
  const useMethodAttributes = () => useActorContext().useMethodAttributes();
19
+ const useActorStore = (selector = (s) => s) => {
20
+ return useActorContext().useActorStore(selector);
21
+ };
19
22
  const useActorState = () => useActorContext().useActorState();
20
23
  const useMethod = (args) => useActorContext().useMethod(args);
21
24
  const useQueryCall = (args) => useActorContext().useQueryCall(args);
@@ -24,6 +27,7 @@ function extractActorContext(actorContext) {
24
27
  const useVisitService = () => useActorContext().useVisitService();
25
28
  const useActorInterface = () => useActorContext().useActorInterface();
26
29
  return {
30
+ useActorStore,
27
31
  useActorState,
28
32
  useMethod,
29
33
  useMethodNames,
@@ -38,7 +38,8 @@ export type LoginParameters = AuthClientLoginOptions;
38
38
  export type LogoutParameters = {
39
39
  returnTo?: string;
40
40
  };
41
- export interface UseActorState extends Omit<ActorState, "methodState"> {
41
+ export type UseActorStore<A> = <T>(callback?: (state: ActorState<A>) => T) => T;
42
+ export interface UseActorStateReturnType extends Omit<ActorState, "methodState"> {
42
43
  canisterId: string;
43
44
  }
44
45
  export interface UseSharedCallParameters<A, M extends FunctionName<A>> extends CallConfig {
@@ -90,7 +91,8 @@ export type UseVisitMethod<A> = <M extends FunctionName<A>>(functionName: M) =>
90
91
  export type UseVisitService<A> = () => VisitService<A>;
91
92
  export interface ActorHooksReturnType<A = BaseActor> {
92
93
  initialize: () => Promise<void>;
93
- useActorState: () => UseActorState;
94
+ useActorStore: UseActorStore<A>;
95
+ useActorState: () => UseActorStateReturnType;
94
96
  useActorInterface: () => IDL.ServiceClass;
95
97
  useMethodNames: <Actor = A>() => FunctionName<Actor>[];
96
98
  useMethodAttributes: <Actor = A>() => MethodAttributes<Actor>;
@@ -2,6 +2,7 @@ export * from "./useMethodNames";
2
2
  export * from "./useQueryCall";
3
3
  export * from "./useUpdateCall";
4
4
  export * from "./useMethodAttributes";
5
+ export * from "./useActorStore";
5
6
  export * from "./useActorState";
6
7
  export * from "./useVisitMethod";
7
8
  export * from "./useVisitService";
@@ -18,6 +18,7 @@ __exportStar(require("./useMethodNames"), exports);
18
18
  __exportStar(require("./useQueryCall"), exports);
19
19
  __exportStar(require("./useUpdateCall"), exports);
20
20
  __exportStar(require("./useMethodAttributes"), exports);
21
+ __exportStar(require("./useActorStore"), exports);
21
22
  __exportStar(require("./useActorState"), exports);
22
23
  __exportStar(require("./useVisitMethod"), exports);
23
24
  __exportStar(require("./useVisitService"), exports);
@@ -18,4 +18,4 @@
18
18
  * }
19
19
  *```
20
20
  */
21
- export declare const useActorState: () => import("../../types").UseActorState;
21
+ export declare const useActorState: () => import("../../types").UseActorStateReturnType;
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Provides a hook for accessing the state of an actor from the actor's store.
3
+ * This hook is part of a set of utilities that facilitate interactions with
4
+ * Internet Computer (IC) canisters by abstracting the complexities associated
5
+ * with actor management and state retrieval.
6
+ *
7
+ * The `useActorStore` hook allows components to subscribe to and retrieve state
8
+ * from the actor store in a reactive way. This enables components to re-render
9
+ * when specific parts of the actor state change, which is managed by a selector function.
10
+ *
11
+ * @template A The type of the actor, extending from a base actor structure.
12
+ * @template T The type of the data selected from the actor's state.
13
+ * @param {Function} [selector=(s: ActorState<A>) => s as T] A function that
14
+ * selects a part of the actor state. This function should accept the actor state
15
+ * and return a portion of the state. If no selector is provided, the entire
16
+ * state is returned.
17
+ *
18
+ * @returns {T} The selected state from the actor store. The nature of the returned
19
+ * state depends on the selector function provided.
20
+ *
21
+ * @example
22
+ * ```tsx
23
+ * // Assuming a 'LedgerActor' has been setup and used to manage the actor store
24
+ * const name = useActorStore(state => state.name);
25
+ *
26
+ * return <div>Canister Name: {name}</div>;
27
+ * ```
28
+ *
29
+ * @see ActorManager for more details about the architecture and management of actor stores.
30
+ * @see ActorState for the structure and possible states an actor can hold.
31
+ */
32
+ export declare const useActorStore: import("../../types").UseActorStore<import("@ic-reactor/core/dist/types").BaseActor>;
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.useActorStore = void 0;
7
+ const hooks_1 = __importDefault(require("./hooks"));
8
+ /**
9
+ * Provides a hook for accessing the state of an actor from the actor's store.
10
+ * This hook is part of a set of utilities that facilitate interactions with
11
+ * Internet Computer (IC) canisters by abstracting the complexities associated
12
+ * with actor management and state retrieval.
13
+ *
14
+ * The `useActorStore` hook allows components to subscribe to and retrieve state
15
+ * from the actor store in a reactive way. This enables components to re-render
16
+ * when specific parts of the actor state change, which is managed by a selector function.
17
+ *
18
+ * @template A The type of the actor, extending from a base actor structure.
19
+ * @template T The type of the data selected from the actor's state.
20
+ * @param {Function} [selector=(s: ActorState<A>) => s as T] A function that
21
+ * selects a part of the actor state. This function should accept the actor state
22
+ * and return a portion of the state. If no selector is provided, the entire
23
+ * state is returned.
24
+ *
25
+ * @returns {T} The selected state from the actor store. The nature of the returned
26
+ * state depends on the selector function provided.
27
+ *
28
+ * @example
29
+ * ```tsx
30
+ * // Assuming a 'LedgerActor' has been setup and used to manage the actor store
31
+ * const name = useActorStore(state => state.name);
32
+ *
33
+ * return <div>Canister Name: {name}</div>;
34
+ * ```
35
+ *
36
+ * @see ActorManager for more details about the architecture and management of actor stores.
37
+ * @see ActorState for the structure and possible states an actor can hold.
38
+ */
39
+ exports.useActorStore = hooks_1.default.useActorStore;
@@ -26,3 +26,25 @@
26
26
  * This setup ensures that `YourComponent` and any of its children can interact with the specified IC actor through the context provided by `ActorProvider`.
27
27
  */
28
28
  export declare const ActorProvider: import("react").FC<import("../context/types").ActorProviderProps>;
29
+ /**
30
+ * `ActorHookProvider` is a React functional component that serves as a context provider for IC actor hooks within a React application.
31
+ * It wraps child components, providing them access to actor-specific hooks and functionalities based on the provided actor hooks and configuration.
32
+ *
33
+ * Props:
34
+ * - `hooks`: ActorHooksReturnType - The actor hooks object containing the various actor interaction hooks.
35
+ * - `children`: React Node - The child components that will have access to the actor hooks context.
36
+ *
37
+ * Behavior:
38
+ * - Validates the presence of the `hooks` object. Throws an error if it is missing, ensuring that the actor hooks are always available for actor operations.
39
+ * - Utilizes `useMemo` to memoize the `hooks` object, optimizing for performance by avoiding unnecessary recalculations.
40
+ * - Renders the child components once the `hooks` object is available, effectively providing them access to the actor hooks context.
41
+ *
42
+ * @example
43
+ * ```jsx
44
+ * <ActorHookProvider hooks={yourActorHooks}>
45
+ * <YourComponent />
46
+ * </ActorHookProvider>
47
+ * ```
48
+ * This setup ensures that `YourComponent` and any of its children can interact with the specified IC actor hooks through the context provided by `ActorHookProvider`.
49
+ */
50
+ export declare const ActorHookProvider: import("react").FC<import("../context/types").ActorHookProviderProps<import("@ic-reactor/core/dist/types").BaseActor>>;
@@ -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.ActorProvider = void 0;
6
+ exports.ActorHookProvider = exports.ActorProvider = void 0;
7
7
  const hooks_1 = __importDefault(require("../hooks/actor/hooks"));
8
8
  /**
9
9
  * `ActorProvider` is a React functional component that serves as a context provider for IC actor interactions within a React application.
@@ -32,3 +32,25 @@ const hooks_1 = __importDefault(require("../hooks/actor/hooks"));
32
32
  * This setup ensures that `YourComponent` and any of its children can interact with the specified IC actor through the context provided by `ActorProvider`.
33
33
  */
34
34
  exports.ActorProvider = hooks_1.default.ActorProvider;
35
+ /**
36
+ * `ActorHookProvider` is a React functional component that serves as a context provider for IC actor hooks within a React application.
37
+ * It wraps child components, providing them access to actor-specific hooks and functionalities based on the provided actor hooks and configuration.
38
+ *
39
+ * Props:
40
+ * - `hooks`: ActorHooksReturnType - The actor hooks object containing the various actor interaction hooks.
41
+ * - `children`: React Node - The child components that will have access to the actor hooks context.
42
+ *
43
+ * Behavior:
44
+ * - Validates the presence of the `hooks` object. Throws an error if it is missing, ensuring that the actor hooks are always available for actor operations.
45
+ * - Utilizes `useMemo` to memoize the `hooks` object, optimizing for performance by avoiding unnecessary recalculations.
46
+ * - Renders the child components once the `hooks` object is available, effectively providing them access to the actor hooks context.
47
+ *
48
+ * @example
49
+ * ```jsx
50
+ * <ActorHookProvider hooks={yourActorHooks}>
51
+ * <YourComponent />
52
+ * </ActorHookProvider>
53
+ * ```
54
+ * This setup ensures that `YourComponent` and any of its children can interact with the specified IC actor hooks through the context provided by `ActorHookProvider`.
55
+ */
56
+ exports.ActorHookProvider = hooks_1.default.ActorHookProvider;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ic-reactor/react",
3
- "version": "1.7.4",
3
+ "version": "1.7.6",
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": "324a59d9cf60331289cb164d4eb8589faf6e0aa2"
50
+ "gitHead": "21ff3717f422184cc9b71f4ce10a49e1ec59be8f"
51
51
  }