applesauce-react 5.0.0 → 5.1.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,3 +1,5 @@
1
1
  import { Link } from "applesauce-content/nast";
2
+ import { ComponentMap } from "./nast.js";
2
3
  export type LinkRenderer = (url: URL, node: Link) => JSX.Element | false | null;
3
- export declare function buildLinkRenderer(handlers: LinkRenderer[]): import("react").NamedExoticComponent<import("./nast.js").ExtraProps<Link>>;
4
+ /** Creates a link renderer component that can be used in the {@link ComponentMap} */
5
+ export declare function buildLinkRenderer(handlers: LinkRenderer[]): ComponentMap["link"];
@@ -1,5 +1,6 @@
1
1
  import { Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime";
2
2
  import { memo, useMemo } from "react";
3
+ /** Creates a link renderer component that can be used in the {@link ComponentMap} */
3
4
  export function buildLinkRenderer(handlers) {
4
5
  const LinkRenderer = ({ node }) => {
5
6
  const content = useMemo(() => {
@@ -5,3 +5,57 @@ export function use$(observable, deps) {
5
5
  const resolved = useMemo(() => (typeof observable === "function" ? observable() : observable) ?? of(undefined), deps ?? [observable]);
6
6
  return useObservableState(resolved);
7
7
  }
8
+ // export function use$<T>(input?: BehaviorSubject<T>): T;
9
+ // export function use$<T>(input?: Observable<T> | undefined): T | undefined;
10
+ // export function use$<T>(input: () => Observable<T> | undefined, deps: unknown[]): T | undefined;
11
+ // export function use$<T>(
12
+ // input?: Observable<T> | BehaviorSubject<T> | (() => Observable<T> | undefined),
13
+ // deps?: unknown[],
14
+ // ): T | undefined {
15
+ // const state$: Observable<T | undefined> = useMemo(
16
+ // () => (typeof input === "function" ? input() : input) ?? of(undefined),
17
+ // deps ?? [input],
18
+ // );
19
+ // const valueRef = useRef<T | undefined>(state$ instanceof BehaviorSubject ? state$.getValue() : undefined);
20
+ // const subRef = useRef<Subscription | null>(null);
21
+ // const callbackRef = useRef<(() => void) | null>(null);
22
+ // const subscribe = useCallback(
23
+ // (callback: () => void) => {
24
+ // // Store the callback
25
+ // callbackRef.current = callback;
26
+ // // Subscribe if not already subscribed
27
+ // if (!subRef.current) {
28
+ // subRef.current = state$.subscribe((v) => {
29
+ // valueRef.current = v;
30
+ // callbackRef.current?.();
31
+ // });
32
+ // }
33
+ // return () => {
34
+ // subRef.current?.unsubscribe();
35
+ // subRef.current = null;
36
+ // callbackRef.current = null;
37
+ // };
38
+ // },
39
+ // [state$],
40
+ // );
41
+ // const getSnapshot = useCallback(() => {
42
+ // let inSnapshot = true;
43
+ // // Server snapshot
44
+ // if (typeof window === "undefined") {
45
+ // // On server: use take(1) and don't store the ref
46
+ // state$.pipe(take(1)).subscribe((v) => {
47
+ // valueRef.current = v;
48
+ // });
49
+ // } else if (!subRef.current) {
50
+ // // Create subscription if needed to get the initial value
51
+ // subRef.current = state$.subscribe((v) => {
52
+ // valueRef.current = v;
53
+ // // Call the callback if it exists (set by subscribe)
54
+ // if (!inSnapshot) callbackRef.current?.();
55
+ // });
56
+ // }
57
+ // inSnapshot = false;
58
+ // return valueRef.current;
59
+ // }, [state$]);
60
+ // return useSyncExternalStore(subscribe, getSnapshot);
61
+ // }
@@ -1,4 +1,5 @@
1
1
  import { AccountManager } from "applesauce-accounts";
2
+ /** Gets the {@link AccountManager} from the {@link AccountsProvider} */
2
3
  export declare function useAccountManager(): AccountManager;
3
4
  export declare function useAccountManager(require: false): AccountManager | undefined;
4
5
  export declare function useAccountManager(require: true): AccountManager;
@@ -1,2 +1,3 @@
1
1
  import { IAccount } from "applesauce-accounts";
2
+ /** Gets the list of accounts from the {@link AccountManager} */
2
3
  export declare function useAccounts(): IAccount[];
@@ -1,5 +1,6 @@
1
1
  import { useObservableEagerState } from "observable-hooks";
2
2
  import { useAccountManager } from "./use-account-manager.js";
3
+ /** Gets the list of accounts from the {@link AccountManager} */
3
4
  export function useAccounts() {
4
5
  const manager = useAccountManager();
5
6
  return useObservableEagerState(manager.accounts$);
@@ -1 +1,2 @@
1
+ /** Gets the {@link ActionRunner} from the {@link ActionsProvider} */
1
2
  export declare function useActionRunner(): import("applesauce-actions").ActionRunner;
@@ -1,5 +1,6 @@
1
1
  import { useContext } from "react";
2
2
  import { ActionsContext } from "../providers/actions-provider.js";
3
+ /** Gets the {@link ActionRunner} from the {@link ActionsProvider} */
3
4
  export function useActionRunner() {
4
5
  const hub = useContext(ActionsContext);
5
6
  if (!hub)
@@ -1,6 +1,11 @@
1
1
  import { ActionBuilder } from "applesauce-actions";
2
- export declare function useAction<Args extends Array<any>>(Action: ActionBuilder<Args>, args: Args | undefined): {
2
+ import { NostrEvent } from "applesauce-core/helpers/event";
3
+ import { Observable } from "rxjs";
4
+ export type UseActionResult<Args extends Array<any>> = {
3
5
  loading: boolean;
4
- run: () => Promise<void>;
5
- exec: () => import("rxjs").Observable<import("nostr-tools").NostrEvent> | undefined;
6
+ run: (...args: Args) => Promise<void>;
7
+ exec: (...args: Args) => Observable<NostrEvent>;
6
8
  };
9
+ /** A hook to run an action inside the {@link ActionsProvider} */
10
+ export declare function useAction<Args extends Array<any>>(Action: ActionBuilder<Args>): UseActionResult<Args>;
11
+ export declare function useAction<Args extends Array<any>>(Action: ActionBuilder<Args>, args: Args | undefined): UseActionResult<Args>;
@@ -3,15 +3,13 @@ import { finalize } from "rxjs";
3
3
  import { useActionRunner } from "./use-action-runner.js";
4
4
  export function useAction(Action, args) {
5
5
  const [loading, setLoading] = useState(false);
6
- const ref = useRef(args);
7
- ref.current = args;
6
+ const staticArgs = useRef(args);
7
+ staticArgs.current = args;
8
8
  const hub = useActionRunner();
9
- const run = useCallback(async () => {
10
- if (args === undefined)
11
- return;
9
+ const run = useCallback(async (...args) => {
12
10
  setLoading(true);
13
11
  try {
14
- await hub.run(Action, ...args);
12
+ await hub.run(Action, ...(staticArgs.current ?? args));
15
13
  setLoading(false);
16
14
  }
17
15
  catch (error) {
@@ -19,12 +17,10 @@ export function useAction(Action, args) {
19
17
  throw error;
20
18
  }
21
19
  }, [Action]);
22
- const exec = useCallback(() => {
23
- if (args === undefined)
24
- return;
20
+ const exec = useCallback((...args) => {
25
21
  setLoading(true);
26
22
  try {
27
- return hub.exec(Action, ...args).pipe(finalize(() => {
23
+ return hub.exec(Action, ...(staticArgs.current ?? args)).pipe(finalize(() => {
28
24
  setLoading(false);
29
25
  }));
30
26
  }
@@ -1,2 +1,3 @@
1
1
  import { IAccount } from "applesauce-accounts";
2
+ /** Gets the currently active account from the {@link AccountsProvider} */
2
3
  export declare function useActiveAccount(): IAccount | undefined;
@@ -1,5 +1,6 @@
1
1
  import { useObservableEagerState } from "observable-hooks";
2
2
  import { useAccountManager } from "./use-account-manager.js";
3
+ /** Gets the currently active account from the {@link AccountsProvider} */
3
4
  export function useActiveAccount() {
4
5
  const manager = useAccountManager();
5
6
  return useObservableEagerState(manager.active$);
@@ -1,4 +1,5 @@
1
1
  import { EventFactory } from "applesauce-core";
2
+ /** Gets the {@link EventFactory} from the {@link EventFactoryProvider} */
2
3
  export declare function useEventFactory(require: false): EventFactory | undefined;
3
4
  export declare function useEventFactory(require: true): EventFactory;
4
5
  export declare function useEventFactory(): EventFactory;
@@ -1,3 +1,3 @@
1
1
  import { ModelConstructor } from "applesauce-core";
2
- /** Runs and subscribes to a model on the event store */
2
+ /** Runs and subscribes to a model on the {@link EventStore} */
3
3
  export declare function useEventModel<T extends unknown, Args extends Array<any>>(factory: ModelConstructor<T, Args>, args?: Args | null): T | undefined;
@@ -3,7 +3,7 @@ import hash_sum from "hash-sum";
3
3
  import { of } from "rxjs";
4
4
  import { useEventStore } from "./use-event-store.js";
5
5
  import { useObservableEagerMemo } from "./use-observable-memo.js";
6
- /** Runs and subscribes to a model on the event store */
6
+ /** Runs and subscribes to a model on the {@link EventStore} */
7
7
  export function useEventModel(factory, args) {
8
8
  const store = useEventStore();
9
9
  return useObservableEagerMemo(() => {
@@ -1,6 +1,3 @@
1
1
  import { IEventStore } from "applesauce-core/event-store";
2
- /**
3
- * Gets the EventStore from a parent {@link EventStoreProvider} component
4
- * If there is none it throws an error
5
- */
2
+ /** Gets the {@link EventStore} from the {@link EventStoreProvider} */
6
3
  export declare function useEventStore(): IEventStore;
@@ -1,9 +1,6 @@
1
1
  import { useContext } from "react";
2
2
  import { EventStoreContext } from "../providers/store-provider.js";
3
- /**
4
- * Gets the EventStore from a parent {@link EventStoreProvider} component
5
- * If there is none it throws an error
6
- */
3
+ /** Gets the {@link EventStore} from the {@link EventStoreProvider} */
7
4
  export function useEventStore() {
8
5
  const store = useContext(EventStoreContext);
9
6
  if (!store)
@@ -1,5 +1,5 @@
1
1
  import { Root } from "applesauce-content/nast";
2
2
  import { ComponentMap } from "../helpers/nast.js";
3
3
  export { ComponentMap };
4
- /** A hook to get the rendered output of a nostr syntax tree */
4
+ /** A hook to get the rendered output of a {@link Root} nostr syntax tree */
5
5
  export declare function useRenderNast(root: Root | undefined, components: ComponentMap): JSX.Element | null;
@@ -1,6 +1,6 @@
1
1
  import { useMemo } from "react";
2
2
  import { renderNast } from "../helpers/nast.js";
3
- /** A hook to get the rendered output of a nostr syntax tree */
3
+ /** A hook to get the rendered output of a {@link Root} nostr syntax tree */
4
4
  export function useRenderNast(root, components) {
5
5
  return useMemo(() => (root ? renderNast(root, components) : null), [root, Object.keys(components).join("|")]);
6
6
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "applesauce-react",
3
- "version": "5.0.0",
3
+ "version": "5.1.0",
4
4
  "description": "React hooks for applesauce",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -47,19 +47,28 @@
47
47
  "types": "./dist/helpers/index.d.ts"
48
48
  }
49
49
  },
50
+ "peerDependencies": {
51
+ "react": "^18.0.0 || ^19.0.0"
52
+ },
50
53
  "dependencies": {
51
- "applesauce-accounts": "^5.0.0",
52
- "applesauce-actions": "^5.0.0",
53
- "applesauce-content": "^5.0.0",
54
- "applesauce-core": "^5.0.0",
55
54
  "hash-sum": "^2.0.0",
56
55
  "observable-hooks": "^4.2.4",
57
- "react": "^18.3.1",
58
56
  "rxjs": "^7.8.1"
59
57
  },
58
+ "optionalDependencies": {
59
+ "applesauce-accounts": "^5.1.0",
60
+ "applesauce-actions": "^5.0.0",
61
+ "applesauce-content": "^5.0.0",
62
+ "applesauce-core": "^5.1.0"
63
+ },
60
64
  "devDependencies": {
61
65
  "@types/hash-sum": "^1.0.2",
62
- "@types/react": "^18.3.18",
66
+ "@types/react": "^18.0.0 || ^19.0.0",
67
+ "applesauce-accounts": "^5.1.0",
68
+ "applesauce-actions": "^5.0.0",
69
+ "applesauce-content": "^5.0.0",
70
+ "applesauce-core": "^5.1.0",
71
+ "react": "^18.0.0 || ^19.0.0",
63
72
  "rimraf": "^6.0.1",
64
73
  "typescript": "^5.8.3",
65
74
  "vitest": "^4.0.15"