import { Banner, Button, Empty, InputArea } from "@refs/design-system"; import { LayerCard, Ago } from "@refs/design-system"; import { ArrowsClockwiseIcon, PathIcon } from "@refs/design-system/icons"; import { MAX_BODY_LENGTH, type CommitSummary } from "@refs/backend/contract"; import { parseDecisions, type DecisionRecord } from "@refs/protocol"; import { useEffect, useRef, useState } from "react"; import { useFetcher } from "react-router"; import { DecisionView } from "./decision-view"; interface Entry { readonly key: string; readonly record: DecisionRecord; readonly commit: CommitSummary; } export function decisionsOf(commits: readonly CommitSummary[]): Entry[] { return commits.flatMap((commit) => parseDecisions(commit.message ?? "", commit.hash).decisions.map((record, index) => ({ key: `${commit.hash}-${index}`, record, commit }))); } interface Shared { readonly refName: string; readonly repo: string; readonly now: number; readonly canRevisit: boolean; } function Revisit({ entry, refName, draft, onDraft }: { entry: Entry; refName: string; draft: string; onDraft: (draft: string | null) => void }) { const fetcher = useFetcher<{ error: string | null }>(); const [body, setBody] = useState(draft); const area = useRef(null); useEffect(() => { setBody(draft); area.current?.querySelector("textarea")?.focus(); }, [draft]); return (
{fetcher.data?.error && }
); } function Detail({ entry, supersededBy, onSelect, shared }: { entry: Entry; supersededBy: Entry | undefined; onSelect: (key: string) => void; shared: Shared }) { const { record, commit } = entry; const [draft, setDraft] = useState(null); const live = supersededBy === undefined; const editable = live && shared.canRevisit; return ( {commit.hash.slice(0, 7)} {commit.title} · {commit.author} · {supersededBy && (

Reconsidered later.

)} setDraft(`This assumption doesn't hold: "${assumption}". `), takePath: (name) => setDraft(`Go with ${name} instead. `) } : undefined} /> {editable && (draft === null ? (
) : ( ))}
); } export function HistoryReasoning({ commits, ...shared }: Shared & { commits: readonly CommitSummary[] }) { const entries = decisionsOf(commits); const supersededBy = new Map(); for (const entry of entries) for (const id of entry.record.supersedes) supersededBy.set(id, entry); const replaced = (entry: Entry) => supersededBy.get(entry.record.decisionId); const [selected, setSelected] = useState(null); if (entries.length === 0) { return } title="No decisions recorded" description={`No commit on this page of ${shared.refName} carries a decision record.`} />; } const current = entries.find((entry) => entry.key === selected) ?? entries.find((entry) => !replaced(entry)) ?? entries[0]; const reconsidered = entries.filter(replaced).length; const assumptions = entries.filter((entry) => !replaced(entry)).reduce((total, entry) => total + entry.record.assumptions.length, 0); const detail = (entry: Entry) => ; return (

{entries.length} {entries.length === 1 ? "decision" : "decisions"} {reconsidered > 0 && ` · ${reconsidered} reconsidered`} {assumptions > 0 && ` · ${assumptions} ${assumptions === 1 ? "assumption" : "assumptions"} to check`}

    {entries.map((entry) => { const active = entry.key === current.key; const live = !replaced(entry); return (
  • {active &&
    {detail(entry)}
    }
  • ); })}
{detail(current)}
); }