applesauce-react 0.8.0 → 0.10.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 +2 -1
- package/dist/helpers/build-link-renderer.d.ts +0 -1
- package/dist/hooks/index.d.ts +4 -3
- package/dist/hooks/index.js +4 -3
- 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 +2 -0
- package/dist/hooks/use-observable.js +13 -5
- package/dist/hooks/use-query-store.js +1 -1
- package/dist/hooks/use-render-nast.d.ts +0 -1
- package/dist/hooks/{use-rendered-text-content.d.ts → use-rendered-content.d.ts} +4 -3
- package/dist/hooks/{use-rendered-text-content.js → use-rendered-content.js} +3 -3
- package/dist/hooks/use-store-query.js +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/providers/factory-provider.d.ts +7 -0
- package/dist/providers/factory-provider.js +7 -0
- package/dist/providers/index.d.ts +2 -0
- package/dist/providers/index.js +2 -0
- package/dist/providers/store-provider.d.ts +12 -0
- package/dist/providers/store-provider.js +12 -0
- package/package.json +20 -17
- package/dist/provider.d.ts +0 -7
- package/dist/provider.js +0 -7
package/README.md
CHANGED
|
@@ -6,7 +6,8 @@ React hooks for applesauce
|
|
|
6
6
|
|
|
7
7
|
```jsx
|
|
8
8
|
import { EventStore, QueryStore, Queries } from "applesauce-core";
|
|
9
|
-
import { QueryStoreProvider
|
|
9
|
+
import { QueryStoreProvider } from "applesauce-react/providers";
|
|
10
|
+
import { useStoreQuery } from "applesauce-react/hooks";
|
|
10
11
|
|
|
11
12
|
const events = new EventStore();
|
|
12
13
|
const store = new QueryStore(events);
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
/// <reference types="react" />
|
|
2
1
|
import { Link } from "applesauce-content/nast";
|
|
3
2
|
export type LinkRenderer = (url: URL, node: Link) => JSX.Element | false | null;
|
|
4
3
|
export declare function buildLinkRenderer(handlers: LinkRenderer[]): import("react").NamedExoticComponent<import("./nast.js").ExtraProps<Link>>;
|
package/dist/hooks/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
export * from "./use-event-factory.js";
|
|
1
2
|
export * from "./use-observable.js";
|
|
2
|
-
export * from "./use-store-query.js";
|
|
3
|
-
export * from "./use-render-nast.js";
|
|
4
3
|
export * from "./use-query-store.js";
|
|
5
|
-
export * from "./use-
|
|
4
|
+
export * from "./use-render-nast.js";
|
|
5
|
+
export * from "./use-rendered-content.js";
|
|
6
|
+
export * from "./use-store-query.js";
|
package/dist/hooks/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
export * from "./use-event-factory.js";
|
|
1
2
|
export * from "./use-observable.js";
|
|
2
|
-
export * from "./use-store-query.js";
|
|
3
|
-
export * from "./use-render-nast.js";
|
|
4
3
|
export * from "./use-query-store.js";
|
|
5
|
-
export * from "./use-
|
|
4
|
+
export * from "./use-render-nast.js";
|
|
5
|
+
export * from "./use-rendered-content.js";
|
|
6
|
+
export * from "./use-store-query.js";
|
|
@@ -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,4 +1,6 @@
|
|
|
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;
|
|
2
4
|
/** Subscribe to the value of an observable */
|
|
3
5
|
export declare function useObservable<T extends unknown>(observable?: BehaviorSubject<T>): T;
|
|
4
6
|
export declare function useObservable<T extends unknown>(observable?: Observable<T>): T | undefined;
|
|
@@ -1,13 +1,21 @@
|
|
|
1
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
|
+
}
|
|
2
7
|
export function useObservable(observable) {
|
|
3
|
-
const
|
|
4
|
-
const [
|
|
8
|
+
const current = observable && getCurrentValue(observable);
|
|
9
|
+
const [_count, setCount] = useState(0);
|
|
10
|
+
const [value, setValue] = useState(current);
|
|
5
11
|
useEffect(() => {
|
|
12
|
+
// Reset the state, the method passed to subscribe will NOT always be called
|
|
13
|
+
setValue(observable && getCurrentValue(observable));
|
|
6
14
|
const sub = observable?.subscribe((v) => {
|
|
7
15
|
setValue(v);
|
|
8
|
-
|
|
16
|
+
setCount((c) => c + 1);
|
|
9
17
|
});
|
|
10
18
|
return () => sub?.unsubscribe();
|
|
11
|
-
}, [observable]);
|
|
12
|
-
return value;
|
|
19
|
+
}, [observable, setValue, setCount]);
|
|
20
|
+
return current || value;
|
|
13
21
|
}
|
|
@@ -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,12 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
import { getParsedTextContent } from "applesauce-content/text";
|
|
1
|
+
import { getParsedContent } from "applesauce-content/text";
|
|
3
2
|
import { EventTemplate, NostrEvent } from "nostr-tools";
|
|
4
3
|
import { ComponentMap } from "../helpers/nast.js";
|
|
5
4
|
import { LinkRenderer } from "../helpers/build-link-renderer.js";
|
|
6
5
|
export { ComponentMap };
|
|
7
6
|
type Options = {
|
|
7
|
+
/** The key to cache the results under, passing null will disable */
|
|
8
|
+
cacheKey: symbol | null;
|
|
8
9
|
/** Override transformers */
|
|
9
|
-
transformers?: Parameters<typeof
|
|
10
|
+
transformers?: Parameters<typeof getParsedContent>[2];
|
|
10
11
|
/** If set will use {@link buildLinkRenderer} to render links */
|
|
11
12
|
linkRenderers?: LinkRenderer[];
|
|
12
13
|
/** Override event content */
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import { useMemo } from "react";
|
|
2
|
-
import {
|
|
2
|
+
import { truncateContent } from "applesauce-content/nast";
|
|
3
|
+
import { getParsedContent } from "applesauce-content/text";
|
|
3
4
|
import { useRenderNast } from "./use-render-nast.js";
|
|
4
5
|
import { buildLinkRenderer } from "../helpers/build-link-renderer.js";
|
|
5
|
-
import { truncateContent } from "applesauce-content/nast";
|
|
6
6
|
/** Returns the parsed and render text content for an event */
|
|
7
7
|
export function useRenderedContent(event, components, opts) {
|
|
8
8
|
// if link renderers are set, override the link components
|
|
9
9
|
const _components = useMemo(() => (opts?.linkRenderers ? { ...components, link: buildLinkRenderer(opts.linkRenderers) } : components), [opts?.linkRenderers, components]);
|
|
10
10
|
// add additional transformers
|
|
11
|
-
const nast = useMemo(() => (event ?
|
|
11
|
+
const nast = useMemo(() => (event ? getParsedContent(event, opts?.content, opts?.transformers, opts?.cacheKey) : undefined), [event, opts?.content, opts?.transformers, opts?.cacheKey]);
|
|
12
12
|
let truncated = nast;
|
|
13
13
|
if (opts?.maxLength && nast)
|
|
14
14
|
truncated = truncateContent(nast, opts.maxLength);
|
|
@@ -10,7 +10,7 @@ export function useStoreQuery(queryConstructor, args) {
|
|
|
10
10
|
const store = useQueryStore();
|
|
11
11
|
const observable = useMemo(() => {
|
|
12
12
|
if (args)
|
|
13
|
-
return store.
|
|
13
|
+
return store.createQuery(queryConstructor, ...args);
|
|
14
14
|
else
|
|
15
15
|
return undefined;
|
|
16
16
|
}, [args, store]);
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -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 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 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 { EventStore, QueryStore } from "applesauce-core";
|
|
2
|
+
import { PropsWithChildren } from "react";
|
|
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.10.0",
|
|
4
4
|
"description": "React hooks for applesauce",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -27,37 +27,40 @@
|
|
|
27
27
|
"import": "./dist/hooks/*.js",
|
|
28
28
|
"types": "./dist/hooks/*.d.ts"
|
|
29
29
|
},
|
|
30
|
+
"./providers": {
|
|
31
|
+
"import": "./dist/providers/index.js",
|
|
32
|
+
"types": "./dist/providers/index.d.ts"
|
|
33
|
+
},
|
|
34
|
+
"./providers/*": {
|
|
35
|
+
"import": "./dist/providers/*.js",
|
|
36
|
+
"types": "./dist/providers/*.d.ts"
|
|
37
|
+
},
|
|
30
38
|
"./helpers": {
|
|
31
39
|
"import": "./dist/helpers/index.js",
|
|
32
40
|
"types": "./dist/helpers/index.d.ts"
|
|
33
41
|
}
|
|
34
42
|
},
|
|
35
43
|
"dependencies": {
|
|
36
|
-
"applesauce-content": "^0.
|
|
37
|
-
"applesauce-core": "^0.
|
|
38
|
-
"
|
|
44
|
+
"applesauce-content": "^0.10.0",
|
|
45
|
+
"applesauce-core": "^0.10.0",
|
|
46
|
+
"applesauce-factory": "^0.10.0",
|
|
47
|
+
"nostr-tools": "^2.10.3",
|
|
39
48
|
"react": "^18.3.1",
|
|
40
49
|
"rxjs": "^7.8.1"
|
|
41
50
|
},
|
|
42
51
|
"devDependencies": {
|
|
43
|
-
"@jest/globals": "^29.7.0",
|
|
44
|
-
"@types/jest": "^29.5.13",
|
|
45
52
|
"@types/react": "^18.3.11",
|
|
46
|
-
"
|
|
47
|
-
"
|
|
53
|
+
"typescript": "^5.6.3",
|
|
54
|
+
"vitest": "^2.1.8"
|
|
48
55
|
},
|
|
49
|
-
"
|
|
50
|
-
"
|
|
51
|
-
|
|
52
|
-
],
|
|
53
|
-
"setupFilesAfterEnv": [
|
|
54
|
-
"jest-extended/all"
|
|
55
|
-
]
|
|
56
|
+
"funding": {
|
|
57
|
+
"type": "lightning",
|
|
58
|
+
"url": "lightning:nostrudel@geyser.fund"
|
|
56
59
|
},
|
|
57
60
|
"scripts": {
|
|
58
61
|
"build": "tsc",
|
|
59
62
|
"watch:build": "tsc --watch > /dev/null",
|
|
60
|
-
"test": "
|
|
61
|
-
"watch:test": "
|
|
63
|
+
"test": "vitest run --passWithNoTests",
|
|
64
|
+
"watch:test": "vitest"
|
|
62
65
|
}
|
|
63
66
|
}
|
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
|
-
}
|