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

49 lines
1.7 KiB
TypeScript
Raw Normal View History

2023-01-17 13:03:15 +00:00
import { useEffect, useMemo, useState } from "react";
2023-01-20 17:07:14 +00:00
import { useSelector } from "react-redux";
2023-03-28 14:34:01 +00:00
import { u256, EventKind } from "@snort/nostr";
2023-01-20 17:07:14 +00:00
import { RootState } from "State/Store";
import { UserPreferences } from "State/Login";
import { appendDedupe, NostrLink } from "Util";
2023-03-28 14:34:01 +00:00
import { FlatNoteStore, RequestBuilder } from "System";
import useRequestBuilder from "Hooks/useRequestBuilder";
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]);
const [allEvents, setAllEvents] = useState<u256[]>([link.id]);
2023-02-09 12:26:54 +00:00
const pref = useSelector<RootState, UserPreferences>(s => s.login.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]
: [EventKind.TextNote, EventKind.ZapReceipt]
)
.tag("e", allEvents);
2023-03-28 14:34:01 +00:00
return sub;
}, [trackingEvents, allEvents, pref, link.id]);
2023-03-28 14:34:01 +00:00
const store = useRequestBuilder<FlatNoteStore>(FlatNoteStore, sub);
useEffect(() => {
2023-03-28 14:34:01 +00:00
if (store.data) {
const mainNotes = store.data?.filter(a => a.kind === EventKind.TextNote) ?? [];
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-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
}