Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/SantaClaas/gigit. git client
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128import { 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<HTMLElement>(`[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<string, (current: number) => 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 ( <div class="h-full overflow-auto"> <Show when={props.graph.rows().length > 0} fallback={ <p class="p-6 text-body-md text-on-surface-variant"> {props.graph.status() === "loading" ? "Reading history…" : "No commits to show."} </p> } > <ol ref={(element) => (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" > <For each={props.graph.rows()}> {(row, index) => ( <CommitRow row={row} isSelected={selected() === index()} onSelect={() => setSelected(index())} onKeyDown={onKeyDown} /> )} </For> </ol> </Show>
<div ref={watchForEnd} aria-hidden="true" class="h-px" />
<Show when={props.graph.status() === "streaming"}> <p class="p-3 text-body-sm text-on-surface-variant">Loading more…</p> </Show> </div> );}