| 1 | import { Check, Copy, GitCommitHorizontal, GitPullRequest, MessagesSquare, Sparkles } from "lucide-react"; |
| 2 | import { Suspense, useState } from "react"; |
| 3 | import { Await, Link, data, redirect } from "react-router"; |
| 4 | |
| 5 | import type { Comparison, Pull } from "@g1t/contracts"; |
| 6 | |
| 7 | import { parseCommitMessage } from "../../lib/commit-message"; |
| 8 | import type { Route } from "./+types/commit"; |
| 9 | import { page } from "../../lib/meta"; |
| 10 | import { DiffView } from "../../components/diff-view"; |
| 11 | import { Avatar, TimeAgo } from "../../components/ui"; |
| 12 | import { Skeleton } from "../../components/ui/skeleton"; |
| 13 | import { immutable } from "../../lib/immutable.server"; |
| 14 | import { pullForCommit } from "../../lib/provenance.server"; |
| 15 | import { accounts, repos } from "../../lib/services.server"; |
| 16 | import { getViewer, unwrap } from "../../lib/session.server"; |
| 17 | |
| 18 | export function headers({ loaderHeaders }: Route.HeadersArgs) { |
| 19 | return { "Server-Timing": loaderHeaders.get("Server-Timing") ?? "" }; |
| 20 | } |
| 21 | |
| 22 | export function meta({ loaderData, params, ...args }: Route.MetaArgs) { |
| 23 | const subject = loaderData?.commit.message.split("\n")[0]; |
| 24 | return page(args, { |
| 25 | title: `${subject ? `${subject} · ` : ""}${params.hash.slice(0, 7)} · ${params.owner}/${params.repo} · g1t`, |
| 26 | }); |
| 27 | } |
| 28 | |
| 29 | /** How far back a short hash is looked for. */ |
| 30 | const SHORT_HASH_SEARCH = 300; |
| 31 | |
| 32 | export async function loader({ params, context }: Route.LoaderArgs) { |
| 33 | const viewer = getViewer(context); |
| 34 | const path = { namespace: params.owner, name: params.repo }; |
| 35 | const hash = params.hash.toLowerCase(); |
| 36 | if (!/^[0-9a-f]{4,40}$/.test(hash)) throw data(null, { status: 404 }); |
| 37 | if (hash.length < 40) { |
| 38 | // A short hash: find the commit it names and go to its full address. |
| 39 | const log = await repos.log(path, viewer, null, SHORT_HASH_SEARCH); |
| 40 | const matches = log.ok ? log.value.filter((commit) => commit.hash.startsWith(hash)) : []; |
| 41 | if (matches.length === 1) throw redirect(`/${params.owner}/${params.repo}/commit/${matches[0]!.hash}`); |
| 42 | throw new Response("Commit not found.", { status: 404 }); |
| 43 | } |
| 44 | const started = Date.now(); |
| 45 | const found = unwrap(await repos.get(path, viewer)); |
| 46 | // The viewer may read the repository, and a commit never changes: after |
| 47 | // the first visit it and its diff come from the cache. |
| 48 | let cached = true; |
| 49 | const loaded = await immutable(`commit:${found.id}:${hash}`, async () => { |
| 50 | cached = false; |
| 51 | // At once: a commit is compared with its first parent, so the diff |
| 52 | // does not wait for the commit. |
| 53 | const [log, compared] = await Promise.all([ |
| 54 | repos.log(path, viewer, hash, 1), |
| 55 | repos.compare(found.id, viewer, null, hash), |
| 56 | ]); |
| 57 | const commit = log.ok ? log.value[0] : undefined; |
| 58 | if (!commit) return null; |
| 59 | const comparison: Comparison = compared.ok |
| 60 | ? compared.value |
| 61 | : { base: null, head: commit.hash, files: [], truncated: false }; |
| 62 | return { commit, comparison }; |
| 63 | }); |
| 64 | if (!loaded) throw new Response("Commit not found.", { status: 404 }); |
| 65 | // The account the author address belongs to, if any; not cached, since |
| 66 | // an address can be confirmed or removed later. |
| 67 | const email = loaded.commit.author.email.toLowerCase(); |
| 68 | const owners = await accounts.emailOwners([email]).catch(() => ({}) as Record<string, never>); |
| 69 | const owner: { username: string; avatar: string | null } | null = owners[email] ?? null; |
| 70 | return data( |
| 71 | { |
| 72 | ...loaded, |
| 73 | owner, |
| 74 | // Streamed: the page shows the commit while this is worked out. |
| 75 | pull: pullForCommit(path, viewer, loaded.commit.hash), |
| 76 | }, |
| 77 | { headers: { "Server-Timing": `load;dur=${Date.now() - started};desc="${cached ? "cached" : "read"}"` } }, |
| 78 | ); |
| 79 | } |
| 80 | |
| 81 | function CopyHash({ hash }: { hash: string }) { |
| 82 | const [copied, setCopied] = useState(false); |
| 83 | return ( |
| 84 | <button |
| 85 | type="button" |
| 86 | onClick={() => { |
| 87 | void navigator.clipboard?.writeText(hash).then(() => { |
| 88 | setCopied(true); |
| 89 | setTimeout(() => setCopied(false), 1500); |
| 90 | }); |
| 91 | }} |
| 92 | className="flex items-center gap-1.5 rounded-md border border-line bg-bg px-2 py-1 font-mono text-xs text-muted transition-colors hover:border-line-strong hover:text-fg" |
| 93 | aria-label="Copy the full hash" |
| 94 | > |
| 95 | {hash.slice(0, 12)} |
| 96 | {copied ? <Check size={12} className="text-accent" /> : <Copy size={12} />} |
| 97 | </button> |
| 98 | ); |
| 99 | } |
| 100 | |
| 101 | /** The pull request a commit arrived in, once it has been worked out. */ |
| 102 | function MergedIn({ base, pull }: { base: string; pull: Pull }) { |
| 103 | const byAgent = pull.runtime === "hosted"; |
| 104 | return ( |
| 105 | <div className="flex flex-wrap items-center gap-x-4 gap-y-2 border-t border-line px-5 py-3 text-sm"> |
| 106 | <span className="flex items-center gap-2"> |
| 107 | <GitPullRequest size={15} className="text-merged" /> |
| 108 | <span className="text-muted">Merged in</span> |
| 109 | <Link to={`${base}/pull/${pull.number}`} className="font-medium hover:underline"> |
| 110 | {pull.title} <span className="font-normal text-faint">#{pull.number}</span> |
| 111 | </Link> |
| 112 | </span> |
| 113 | {byAgent && ( |
| 114 | <span className="flex items-center gap-1.5 rounded-full border border-merged/30 bg-merged/10 px-2 py-0.5 text-xs text-merged"> |
| 115 | <Sparkles size={12} /> |
| 116 | written by {pull.agent} |
| 117 | </span> |
| 118 | )} |
| 119 | <Link |
| 120 | to={`${base}/pull/${pull.number}?tab=session`} |
| 121 | className="ml-auto flex items-center gap-1.5 rounded-md border border-line px-2.5 py-1 text-xs text-muted transition-colors hover:border-line-strong hover:text-fg" |
| 122 | > |
| 123 | <MessagesSquare size={13} /> |
| 124 | Why it was written |
| 125 | </Link> |
| 126 | </div> |
| 127 | ); |
| 128 | } |
| 129 | |
| 130 | export default function CommitPage({ loaderData, params }: Route.ComponentProps) { |
| 131 | const { commit, pull, comparison, owner } = loaderData; |
| 132 | const base = `/${params.owner}/${params.repo}`; |
| 133 | const { subject, body, coAuthors, trailers } = parseCommitMessage(commit.message); |
| 134 | return ( |
| 135 | <div> |
| 136 | <section className="overflow-hidden rounded-xl border border-line bg-surface"> |
| 137 | <div className="p-5"> |
| 138 | <p className="flex items-center gap-2 text-xs text-faint"> |
| 139 | <GitCommitHorizontal size={14} /> |
| 140 | <Link to={`${base}/commits`} className="hover:text-fg"> |
| 141 | Commit |
| 142 | </Link> |
| 143 | </p> |
| 144 | <h2 className="mt-2 text-xl font-semibold tracking-tight text-balance">{subject}</h2> |
| 145 | {body && <p className="mt-3 max-w-3xl text-sm whitespace-pre-wrap text-muted">{body}</p>} |
| 146 | {trailers.length > 0 && ( |
| 147 | <dl className="mt-3 space-y-0.5 font-mono text-xs text-faint"> |
| 148 | {trailers.map((trailer) => ( |
| 149 | <div key={`${trailer.key}:${trailer.value}`}> |
| 150 | <dt className="inline">{trailer.key}:</dt> <dd className="inline text-muted">{trailer.value}</dd> |
| 151 | </div> |
| 152 | ))} |
| 153 | </dl> |
| 154 | )} |
| 155 | </div> |
| 156 | <div className="flex flex-wrap items-center gap-x-5 gap-y-3 border-t border-line bg-bg/40 px-5 py-3 text-sm"> |
| 157 | <span className="flex items-center gap-2"> |
| 158 | <Avatar name={owner?.username ?? commit.author.name} image={owner?.avatar} size={20} /> |
| 159 | {owner ? ( |
| 160 | <Link to={`/u/${owner.username}`} className="font-medium hover:underline" title={commit.author.name}> |
| 161 | {owner.username} |
| 162 | </Link> |
| 163 | ) : ( |
| 164 | <span className="font-medium">{commit.author.name}</span> |
| 165 | )} |
| 166 | {coAuthors.length > 0 && ( |
| 167 | <span className="text-muted"> |
| 168 | and <span className="font-medium text-fg">{coAuthors.join(", ")}</span> |
| 169 | </span> |
| 170 | )} |
| 171 | <span className="text-muted"> |
| 172 | committed <TimeAgo at={commit.authoredAt} /> |
| 173 | </span> |
| 174 | </span> |
| 175 | <span className="flex items-center gap-2 text-xs text-muted"> |
| 176 | {commit.parents.length === 0 ? ( |
| 177 | "The first commit" |
| 178 | ) : ( |
| 179 | <> |
| 180 | {commit.parents.length === 1 ? "Parent" : "Parents"} |
| 181 | {commit.parents.map((parent) => ( |
| 182 | <Link |
| 183 | key={parent} |
| 184 | to={`${base}/commit/${parent}`} |
| 185 | prefetch="intent" |
| 186 | className="font-mono text-fg/80 hover:text-accent hover:underline" |
| 187 | > |
| 188 | {parent.slice(0, 7)} |
| 189 | </Link> |
| 190 | ))} |
| 191 | </> |
| 192 | )} |
| 193 | </span> |
| 194 | <span className="ml-auto flex items-center gap-2"> |
| 195 | <Link |
| 196 | to={`${base}/tree/${commit.hash}/`} |
| 197 | className="rounded-md px-2 py-1 text-xs text-muted hover:bg-raised hover:text-fg" |
| 198 | > |
| 199 | Browse files |
| 200 | </Link> |
| 201 | <CopyHash hash={commit.hash} /> |
| 202 | </span> |
| 203 | </div> |
| 204 | <Suspense |
| 205 | fallback={ |
| 206 | <div aria-busy="true" className="flex h-12 items-center gap-3 border-t border-line px-4"> |
| 207 | <Skeleton className="size-4 rounded-full" /> |
| 208 | <Skeleton className="h-3 w-64 max-w-[60%]" /> |
| 209 | </div> |
| 210 | } |
| 211 | > |
| 212 | <Await resolve={pull}>{(pull) => (pull ? <MergedIn base={base} pull={pull} /> : null)}</Await> |
| 213 | </Suspense> |
| 214 | </section> |
| 215 | <div className="mt-6"> |
| 216 | <DiffView comparison={comparison} empty="This commit changes no files." fileBase={`${base}/blob/${commit.hash}`} /> |
| 217 | </div> |
| 218 | </div> |
| 219 | ); |
| 220 | } |