import { COMMITS_PER_PAGE, MAX_TITLE_LENGTH, repoLink, splatSegments, type CommitSummary } from "@refs/backend/contract"; import { AgentAvatar, Ago, Avatar, Empty, LayerCard, Pagination, TabCount, Tabs } from "@refs/design-system"; import { GitCommitIcon, PathIcon } from "@refs/design-system/icons"; import { parseDecisions } from "@refs/protocol"; import { Link, data, redirect, useNavigate } from "react-router"; import type { Route } from "./+types/repo-commits"; import { BranchPicker } from "@refs/design-system"; import { formatDate } from "@refs/design-system"; import { failed, formText, found, refs, signedIn, fullName, nowSeconds } from "../lib/session"; import { HistoryReasoning, decisionsOf } from "../components/history-reasoning"; import { AppLink } from "../components/link"; export const meta: Route.MetaFunction = ({ params }) => [{ title: `History · ${fullName(params)} · refs` }]; export async function loader({ request, params, context }: Route.LoaderArgs) { const segments = splatSegments(params["*"]); if (!segments) throw data(null, { status: 404 }); const query = new URL(request.url).searchParams; const page = Math.max(1, Number(query.get("page")) || 1); const tab = query.get("tab") === "reasoning" ? "reasoning" : "commits"; const viewer = refs(context); const repo = await found(viewer.repo(fullName(params))); const [history, refList] = await found(Promise.all([repo.history(segments, { cursor: String(page) }), repo.refs()])); return { repo: repo.summary.name, refs: refList, ref: history.location.ref, page, tab, canRevisit: viewer.viewer.agent === null && repo.summary.can.write, now: nowSeconds(), commits: history.page.items, hasOlder: history.page.next !== null, }; } export async function action({ request, params, context }: Route.ActionArgs) { const form = await request.formData(); if (formText(form, "intent") !== "revisit") throw data(null, { status: 400 }); const name = fullName(params); const repo = await found(signedIn(context).repo(name)); const ref = formText(form, "ref"); const commit = await found(repo.commit(formText(form, "commit"))); const record = parseDecisions(commit.message, commit.hash).decisions.find((candidate) => candidate.decisionId === formText(form, "decision")); if (!record) return { error: "This decision is no longer in that commit. Reload and try again." }; const hash = commit.hash.slice(0, 7); const body = [ `Revisit decision ${record.decisionId} from ${hash} "${commit.message.split("\n")[0]}" on ${ref}:`, `> ${record.decision}`, formText(form, "body").trim(), `Start from the head of ${ref}, not from ${hash}. Review every commit after ${hash} that builds on this decision, and change them so the new direction holds throughout. Record a decision that supersedes ${record.decisionId}.`, ].join("\n\n"); const outcome = await signedIn(context).createTask({ title: `Revisit: ${record.decision}`.slice(0, MAX_TITLE_LENGTH), body, repo: name, assignee: null, labels: [] }); return outcome.ok ? redirect(`/${name}/tasks/${outcome.value.number}`) : failed(outcome.problem, {}); } function groupByDay(commits: readonly CommitSummary[]): [string, CommitSummary[]][] { const days = new Map(); for (const commit of commits) { const day = formatDate(commit.committedAt); days.set(day, [...(days.get(day) ?? []), commit]); } return [...days]; } export default function RepoCommits({ loaderData }: Route.ComponentProps) { const { repo, refs: refList, ref, page, tab, canRevisit, commits, hasOlder, now: current } = loaderData; const navigate = useNavigate(); const base = repoLink(repo, "commits", ref); const href = (view: string, at = 1) => `${base}?${new URLSearchParams({ ...(view === "reasoning" && { tab: view }), ...(at > 1 && { page: String(at) }) })}`.replace(/\?$/, ""); const decisions = decisionsOf(commits).length; const tabLink = (value: string, icon: React.ReactNode, label: string, count: number) => ({ value, label: ( {icon} {label} {count} ), nativeButton: false, render: (props: React.AnchorHTMLAttributes) => , }); return (

History

, "Commits", commits.length), tabLink("reasoning", , "Reasoning", decisions)]} /> {tab === "reasoning" ? ( ) : commits.length === 0 ? ( } title="No commits" description="There are no commits on this ref." /> ) : ( groupByDay(commits).map(([day, dayCommits]) => (

{day}

    {dayCommits.map((commit) => { const count = parseDecisions(commit.message ?? "", commit.hash).decisions.length; const agent = commit.pushedBy?.actor.kind === "agent"; return (
  • {count > 0 ? count : null}
    {commit.title} {commit.author} committed
    {count > 0 && ( {count} {count === 1 ? "decision" : "decisions"} )} {commit.hash.slice(0, 7)}
  • ); })}
)) )} {(page > 1 || hasOlder) && ( navigate(href(tab, next))} /> )}
); }