use IntersectionObserver to set timeline cursor

This commit is contained in:
hzrd149
2023-06-30 06:59:03 -05:00
parent b23fe91476
commit c036a9a541
20 changed files with 505 additions and 281 deletions

View File

@@ -0,0 +1,27 @@
import { useInterval } from "react-use";
import { TimelineLoader } from "../classes/timeline-loader";
import { useIntersectionMapCallback } from "../providers/intersection-observer";
export function useTimelineCurserIntersectionCallback(timeline: TimelineLoader) {
// if the cursor is set too far ahead and the last block did not overlap with the cursor
// we need to keep loading blocks until the timeline is complete or the blocks pass the cursor
useInterval(() => {
timeline.loadNextBlocks();
}, 1000);
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(event.id)?.isIntersecting) {
timeline.setCursor(event.created_at);
timeline.loadNextBlocks();
return;
}
}
},
[timeline]
);
}