small timeline fixes

This commit is contained in:
hzrd149
2023-08-30 14:54:38 -05:00
parent 714b599d58
commit 64e8909e3e
4 changed files with 27 additions and 22 deletions

View File

@@ -1,7 +1,7 @@
import { useInterval } from "react-use";
import { TimelineLoader } from "../classes/timeline-loader";
import { useIntersectionMapCallback } from "../providers/intersection-observer";
import { getEventUID } from "../helpers/nostr/events";
import { NostrEvent } from "../types/nostr-event";
export function useTimelineCurserIntersectionCallback(timeline: TimelineLoader) {
// if the cursor is set too far ahead and the last block did not overlap with the cursor
@@ -13,15 +13,20 @@ export function useTimelineCurserIntersectionCallback(timeline: TimelineLoader)
return useIntersectionMapCallback<string>(
(map) => {
// find oldest event that is visible
for (let i = timeline.timeline.value.length - 1; i >= 0; i--) {
const event = timeline.timeline.value[i];
if (map.get(getEventUID(event))?.isIntersecting) {
timeline.setCursor(event.created_at);
timeline.loadNextBlocks();
return;
let oldestEvent: NostrEvent | undefined = undefined;
for (const [id, intersection] of map) {
if (!intersection.isIntersecting) continue;
const event = timeline.events.getEvent(id);
if (!event) continue;
if (!oldestEvent || event.created_at < oldestEvent.created_at) {
oldestEvent = event;
}
}
if (oldestEvent) {
timeline.setCursor(oldestEvent.created_at);
timeline.loadNextBlocks();
}
},
[timeline],
);