Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

DiffFileLoadingBoundary.tsx

34 lines1.5 KB

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 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- requests the next diff batch while the boundary is near the viewport and files remain
21 useEffect(() => {
22 if (isNear && count > 0) loadLatest();
23 }, [isNear, count, loadLatest]);
24​
25 return (
26 <div ref={observe} role="status" aria-label="Loading diff…">
27 {Array.from({ length: Math.min(count, 4) }, (_, index) => (
28 <div key={index} aria-hidden className="border-b border-border/40">
29 <DiffFileHeaderSkeleton titleWidth="medium" />
30 </div>
31 ))}
32 </div>
33 );