applesauce-react 0.10.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.
package/README.md CHANGED
@@ -1,6 +1,12 @@
1
1
  # applesauce-react
2
2
 
3
- React hooks for applesauce
3
+ React hooks and providers for applesauce
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install applesauce-react
9
+ ```
4
10
 
5
11
  ## Example
6
12
 
@@ -9,8 +15,8 @@ import { EventStore, QueryStore, Queries } from "applesauce-core";
9
15
  import { QueryStoreProvider } from "applesauce-react/providers";
10
16
  import { useStoreQuery } from "applesauce-react/hooks";
11
17
 
12
- const events = new EventStore();
13
- const store = new QueryStore(events);
18
+ const eventStore = new EventStore();
19
+ const queryStore = new QueryStore(eventStore);
14
20
 
15
21
  function UserName({ pubkey }) {
16
22
  const profile = useStoreQuery(Queries.ProfileQuery, [pubkey]);
@@ -20,7 +26,7 @@ function UserName({ pubkey }) {
20
26
 
21
27
  function App() {
22
28
  return (
23
- <QueryStoreProvider store={store}>
29
+ <QueryStoreProvider queryStore={queryStore}>
24
30
  <h1>App</h1>
25
31
 
26
32
  <UserName pubkey="82341f882b6eabcd2ba7f1ef90aad961cf074af15b9ef44a09f9d2a8fbfbe6a2" />
@@ -1,4 +1,10 @@
1
+ export * from "./use-account-manager.js";
2
+ export * from "./use-accounts.js";
3
+ export * from "./use-action-hub.js";
4
+ export * from "./use-action.js";
5
+ export * from "./use-active-account.js";
1
6
  export * from "./use-event-factory.js";
7
+ export * from "./use-event-store.js";
2
8
  export * from "./use-observable.js";
3
9
  export * from "./use-query-store.js";
4
10
  export * from "./use-render-nast.js";
@@ -1,4 +1,10 @@
1
+ export * from "./use-account-manager.js";
2
+ export * from "./use-accounts.js";
3
+ export * from "./use-action-hub.js";
4
+ export * from "./use-action.js";
5
+ export * from "./use-active-account.js";
1
6
  export * from "./use-event-factory.js";
7
+ export * from "./use-event-store.js";
2
8
  export * from "./use-observable.js";
3
9
  export * from "./use-query-store.js";
4
10
  export * from "./use-render-nast.js";
@@ -0,0 +1,4 @@
1
+ import { AccountManager } from "applesauce-accounts";
2
+ export declare function useAccountManager(): AccountManager;
3
+ export declare function useAccountManager(require: false): AccountManager | undefined;
4
+ export declare function useAccountManager(require: true): AccountManager;
@@ -0,0 +1,8 @@
1
+ import { useContext } from "react";
2
+ import { AccountsContext } from "../providers/accounts-provider.js";
3
+ export function useAccountManager(require = true) {
4
+ const manager = useContext(AccountsContext);
5
+ if (!manager && require)
6
+ throw new Error("Missing AccountsProvider");
7
+ return manager;
8
+ }
@@ -0,0 +1,2 @@
1
+ import { IAccount } from "applesauce-accounts";
2
+ export declare function useAccounts(): IAccount[];
@@ -0,0 +1,6 @@
1
+ import { useObservableEagerState } from "observable-hooks";
2
+ import { useAccountManager } from "./use-account-manager.js";
3
+ export function useAccounts() {
4
+ const manager = useAccountManager();
5
+ return useObservableEagerState(manager.accounts$);
6
+ }
@@ -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
+ }
@@ -0,0 +1,2 @@
1
+ import { IAccount } from "applesauce-accounts";
2
+ export declare function useActiveAccount(): IAccount | undefined;
@@ -0,0 +1,6 @@
1
+ import { useObservableEagerState } from "observable-hooks";
2
+ import { useAccountManager } from "./use-account-manager.js";
3
+ export function useActiveAccount() {
4
+ const manager = useAccountManager();
5
+ return useObservableEagerState(manager.active$);
6
+ }
@@ -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;
@@ -1,6 +1,4 @@
1
1
  import { type BehaviorSubject, type Observable } from "rxjs";
2
- export declare function getCurrentValue<T extends unknown>(observable: BehaviorSubject<T>): T;
3
- export declare function getCurrentValue<T extends unknown>(observable: Observable<T>): T | undefined;
4
- /** Subscribe to the value of an observable */
2
+ /** A thing wrapper around useObservableState that allows undefined */
5
3
  export declare function useObservable<T extends unknown>(observable?: BehaviorSubject<T>): T;
6
4
  export declare function useObservable<T extends unknown>(observable?: Observable<T>): T | undefined;
@@ -1,21 +1,5 @@
1
- import { useState, useEffect } from "react";
2
- export function getCurrentValue(observable) {
3
- if (Reflect.has(observable, "value"))
4
- return Reflect.get(observable, "value");
5
- return undefined;
6
- }
1
+ import { useObservableState } from "observable-hooks";
2
+ import { EMPTY } from "rxjs";
7
3
  export function useObservable(observable) {
8
- const current = observable && getCurrentValue(observable);
9
- const [_count, setCount] = useState(0);
10
- const [value, setValue] = useState(current);
11
- useEffect(() => {
12
- // Reset the state, the method passed to subscribe will NOT always be called
13
- setValue(observable && getCurrentValue(observable));
14
- const sub = observable?.subscribe((v) => {
15
- setValue(v);
16
- setCount((c) => c + 1);
17
- });
18
- return () => sub?.unsubscribe();
19
- }, [observable, setValue, setCount]);
20
- return current || value;
4
+ return useObservableState(observable ?? EMPTY);
21
5
  }
@@ -1,5 +1,6 @@
1
1
  import { useMemo } from "react";
2
- import { useObservable } from "./use-observable.js";
2
+ import { of } from "rxjs";
3
+ import { useObservableEagerState } from "observable-hooks";
3
4
  import { useQueryStore } from "./use-query-store.js";
4
5
  /**
5
6
  * Runs and subscribes to a query in the query store
@@ -12,7 +13,7 @@ export function useStoreQuery(queryConstructor, args) {
12
13
  if (args)
13
14
  return store.createQuery(queryConstructor, ...args);
14
15
  else
15
- return undefined;
16
+ return of(undefined);
16
17
  }, [args, store]);
17
- return useObservable(observable);
18
+ return useObservableEagerState(observable);
18
19
  }
@@ -0,0 +1,7 @@
1
+ import { PropsWithChildren } from "react";
2
+ import { AccountManager } from "applesauce-accounts";
3
+ export declare const AccountsContext: import("react").Context<AccountManager<any> | undefined>;
4
+ /** Provides an AccountManager to the component tree */
5
+ export declare function AccountsProvider({ manager, children }: PropsWithChildren<{
6
+ manager?: AccountManager;
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 AccountsContext = createContext(undefined);
4
+ /** Provides an AccountManager to the component tree */
5
+ export function AccountsProvider({ manager, children }) {
6
+ return _jsx(AccountsContext.Provider, { value: manager, children: children });
7
+ }
@@ -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,7 +1,7 @@
1
1
  import { EventFactory } from "applesauce-factory";
2
2
  import { PropsWithChildren } from "react";
3
3
  export declare const FactoryContext: import("react").Context<EventFactory | undefined>;
4
- /** Provides an EventFactory to the component tree */
4
+ /** Provides an {@link EventFactory} to the component tree */
5
5
  export declare function FactoryProvider({ factory, children }: PropsWithChildren<{
6
6
  factory?: EventFactory;
7
7
  }>): import("react/jsx-runtime").JSX.Element;
@@ -1,7 +1,7 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
2
  import { createContext } from "react";
3
3
  export const FactoryContext = createContext(undefined);
4
- /** Provides an EventFactory to the component tree */
4
+ /** Provides an {@link EventFactory} to the component tree */
5
5
  export function FactoryProvider({ factory, children }) {
6
6
  return _jsx(FactoryContext.Provider, { value: factory, children: children });
7
7
  }
@@ -1,2 +1,4 @@
1
1
  export * from "./factory-provider.js";
2
2
  export * from "./store-provider.js";
3
+ export * from "./accounts-provider.js";
4
+ export * from "./actions-provider.js";
@@ -1,2 +1,4 @@
1
1
  export * from "./factory-provider.js";
2
2
  export * from "./store-provider.js";
3
+ export * from "./accounts-provider.js";
4
+ export * from "./actions-provider.js";
@@ -1,10 +1,10 @@
1
- import { EventStore, QueryStore } from "applesauce-core";
2
1
  import { PropsWithChildren } from "react";
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,57 +1,68 @@
1
1
  {
2
2
  "name": "applesauce-react",
3
- "version": "0.10.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",
14
15
  "files": [
15
- "dist"
16
+ "dist",
17
+ "applesauce"
16
18
  ],
17
19
  "exports": {
18
20
  ".": {
19
21
  "import": "./dist/index.js",
22
+ "require": "./dist/index.js",
20
23
  "types": "./dist/index.d.ts"
21
24
  },
22
25
  "./hooks": {
23
26
  "import": "./dist/hooks/index.js",
27
+ "require": "./dist/hooks/index.js",
24
28
  "types": "./dist/hooks/index.d.ts"
25
29
  },
26
30
  "./hooks/*": {
27
31
  "import": "./dist/hooks/*.js",
32
+ "require": "./dist/hooks/*.js",
28
33
  "types": "./dist/hooks/*.d.ts"
29
34
  },
30
35
  "./providers": {
31
36
  "import": "./dist/providers/index.js",
37
+ "require": "./dist/providers/index.js",
32
38
  "types": "./dist/providers/index.d.ts"
33
39
  },
34
40
  "./providers/*": {
35
41
  "import": "./dist/providers/*.js",
42
+ "require": "./dist/providers/*.js",
36
43
  "types": "./dist/providers/*.d.ts"
37
44
  },
38
45
  "./helpers": {
39
46
  "import": "./dist/helpers/index.js",
47
+ "require": "./dist/helpers/index.js",
40
48
  "types": "./dist/helpers/index.d.ts"
41
49
  }
42
50
  },
43
51
  "dependencies": {
44
- "applesauce-content": "^0.10.0",
45
- "applesauce-core": "^0.10.0",
46
- "applesauce-factory": "^0.10.0",
47
- "nostr-tools": "^2.10.3",
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",
57
+ "nostr-tools": "^2.10.4",
58
+ "observable-hooks": "^4.2.4",
48
59
  "react": "^18.3.1",
49
60
  "rxjs": "^7.8.1"
50
61
  },
51
62
  "devDependencies": {
52
- "@types/react": "^18.3.11",
53
- "typescript": "^5.6.3",
54
- "vitest": "^2.1.8"
63
+ "@types/react": "^18.3.18",
64
+ "typescript": "^5.7.3",
65
+ "vitest": "^3.0.5"
55
66
  },
56
67
  "funding": {
57
68
  "type": "lightning",