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";
|
2023-04-04 19:50:36 +00:00
|
|
|
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]);
|
2023-04-04 19:50:36 +00:00
|
|
|
const [allEvents, setAllEvents] = useState<u256[]>([link.id]);
|
2023-02-09 12:26:54 +00:00
|
|
|
const pref = useSelector<RootState, UserPreferences>(s => s.login.preferences);
|
2023-02-07 20:04:50 +00:00
|
|
|
|
|
|
|
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]
|
|
|
|
)
|
2023-04-04 19:50:36 +00:00
|
|
|
.tag("e", allEvents);
|
2023-03-28 14:34:01 +00:00
|
|
|
|
|
|
|
return sub;
|
2023-04-04 19:50:36 +00:00
|
|
|
}, [trackingEvents, allEvents, pref, link.id]);
|
2023-02-07 20:04:50 +00:00
|
|
|
|
2023-03-28 14:34:01 +00:00
|
|
|
const store = useRequestBuilder<FlatNoteStore>(FlatNoteStore, sub);
|
2023-02-07 20:04:50 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
2023-03-28 14:34:01 +00:00
|
|
|
if (store.data) {
|
2023-04-04 19:50:36 +00:00
|
|
|
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
|
|
|
}
|