The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | /** |
| 2 | * The commit view (`/:owner/:repo/commit/:oid`): GitHub-style page with |
| 3 | * the commit message/metadata on top and one rendered diff per changed |
| 4 | * file below. |
| 5 | * |
| 6 | * The server returns the changed-file *list* only (`commits/:oid/diff`); |
| 7 | * the shared `FileDiffList` (see `DiffList.tsx`) fetches blob contents |
| 8 | * and diffs locally with `@pierre/diffs`. This module is |
| 9 | * `React.lazy`-loaded from `Repo.tsx` so the diffs+shiki runtime stays |
| 10 | * out of the initial bundle. |
| 11 | */ |
| 12 | import { useEffect, useMemo, useState } from "react"; |
| 13 | import { getCommit, getCommitDiff, type CommitDiff, type CommitInfo } from "../client.ts"; |
| 14 | import { CopyButton, ErrorBox, Spinner } from "../components.tsx"; |
| 15 | import { shortOid, subject, timeAgo } from "../format.ts"; |
| 16 | import { href, Link } from "../router.tsx"; |
| 17 | import FileDiffList from "./DiffList.tsx"; |
| 18 | import type { RepoContext } from "./Repo.tsx"; |
| 19 | |
| 20 | const CommitPage = ({ |
| 21 | context, |
| 22 | oid, |
| 23 | }: { |
| 24 | context: RepoContext; |
| 25 | /** Full 40-hex commit oid (links always carry the full oid). */ |
| 26 | oid: string; |
| 27 | }) => { |
| 28 | const [data, setData] = useState<{ |
| 29 | commit: CommitInfo; |
| 30 | diff: CommitDiff; |
| 31 | } | null>(null); |
| 32 | const [error, setError] = useState<unknown>(null); |
| 33 | const { connection, repo } = context; |
| 34 | |
| 35 | useEffect(() => { |
| 36 | let cancelled = false; |
| 37 | setData(null); |
| 38 | setError(null); |
| 39 | void Promise.all([ |
| 40 | getCommit(connection, repo.owner, repo.name, oid), |
| 41 | getCommitDiff(connection, repo.owner, repo.name, oid), |
| 42 | ]) |
| 43 | .then(([commit, diff]) => { |
| 44 | if (!cancelled) setData({ commit, diff }); |
| 45 | }) |
| 46 | .catch((cause) => { |
| 47 | if (!cancelled) setError(cause); |
| 48 | }); |
| 49 | return () => { |
| 50 | cancelled = true; |
| 51 | }; |
| 52 | // eslint-disable-next-line react-hooks/exhaustive-deps |
| 53 | }, [oid, repo.repoId]); |
| 54 | |
| 55 | const body = useMemo( |
| 56 | () => (data === null ? "" : data.commit.message.split("\n").slice(1).join("\n").trim()), |
| 57 | [data], |
| 58 | ); |
| 59 | |
| 60 | if (error != null) return <ErrorBox error={error} />; |
| 61 | if (data === null) return <Spinner />; |
| 62 | const { commit, diff } = data; |
| 63 | const files = diff.files; |
| 64 | |
| 65 | return ( |
| 66 | <div> |
| 67 | {/* commit header */} |
| 68 | <div className="mb-4 overflow-hidden rounded-md border border-border-muted"> |
| 69 | <div className="px-4 py-3"> |
| 70 | <h2 className="text-base font-semibold">{subject(commit.message)}</h2> |
| 71 | {body.length > 0 && ( |
| 72 | <pre className="mt-2 overflow-x-auto font-mono text-xs whitespace-pre-wrap text-fg-muted"> |
| 73 | {body} |
| 74 | </pre> |
| 75 | )} |
| 76 | <p className="mt-2 text-xs text-fg-muted"> |
| 77 | <span className="font-semibold text-fg-default">{commit.author.name}</span> committed{" "} |
| 78 | {timeAgo(commit.author.date * 1000)} |
| 79 | {commit.committer.name !== commit.author.name && ( |
| 80 | <> · committed by {commit.committer.name}</> |
| 81 | )} |
| 82 | </p> |
| 83 | </div> |
| 84 | <div className="flex flex-wrap items-center justify-between gap-2 border-t border-border-muted bg-canvas-subtle px-4 py-2 text-xs text-fg-muted"> |
| 85 | <span className="flex items-center gap-1.5"> |
| 86 | {commit.parents.length === 0 ? ( |
| 87 | <span>root commit</span> |
| 88 | ) : ( |
| 89 | <> |
| 90 | {commit.parents.length === 1 ? "parent" : "parents"}{" "} |
| 91 | {commit.parents.map((parent, index) => ( |
| 92 | <span key={parent}> |
| 93 | {index > 0 && <span className="mr-1.5">+</span>} |
| 94 | <Link |
| 95 | to={href(repo.owner, repo.name, "commit", parent)} |
| 96 | className="font-mono text-accent hover:underline" |
| 97 | > |
| 98 | {shortOid(parent)} |
| 99 | </Link> |
| 100 | </span> |
| 101 | ))} |
| 102 | </> |
| 103 | )} |
| 104 | </span> |
| 105 | <span className="flex items-center gap-1"> |
| 106 | commit{" "} |
| 107 | <code className="rounded border border-border-muted px-2 py-0.5 font-mono text-accent"> |
| 108 | {shortOid(commit.oid)} |
| 109 | </code> |
| 110 | <CopyButton text={commit.oid} /> |
| 111 | </span> |
| 112 | </div> |
| 113 | </div> |
| 114 | |
| 115 | {/* summary + truncation notice */} |
| 116 | <p className="mb-3 text-sm text-fg-muted"> |
| 117 | {files.length} file{files.length === 1 ? "" : "s"} changed |
| 118 | {diff.parent === null && " (root commit — diffed against the empty tree)"} |
| 119 | </p> |
| 120 | {diff.truncated && ( |
| 121 | <div className="mb-3 rounded-md border border-attention/40 bg-attention/5 px-4 py-2 text-sm text-attention"> |
| 122 | This commit changes more files than shown — the list was truncated at {files.length} files |
| 123 | by the server. |
| 124 | </div> |
| 125 | )} |
| 126 | |
| 127 | {/* file diffs */} |
| 128 | <FileDiffList context={context} files={files} /> |
| 129 | </div> |
| 130 | ); |
| 131 | }; |
| 132 | |
| 133 | export default CommitPage; |