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";
|
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 useTimelineFeed(pubKeys: HexKey | Array<HexKey>, global: boolean = false) {
|
2023-01-17 17:46:58 +00:00
|
|
|
const [until, setUntil] = useState<number>();
|
2023-01-17 13:03:15 +00:00
|
|
|
const [trackingEvents, setTrackingEvent] = useState<u256[]>([]);
|
|
|
|
|
2023-01-05 19:20:48 +00:00
|
|
|
const subTab = 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-02 11:59:03 +00:00
|
|
|
if (!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-16 18:24:14 +00:00
|
|
|
sub.Authors = global ? undefined : new Set(pubKeys);
|
2023-01-15 19:40:47 +00:00
|
|
|
sub.Kinds = new Set([EventKind.TextNote, EventKind.Repost]);
|
2023-01-17 17:46:58 +00:00
|
|
|
sub.Limit = 20;
|
|
|
|
sub.Until = until;
|
2022-12-18 14:51:47 +00:00
|
|
|
|
2022-12-30 23:35:02 +00:00
|
|
|
return sub;
|
2023-01-17 17:46:58 +00:00
|
|
|
}, [pubKeys, global, until]);
|
2022-12-18 14:51:47 +00:00
|
|
|
|
2023-01-04 13:23:05 +00:00
|
|
|
const main = useSubscription(sub, { leaveOpen: true });
|
|
|
|
|
|
|
|
const subNext = useMemo(() => {
|
2023-01-17 13:03:15 +00:00
|
|
|
if (trackingEvents.length > 0) {
|
2023-01-04 13:23:05 +00:00
|
|
|
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);
|
2023-01-04 13:23:05 +00:00
|
|
|
return sub;
|
|
|
|
}
|
2023-01-17 13:03:15 +00:00
|
|
|
return null;
|
|
|
|
}, [trackingEvents]);
|
2023-01-04 13:23:05 +00:00
|
|
|
|
|
|
|
const others = useSubscription(subNext, { leaveOpen: true });
|
|
|
|
|
2023-01-17 13:03:15 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (main.notes.length > 0) {
|
|
|
|
// debounce
|
|
|
|
let t = setTimeout(() => {
|
|
|
|
setTrackingEvent(s => {
|
|
|
|
let ids = main.notes.map(a => a.id);
|
|
|
|
let temp = new Set([...s, ...ids]);
|
|
|
|
return Array.from(temp);
|
|
|
|
});
|
|
|
|
}, 200);
|
|
|
|
return () => clearTimeout(t);
|
|
|
|
}
|
|
|
|
}, [main.notes]);
|
2023-01-17 17:13:22 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
main: main.notes,
|
|
|
|
others: others.notes,
|
|
|
|
loadMore: () => {
|
2023-01-17 17:46:58 +00:00
|
|
|
let now = Math.floor(new Date().getTime() / 1000);
|
|
|
|
let oldest = main.notes.reduce((acc, v) => acc = v.created_at < acc ? v.created_at : acc, now);
|
|
|
|
setUntil(oldest);
|
|
|
|
},
|
|
|
|
until
|
2023-01-17 17:13:22 +00:00
|
|
|
};
|
2022-12-18 14:51:47 +00:00
|
|
|
}
|