snort/src/feed/TimelineFeed.ts

94 lines
2.9 KiB
TypeScript
Raw Normal View History

2023-01-17 13:03:15 +00:00
import { useEffect, useMemo, useState } from "react";
import { u256 } from "../nostr";
2022-12-29 22:23:41 +00:00
import EventKind from "../nostr/EventKind";
import { Subscriptions } from "../nostr/Subscriptions";
2023-01-17 21:55:53 +00:00
import { unixNow } from "../Util";
2022-12-30 23:35:02 +00:00
import useSubscription from "./Subscription";
2022-12-18 14:51:47 +00:00
2023-01-17 21:55:53 +00:00
export interface TimelineFeedOptions {
method: "TIME_RANGE" | "LIMIT_UNTIL"
}
export interface TimelineSubject {
type: "pubkey" | "hashtag" | "global",
items: string[]
}
export default function useTimelineFeed(subject: TimelineSubject, options: TimelineFeedOptions) {
2023-01-17 21:55:53 +00:00
const now = unixNow();
const [window, setWindow] = useState<number>(60 * 60);
const [until, setUntil] = useState<number>(now);
const [since, setSince] = useState<number>(now - window);
2023-01-17 13:03:15 +00:00
const [trackingEvents, setTrackingEvent] = useState<u256[]>([]);
2022-12-30 23:35:02 +00:00
const sub = useMemo(() => {
if (subject.type !== "global" && subject.items.length == 0) {
2022-12-31 20:11:43 +00:00
return null;
}
2022-12-30 23:35:02 +00:00
let sub = new Subscriptions();
sub.Id = `timeline:${subject.type}`;
2023-01-15 19:40:47 +00:00
sub.Kinds = new Set([EventKind.TextNote, EventKind.Repost]);
switch (subject.type) {
case "pubkey": {
sub.Authors = new Set(subject.items);
break;
}
case "hashtag": {
sub.HashTags = new Set(subject.items);
break;
}
}
2023-01-17 21:55:53 +00:00
if (options.method === "LIMIT_UNTIL") {
sub.Until = until;
sub.Limit = 10;
} else {
sub.Since = since;
sub.Until = until;
if (since === undefined) {
sub.Limit = 50;
}
}
2022-12-18 14:51:47 +00:00
2022-12-30 23:35:02 +00:00
return sub;
}, [subject.type, subject.items, until, since, window]);
2022-12-18 14:51:47 +00:00
const main = useSubscription(sub, { leaveOpen: true });
const subNext = useMemo(() => {
2023-01-17 13:03:15 +00:00
if (trackingEvents.length > 0) {
let sub = new Subscriptions();
sub.Id = `timeline-related:${subject.type}`;
2023-01-17 13:03:15 +00:00
sub.Kinds = new Set([EventKind.Reaction, EventKind.Deletion, EventKind.Repost]);
sub.ETags = new Set(trackingEvents);
return sub;
}
2023-01-17 13:03:15 +00:00
return null;
}, [trackingEvents]);
const others = useSubscription(subNext, { leaveOpen: true });
2023-01-17 13:03:15 +00:00
useEffect(() => {
if (main.notes.length > 0) {
2023-01-17 21:55:53 +00:00
setTrackingEvent(s => {
let ids = main.notes.map(a => a.id);
let temp = new Set([...s, ...ids]);
return Array.from(temp);
});
2023-01-17 13:03:15 +00:00
}
}, [main.notes]);
2023-01-17 17:13:22 +00:00
return {
main: main.notes,
others: others.notes,
loadMore: () => {
2023-01-17 21:55:53 +00:00
if (options.method === "LIMIT_UNTIL") {
let oldest = main.notes.reduce((acc, v) => acc = v.created_at < acc ? v.created_at : acc, unixNow());
setUntil(oldest);
} else {
setUntil(s => s - window);
setSince(s => s - window);
}
}
2023-01-17 17:13:22 +00:00
};
2022-12-18 14:51:47 +00:00
}