2023-01-17 13:03:15 +00:00
|
|
|
import { useEffect, useMemo, useState } from "react";
|
2023-01-20 11:11:50 +00:00
|
|
|
import { u256 } from "Nostr";
|
|
|
|
import EventKind from "Nostr/EventKind";
|
|
|
|
import { Subscriptions } from "Nostr/Subscriptions";
|
|
|
|
import { unixNow } from "Util";
|
|
|
|
import useSubscription from "Feed/Subscription";
|
2023-01-20 17:07:14 +00:00
|
|
|
import { useSelector } from "react-redux";
|
|
|
|
import { RootState } from "State/Store";
|
|
|
|
import { UserPreferences } from "State/Login";
|
2022-12-18 14:51:47 +00:00
|
|
|
|
2023-01-17 21:55:53 +00:00
|
|
|
export interface TimelineFeedOptions {
|
|
|
|
method: "TIME_RANGE" | "LIMIT_UNTIL"
|
|
|
|
}
|
|
|
|
|
2023-01-19 18:00:56 +00:00
|
|
|
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();
|
2023-01-21 16:09:35 +00:00
|
|
|
const [window, setWindow] = useState<number>(60 * 10);
|
2023-01-17 21:55:53 +00:00
|
|
|
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-20 17:07:14 +00:00
|
|
|
const pref = useSelector<RootState, UserPreferences>(s => s.login.preferences);
|
2023-01-17 13:03:15 +00:00
|
|
|
|
2023-01-21 16:09:35 +00:00
|
|
|
function createSub() {
|
2023-01-19 18:00:56 +00:00
|
|
|
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();
|
2023-01-19 18:00:56 +00:00
|
|
|
sub.Id = `timeline:${subject.type}`;
|
2023-01-15 19:40:47 +00:00
|
|
|
sub.Kinds = new Set([EventKind.TextNote, EventKind.Repost]);
|
2023-01-19 18:00:56 +00:00
|
|
|
switch (subject.type) {
|
|
|
|
case "pubkey": {
|
|
|
|
sub.Authors = new Set(subject.items);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "hashtag": {
|
|
|
|
sub.HashTags = new Set(subject.items);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-01-21 16:09:35 +00:00
|
|
|
return sub;
|
|
|
|
}
|
|
|
|
|
|
|
|
const sub = useMemo(() => {
|
|
|
|
let sub = createSub();
|
|
|
|
if (sub) {
|
|
|
|
if (options.method === "LIMIT_UNTIL") {
|
|
|
|
sub.Until = until;
|
|
|
|
sub.Limit = 10;
|
|
|
|
} else {
|
|
|
|
sub.Since = since;
|
|
|
|
sub.Until = until;
|
|
|
|
if (since === undefined) {
|
|
|
|
sub.Limit = 50;
|
|
|
|
}
|
2023-01-17 21:55:53 +00:00
|
|
|
}
|
2022-12-18 14:51:47 +00:00
|
|
|
|
2023-01-21 16:09:35 +00:00
|
|
|
if (pref.autoShowLatest) {
|
|
|
|
// copy properties of main sub but with limit 0
|
|
|
|
// this will put latest directly into main feed
|
|
|
|
let latestSub = new Subscriptions();
|
|
|
|
latestSub.Ids = sub.Ids;
|
|
|
|
latestSub.Kinds = sub.Kinds;
|
|
|
|
latestSub.Limit = 0;
|
|
|
|
sub.AddSubscription(latestSub);
|
|
|
|
}
|
|
|
|
}
|
2022-12-30 23:35:02 +00:00
|
|
|
return sub;
|
2023-01-19 18:12:22 +00:00
|
|
|
}, [subject.type, subject.items, until, since, window]);
|
2022-12-18 14:51:47 +00:00
|
|
|
|
2023-01-04 13:23:05 +00:00
|
|
|
const main = useSubscription(sub, { leaveOpen: true });
|
|
|
|
|
2023-01-21 16:09:35 +00:00
|
|
|
const subRealtime = useMemo(() => {
|
|
|
|
let subLatest = createSub();
|
|
|
|
if (subLatest && !pref.autoShowLatest) {
|
|
|
|
subLatest.Id = `${subLatest.Id}:latest`;
|
|
|
|
subLatest.Limit = 0;
|
|
|
|
}
|
|
|
|
return subLatest;
|
|
|
|
}, [subject.type, subject.items]);
|
|
|
|
|
|
|
|
const latest = useSubscription(subRealtime, { leaveOpen: true });
|
|
|
|
|
2023-01-04 13:23:05 +00:00
|
|
|
const subNext = useMemo(() => {
|
2023-01-20 17:07:14 +00:00
|
|
|
if (trackingEvents.length > 0 && pref.enableReactions) {
|
2023-01-04 13:23:05 +00:00
|
|
|
let sub = new Subscriptions();
|
2023-01-19 18:00:56 +00:00
|
|
|
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);
|
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(() => {
|
2023-01-21 16:09:35 +00:00
|
|
|
if (main.store.notes.length > 0) {
|
2023-01-17 21:55:53 +00:00
|
|
|
setTrackingEvent(s => {
|
2023-01-21 16:09:35 +00:00
|
|
|
let ids = main.store.notes.map(a => a.id);
|
2023-01-17 21:55:53 +00:00
|
|
|
let temp = new Set([...s, ...ids]);
|
|
|
|
return Array.from(temp);
|
|
|
|
});
|
2023-01-17 13:03:15 +00:00
|
|
|
}
|
2023-01-21 16:09:35 +00:00
|
|
|
}, [main.store]);
|
2023-01-17 17:13:22 +00:00
|
|
|
|
|
|
|
return {
|
2023-01-21 16:09:35 +00:00
|
|
|
main: main.store,
|
|
|
|
related: others.store,
|
|
|
|
latest: latest.store,
|
2023-01-17 17:13:22 +00:00
|
|
|
loadMore: () => {
|
2023-01-21 16:09:35 +00:00
|
|
|
console.debug("Timeline load more!")
|
2023-01-17 21:55:53 +00:00
|
|
|
if (options.method === "LIMIT_UNTIL") {
|
2023-01-21 16:09:35 +00:00
|
|
|
let oldest = main.store.notes.reduce((acc, v) => acc = v.created_at < acc ? v.created_at : acc, unixNow());
|
2023-01-17 21:55:53 +00:00
|
|
|
setUntil(oldest);
|
|
|
|
} else {
|
|
|
|
setUntil(s => s - window);
|
|
|
|
setSince(s => s - window);
|
|
|
|
}
|
2023-01-21 16:09:35 +00:00
|
|
|
},
|
|
|
|
showLatest: () => {
|
|
|
|
main.append(latest.store.notes);
|
|
|
|
latest.clear();
|
2023-01-17 21:55:53 +00:00
|
|
|
}
|
2023-01-17 17:13:22 +00:00
|
|
|
};
|
2022-12-18 14:51:47 +00:00
|
|
|
}
|