applesauce-react 0.10.0 → 0.11.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,6 +1,10 @@
1
+ export * from "./use-account-manager.js";
2
+ export * from "./use-accounts.js";
3
+ export * from "./use-active-account.js";
1
4
  export * from "./use-event-factory.js";
2
5
  export * from "./use-observable.js";
3
6
  export * from "./use-query-store.js";
4
7
  export * from "./use-render-nast.js";
5
8
  export * from "./use-rendered-content.js";
6
9
  export * from "./use-store-query.js";
10
+ export * from "./use-event-store.js";
@@ -1,6 +1,10 @@
1
+ export * from "./use-account-manager.js";
2
+ export * from "./use-accounts.js";
3
+ export * from "./use-active-account.js";
1
4
  export * from "./use-event-factory.js";
2
5
  export * from "./use-observable.js";
3
6
  export * from "./use-query-store.js";
4
7
  export * from "./use-render-nast.js";
5
8
  export * from "./use-rendered-content.js";
6
9
  export * from "./use-store-query.js";
10
+ export * from "./use-event-store.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,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,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
+ }
@@ -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,3 @@
1
1
  export * from "./factory-provider.js";
2
2
  export * from "./store-provider.js";
3
+ export * from "./accounts-provider.js";
@@ -1,2 +1,3 @@
1
1
  export * from "./factory-provider.js";
2
2
  export * from "./store-provider.js";
3
+ export * from "./accounts-provider.js";
@@ -1,5 +1,5 @@
1
- import { EventStore, QueryStore } from "applesauce-core";
2
1
  import { PropsWithChildren } from "react";
2
+ import { EventStore, QueryStore } from "applesauce-core";
3
3
  export declare const QueryStoreContext: import("react").Context<QueryStore | null>;
4
4
  export declare const EventStoreContext: import("react").Context<EventStore | null>;
5
5
  /** Provides a EventStore to the component tree */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "applesauce-react",
3
- "version": "0.10.0",
3
+ "version": "0.11.0",
4
4
  "description": "React hooks for applesauce",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -12,46 +12,55 @@
12
12
  "author": "hzrd149",
13
13
  "license": "MIT",
14
14
  "files": [
15
- "dist"
15
+ "dist",
16
+ "applesauce"
16
17
  ],
17
18
  "exports": {
18
19
  ".": {
19
20
  "import": "./dist/index.js",
21
+ "require": "./dist/index.js",
20
22
  "types": "./dist/index.d.ts"
21
23
  },
22
24
  "./hooks": {
23
25
  "import": "./dist/hooks/index.js",
26
+ "require": "./dist/hooks/index.js",
24
27
  "types": "./dist/hooks/index.d.ts"
25
28
  },
26
29
  "./hooks/*": {
27
30
  "import": "./dist/hooks/*.js",
31
+ "require": "./dist/hooks/*.js",
28
32
  "types": "./dist/hooks/*.d.ts"
29
33
  },
30
34
  "./providers": {
31
35
  "import": "./dist/providers/index.js",
36
+ "require": "./dist/providers/index.js",
32
37
  "types": "./dist/providers/index.d.ts"
33
38
  },
34
39
  "./providers/*": {
35
40
  "import": "./dist/providers/*.js",
41
+ "require": "./dist/providers/*.js",
36
42
  "types": "./dist/providers/*.d.ts"
37
43
  },
38
44
  "./helpers": {
39
45
  "import": "./dist/helpers/index.js",
46
+ "require": "./dist/helpers/index.js",
40
47
  "types": "./dist/helpers/index.d.ts"
41
48
  }
42
49
  },
43
50
  "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",
51
+ "applesauce-accounts": "^0.11.0",
52
+ "applesauce-content": "^0.11.0",
53
+ "applesauce-core": "^0.11.0",
54
+ "applesauce-factory": "^0.11.0",
55
+ "nostr-tools": "^2.10.4",
56
+ "observable-hooks": "^4.2.4",
48
57
  "react": "^18.3.1",
49
58
  "rxjs": "^7.8.1"
50
59
  },
51
60
  "devDependencies": {
52
- "@types/react": "^18.3.11",
53
- "typescript": "^5.6.3",
54
- "vitest": "^2.1.8"
61
+ "@types/react": "^18.3.18",
62
+ "typescript": "^5.7.3",
63
+ "vitest": "^3.0.5"
55
64
  },
56
65
  "funding": {
57
66
  "type": "lightning",