Open History to see the last commit
1import { useEffect, useState } from "react";
2
3import { useStableCallback } from "~/hooks/useStableCallback";
4import { observeIntersection } from "~/lib/observeIntersection";
5import { DiffFileHeaderSkeleton } from "../DiffPanelShell";
6
7export function DiffFileLoadingBoundary({ load, count }: { load: () => void; count: number }) {
8 const [isNear, setIsNear] = useState(false);
9 const loadLatest = useStableCallback(load);
10 // A fresh observe redelivers the current intersection, so this ref must keep its identity
11 // across renders: it captures nothing but the state setter.
12 const observe = (node: HTMLDivElement | null) => {
13 if (!node) return;
14 return observeIntersection(node, (entry) => setIsNear(entry.isIntersecting), {
15 rootMargin: "600px",
16 });
17 };
18 // A settled batch shrinks `count` without moving the boundary out of range, and no new
19 // intersection entry arrives for that, so ask for the next batch whenever it changes.
20 useEffect(() => {
21 if (isNear && count > 0) loadLatest();
22 }, [isNear, count, loadLatest]);
23
24 return (
25 <div ref={observe} role="status" aria-label="Loading diff…">
26 {Array.from({ length: Math.min(count, 4) }, (_, index) => (
27 <div key={index} aria-hidden className="border-b border-border/40">
28 <DiffFileHeaderSkeleton titleWidth="medium" />
29 </div>
30 ))}
31 </div>
32 );
33}