/** * The repository's references, as the sidebar sees them. */ import { createSignal } from "solid-js"; import { listReferences } from "./api"; import { isGigitError, type RefEntry, type RepositoryId } from "./bindings"; /** Refs split into the groups the sidebar shows. */ export type GroupedRefs = { branches: RefEntry[]; remotes: RefEntry[]; tags: RefEntry[]; }; /** * Sorts names the way a person reads them, so `v9` comes before `v10` rather * than after it. */ const BY_NAME = new Intl.Collator(undefined, { numeric: true, sensitivity: "base" }); /** * Split refs by kind and sort each group. * * `other` refs — notes, `refs/stash`, replacements — are deliberately dropped. * They are machinery rather than things a person navigates by, and showing * them would bury the branches. */ export function groupRefs(entries: RefEntry[]): GroupedRefs { const sorted = (kind: RefEntry["kind"]) => entries .filter((entry) => entry.kind === kind) // Sorting in place is safe here: filter already returned a fresh array, // so there is nothing of the caller's to mutate. // oxlint-disable-next-line unicorn/no-array-sort .sort((left, right) => BY_NAME.compare(left.shorthand, right.shorthand)); return { branches: sorted("localBranch"), remotes: sorted("remoteBranch"), tags: sorted("tag"), }; } /** * Keep the refs whose shorthand contains `query`, ignoring case. * * A plain substring match rather than anything fuzzy: it is predictable, and a * person can see why a result appeared without having to learn the rules. The * shorthand is what the sidebar shows, so it is what gets matched — searching * text that is not on screen would be a surprise. */ export function filterRefs(entries: RefEntry[], query: string): RefEntry[] { const wanted = query.trim().toLowerCase(); if (!wanted) return entries; return entries.filter((entry) => entry.shorthand.toLowerCase().includes(wanted)); } /** A run of text, and whether the filter matched it. */ export type Segment = { text: string; matched: boolean }; /** * Split text into matched and unmatched runs, so the matches can be marked. * * Done by scanning rather than with a regular expression, which would mean * escaping whatever the user typed — a branch name can contain `.` and `+`, * and `*` would otherwise throw. */ export function highlight(text: string, query: string): Segment[] { const wanted = query.trim().toLowerCase(); if (!wanted) return [{ text, matched: false }]; const haystack = text.toLowerCase(); const segments: Segment[] = []; let cursor = 0; for (;;) { const found = haystack.indexOf(wanted, cursor); if (found === -1) break; if (found > cursor) segments.push({ text: text.slice(cursor, found), matched: false }); segments.push({ text: text.slice(found, found + wanted.length), matched: true }); cursor = found + wanted.length; } if (cursor < text.length) segments.push({ text: text.slice(cursor), matched: false }); return segments; } export function createRefs() { const [entries, setEntries] = createSignal([]); const [query, setQuery] = createSignal(""); const [message, setMessage] = createSignal(); const matching = () => filterRefs(entries(), query()); /** Load a repository's refs, replacing whatever was shown before. */ async function show(id: RepositoryId) { setMessage(undefined); // A filter written for one repository almost never means anything in the // next, and leaving it on would make a full sidebar look empty. setQuery(""); try { setEntries(await listReferences(id)); } catch (error) { // The sidebar failing is not a reason to lose the history beside it. setEntries([]); setMessage(isGigitError(error) ? error.message : String(error)); } } function clear() { setEntries([]); setQuery(""); setMessage(undefined); } return { entries, query, setQuery, /** Grouped and sorted, with the filter applied. */ grouped: () => groupRefs(matching()), /** How many refs the filter kept, of how many there are worth showing. */ counts: () => { const shown = groupRefs(matching()); const all = groupRefs(entries()); return { matched: shown.branches.length + shown.remotes.length + shown.tags.length, total: all.branches.length + all.remotes.length + all.tags.length, }; }, isFiltering: () => query().trim().length > 0, message, show, clear, }; } export type Refs = ReturnType;