applesauce-react 0.7.0 → 0.9.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/dist/helpers/build-link-renderer.d.ts +0 -1
- package/dist/hooks/index.d.ts +2 -1
- package/dist/hooks/index.js +2 -1
- package/dist/hooks/use-observable.d.ts +4 -1
- package/dist/hooks/use-observable.js +12 -33
- package/dist/hooks/use-query-store.d.ts +5 -0
- package/dist/hooks/use-query-store.js +12 -0
- 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.d.ts +5 -1
- package/dist/hooks/use-store-query.js +6 -2
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/dist/provider.d.ts +2 -1
- package/dist/provider.js +3 -8
- package/package.json +11 -7
|
@@ -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
package/dist/hooks/index.js
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
-
import Observable from "
|
|
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 */
|
|
5
|
+
export declare function useObservable<T extends unknown>(observable?: BehaviorSubject<T>): T;
|
|
3
6
|
export declare function useObservable<T extends unknown>(observable?: Observable<T>): T | undefined;
|
|
@@ -1,38 +1,17 @@
|
|
|
1
1
|
import { useState, useEffect } from "react";
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
export function getCurrentValue(observable) {
|
|
3
|
+
if (Reflect.has(observable, "value"))
|
|
4
|
+
return Reflect.get(observable, "value");
|
|
5
|
+
return undefined;
|
|
6
|
+
}
|
|
4
7
|
export function useObservable(observable) {
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
// const value = useRef<T | undefined>(observable && isStateful(observable) ? observable.value : undefined);
|
|
8
|
-
// const prev = useRef(observable);
|
|
9
|
-
// const sub = useRef<ZenObservable.Subscription>();
|
|
10
|
-
// // This intentionally does not use useEffect
|
|
11
|
-
// // because we want the value to be returned on the first render
|
|
12
|
-
// if (!sub.current || prev.current !== observable) {
|
|
13
|
-
// prev.current = observable;
|
|
14
|
-
// if (sub.current) sub.current.unsubscribe();
|
|
15
|
-
// sub.current = observable?.subscribe((v) => {
|
|
16
|
-
// value.current = v;
|
|
17
|
-
// // only explicitly update if its not the first render
|
|
18
|
-
// if (!init.current) update(count + 1);
|
|
19
|
-
// });
|
|
20
|
-
// init.current = false;
|
|
21
|
-
// }
|
|
22
|
-
// // unsubscribe when unmount
|
|
23
|
-
// useEffect(() => {
|
|
24
|
-
// return () => {
|
|
25
|
-
// if (sub.current) sub.current.unsubscribe();
|
|
26
|
-
// };
|
|
27
|
-
// }, []);
|
|
28
|
-
// return value.current;
|
|
29
|
-
const [value, setValue] = useState(observable && isStateful(observable) ? observable.value : undefined);
|
|
8
|
+
const current = observable && getCurrentValue(observable);
|
|
9
|
+
const [value, setValue] = useState(current);
|
|
30
10
|
useEffect(() => {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
});
|
|
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);
|
|
35
14
|
return () => sub?.unsubscribe();
|
|
36
|
-
}, [observable]);
|
|
37
|
-
return value;
|
|
15
|
+
}, [observable, setValue]);
|
|
16
|
+
return current || value;
|
|
38
17
|
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { useContext } from "react";
|
|
2
|
+
import { QueryStoreContext } from "../provider.js";
|
|
3
|
+
/**
|
|
4
|
+
* Gets the QueryStore from a parent {@link QueryStoreProvider} component
|
|
5
|
+
* If there is none it throws an error
|
|
6
|
+
*/
|
|
7
|
+
export function useQueryStore() {
|
|
8
|
+
const store = useContext(QueryStoreContext);
|
|
9
|
+
if (!store)
|
|
10
|
+
throw new Error("Missing QueryStoreProvider");
|
|
11
|
+
return store;
|
|
12
|
+
}
|
|
@@ -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);
|
|
@@ -1,3 +1,7 @@
|
|
|
1
1
|
import { QueryConstructor } from "applesauce-core";
|
|
2
|
-
/**
|
|
2
|
+
/**
|
|
3
|
+
* Runs and subscribes to a query in the query store
|
|
4
|
+
* @example
|
|
5
|
+
* const events = useStoreQuery(TimelineQuery, [{kinds: [1]}])
|
|
6
|
+
*/
|
|
3
7
|
export declare function useStoreQuery<T extends unknown, Args extends Array<any>>(queryConstructor: QueryConstructor<T, Args>, args?: Args | null): T | undefined;
|
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
import { useMemo } from "react";
|
|
2
2
|
import { useObservable } from "./use-observable.js";
|
|
3
|
-
import { useQueryStore } from "
|
|
4
|
-
/**
|
|
3
|
+
import { useQueryStore } from "./use-query-store.js";
|
|
4
|
+
/**
|
|
5
|
+
* Runs and subscribes to a query in the query store
|
|
6
|
+
* @example
|
|
7
|
+
* const events = useStoreQuery(TimelineQuery, [{kinds: [1]}])
|
|
8
|
+
*/
|
|
5
9
|
export function useStoreQuery(queryConstructor, args) {
|
|
6
10
|
const store = useQueryStore();
|
|
7
11
|
const observable = useMemo(() => {
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/dist/provider.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { QueryStore } from "applesauce-core";
|
|
2
2
|
import { PropsWithChildren } from "react";
|
|
3
|
-
export declare
|
|
3
|
+
export declare const QueryStoreContext: import("react").Context<QueryStore | null>;
|
|
4
|
+
/** Provides a QueryStore to the component tree */
|
|
4
5
|
export declare function QueryStoreProvider({ store, children }: PropsWithChildren<{
|
|
5
6
|
store: QueryStore;
|
|
6
7
|
}>): import("react/jsx-runtime").JSX.Element;
|
package/dist/provider.js
CHANGED
|
@@ -1,12 +1,7 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
-
import { createContext
|
|
3
|
-
const QueryStoreContext = createContext(null);
|
|
4
|
-
|
|
5
|
-
const store = useContext(QueryStoreContext);
|
|
6
|
-
if (!store)
|
|
7
|
-
throw new Error("Missing QueryStoreProvider");
|
|
8
|
-
return store;
|
|
9
|
-
}
|
|
2
|
+
import { createContext } from "react";
|
|
3
|
+
export const QueryStoreContext = createContext(null);
|
|
4
|
+
/** Provides a QueryStore to the component tree */
|
|
10
5
|
export function QueryStoreProvider({ store, children }) {
|
|
11
6
|
return _jsx(QueryStoreContext.Provider, { value: store, children: children });
|
|
12
7
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "applesauce-react",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
4
4
|
"description": "React hooks for applesauce",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -33,19 +33,19 @@
|
|
|
33
33
|
}
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"applesauce-content": "^0.
|
|
37
|
-
"applesauce-core": "^0.
|
|
38
|
-
"nostr-tools": "^2.
|
|
36
|
+
"applesauce-content": "^0.9.0",
|
|
37
|
+
"applesauce-core": "^0.9.0",
|
|
38
|
+
"nostr-tools": "^2.10.1",
|
|
39
39
|
"react": "^18.3.1",
|
|
40
|
-
"
|
|
40
|
+
"rxjs": "^7.8.1"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
43
|
"@jest/globals": "^29.7.0",
|
|
44
44
|
"@types/jest": "^29.5.13",
|
|
45
45
|
"@types/react": "^18.3.11",
|
|
46
|
-
"@types/zen-observable": "^0.8.7",
|
|
47
46
|
"jest": "^29.7.0",
|
|
48
|
-
"jest-extended": "^4.0.2"
|
|
47
|
+
"jest-extended": "^4.0.2",
|
|
48
|
+
"typescript": "^5.6.3"
|
|
49
49
|
},
|
|
50
50
|
"jest": {
|
|
51
51
|
"roots": [
|
|
@@ -55,6 +55,10 @@
|
|
|
55
55
|
"jest-extended/all"
|
|
56
56
|
]
|
|
57
57
|
},
|
|
58
|
+
"funding": {
|
|
59
|
+
"type": "lightning",
|
|
60
|
+
"url": "lightning:nostrudel@geyser.fund"
|
|
61
|
+
},
|
|
58
62
|
"scripts": {
|
|
59
63
|
"build": "tsc",
|
|
60
64
|
"watch:build": "tsc --watch > /dev/null",
|