snort/src/feed/ThreadFeed.ts

45 lines
1.5 KiB
TypeScript
Raw Normal View History

2023-01-17 13:03:15 +00:00
import { useEffect, useMemo, useState } from "react";
2023-01-15 19:40:47 +00:00
import { u256 } from "../nostr";
2022-12-30 23:35:02 +00:00
import EventKind from "../nostr/EventKind";
2022-12-29 22:23:41 +00:00
import { Subscriptions } from "../nostr/Subscriptions";
2022-12-30 23:35:02 +00:00
import useSubscription from "./Subscription";
2022-12-18 14:51:47 +00:00
2023-01-15 19:40:47 +00:00
export default function useThreadFeed(id: u256) {
2023-01-17 13:03:15 +00:00
const [trackingEvents, setTrackingEvent] = useState<u256[]>([id]);
function addId(id: u256[]) {
setTrackingEvent((s) => {
let tmp = new Set([...s, ...id]);
return Array.from(tmp);
})
}
2022-12-30 23:35:02 +00:00
const sub = useMemo(() => {
const thisSub = new Subscriptions();
2023-01-02 11:15:13 +00:00
thisSub.Id = `thread:${id.substring(0, 8)}`;
2023-01-17 13:03:15 +00:00
thisSub.Ids = new Set(trackingEvents);
2022-12-30 23:35:02 +00:00
// get replies to this event
const subRelated = new Subscriptions();
2023-01-17 13:03:15 +00:00
subRelated.Kinds = new Set([EventKind.Reaction, EventKind.TextNote, EventKind.Deletion, EventKind.Repost]);
2022-12-30 23:35:02 +00:00
subRelated.ETags = thisSub.Ids;
thisSub.AddSubscription(subRelated);
return thisSub;
2023-01-17 13:03:15 +00:00
}, [trackingEvents]);
2022-12-30 23:35:02 +00:00
const main = useSubscription(sub, { leaveOpen: true });
2023-01-17 13:03:15 +00:00
useEffect(() => {
// debounce
let t = setTimeout(() => {
let eTags = main.notes.map(a => a.tags.filter(b => b[0] === "e").map(b => b[1])).flat();
let ids = main.notes.map(a => a.id);
let allEvents = new Set([...eTags, ...ids]);
addId(Array.from(allEvents));
}, 200);
return () => clearTimeout(t);
}, [main.notes]);
return main;
2022-12-18 14:51:47 +00:00
}