feat: timeline fragments

This commit is contained in:
2023-11-09 12:20:53 +00:00
parent ffda4b1a43
commit ff2da6c5fd
15 changed files with 140 additions and 134 deletions

View File

@ -5,3 +5,5 @@ export * from "./useSystemState";
export * from "./useUserProfile";
export * from "./useUserSearch";
export * from "./useEventReactions";
export * from "./useReactions";
export * from "./useEventFeed";

View File

@ -0,0 +1,23 @@
import { useMemo } from "react";
import { RequestBuilder, ReplaceableNoteStore, NostrLink, NoteCollection } from "@snort/system";
import { useRequestBuilder } from "./useRequestBuilder";
export function useEventFeed(link: NostrLink) {
const sub = useMemo(() => {
const b = new RequestBuilder(`event:${link.id.slice(0, 12)}`);
b.withFilter().link(link);
return b;
}, [link]);
return useRequestBuilder(ReplaceableNoteStore, sub);
}
export function useEventsFeed(id: string, links: Array<NostrLink>) {
const sub = useMemo(() => {
const b = new RequestBuilder(`events:${id}`);
links.forEach(v => b.withFilter().link(v));
return b;
}, [id, links]);
return useRequestBuilder(NoteCollection, sub);
}

View File

@ -0,0 +1,34 @@
import { useMemo } from "react";
import { RequestBuilder, EventKind, NoteCollection, NostrLink } from "@snort/system";
import { useRequestBuilder } from "./useRequestBuilder";
export function useReactions(
subId: string,
ids: Array<NostrLink>,
others?: (rb: RequestBuilder) => void,
leaveOpen?: boolean,
) {
const sub = useMemo(() => {
const rb = new RequestBuilder(subId);
rb.withOptions({ leaveOpen });
if (ids.length > 0) {
const grouped = ids.reduce(
(acc, v) => {
acc[v.type] ??= [];
acc[v.type].push(v);
return acc;
},
{} as Record<string, Array<NostrLink>>,
);
for (const [, v] of Object.entries(grouped)) {
rb.withFilter().kinds([EventKind.Reaction, EventKind.Repost, EventKind.ZapReceipt]).replyToLink(v);
}
}
others?.(rb);
return rb.numFilters > 0 ? rb : null;
}, [ids]);
return useRequestBuilder(NoteCollection, sub);
}