From a15a20393c23a44d21d9ab4e6e1b61882aafe36a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C5=ABdolfs=20O=C5=A1i=C5=86=C5=A1?= Date: Wed, 30 Sep 2026 15:11:09 +0200 Subject: [PATCH] Move commit day grouping into lib Today/Yesterday labels and grouping by day had date edge cases that nothing tested. --- src/lib/commitGroups.ts | 82 ++++++++++++++++++++++ src/views/repo/RepoCommits.svelte | 84 +---------------------- tests/unit/commitGroups.test.ts | 109 ++++++++++++++++++++++++++++++ 3 files changed, 193 insertions(+), 82 deletions(-) create mode 100644 src/lib/commitGroups.ts create mode 100644 tests/unit/commitGroups.test.ts diff --git a/src/lib/commitGroups.ts b/src/lib/commitGroups.ts new file mode 100644 index 000000000..e6e3e9ab2 --- /dev/null +++ b/src/lib/commitGroups.ts @@ -0,0 +1,82 @@ +import type { Commit } from "@bindings/repo/Commit"; + +export type CommitGroup = { + key: string; + label: string; + commits: Commit[]; +}; + +export type CommitRow = + | { type: "header"; key: string; label: string } + | { type: "commit"; commit: Commit; first: boolean; last: boolean }; + +/// The local calendar day of a timestamp in milliseconds, as `YYYY-MM-DD`. +export function dayKey(timestamp: number): string { + const date = new Date(timestamp); + const month = `${date.getMonth() + 1}`.padStart(2, "0"); + const day = `${date.getDate()}`.padStart(2, "0"); + + return `${date.getFullYear()}-${month}-${day}`; +} + +/// "Today", "Yesterday", or the full date. +export function dayLabel(timestamp: number, now = Date.now()): string { + const today = new Date(now); + const yesterday = new Date( + today.getFullYear(), + today.getMonth(), + today.getDate() - 1, + ); + + if (dayKey(timestamp) === dayKey(today.getTime())) { + return "Today"; + } + if (dayKey(timestamp) === dayKey(yesterday.getTime())) { + return "Yesterday"; + } + + return new Intl.DateTimeFormat("en", { + weekday: "long", + month: "long", + day: "numeric", + year: "numeric", + }).format(new Date(timestamp)); +} + +/// Commits grouped by the day they were committed, in order of each day's first +/// commit. +export function groupCommitsByDay( + commits: Commit[], + now = Date.now(), +): CommitGroup[] { + const groups = new Map(); + for (const commit of commits) { + const timestamp = commit.committer.time * 1000; + const key = dayKey(timestamp); + const group = groups.get(key); + if (group) { + group.commits.push(commit); + } else { + groups.set(key, { + key, + label: dayLabel(timestamp, now), + commits: [commit], + }); + } + } + return [...groups.values()]; +} + +/// The groups as one stream of rows, so the list can be virtualized. `first` +/// and `last` mark a group's boundaries so its commits form a bordered card. +export function commitRowsOf(groups: CommitGroup[]): CommitRow[] { + return groups.flatMap(group => [ + { type: "header" as const, key: group.key, label: group.label }, + ...group.commits.map((commit, i) => ({ + type: "commit" as const, + commit, + first: i === 0, + last: i === group.commits.length - 1, + })), + ]); +} diff --git a/src/views/repo/RepoCommits.svelte b/src/views/repo/RepoCommits.svelte index 04da949dc..c9b0ac305 100644 --- a/src/views/repo/RepoCommits.svelte +++ b/src/views/repo/RepoCommits.svelte @@ -6,6 +6,7 @@ import { COMMITS_PAGE_SIZE } from "@app/views/repo/router"; import fuzzysort from "fuzzysort"; + import { commitRowsOf, groupCommitsByDay } from "@app/lib/commitGroups"; import { invoke } from "@app/lib/invoke"; import { createPaginatedList } from "@app/lib/paginatedList.svelte"; import * as router from "@app/lib/router"; @@ -41,12 +42,6 @@ revision, }); - type CommitGroup = { - key: string; - label: string; - commits: Commit[]; - }; - function listKey(): string { return `repo.commits:${repo.rid}:${peer ?? ""}:${revision ?? ""}`; } @@ -79,38 +74,6 @@ return () => clearTimeout(timer); }); - function dayKey(timestamp: number) { - const date = new Date(timestamp); - const month = `${date.getMonth() + 1}`.padStart(2, "0"); - const day = `${date.getDate()}`.padStart(2, "0"); - - return `${date.getFullYear()}-${month}-${day}`; - } - - function dayLabel(timestamp: number) { - const date = new Date(timestamp); - const today = new Date(); - const yesterday = new Date( - today.getFullYear(), - today.getMonth(), - today.getDate() - 1, - ); - - if (dayKey(date.getTime()) === dayKey(today.getTime())) { - return "Today"; - } - if (dayKey(date.getTime()) === dayKey(yesterday.getTime())) { - return "Yesterday"; - } - - return new Intl.DateTimeFormat("en", { - weekday: "long", - month: "long", - day: "numeric", - year: "numeric", - }).format(date); - } - const searchableCommits = $derived( list.items.map(c => ({ commit: c, @@ -130,50 +93,7 @@ const filteredCommits = $derived(searchResults.map(r => r.obj.commit)); - const groupedCommits = $derived.by(() => { - const groups: Record = {}; - - for (const commit of filteredCommits) { - const timestamp = commit.committer.time * 1000; - const key = dayKey(timestamp); - const current = groups[key]; - - if (current) { - current.commits.push(commit); - } else { - groups[key] = { - key, - label: dayLabel(timestamp), - commits: [commit], - }; - } - } - - return Object.values(groups); - }); - - type CommitRow = - | { type: "header"; key: string; label: string } - | { type: "commit"; commit: Commit; first: boolean; last: boolean }; - - // Flatten the day groups into a single row stream so the list can be - // virtualized; the row snippet renders headers and commits differently. - // `first`/`last` mark a group's boundaries so its commits form a bordered card. - const commitRows = $derived.by(() => { - const rows: CommitRow[] = []; - for (const group of groupedCommits) { - rows.push({ type: "header", key: group.key, label: group.label }); - group.commits.forEach((commit, i) => { - rows.push({ - type: "commit", - commit, - first: i === 0, - last: i === group.commits.length - 1, - }); - }); - } - return rows; - }); + const commitRows = $derived(commitRowsOf(groupCommitsByDay(filteredCommits)));