| 62019fc | | | 1 | "use client"; |
| 62019fc | | | 2 | |
| 818dc90 | | | 3 | import { useState, useEffect } from "react"; |
| 62019fc | | | 4 | import type { ParsedFile, DiffHunk, DiffLine } from "./page"; |
| 62019fc | | | 5 | |
| 818dc90 | | | 6 | const DIFF_VIEW_KEY = "grove:diff-view"; |
| 818dc90 | | | 7 | |
| 818dc90 | | | 8 | function getStoredView(): "unified" | "split" { |
| 818dc90 | | | 9 | if (typeof window === "undefined") return "unified"; |
| 818dc90 | | | 10 | const stored = localStorage.getItem(DIFF_VIEW_KEY); |
| 818dc90 | | | 11 | return stored === "split" ? "split" : "unified"; |
| 818dc90 | | | 12 | } |
| 818dc90 | | | 13 | |
| 62019fc | | | 14 | interface Props { |
| 62019fc | | | 15 | files: ParsedFile[]; |
| 62019fc | | | 16 | owner: string; |
| 62019fc | | | 17 | repo: string; |
| 62019fc | | | 18 | gitSha: string; |
| 62019fc | | | 19 | } |
| 62019fc | | | 20 | |
| 62019fc | | | 21 | interface SplitSide { |
| 62019fc | | | 22 | num: number | null; |
| 62019fc | | | 23 | content: string; |
| 62019fc | | | 24 | html?: string; |
| 62019fc | | | 25 | type: "del" | "add" | "context" | "empty"; |
| 62019fc | | | 26 | } |
| 62019fc | | | 27 | |
| 62019fc | | | 28 | interface SplitRow { |
| 62019fc | | | 29 | type: "context" | "add" | "del" | "change"; |
| 62019fc | | | 30 | left: SplitSide; |
| 62019fc | | | 31 | right: SplitSide; |
| 62019fc | | | 32 | } |
| 62019fc | | | 33 | |
| 62019fc | | | 34 | function buildSplitRows(hunk: DiffHunk): SplitRow[] { |
| 62019fc | | | 35 | const rows: SplitRow[] = []; |
| 62019fc | | | 36 | const lines = hunk.lines; |
| 62019fc | | | 37 | let i = 0; |
| 62019fc | | | 38 | |
| 62019fc | | | 39 | while (i < lines.length) { |
| 62019fc | | | 40 | const line = lines[i]; |
| 62019fc | | | 41 | |
| 62019fc | | | 42 | if (line.type === "context") { |
| 62019fc | | | 43 | rows.push({ |
| 62019fc | | | 44 | type: "context", |
| 62019fc | | | 45 | left: { num: line.oldNum, content: line.content, html: line.html, type: "context" }, |
| 62019fc | | | 46 | right: { num: line.newNum, content: line.content, html: line.html, type: "context" }, |
| 62019fc | | | 47 | }); |
| 62019fc | | | 48 | i++; |
| 62019fc | | | 49 | } else { |
| 62019fc | | | 50 | // Collect consecutive del lines then add lines |
| 62019fc | | | 51 | const dels: typeof lines = []; |
| 62019fc | | | 52 | const adds: typeof lines = []; |
| 62019fc | | | 53 | while (i < lines.length && lines[i].type === "del") { |
| 62019fc | | | 54 | dels.push(lines[i]); |
| 62019fc | | | 55 | i++; |
| 62019fc | | | 56 | } |
| 62019fc | | | 57 | while (i < lines.length && lines[i].type === "add") { |
| 62019fc | | | 58 | adds.push(lines[i]); |
| 62019fc | | | 59 | i++; |
| 62019fc | | | 60 | } |
| 62019fc | | | 61 | |
| 62019fc | | | 62 | const maxLen = Math.max(dels.length, adds.length); |
| 62019fc | | | 63 | for (let j = 0; j < maxLen; j++) { |
| 62019fc | | | 64 | const del = dels[j]; |
| 62019fc | | | 65 | const add = adds[j]; |
| 62019fc | | | 66 | rows.push({ |
| 62019fc | | | 67 | type: del && add ? "change" : del ? "del" : "add", |
| 62019fc | | | 68 | left: del |
| 62019fc | | | 69 | ? { num: del.oldNum, content: del.content, html: del.html, type: "del" } |
| 62019fc | | | 70 | : { num: null, content: "", type: "empty" }, |
| 62019fc | | | 71 | right: add |
| 62019fc | | | 72 | ? { num: add.newNum, content: add.content, html: add.html, type: "add" } |
| 62019fc | | | 73 | : { num: null, content: "", type: "empty" }, |
| 62019fc | | | 74 | }); |
| 62019fc | | | 75 | } |
| 62019fc | | | 76 | } |
| 62019fc | | | 77 | } |
| 62019fc | | | 78 | |
| 62019fc | | | 79 | return rows; |
| 62019fc | | | 80 | } |
| 62019fc | | | 81 | |
| 62019fc | | | 82 | function cellBg(type: "del" | "add" | "context" | "empty"): string { |
| 62019fc | | | 83 | if (type === "add") return "var(--diff-add-bg)"; |
| 62019fc | | | 84 | if (type === "del") return "var(--diff-del-bg)"; |
| 62019fc | | | 85 | return "transparent"; |
| 62019fc | | | 86 | } |
| 62019fc | | | 87 | |
| 62019fc | | | 88 | function cellColor(type: "del" | "add" | "context" | "empty"): string { |
| 62019fc | | | 89 | if (type === "add") return "var(--diff-add-text)"; |
| 62019fc | | | 90 | if (type === "del") return "var(--diff-del-text)"; |
| 62019fc | | | 91 | return "var(--text-secondary)"; |
| 62019fc | | | 92 | } |
| 62019fc | | | 93 | |
| 62019fc | | | 94 | export function DiffViewer({ files, owner, repo, gitSha }: Props) { |
| 818dc90 | | | 95 | const [view, setView] = useState<"unified" | "split">(getStoredView); |
| 818dc90 | | | 96 | |
| 818dc90 | | | 97 | useEffect(() => { |
| 818dc90 | | | 98 | localStorage.setItem(DIFF_VIEW_KEY, view); |
| 818dc90 | | | 99 | }, [view]); |
| 62019fc | | | 100 | |
| 62019fc | | | 101 | let additions = 0; |
| 62019fc | | | 102 | let deletions = 0; |
| 62019fc | | | 103 | for (const file of files) { |
| 62019fc | | | 104 | for (const hunk of file.hunks) { |
| 62019fc | | | 105 | for (const line of hunk.lines) { |
| 62019fc | | | 106 | if (line.type === "add") additions++; |
| 62019fc | | | 107 | if (line.type === "del") deletions++; |
| 62019fc | | | 108 | } |
| 62019fc | | | 109 | } |
| 62019fc | | | 110 | } |
| 62019fc | | | 111 | |
| 62019fc | | | 112 | return ( |
| 62019fc | | | 113 | <> |
| 62019fc | | | 114 | <div className="flex items-center gap-3 text-xs mb-4" style={{ color: "var(--text-muted)" }}> |
| 62019fc | | | 115 | <span>{files.length} file{files.length !== 1 ? "s" : ""} changed</span> |
| 62019fc | | | 116 | {additions > 0 && ( |
| 62019fc | | | 117 | <span style={{ color: "var(--diff-add-text)" }}>+{additions}</span> |
| 62019fc | | | 118 | )} |
| 62019fc | | | 119 | {deletions > 0 && ( |
| 62019fc | | | 120 | <span style={{ color: "var(--diff-del-text)" }}>-{deletions}</span> |
| 62019fc | | | 121 | )} |
| 62019fc | | | 122 | <div className="ml-auto flex gap-1"> |
| 62019fc | | | 123 | <button |
| 62019fc | | | 124 | onClick={() => setView("unified")} |
| 62019fc | | | 125 | className="px-2 py-0.5" |
| 62019fc | | | 126 | style={{ |
| 62019fc | | | 127 | color: view === "unified" ? "var(--accent)" : "var(--text-faint)", |
| 62019fc | | | 128 | backgroundColor: view === "unified" ? "var(--bg-inset)" : "transparent", |
| 62019fc | | | 129 | border: "1px solid", |
| 62019fc | | | 130 | borderColor: view === "unified" ? "var(--border-subtle)" : "transparent", |
| 62019fc | | | 131 | cursor: "pointer", |
| 62019fc | | | 132 | fontSize: "0.75rem", |
| 62019fc | | | 133 | }} |
| 62019fc | | | 134 | > |
| 62019fc | | | 135 | Unified |
| 62019fc | | | 136 | </button> |
| 62019fc | | | 137 | <button |
| 62019fc | | | 138 | onClick={() => setView("split")} |
| 62019fc | | | 139 | className="px-2 py-0.5" |
| 62019fc | | | 140 | style={{ |
| 62019fc | | | 141 | color: view === "split" ? "var(--accent)" : "var(--text-faint)", |
| 62019fc | | | 142 | backgroundColor: view === "split" ? "var(--bg-inset)" : "transparent", |
| 62019fc | | | 143 | border: "1px solid", |
| 62019fc | | | 144 | borderColor: view === "split" ? "var(--border-subtle)" : "transparent", |
| 62019fc | | | 145 | cursor: "pointer", |
| 62019fc | | | 146 | fontSize: "0.75rem", |
| 62019fc | | | 147 | }} |
| 62019fc | | | 148 | > |
| 62019fc | | | 149 | Split |
| 62019fc | | | 150 | </button> |
| 62019fc | | | 151 | </div> |
| 62019fc | | | 152 | </div> |
| 62019fc | | | 153 | |
| 62019fc | | | 154 | {files.map((file) => ( |
| 62019fc | | | 155 | <div key={file.path} className="mb-4"> |
| 62019fc | | | 156 | <div |
| 62019fc | | | 157 | className="text-sm font-mono py-2 px-3" |
| 62019fc | | | 158 | style={{ |
| 62019fc | | | 159 | backgroundColor: "var(--bg-inset)", |
| 62019fc | | | 160 | border: "1px solid var(--border-subtle)", |
| 62019fc | | | 161 | borderBottom: "none", |
| 62019fc | | | 162 | color: "var(--text-secondary)", |
| 62019fc | | | 163 | }} |
| 62019fc | | | 164 | > |
| 818dc90 | | | 165 | <a |
| 62019fc | | | 166 | href={`/${owner}/${repo}/blob/${gitSha}/${file.path}`} |
| 62019fc | | | 167 | className="hover:underline" |
| 62019fc | | | 168 | style={{ color: "var(--accent)" }} |
| 62019fc | | | 169 | > |
| 62019fc | | | 170 | {file.path} |
| 818dc90 | | | 171 | </a> |
| 62019fc | | | 172 | </div> |
| 62019fc | | | 173 | {file.hunks.length === 0 ? ( |
| 62019fc | | | 174 | <div |
| 62019fc | | | 175 | className="text-sm py-3 px-3" |
| 62019fc | | | 176 | style={{ |
| 62019fc | | | 177 | border: "1px solid var(--border-subtle)", |
| 62019fc | | | 178 | color: "var(--text-faint)", |
| 62019fc | | | 179 | }} |
| 62019fc | | | 180 | > |
| 62019fc | | | 181 | Binary file |
| 62019fc | | | 182 | </div> |
| 62019fc | | | 183 | ) : view === "unified" ? ( |
| 62019fc | | | 184 | <UnifiedView hunks={file.hunks} /> |
| 62019fc | | | 185 | ) : ( |
| 62019fc | | | 186 | <SplitView hunks={file.hunks} /> |
| 62019fc | | | 187 | )} |
| 62019fc | | | 188 | </div> |
| 62019fc | | | 189 | ))} |
| 62019fc | | | 190 | </> |
| 62019fc | | | 191 | ); |
| 62019fc | | | 192 | } |
| 62019fc | | | 193 | |
| 62019fc | | | 194 | function UnifiedView({ hunks }: { hunks: DiffHunk[] }) { |
| 62019fc | | | 195 | return ( |
| 62019fc | | | 196 | <div className="overflow-x-auto shiki" style={{ border: "1px solid var(--border-subtle)" }}> |
| 62019fc | | | 197 | <table className="w-full text-sm font-mono" style={{ borderCollapse: "collapse" }}> |
| 62019fc | | | 198 | <tbody> |
| 62019fc | | | 199 | {hunks.flatMap((hunk, hi) => [ |
| 62019fc | | | 200 | <tr key={`hdr-${hi}`}> |
| 62019fc | | | 201 | <td |
| 62019fc | | | 202 | colSpan={3} |
| 62019fc | | | 203 | className="text-xs py-1 px-3" |
| 62019fc | | | 204 | style={{ |
| 62019fc | | | 205 | backgroundColor: "var(--bg-inset)", |
| 62019fc | | | 206 | color: "var(--text-faint)", |
| 62019fc | | | 207 | }} |
| 62019fc | | | 208 | > |
| 62019fc | | | 209 | {hunk.header} |
| 62019fc | | | 210 | </td> |
| 62019fc | | | 211 | </tr>, |
| 62019fc | | | 212 | ...hunk.lines.map((line, li) => ( |
| 62019fc | | | 213 | <tr |
| 62019fc | | | 214 | key={`${hi}-${li}`} |
| 62019fc | | | 215 | style={{ |
| 62019fc | | | 216 | backgroundColor: |
| 62019fc | | | 217 | line.type === "add" |
| 62019fc | | | 218 | ? "var(--diff-add-bg)" |
| 62019fc | | | 219 | : line.type === "del" |
| 62019fc | | | 220 | ? "var(--diff-del-bg)" |
| 62019fc | | | 221 | : "transparent", |
| 62019fc | | | 222 | }} |
| 62019fc | | | 223 | > |
| 62019fc | | | 224 | <td |
| 818dc90 | | | 225 | className="text-right select-none px-1 py-0 w-8" |
| 62019fc | | | 226 | style={{ |
| 62019fc | | | 227 | color: "var(--text-faint)", |
| 62019fc | | | 228 | borderRight: "1px solid var(--divide)", |
| 62019fc | | | 229 | fontSize: "0.75rem", |
| 62019fc | | | 230 | }} |
| 62019fc | | | 231 | > |
| 62019fc | | | 232 | {line.oldNum ?? ""} |
| 62019fc | | | 233 | </td> |
| 62019fc | | | 234 | <td |
| 818dc90 | | | 235 | className="text-right select-none px-1 py-0 w-8" |
| 62019fc | | | 236 | style={{ |
| 62019fc | | | 237 | color: "var(--text-faint)", |
| 62019fc | | | 238 | borderRight: "1px solid var(--divide)", |
| 62019fc | | | 239 | fontSize: "0.75rem", |
| 62019fc | | | 240 | }} |
| 62019fc | | | 241 | > |
| 62019fc | | | 242 | {line.newNum ?? ""} |
| 62019fc | | | 243 | </td> |
| 62019fc | | | 244 | {line.html ? ( |
| 62019fc | | | 245 | <td |
| 62019fc | | | 246 | className="pl-3 py-0 whitespace-pre" |
| 62019fc | | | 247 | dangerouslySetInnerHTML={{ __html: line.html }} |
| 62019fc | | | 248 | /> |
| 62019fc | | | 249 | ) : ( |
| 62019fc | | | 250 | <td |
| 62019fc | | | 251 | className="pl-3 py-0 whitespace-pre" |
| 62019fc | | | 252 | style={{ |
| 62019fc | | | 253 | color: |
| 62019fc | | | 254 | line.type === "add" |
| 62019fc | | | 255 | ? "var(--diff-add-text)" |
| 62019fc | | | 256 | : line.type === "del" |
| 62019fc | | | 257 | ? "var(--diff-del-text)" |
| 62019fc | | | 258 | : "var(--text-secondary)", |
| 62019fc | | | 259 | }} |
| 62019fc | | | 260 | > |
| 62019fc | | | 261 | {line.type === "add" ? "+" : line.type === "del" ? "-" : " "} |
| 62019fc | | | 262 | {line.content} |
| 62019fc | | | 263 | </td> |
| 62019fc | | | 264 | )} |
| 62019fc | | | 265 | </tr> |
| 62019fc | | | 266 | )), |
| 62019fc | | | 267 | ])} |
| 62019fc | | | 268 | </tbody> |
| 62019fc | | | 269 | </table> |
| 62019fc | | | 270 | </div> |
| 62019fc | | | 271 | ); |
| 62019fc | | | 272 | } |
| 62019fc | | | 273 | |
| 62019fc | | | 274 | function SplitView({ hunks }: { hunks: DiffHunk[] }) { |
| 62019fc | | | 275 | return ( |
| 62019fc | | | 276 | <div className="overflow-x-auto shiki" style={{ border: "1px solid var(--border-subtle)" }}> |
| 62019fc | | | 277 | <table className="w-full text-sm font-mono" style={{ borderCollapse: "collapse", tableLayout: "fixed" }}> |
| 818dc90 | | | 278 | <colgroup> |
| 818dc90 | | | 279 | <col style={{ width: "2.5rem" }} /> |
| 818dc90 | | | 280 | <col /> |
| 818dc90 | | | 281 | <col style={{ width: "2.5rem" }} /> |
| 818dc90 | | | 282 | <col /> |
| 818dc90 | | | 283 | </colgroup> |
| 62019fc | | | 284 | <tbody> |
| 62019fc | | | 285 | {hunks.flatMap((hunk, hi) => { |
| 62019fc | | | 286 | const rows = buildSplitRows(hunk); |
| 62019fc | | | 287 | return [ |
| 62019fc | | | 288 | <tr key={`hdr-${hi}`}> |
| 62019fc | | | 289 | <td |
| 62019fc | | | 290 | colSpan={4} |
| 62019fc | | | 291 | className="text-xs py-1 px-3" |
| 62019fc | | | 292 | style={{ |
| 62019fc | | | 293 | backgroundColor: "var(--bg-inset)", |
| 62019fc | | | 294 | color: "var(--text-faint)", |
| 62019fc | | | 295 | }} |
| 62019fc | | | 296 | > |
| 62019fc | | | 297 | {hunk.header} |
| 62019fc | | | 298 | </td> |
| 62019fc | | | 299 | </tr>, |
| 62019fc | | | 300 | ...rows.map((row, ri) => ( |
| 62019fc | | | 301 | <tr key={`${hi}-${ri}`}> |
| 62019fc | | | 302 | {/* Left side (old) */} |
| 62019fc | | | 303 | <td |
| 818dc90 | | | 304 | className="text-right select-none px-1 py-0" |
| 62019fc | | | 305 | style={{ |
| 62019fc | | | 306 | color: "var(--text-faint)", |
| 62019fc | | | 307 | backgroundColor: cellBg(row.left.type), |
| 62019fc | | | 308 | borderRight: "1px solid var(--divide)", |
| 62019fc | | | 309 | fontSize: "0.75rem", |
| 62019fc | | | 310 | }} |
| 62019fc | | | 311 | > |
| 62019fc | | | 312 | {row.left.num ?? ""} |
| 62019fc | | | 313 | </td> |
| 62019fc | | | 314 | {row.left.html ? ( |
| 62019fc | | | 315 | <td |
| 62019fc | | | 316 | className="pl-3 py-0 whitespace-pre overflow-hidden" |
| 62019fc | | | 317 | style={{ |
| 62019fc | | | 318 | backgroundColor: cellBg(row.left.type), |
| 62019fc | | | 319 | borderRight: "1px solid var(--border-subtle)", |
| 62019fc | | | 320 | }} |
| 62019fc | | | 321 | dangerouslySetInnerHTML={{ __html: row.left.html }} |
| 62019fc | | | 322 | /> |
| 62019fc | | | 323 | ) : ( |
| 62019fc | | | 324 | <td |
| 62019fc | | | 325 | className="pl-3 py-0 whitespace-pre overflow-hidden" |
| 62019fc | | | 326 | style={{ |
| 62019fc | | | 327 | color: cellColor(row.left.type), |
| 62019fc | | | 328 | backgroundColor: cellBg(row.left.type), |
| 62019fc | | | 329 | borderRight: "1px solid var(--border-subtle)", |
| 62019fc | | | 330 | }} |
| 62019fc | | | 331 | > |
| 62019fc | | | 332 | {row.left.type === "del" ? "-" : row.left.type === "context" ? " " : ""} |
| 62019fc | | | 333 | {row.left.content} |
| 62019fc | | | 334 | </td> |
| 62019fc | | | 335 | )} |
| 62019fc | | | 336 | {/* Right side (new) */} |
| 62019fc | | | 337 | <td |
| 818dc90 | | | 338 | className="text-right select-none px-1 py-0" |
| 62019fc | | | 339 | style={{ |
| 62019fc | | | 340 | color: "var(--text-faint)", |
| 62019fc | | | 341 | backgroundColor: cellBg(row.right.type), |
| 62019fc | | | 342 | borderRight: "1px solid var(--divide)", |
| 62019fc | | | 343 | fontSize: "0.75rem", |
| 62019fc | | | 344 | }} |
| 62019fc | | | 345 | > |
| 62019fc | | | 346 | {row.right.num ?? ""} |
| 62019fc | | | 347 | </td> |
| 62019fc | | | 348 | {row.right.html ? ( |
| 62019fc | | | 349 | <td |
| 62019fc | | | 350 | className="pl-3 py-0 whitespace-pre overflow-hidden" |
| 62019fc | | | 351 | style={{ |
| 62019fc | | | 352 | backgroundColor: cellBg(row.right.type), |
| 62019fc | | | 353 | }} |
| 62019fc | | | 354 | dangerouslySetInnerHTML={{ __html: row.right.html }} |
| 62019fc | | | 355 | /> |
| 62019fc | | | 356 | ) : ( |
| 62019fc | | | 357 | <td |
| 62019fc | | | 358 | className="pl-3 py-0 whitespace-pre overflow-hidden" |
| 62019fc | | | 359 | style={{ |
| 62019fc | | | 360 | color: cellColor(row.right.type), |
| 62019fc | | | 361 | backgroundColor: cellBg(row.right.type), |
| 62019fc | | | 362 | }} |
| 62019fc | | | 363 | > |
| 62019fc | | | 364 | {row.right.type === "add" ? "+" : row.right.type === "context" ? " " : ""} |
| 62019fc | | | 365 | {row.right.content} |
| 62019fc | | | 366 | </td> |
| 62019fc | | | 367 | )} |
| 62019fc | | | 368 | </tr> |
| 62019fc | | | 369 | )), |
| 62019fc | | | 370 | ]; |
| 62019fc | | | 371 | })} |
| 62019fc | | | 372 | </tbody> |
| 62019fc | | | 373 | </table> |
| 62019fc | | | 374 | </div> |
| 62019fc | | | 375 | ); |
| 62019fc | | | 376 | } |