applesauce-react 0.0.0-next-20250120191411 → 0.0.0-next-20250123205825
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/package.json +4 -4
- package/dist/helpers/build-link-renderer.d.ts +0 -3
- package/dist/helpers/build-link-renderer.js +0 -23
- package/dist/helpers/index.d.ts +0 -2
- package/dist/helpers/index.js +0 -2
- package/dist/helpers/nast.d.ts +0 -12
- package/dist/helpers/nast.js +0 -15
- package/dist/hooks/index.d.ts +0 -6
- package/dist/hooks/index.js +0 -6
- package/dist/hooks/use-event-factory.d.ts +0 -4
- package/dist/hooks/use-event-factory.js +0 -8
- package/dist/hooks/use-event-store.d.ts +0 -5
- package/dist/hooks/use-event-store.js +0 -12
- package/dist/hooks/use-observable.d.ts +0 -6
- package/dist/hooks/use-observable.js +0 -21
- package/dist/hooks/use-query-store.d.ts +0 -5
- package/dist/hooks/use-query-store.js +0 -12
- package/dist/hooks/use-render-nast.d.ts +0 -5
- package/dist/hooks/use-render-nast.js +0 -6
- package/dist/hooks/use-rendered-content.d.ts +0 -19
- package/dist/hooks/use-rendered-content.js +0 -16
- package/dist/hooks/use-store-query.d.ts +0 -7
- package/dist/hooks/use-store-query.js +0 -18
- package/dist/index.d.ts +0 -3
- package/dist/index.js +0 -3
- package/dist/provider.d.ts +0 -7
- package/dist/provider.js +0 -7
- package/dist/providers/factory-provider.d.ts +0 -7
- package/dist/providers/factory-provider.js +0 -7
- package/dist/providers/index.d.ts +0 -2
- package/dist/providers/index.js +0 -2
- package/dist/providers/query-store-provider.d.ts +0 -7
- package/dist/providers/query-store-provider.js +0 -7
- package/dist/providers/store-provider.d.ts +0 -12
- package/dist/providers/store-provider.js +0 -12
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "applesauce-react",
|
|
3
|
-
"version": "0.0.0-next-
|
|
3
|
+
"version": "0.0.0-next-20250123205825",
|
|
4
4
|
"description": "React hooks for applesauce",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -47,9 +47,9 @@
|
|
|
47
47
|
}
|
|
48
48
|
},
|
|
49
49
|
"dependencies": {
|
|
50
|
-
"applesauce-content": "0.0.0-next-
|
|
51
|
-
"applesauce-core": "0.0.0-next-
|
|
52
|
-
"applesauce-factory": "0.0.0-next-
|
|
50
|
+
"applesauce-content": "0.0.0-next-20250123205825",
|
|
51
|
+
"applesauce-core": "0.0.0-next-20250123205825",
|
|
52
|
+
"applesauce-factory": "0.0.0-next-20250123205825",
|
|
53
53
|
"nostr-tools": "^2.10.3",
|
|
54
54
|
"react": "^18.3.1",
|
|
55
55
|
"rxjs": "^7.8.1"
|
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
import { Link } from "applesauce-content/nast";
|
|
2
|
-
export type LinkRenderer = (url: URL, node: Link) => JSX.Element | false | null;
|
|
3
|
-
export declare function buildLinkRenderer(handlers: LinkRenderer[]): import("react").NamedExoticComponent<import("./nast.js").ExtraProps<Link>>;
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
-
import { memo, useMemo } from "react";
|
|
3
|
-
export function buildLinkRenderer(handlers) {
|
|
4
|
-
const LinkRenderer = ({ node }) => {
|
|
5
|
-
const content = useMemo(() => {
|
|
6
|
-
try {
|
|
7
|
-
const url = new URL(node.href);
|
|
8
|
-
for (const handler of handlers) {
|
|
9
|
-
try {
|
|
10
|
-
const content = handler(url, node);
|
|
11
|
-
if (content)
|
|
12
|
-
return content;
|
|
13
|
-
}
|
|
14
|
-
catch (e) { }
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
catch (error) { }
|
|
18
|
-
return null;
|
|
19
|
-
}, [node.href, node.value]);
|
|
20
|
-
return content || _jsx(_Fragment, { children: node.value });
|
|
21
|
-
};
|
|
22
|
-
return memo(LinkRenderer);
|
|
23
|
-
}
|
package/dist/helpers/index.d.ts
DELETED
package/dist/helpers/index.js
DELETED
package/dist/helpers/nast.d.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import React from "react";
|
|
2
|
-
import { Content, ContentMap, Root } from "applesauce-content/nast";
|
|
3
|
-
type Component<ComponentProps> = React.FunctionComponent<ComponentProps> | React.ComponentClass;
|
|
4
|
-
export type ExtraProps<T extends Content> = {
|
|
5
|
-
node: T;
|
|
6
|
-
};
|
|
7
|
-
export type ComponentMap = Partial<{
|
|
8
|
-
[k in keyof ContentMap]: Component<ExtraProps<ContentMap[k]>>;
|
|
9
|
-
}>;
|
|
10
|
-
/** Render a nostr syntax tree to JSX components */
|
|
11
|
-
export declare function renderNast(root: Root, components: ComponentMap): import("react/jsx-runtime").JSX.Element;
|
|
12
|
-
export {};
|
package/dist/helpers/nast.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
|
-
/** Render a nostr syntax tree to JSX components */
|
|
3
|
-
export function renderNast(root, components) {
|
|
4
|
-
const indexes = {};
|
|
5
|
-
return (_jsx(_Fragment, { children: root.children.map((node) => {
|
|
6
|
-
indexes[node.type] = indexes[node.type] ?? 0;
|
|
7
|
-
const index = indexes[node.type];
|
|
8
|
-
indexes[node.type]++;
|
|
9
|
-
const Component = components[node.type];
|
|
10
|
-
if (!Component)
|
|
11
|
-
return null;
|
|
12
|
-
// @ts-expect-error
|
|
13
|
-
return _jsx(Component, { node: node }, node.type + "-" + index);
|
|
14
|
-
}) }));
|
|
15
|
-
}
|
package/dist/hooks/index.d.ts
DELETED
package/dist/hooks/index.js
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,12 +0,0 @@
|
|
|
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 +0,0 @@
|
|
|
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 */
|
|
5
|
-
export declare function useObservable<T extends unknown>(observable?: BehaviorSubject<T>): T;
|
|
6
|
-
export declare function useObservable<T extends unknown>(observable?: Observable<T>): T | undefined;
|
|
@@ -1,21 +0,0 @@
|
|
|
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
|
-
}
|
|
7
|
-
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;
|
|
21
|
-
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { useContext } from "react";
|
|
2
|
-
import { QueryStoreContext } from "../providers/store-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,5 +0,0 @@
|
|
|
1
|
-
import { Root } from "applesauce-content/nast";
|
|
2
|
-
import { ComponentMap } from "../helpers/nast.js";
|
|
3
|
-
export { ComponentMap };
|
|
4
|
-
/** A hook to get the rendered output of a nostr syntax tree */
|
|
5
|
-
export declare function useRenderNast(root: Root | undefined, components: ComponentMap): JSX.Element | null;
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { useMemo } from "react";
|
|
2
|
-
import { renderNast } from "../helpers/nast.js";
|
|
3
|
-
/** A hook to get the rendered output of a nostr syntax tree */
|
|
4
|
-
export function useRenderNast(root, components) {
|
|
5
|
-
return useMemo(() => (root ? renderNast(root, components) : null), [root, Object.keys(components).join("|")]);
|
|
6
|
-
}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { getParsedContent } from "applesauce-content/text";
|
|
2
|
-
import { EventTemplate, NostrEvent } from "nostr-tools";
|
|
3
|
-
import { ComponentMap } from "../helpers/nast.js";
|
|
4
|
-
import { LinkRenderer } from "../helpers/build-link-renderer.js";
|
|
5
|
-
export { ComponentMap };
|
|
6
|
-
type Options = {
|
|
7
|
-
/** The key to cache the results under, passing null will disable */
|
|
8
|
-
cacheKey: symbol | null;
|
|
9
|
-
/** Override transformers */
|
|
10
|
-
transformers?: Parameters<typeof getParsedContent>[2];
|
|
11
|
-
/** If set will use {@link buildLinkRenderer} to render links */
|
|
12
|
-
linkRenderers?: LinkRenderer[];
|
|
13
|
-
/** Override event content */
|
|
14
|
-
content?: string;
|
|
15
|
-
/** Maximum length */
|
|
16
|
-
maxLength?: number;
|
|
17
|
-
};
|
|
18
|
-
/** Returns the parsed and render text content for an event */
|
|
19
|
-
export declare function useRenderedContent(event: NostrEvent | EventTemplate | string | undefined, components: ComponentMap, opts?: Options): JSX.Element | null;
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { useMemo } from "react";
|
|
2
|
-
import { truncateContent } from "applesauce-content/nast";
|
|
3
|
-
import { getParsedContent } from "applesauce-content/text";
|
|
4
|
-
import { useRenderNast } from "./use-render-nast.js";
|
|
5
|
-
import { buildLinkRenderer } from "../helpers/build-link-renderer.js";
|
|
6
|
-
/** Returns the parsed and render text content for an event */
|
|
7
|
-
export function useRenderedContent(event, components, opts) {
|
|
8
|
-
// if link renderers are set, override the link components
|
|
9
|
-
const _components = useMemo(() => (opts?.linkRenderers ? { ...components, link: buildLinkRenderer(opts.linkRenderers) } : components), [opts?.linkRenderers, components]);
|
|
10
|
-
// add additional transformers
|
|
11
|
-
const nast = useMemo(() => (event ? getParsedContent(event, opts?.content, opts?.transformers, opts?.cacheKey) : undefined), [event, opts?.content, opts?.transformers, opts?.cacheKey]);
|
|
12
|
-
let truncated = nast;
|
|
13
|
-
if (opts?.maxLength && nast)
|
|
14
|
-
truncated = truncateContent(nast, opts.maxLength);
|
|
15
|
-
return useRenderNast(truncated, _components);
|
|
16
|
-
}
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { QueryConstructor } from "applesauce-core";
|
|
2
|
-
/**
|
|
3
|
-
* Runs and subscribes to a query in the query store
|
|
4
|
-
* @example
|
|
5
|
-
* const events = useStoreQuery(TimelineQuery, [{kinds: [1]}])
|
|
6
|
-
*/
|
|
7
|
-
export declare function useStoreQuery<T extends unknown, Args extends Array<any>>(queryConstructor: QueryConstructor<T, Args>, args?: Args | null): T | undefined;
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { useMemo } from "react";
|
|
2
|
-
import { useObservable } from "./use-observable.js";
|
|
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
|
-
*/
|
|
9
|
-
export function useStoreQuery(queryConstructor, args) {
|
|
10
|
-
const store = useQueryStore();
|
|
11
|
-
const observable = useMemo(() => {
|
|
12
|
-
if (args)
|
|
13
|
-
return store.createQuery(queryConstructor, ...args);
|
|
14
|
-
else
|
|
15
|
-
return undefined;
|
|
16
|
-
}, [args, store]);
|
|
17
|
-
return useObservable(observable);
|
|
18
|
-
}
|
package/dist/index.d.ts
DELETED
package/dist/index.js
DELETED
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
|
-
}
|
|
@@ -1,7 +0,0 @@
|
|
|
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;
|
|
@@ -1,7 +0,0 @@
|
|
|
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
|
-
}
|
package/dist/providers/index.js
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;
|
|
@@ -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
|
-
}
|
|
@@ -1,12 +0,0 @@
|
|
|
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;
|
|
@@ -1,12 +0,0 @@
|
|
|
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
|
-
}
|