applesauce-react 0.9.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,15 +1,22 @@
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
 
7
13
  ```jsx
8
14
  import { EventStore, QueryStore, Queries } from "applesauce-core";
9
- import { QueryStoreProvider, useStoreQuery } from "applesauce-react";
15
+ import { QueryStoreProvider } from "applesauce-react/providers";
16
+ import { useStoreQuery } from "applesauce-react/hooks";
10
17
 
11
- const events = new EventStore();
12
- const store = new QueryStore(events);
18
+ const eventStore = new EventStore();
19
+ const queryStore = new QueryStore(eventStore);
13
20
 
14
21
  function UserName({ pubkey }) {
15
22
  const profile = useStoreQuery(Queries.ProfileQuery, [pubkey]);
@@ -19,7 +26,7 @@ function UserName({ pubkey }) {
19
26
 
20
27
  function App() {
21
28
  return (
22
- <QueryStoreProvider store={store}>
29
+ <QueryStoreProvider queryStore={queryStore}>
23
30
  <h1>App</h1>
24
31
 
25
32
  <UserName pubkey="82341f882b6eabcd2ba7f1ef90aad961cf074af15b9ef44a09f9d2a8fbfbe6a2" />
@@ -1,5 +1,10 @@
1
+ export * from "./use-account-manager.js";
2
+ export * from "./use-accounts.js";
3
+ export * from "./use-active-account.js";
4
+ export * from "./use-event-factory.js";
1
5
  export * from "./use-observable.js";
2
- export * from "./use-store-query.js";
3
- export * from "./use-render-nast.js";
4
6
  export * from "./use-query-store.js";
7
+ export * from "./use-render-nast.js";
5
8
  export * from "./use-rendered-content.js";
9
+ export * from "./use-store-query.js";
10
+ export * from "./use-event-store.js";
@@ -1,5 +1,10 @@
1
+ export * from "./use-account-manager.js";
2
+ export * from "./use-accounts.js";
3
+ export * from "./use-active-account.js";
4
+ export * from "./use-event-factory.js";
1
5
  export * from "./use-observable.js";
2
- export * from "./use-store-query.js";
3
- export * from "./use-render-nast.js";
4
6
  export * from "./use-query-store.js";
7
+ export * from "./use-render-nast.js";
5
8
  export * from "./use-rendered-content.js";
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
+ }
@@ -0,0 +1,4 @@
1
+ import { EventFactory } from "applesauce-factory";
2
+ export declare function useEventFactory(require: false): EventFactory | undefined;
3
+ export declare function useEventFactory(require: true): EventFactory;
4
+ export declare function useEventFactory(): EventFactory;
@@ -0,0 +1,8 @@
1
+ import { useContext } from "react";
2
+ import { FactoryContext } from "../providers/factory-provider.js";
3
+ export function useEventFactory(require = true) {
4
+ const factory = useContext(FactoryContext);
5
+ if (!require && !factory)
6
+ throw new Error("Missing EventFactoryProvider");
7
+ return factory;
8
+ }
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Gets the EventStore from a parent {@link EventStoreProvider} component
3
+ * If there is none it throws an error
4
+ */
5
+ export declare function useEventStore(): import("applesauce-core").EventStore;
@@ -0,0 +1,12 @@
1
+ import { useContext } from "react";
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
+ */
7
+ export function useEventStore() {
8
+ const store = useContext(EventStoreContext);
9
+ if (!store)
10
+ throw new Error("Missing EventStoreProvider");
11
+ return store;
12
+ }
@@ -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,17 +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 [value, setValue] = useState(current);
10
- useEffect(() => {
11
- // Reset the state, the method passed to subscribe will NOT always be called
12
- setValue(observable && getCurrentValue(observable));
13
- const sub = observable?.subscribe(setValue);
14
- return () => sub?.unsubscribe();
15
- }, [observable, setValue]);
16
- return current || value;
4
+ return useObservableState(observable ?? EMPTY);
17
5
  }
@@ -1,5 +1,5 @@
1
1
  import { useContext } from "react";
2
- import { QueryStoreContext } from "../provider.js";
2
+ import { QueryStoreContext } from "../providers/store-provider.js";
3
3
  /**
4
4
  * Gets the QueryStore from a parent {@link QueryStoreProvider} component
5
5
  * If there is none it throws an error
@@ -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
@@ -10,9 +11,9 @@ export function useStoreQuery(queryConstructor, args) {
10
11
  const store = useQueryStore();
11
12
  const observable = useMemo(() => {
12
13
  if (args)
13
- return store.runQuery(queryConstructor)(...args);
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
  }
package/dist/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- export * from "./provider.js";
2
1
  export * as Helpers from "./helpers/index.js";
3
2
  export * as Hooks from "./hooks/index.js";
3
+ export * from "./providers/index.js";
package/dist/index.js CHANGED
@@ -1,3 +1,3 @@
1
- export * from "./provider.js";
2
1
  export * as Helpers from "./helpers/index.js";
3
2
  export * as Hooks from "./hooks/index.js";
3
+ export * from "./providers/index.js";
@@ -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 { EventFactory } from "applesauce-factory";
2
+ import { PropsWithChildren } from "react";
3
+ export declare const FactoryContext: import("react").Context<EventFactory | undefined>;
4
+ /** Provides an {@link EventFactory} to the component tree */
5
+ export declare function FactoryProvider({ factory, children }: PropsWithChildren<{
6
+ factory?: EventFactory;
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 FactoryContext = createContext(undefined);
4
+ /** Provides an {@link EventFactory} to the component tree */
5
+ export function FactoryProvider({ factory, children }) {
6
+ return _jsx(FactoryContext.Provider, { value: factory, children: children });
7
+ }
@@ -0,0 +1,3 @@
1
+ export * from "./factory-provider.js";
2
+ export * from "./store-provider.js";
3
+ export * from "./accounts-provider.js";
@@ -0,0 +1,3 @@
1
+ export * from "./factory-provider.js";
2
+ export * from "./store-provider.js";
3
+ export * from "./accounts-provider.js";
@@ -0,0 +1,12 @@
1
+ import { PropsWithChildren } from "react";
2
+ import { EventStore, QueryStore } from "applesauce-core";
3
+ export declare const QueryStoreContext: import("react").Context<QueryStore | null>;
4
+ export declare const EventStoreContext: import("react").Context<EventStore | null>;
5
+ /** Provides a EventStore to the component tree */
6
+ export declare function EventStoreProvider({ eventStore, children }: PropsWithChildren<{
7
+ eventStore: EventStore;
8
+ }>): import("react/jsx-runtime").JSX.Element;
9
+ /** Provides a QueryStore and EventStore to the component tree */
10
+ export declare function QueryStoreProvider({ queryStore, children }: PropsWithChildren<{
11
+ queryStore: QueryStore;
12
+ }>): import("react/jsx-runtime").JSX.Element;
@@ -0,0 +1,12 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { createContext } from "react";
3
+ export const QueryStoreContext = createContext(null);
4
+ export const EventStoreContext = createContext(null);
5
+ /** Provides a EventStore to the component tree */
6
+ export function EventStoreProvider({ eventStore, children }) {
7
+ return _jsx(EventStoreContext.Provider, { value: eventStore, children: children });
8
+ }
9
+ /** Provides a QueryStore and EventStore to the component tree */
10
+ export function QueryStoreProvider({ queryStore, children }) {
11
+ return (_jsx(EventStoreProvider, { eventStore: queryStore.store, children: _jsx(QueryStoreContext.Provider, { value: queryStore, children: children }) }));
12
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "applesauce-react",
3
- "version": "0.9.0",
3
+ "version": "0.11.0",
4
4
  "description": "React hooks for applesauce",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -12,48 +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
  },
34
+ "./providers": {
35
+ "import": "./dist/providers/index.js",
36
+ "require": "./dist/providers/index.js",
37
+ "types": "./dist/providers/index.d.ts"
38
+ },
39
+ "./providers/*": {
40
+ "import": "./dist/providers/*.js",
41
+ "require": "./dist/providers/*.js",
42
+ "types": "./dist/providers/*.d.ts"
43
+ },
30
44
  "./helpers": {
31
45
  "import": "./dist/helpers/index.js",
46
+ "require": "./dist/helpers/index.js",
32
47
  "types": "./dist/helpers/index.d.ts"
33
48
  }
34
49
  },
35
50
  "dependencies": {
36
- "applesauce-content": "^0.9.0",
37
- "applesauce-core": "^0.9.0",
38
- "nostr-tools": "^2.10.1",
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",
39
57
  "react": "^18.3.1",
40
58
  "rxjs": "^7.8.1"
41
59
  },
42
60
  "devDependencies": {
43
- "@jest/globals": "^29.7.0",
44
- "@types/jest": "^29.5.13",
45
- "@types/react": "^18.3.11",
46
- "jest": "^29.7.0",
47
- "jest-extended": "^4.0.2",
48
- "typescript": "^5.6.3"
49
- },
50
- "jest": {
51
- "roots": [
52
- "dist"
53
- ],
54
- "setupFilesAfterEnv": [
55
- "jest-extended/all"
56
- ]
61
+ "@types/react": "^18.3.18",
62
+ "typescript": "^5.7.3",
63
+ "vitest": "^3.0.5"
57
64
  },
58
65
  "funding": {
59
66
  "type": "lightning",
@@ -62,7 +69,7 @@
62
69
  "scripts": {
63
70
  "build": "tsc",
64
71
  "watch:build": "tsc --watch > /dev/null",
65
- "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js --passWithNoTests",
66
- "watch:test": "(trap 'kill 0' SIGINT; pnpm run build -w > /dev/null & pnpm run test --watch)"
72
+ "test": "vitest run --passWithNoTests",
73
+ "watch:test": "vitest"
67
74
  }
68
75
  }
@@ -1,7 +0,0 @@
1
- import { QueryStore } from "applesauce-core";
2
- import { PropsWithChildren } from "react";
3
- export declare const QueryStoreContext: import("react").Context<QueryStore | null>;
4
- /** Provides a QueryStore to the component tree */
5
- export declare function QueryStoreProvider({ store, children }: PropsWithChildren<{
6
- store: QueryStore;
7
- }>): import("react/jsx-runtime").JSX.Element;
package/dist/provider.js DELETED
@@ -1,7 +0,0 @@
1
- import { jsx as _jsx } from "react/jsx-runtime";
2
- import { createContext } from "react";
3
- export const QueryStoreContext = createContext(null);
4
- /** Provides a QueryStore to the component tree */
5
- export function QueryStoreProvider({ store, children }) {
6
- return _jsx(QueryStoreContext.Provider, { value: store, children: children });
7
- }