snort/packages/app/src/Feed/ThreadFeed.ts

68 lines
2.2 KiB
TypeScript
Raw Normal View History

2023-01-17 13:03:15 +00:00
import { useEffect, useMemo, useState } from "react";
2023-03-28 14:34:01 +00:00
import { u256, EventKind } from "@snort/nostr";
import { appendDedupe, NostrLink } from "Util";
2023-03-28 14:34:01 +00:00
import { FlatNoteStore, RequestBuilder } from "System";
import useRequestBuilder from "Hooks/useRequestBuilder";
2023-04-14 11:33:19 +00:00
import useLogin from "Hooks/useLogin";
2022-12-18 14:51:47 +00:00
2023-03-25 22:55:34 +00:00
export default function useThreadFeed(link: NostrLink) {
const [trackingEvents, setTrackingEvent] = useState<u256[]>([link.id]);
2023-04-25 10:04:20 +00:00
const [trackingATags, setTrackingATags] = useState<string[]>([]);
const [allEvents, setAllEvents] = useState<u256[]>([link.id]);
2023-04-14 11:33:19 +00:00
const pref = useLogin().preferences;
const sub = useMemo(() => {
2023-03-28 14:34:01 +00:00
const sub = new RequestBuilder(`thread:${link.id.substring(0, 8)}`);
sub.withOptions({
leaveOpen: true,
});
sub.withFilter().ids(trackingEvents);
sub
.withFilter()
.kinds(
pref.enableReactions
? [EventKind.Reaction, EventKind.TextNote, EventKind.Repost, EventKind.ZapReceipt]
2023-04-20 21:05:11 +00:00
: [EventKind.TextNote, EventKind.ZapReceipt, EventKind.Repost]
2023-03-28 14:34:01 +00:00
)
.tag("e", allEvents);
2023-03-28 14:34:01 +00:00
2023-04-25 10:04:20 +00:00
if (trackingATags.length > 0) {
const parsed = trackingATags.map(a => a.split(":"));
sub
.withFilter()
.kinds(parsed.map(a => Number(a[0])))
.authors(parsed.map(a => a[1]))
.tag(
"d",
parsed.map(a => a[2])
);
}
2023-03-28 14:34:01 +00:00
return sub;
2023-04-25 10:33:31 +00:00
}, [trackingEvents, trackingATags, allEvents, pref]);
2023-03-28 14:34:01 +00:00
const store = useRequestBuilder<FlatNoteStore>(FlatNoteStore, sub);
2023-04-25 10:33:31 +00:00
useEffect(() => {
setTrackingATags([]);
setTrackingEvent([link.id]);
setAllEvents([link.id]);
}, [link.id]);
useEffect(() => {
2023-03-28 14:34:01 +00:00
if (store.data) {
2023-04-05 15:10:14 +00:00
const mainNotes = store.data?.filter(a => a.kind === EventKind.TextNote || a.kind === EventKind.Polls) ?? [];
const eTags = mainNotes.map(a => a.tags.filter(b => b[0] === "e").map(b => b[1])).flat();
const eTagsMissing = eTags.filter(a => !mainNotes.some(b => b.id === a));
setTrackingEvent(s => appendDedupe(s, eTagsMissing));
setAllEvents(s => appendDedupe(s, eTags));
2023-04-25 10:04:20 +00:00
const aTags = mainNotes.map(a => a.tags.filter(b => b[0] === "a").map(b => b[1])).flat();
setTrackingATags(s => appendDedupe(s, aTags));
2023-01-17 13:03:15 +00:00
}
2023-03-28 14:34:01 +00:00
}, [store]);
2023-01-17 13:03:15 +00:00
2023-03-28 14:34:01 +00:00
return store;
2023-02-03 21:38:14 +00:00
}