import { parsePatchFiles } from "@pierre/diffs"; import { preloadFileDiff } from "@pierre/diffs/ssr"; import type { GitStatus } from "@pierre/trees"; import { preloadFileTree, serializeFileTreeSsrPayload, } from "@pierre/trees/ssr"; import type { CollectedDiff, FindingDraft, ReviewPayload } from "./types.js"; export async function renderPage(options: { diff: CollectedDiff; round: number; findings: FindingDraft[]; notes?: string; carriedFindings?: import("./types.js").Finding[]; }): Promise { const { diff, round, findings, notes, carriedFindings = [] } = options; const files = parsePatchFiles( diff.diffText, `diff-review-${round}`, true, ).flatMap((patch) => patch.files); const fileTreePayload = preloadFileTree({ paths: diff.files.flatMap((f) => f.prevPath && f.prevPath !== f.path ? [f.prevPath, f.path] : [f.path], ), gitStatus: diff.files.map((f) => ({ path: f.path, status: gitStatusFor(f.status), })), initialExpansion: "open", }); const fileTreeHtml = serializeFileTreeSsrPayload( fileTreePayload, "declarative", ); const diffHtmls: string[] = []; for (const file of files) { const fileFindings = carriedFindings.filter( (f) => f.file === file.name && f.status === "open", ); const annotations = fileFindings.flatMap((f) => Array.from({ length: f.endLine - f.startLine + 1 }, (_, i) => ({ side: f.side as "additions" | "deletions", lineNumber: f.startLine + i, metadata: { findingId: f.id, severity: f.severity, category: f.category, comment: f.comment, }, })), ); const result = await preloadFileDiff({ fileDiff: file, options: { diffStyle: "split", overflow: "scroll", // SSR must stay interactive on local review startup. // Plain text avoids cold Shiki init while keeping lines/anchors/annotations. tokenizeMaxLength: 0, }, annotations, }); diffHtmls.push(`

${esc(file.prevName && file.prevName !== file.name ? `${file.prevName} → ${file.name}` : file.name)}

${result.prerenderedHTML}
`); } const totals = diff.files.reduce( (sum, f) => ({ additions: sum.additions + f.additions, deletions: sum.deletions + f.deletions, }), { additions: 0, deletions: 0 }, ); return ` Pi Diff Review
Review changed lines
${diff.files.length} files · +${totals.additions} −${totals.deletions}${diff.skippedFiles.length ? ` · skipped ${diff.skippedFiles.length}` : ""}
${findings.length} finding${findings.length === 1 ? "" : "s"}
${diffHtmls.join("\n")}

New finding

Click a changed line.
Ready
`; } export function renderSearchResults( query: string, hits: import("./search.js").SearchMatch[], ): string { if (!query.trim()) return '
Type to search
'; if (hits.length === 0) return '
No matches.
'; const max = Math.min(hits.length, 200); let html = '
'; for (let i = 0; i < max; i++) { const h = hits[i]; const before = esc(h.text.slice(0, h.matchStart)); const match = esc(h.text.slice(h.matchStart, h.matchEnd)); const after = esc(h.text.slice(h.matchEnd)); html += `
${esc(h.file)}:${h.line} ${h.side}
${before}${match}${after}
`; } if (hits.length > max) { html += `
${hits.length - max} more matches
`; } html += "
"; return html; } export function renderFindings(findings: FindingDraft[]): string { if (findings.length === 0) return "No findings yet."; return findings .map( (f, i) => `
${i + 1} ${f.severity} ${f.category}
${esc(f.file)}:${f.startLine}${f.startLine === f.endLine ? "" : `-${f.endLine}`} ${f.side}
${esc(f.comment)}
`, ) .join("\n"); } function sourceLabel(source: ReviewPayload["source"]): string { const scope = source.files?.length ? ` · ${source.files.length} path${source.files.length === 1 ? "" : "s"}` : ""; if (source.kind === "working-tree") return `unstaged changes${scope}`; if (source.kind === "staged") return `staged changes${scope}`; return `changes vs ${source.base}${scope}`; } function gitStatusFor(status: string): GitStatus { switch (status) { case "added": return "added"; case "modified": return "modified"; case "deleted": return "deleted"; case "renamed": return "renamed"; default: return "untracked"; } } function esc(text: string): string { return text .replace(/&/g, "&") .replace(//g, ">") .replace(/"/g, """) .replace(/'/g, "'"); } const INLINE_CSS = ` * { box-sizing: border-box; } :root { --bg: #0b0d10; --fg: #e2e4e9; --panel: #11131a; --panel-2: #161922; --border: #1f232e; --muted: #6b7280; --accent: #2563eb; } html[data-theme="light"] { --bg: #ffffff; --fg: #0a0a0a; --panel: #f3f4f6; --panel-2: #e5e7eb; --border: #d1d5db; --muted: #6b7280; --accent: #2563eb; } html, body { margin: 0; height: 100%; font-family: Inter, ui-sans-serif, system-ui, sans-serif; font-size: 13px; color: var(--fg); background: var(--bg); } #app { display: grid; grid-template-columns: 288px 1fr 320px; height: 100vh; overflow: hidden; position: relative; } #app:not([data-resizing]) { transition: grid-template-columns .2s ease; } #app[data-left-collapsed="true"] { grid-template-columns: 0px 1fr 320px; } #app[data-right-collapsed="true"] { grid-template-columns: 288px 1fr 0px; } #app[data-left-collapsed="true"][data-right-collapsed="true"] { grid-template-columns: 0px 1fr 0px; } #app[data-left-collapsed="true"] #sidebar { visibility: hidden; } #app[data-right-collapsed="true"] #drawer { visibility: hidden; } .pane-toggle { position: absolute; top: 50%; transform: translateY(-50%); z-index: 10; background: var(--panel-2); border: 1px solid var(--border); border-radius: 4px; color: var(--muted); cursor: pointer; font-size: 14px; padding: 4px 6px; line-height: 1; } .pane-toggle:hover { color: var(--fg); } #toggle-left { left: 288px; } #app:not([data-resizing]) #toggle-left { transition: left .2s ease; } #toggle-right { right: 320px; } #app:not([data-resizing]) #toggle-right { transition: right .2s ease; } #app[data-left-collapsed="true"] #toggle-left { left: 0; } #app[data-right-collapsed="true"] #toggle-right { right: 0; } #sidebar { background: var(--panel); border-right: 1px solid var(--border); display: flex; flex-direction: column; overflow: hidden; } .brand { padding: 14px 12px; border-bottom: 1px solid var(--border); display: flex; justify-content: space-between; align-items: center; } .brand h1 { margin: 0; font-size: 15px; } .brand .subtitle { font-size: 11px; color: var(--muted); } #theme-toggle { background: none; border: none; color: var(--muted); cursor: pointer; font-size: 16px; line-height: 1; padding: 4px; border-radius: 4px; } #theme-toggle:hover { background: var(--panel-2); } #meta { padding: 8px 12px; display: flex; gap: 6px; flex-wrap: wrap; } .meta-chip { display: inline-flex; align-items: center; gap: 4px; background: var(--panel-2); border: 1px solid var(--border); border-radius: 4px; padding: 2px 8px; font-size: 11px; } .meta-label { color: var(--muted); text-transform: uppercase; font-size: 10px; letter-spacing: .04em; } #tree { flex: 1; overflow: auto; padding: 4px; } #main { display: flex; flex-direction: column; overflow: hidden; } #topbar { padding: 10px 16px; border-bottom: 1px solid var(--border); display: flex; justify-content: space-between; align-items: center; } .topbar-title { font-weight: 600; } #summary { font-size: 12px; color: var(--muted); } .topbar-actions { display: flex; gap: 8px; align-items: center; } .search-wrap { position: relative; display: flex; align-items: center; } #search { background: var(--panel-2); border: 1px solid var(--border); border-radius: 6px; padding: 5px 10px; color: inherit; font-size: 12px; width: 180px; padding-right: 24px; } #search-clear { position: absolute; right: 6px; background: none; border: none; color: var(--muted); cursor: pointer; font-size: 14px; line-height: 1; padding: 2px; } #search-clear:hover { color: var(--fg); } #search-badge { position: absolute; right: 24px; font-size: 10px; color: var(--muted); pointer-events: none; } .count-pill { background: var(--panel-2); border: 1px solid var(--border); border-radius: 999px; padding: 2px 10px; font-size: 11px; } #diffs { flex: 1; overflow: auto; padding: 12px 16px; } .file-section { margin-bottom: 24px; } .file-title { font-size: 13px; font-weight: 600; margin: 0 0 8px; } #drawer { background: var(--panel); border-left: 1px solid var(--border); display: flex; flex-direction: column; overflow: hidden; padding: 12px; gap: 12px; } .card { background: var(--panel-2); border: 1px solid var(--border); border-radius: 8px; padding: 10px; } .card-title { font-weight: 600; font-size: 12px; margin-bottom: 8px; display: flex; justify-content: space-between; } textarea { width: 100%; background: var(--bg); border: 1px solid var(--border); border-radius: 6px; padding: 8px; color: inherit; font-family: inherit; font-size: 12px; resize: vertical; } .actions { display: flex; gap: 8px; margin-top: 8px; } .actions button { flex: 1; background: var(--panel-2); border: 1px solid var(--border); border-radius: 6px; padding: 6px; color: inherit; cursor: pointer; font-size: 12px; } .actions button[type="submit"]:last-child { background: var(--accent); border-color: var(--accent); } .finding-item { padding: 8px; border-radius: 6px; background: var(--bg); border: 1px solid var(--border); margin-bottom: 6px; } .finding-item-head { display: flex; align-items: center; gap: 6px; margin-bottom: 4px; } .finding-number { font-size: 11px; color: var(--muted); } .finding-pill { font-size: 10px; text-transform: uppercase; letter-spacing: .04em; padding: 1px 6px; border-radius: 4px; } .finding-pill.high { background: #7f1d1d; color: #fecaca; } .finding-pill.medium { background: #713f12; color: #fde68a; } .finding-pill.low { background: #064e3b; color: #a7f3d0; } .finding-pill.category { background: #1e3a8a; color: #bfdbfe; } .finding-location { font-size: 11px; color: var(--muted); margin-bottom: 2px; } .finding-comment { font-size: 12px; } .remove-finding { margin-left: auto; background: none; border: none; color: var(--muted); cursor: pointer; font-size: 14px; line-height: 1; } #finding-dialog { background: var(--panel-2); border: 1px solid var(--border); border-radius: 10px; color: var(--fg); padding: 16px; width: 400px; max-width: 90vw; } #finding-dialog::backdrop { background: rgba(0,0,0,.6); } #finding-dialog h3 { margin: 0 0 10px; font-size: 14px; } #finding-dialog .field-row { display: flex; gap: 10px; margin-bottom: 10px; } #finding-dialog label { display: block; font-size: 12px; margin-bottom: 8px; } #finding-dialog select, #finding-dialog textarea { width: 100%; background: var(--bg); border: 1px solid var(--border); border-radius: 6px; padding: 6px; color: inherit; font-family: inherit; font-size: 12px; } #selection-label { font-size: 11px; color: var(--muted); margin-bottom: 10px; } .field-subtitle { font-size: 11px; color: var(--muted); margin-top: 2px; font-style: italic; } [data-line] { cursor: pointer; } [data-line]:hover { outline: 1px solid color-mix(in srgb, var(--focus, #2563eb) 40%, transparent); outline-offset: -1px; } #search-results { position: absolute; top: 44px; right: 16px; width: 360px; max-height: 60vh; overflow: auto; background: var(--panel-2); border: 1px solid var(--border); border-radius: 8px; z-index: 100; padding: 8px; } .search-list { display: flex; flex-direction: column; gap: 4px; } .search-result { display: block; padding: 6px 8px; border-radius: 6px; text-decoration: none; color: inherit; font-size: 12px; } .search-result:hover { background: var(--panel-2); } .search-file { font-size: 11px; color: #6b7280; margin-bottom: 2px; } .search-side { text-transform: uppercase; font-size: 10px; } .search-line { font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace; white-space: pre-wrap; word-break: break-all; } .search-line mark { background: #713f12; color: #fde68a; border-radius: 2px; padding: 0 2px; } .search-empty { padding: 12px; text-align: center; color: #6b7280; font-size: 12px; } .search-more { padding: 6px 8px; color: #6b7280; font-size: 11px; text-align: center; } #topbar { position: relative; } #status { position: fixed; bottom: 0; left: 0; right: 0; padding: 4px 12px; font-size: 11px; color: var(--muted); background: var(--panel); border-top: 1px solid var(--border); z-index: 100; } #status.error { color: #f87171; } #status.ok { color: #34d399; } `;