import { For, Show, createSignal, onCleanup } from "solid-js"; import type { GraphView } from "../lib/graph"; import CommitRow from "./CommitRow"; /** How many rows a Page Up or Page Down jumps. */ const PAGE = 20; /** * The commit history. * * Two things keep this affordable on a repository with tens of thousands of * commits. Rows arrive in batches as the end of the list comes into view, so * the DOM only ever holds what has been scrolled to; and rows past the first * screenful get `content-visibility`, so the browser skips laying out and * painting the ones that are off screen. Between them there is no need for a * virtual list library. */ export default function History(props: { graph: GraphView }) { const [selected, setSelected] = createSignal(0); let list: HTMLOListElement | undefined; /** * Move the selection and take focus with it, so the keyboard follows. * * `next` is a function of the current row rather than an absolute index, and * the update goes through the functional setter, because Solid batches signal * writes: reading `selected()` to compute the next value gives a stale answer * when two key presses land in the same batch, and the second press is lost. */ function moveSelection(next: (current: number) => number) { const last = props.graph.rows().length - 1; let landed = 0; setSelected((current) => { landed = Math.max(0, Math.min(next(current), last)); return landed; }); // Found by row number rather than by index into a list of refs, so a jump // past rows that have not rendered yet still lands somewhere sensible. const row = list?.querySelector(`[data-row="${landed}"]`); row?.focus(); row?.scrollIntoView({ block: "nearest" }); } /** * Arrow-key navigation. Handled on the row itself rather than on the * container: the row is the focused, interactive element, so it is the one * that should be listening. */ function onKeyDown(event: KeyboardEvent) { const moves: Record number> = { ArrowDown: (current) => current + 1, ArrowUp: (current) => current - 1, PageDown: (current) => current + PAGE, PageUp: (current) => current - PAGE, Home: () => 0, // Clamped to the last row, so this does not need to know how many there // are — which is just as well, since more may still be streaming in. End: () => Number.MAX_SAFE_INTEGER, }; const move = moves[event.key]; if (!move) return; event.preventDefault(); moveSelection(move); } /** * Watch for the end of the list coming into view. The margin asks for more * before the user actually gets there, so scrolling does not stall. */ function watchForEnd(element: HTMLDivElement) { const observer = new IntersectionObserver( (entries) => { if (entries.some((entry) => entry.isIntersecting)) { props.graph.loadMore(); } }, { rootMargin: "800px" }, ); observer.observe(element); onCleanup(() => observer.disconnect()); } return (
0} fallback={

{props.graph.status() === "loading" ? "Reading history…" : "No commits to show."}

} >
    (list = element)} // Rows are as wide as their content; the container scrolls to reach // the rest. A graph's width comes from the history, not the window, // so there is no width at which capping would help. class="history-list w-max min-w-full" > {(row, index) => ( setSelected(index())} onKeyDown={onKeyDown} /> )}
); }