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 +12 -5
- package/dist/hooks/index.d.ts +7 -2
- package/dist/hooks/index.js +7 -2
- package/dist/hooks/use-account-manager.d.ts +4 -0
- package/dist/hooks/use-account-manager.js +8 -0
- package/dist/hooks/use-accounts.d.ts +2 -0
- package/dist/hooks/use-accounts.js +6 -0
- package/dist/hooks/use-active-account.d.ts +2 -0
- package/dist/hooks/use-active-account.js +6 -0
- package/dist/hooks/use-event-factory.d.ts +4 -0
- package/dist/hooks/use-event-factory.js +8 -0
- package/dist/hooks/use-event-store.d.ts +5 -0
- package/dist/hooks/use-event-store.js +12 -0
- package/dist/hooks/use-observable.d.ts +1 -3
- package/dist/hooks/use-observable.js +3 -15
- package/dist/hooks/use-query-store.js +1 -1
- package/dist/hooks/use-store-query.js +5 -4
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/providers/accounts-provider.d.ts +7 -0
- package/dist/providers/accounts-provider.js +7 -0
- package/dist/providers/factory-provider.d.ts +7 -0
- package/dist/providers/factory-provider.js +7 -0
- package/dist/providers/index.d.ts +3 -0
- package/dist/providers/index.js +3 -0
- package/dist/providers/store-provider.d.ts +12 -0
- package/dist/providers/store-provider.js +12 -0
- package/package.json +28 -21
- package/dist/provider.d.ts +0 -7
- package/dist/provider.js +0 -7
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
|
|
15
|
+
import { QueryStoreProvider } from "applesauce-react/providers";
|
|
16
|
+
import { useStoreQuery } from "applesauce-react/hooks";
|
|
10
17
|
|
|
11
|
-
const
|
|
12
|
-
const
|
|
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
|
|
29
|
+
<QueryStoreProvider queryStore={queryStore}>
|
|
23
30
|
<h1>App</h1>
|
|
24
31
|
|
|
25
32
|
<UserName pubkey="82341f882b6eabcd2ba7f1ef90aad961cf074af15b9ef44a09f9d2a8fbfbe6a2" />
|
package/dist/hooks/index.d.ts
CHANGED
|
@@ -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";
|
package/dist/hooks/index.js
CHANGED
|
@@ -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,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,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
|
-
|
|
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 {
|
|
2
|
-
|
|
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
|
-
|
|
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 {
|
|
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.
|
|
14
|
+
return store.createQuery(queryConstructor, ...args);
|
|
14
15
|
else
|
|
15
|
-
return undefined;
|
|
16
|
+
return of(undefined);
|
|
16
17
|
}, [args, store]);
|
|
17
|
-
return
|
|
18
|
+
return useObservableEagerState(observable);
|
|
18
19
|
}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -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,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.
|
|
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-
|
|
37
|
-
"applesauce-
|
|
38
|
-
"
|
|
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
|
-
"@
|
|
44
|
-
"
|
|
45
|
-
"
|
|
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": "
|
|
66
|
-
"watch:test": "
|
|
72
|
+
"test": "vitest run --passWithNoTests",
|
|
73
|
+
"watch:test": "vitest"
|
|
67
74
|
}
|
|
68
75
|
}
|
package/dist/provider.d.ts
DELETED
|
@@ -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
|
-
}
|