snort/src/feed/TimelineFeed.ts

86 lines
2.8 KiB
TypeScript
Raw Normal View History

2023-01-17 13:03:15 +00:00
import { useEffect, useMemo, useState } from "react";
import { HexKey, 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 {
global: boolean,
method: "TIME_RANGE" | "LIMIT_UNTIL"
}
export default function useTimelineFeed(pubKeys: HexKey | Array<HexKey>, options: TimelineFeedOptions) {
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[]>([]);
2023-01-17 21:55:53 +00:00
const subTab = options.global ? "global" : "follows";
2022-12-30 23:35:02 +00:00
const sub = useMemo(() => {
2023-01-01 10:44:38 +00:00
if (!Array.isArray(pubKeys)) {
pubKeys = [pubKeys];
}
2023-01-17 21:55:53 +00:00
if (!options.global && (!pubKeys || pubKeys.length === 0)) {
2022-12-31 20:11:43 +00:00
return null;
}
2022-12-30 23:35:02 +00:00
let sub = new Subscriptions();
2023-01-05 19:20:48 +00:00
sub.Id = `timeline:${subTab}`;
2023-01-17 21:55:53 +00:00
sub.Authors = options.global ? undefined : new Set(pubKeys);
2023-01-15 19:40:47 +00:00
sub.Kinds = new Set([EventKind.TextNote, EventKind.Repost]);
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;
2023-01-17 21:55:53 +00:00
}, [pubKeys, 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();
2023-01-05 19:20:48 +00:00
sub.Id = `timeline-related:${subTab}`;
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
}