applesauce-react 0.11.0 → 0.12.0

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.
@@ -1,10 +1,12 @@
1
1
  export * from "./use-account-manager.js";
2
2
  export * from "./use-accounts.js";
3
+ export * from "./use-action-hub.js";
4
+ export * from "./use-action.js";
3
5
  export * from "./use-active-account.js";
4
6
  export * from "./use-event-factory.js";
7
+ export * from "./use-event-store.js";
5
8
  export * from "./use-observable.js";
6
9
  export * from "./use-query-store.js";
7
10
  export * from "./use-render-nast.js";
8
11
  export * from "./use-rendered-content.js";
9
12
  export * from "./use-store-query.js";
10
- export * from "./use-event-store.js";
@@ -1,10 +1,12 @@
1
1
  export * from "./use-account-manager.js";
2
2
  export * from "./use-accounts.js";
3
+ export * from "./use-action-hub.js";
4
+ export * from "./use-action.js";
3
5
  export * from "./use-active-account.js";
4
6
  export * from "./use-event-factory.js";
7
+ export * from "./use-event-store.js";
5
8
  export * from "./use-observable.js";
6
9
  export * from "./use-query-store.js";
7
10
  export * from "./use-render-nast.js";
8
11
  export * from "./use-rendered-content.js";
9
12
  export * from "./use-store-query.js";
10
- export * from "./use-event-store.js";
@@ -0,0 +1 @@
1
+ export declare function useActionHub(): import("applesauce-actions").ActionHub;
@@ -0,0 +1,8 @@
1
+ import { useContext } from "react";
2
+ import { ActionsContext } from "../providers/actions-provider.js";
3
+ export function useActionHub() {
4
+ const hub = useContext(ActionsContext);
5
+ if (!hub)
6
+ throw new Error("Missing ActionsProvider");
7
+ return hub;
8
+ }
@@ -0,0 +1,6 @@
1
+ import { ActionConstructor } from "applesauce-actions";
2
+ export declare function useAction<Args extends Array<any>>(Action: ActionConstructor<Args>, args: Args | undefined): {
3
+ loading: boolean;
4
+ run: () => Promise<void>;
5
+ exec: () => import("rxjs").Observable<import("nostr-tools").Event> | undefined;
6
+ };
@@ -0,0 +1,37 @@
1
+ import { useCallback, useRef, useState } from "react";
2
+ import { finalize } from "rxjs";
3
+ import { useActionHub } from "./use-action-hub.js";
4
+ export function useAction(Action, args) {
5
+ const [loading, setLoading] = useState(false);
6
+ const ref = useRef(args);
7
+ ref.current = args;
8
+ const hub = useActionHub();
9
+ const run = useCallback(async () => {
10
+ if (args === undefined)
11
+ return;
12
+ setLoading(true);
13
+ try {
14
+ await hub.run(Action, ...args);
15
+ setLoading(false);
16
+ }
17
+ catch (error) {
18
+ setLoading(false);
19
+ throw error;
20
+ }
21
+ }, [Action]);
22
+ const exec = useCallback(() => {
23
+ if (args === undefined)
24
+ return;
25
+ setLoading(true);
26
+ try {
27
+ return hub.exec(Action, ...args).pipe(finalize(() => {
28
+ setLoading(false);
29
+ }));
30
+ }
31
+ catch (error) {
32
+ setLoading(false);
33
+ throw error;
34
+ }
35
+ }, [Action]);
36
+ return { loading, run, exec };
37
+ }
@@ -1,5 +1,6 @@
1
+ import { IEventStore } from "applesauce-core/event-store";
1
2
  /**
2
3
  * Gets the EventStore from a parent {@link EventStoreProvider} component
3
4
  * If there is none it throws an error
4
5
  */
5
- export declare function useEventStore(): import("applesauce-core").EventStore;
6
+ export declare function useEventStore(): IEventStore;
@@ -0,0 +1,7 @@
1
+ import { PropsWithChildren } from "react";
2
+ import { ActionHub } from "applesauce-actions";
3
+ export declare const ActionsContext: import("react").Context<ActionHub | undefined>;
4
+ /** Provides an ActionHub to the component tree */
5
+ export declare function ActionsProvider({ actionHub, children }: PropsWithChildren<{
6
+ actionHub?: ActionHub;
7
+ }>): import("react/jsx-runtime").JSX.Element;
@@ -0,0 +1,7 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { createContext } from "react";
3
+ export const ActionsContext = createContext(undefined);
4
+ /** Provides an ActionHub to the component tree */
5
+ export function ActionsProvider({ actionHub, children }) {
6
+ return _jsx(ActionsContext.Provider, { value: actionHub, children: children });
7
+ }
@@ -1,3 +1,4 @@
1
1
  export * from "./factory-provider.js";
2
2
  export * from "./store-provider.js";
3
3
  export * from "./accounts-provider.js";
4
+ export * from "./actions-provider.js";
@@ -1,3 +1,4 @@
1
1
  export * from "./factory-provider.js";
2
2
  export * from "./store-provider.js";
3
3
  export * from "./accounts-provider.js";
4
+ export * from "./actions-provider.js";
@@ -1,10 +1,10 @@
1
1
  import { PropsWithChildren } from "react";
2
- import { EventStore, QueryStore } from "applesauce-core";
2
+ import { IEventStore, QueryStore } from "applesauce-core";
3
3
  export declare const QueryStoreContext: import("react").Context<QueryStore | null>;
4
- export declare const EventStoreContext: import("react").Context<EventStore | null>;
4
+ export declare const EventStoreContext: import("react").Context<IEventStore | null>;
5
5
  /** Provides a EventStore to the component tree */
6
6
  export declare function EventStoreProvider({ eventStore, children }: PropsWithChildren<{
7
- eventStore: EventStore;
7
+ eventStore: IEventStore;
8
8
  }>): import("react/jsx-runtime").JSX.Element;
9
9
  /** Provides a QueryStore and EventStore to the component tree */
10
10
  export declare function QueryStoreProvider({ queryStore, children }: PropsWithChildren<{
package/package.json CHANGED
@@ -1,13 +1,14 @@
1
1
  {
2
2
  "name": "applesauce-react",
3
- "version": "0.11.0",
3
+ "version": "0.12.0",
4
4
  "description": "React hooks for applesauce",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",
8
8
  "keywords": [
9
9
  "nostr",
10
- "react"
10
+ "react",
11
+ "applesauce"
11
12
  ],
12
13
  "author": "hzrd149",
13
14
  "license": "MIT",
@@ -48,10 +49,11 @@
48
49
  }
49
50
  },
50
51
  "dependencies": {
51
- "applesauce-accounts": "^0.11.0",
52
- "applesauce-content": "^0.11.0",
53
- "applesauce-core": "^0.11.0",
54
- "applesauce-factory": "^0.11.0",
52
+ "applesauce-accounts": "^0.12.0",
53
+ "applesauce-actions": "^0.12.0",
54
+ "applesauce-content": "^0.12.0",
55
+ "applesauce-core": "^0.12.0",
56
+ "applesauce-factory": "^0.12.0",
55
57
  "nostr-tools": "^2.10.4",
56
58
  "observable-hooks": "^4.2.4",
57
59
  "react": "^18.3.1",