/** * 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"), }; } export function createRefs() { const [entries, setEntries] = createSignal([]); const [message, setMessage] = createSignal(); /** Load a repository's refs, replacing whatever was shown before. */ async function show(id: RepositoryId) { setMessage(undefined); 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([]); setMessage(undefined); } return { entries, grouped: () => groupRefs(entries()), message, show, clear }; } export type Refs = ReturnType;