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 | /** The Commits tab: paged history of the selected ref. */ |
| 2 | import { useEffect, useState } from "react"; |
| 3 | import { getLog, type CommitInfo } from "../client.ts"; |
| 4 | import { Button, CopyButton, ErrorBox, Spinner } from "../components.tsx"; |
| 5 | import { shortOid, subject, timeAgo } from "../format.ts"; |
| 6 | import { href, Link } from "../router.tsx"; |
| 7 | import type { RepoContext } from "./Repo.tsx"; |
| 8 | |
| 9 | const CommitRow = ({ context, commit }: { context: RepoContext; commit: CommitInfo }) => { |
| 10 | const [expanded, setExpanded] = useState(false); |
| 11 | const body = commit.message.split("\n").slice(1).join("\n").trim(); |
| 12 | const commitHref = href(context.repo.owner, context.repo.name, "commit", commit.oid); |
| 13 | return ( |
| 14 | <li className="border-b border-border-muted px-4 py-3 last:border-b-0"> |
| 15 | <div className="flex items-start justify-between gap-4"> |
| 16 | <div className="min-w-0"> |
| 17 | <Link |
| 18 | to={commitHref} |
| 19 | className="text-left text-sm font-semibold hover:text-accent hover:underline" |
| 20 | > |
| 21 | {subject(commit.message)} |
| 22 | </Link> |
| 23 | {body.length > 0 && ( |
| 24 | <button |
| 25 | type="button" |
| 26 | onClick={() => setExpanded((value) => !value)} |
| 27 | className="ml-2 cursor-pointer rounded border border-border-muted px-1 text-xs text-fg-muted hover:text-accent" |
| 28 | title={expanded ? "Hide description" : "Show description"} |
| 29 | > |
| 30 | … |
| 31 | </button> |
| 32 | )} |
| 33 | <p className="mt-0.5 text-xs text-fg-muted"> |
| 34 | {commit.author.name} committed {timeAgo(commit.author.date * 1000)} |
| 35 | </p> |
| 36 | {expanded && body.length > 0 && ( |
| 37 | <pre className="mt-2 overflow-x-auto rounded bg-canvas-subtle p-3 font-mono text-xs whitespace-pre-wrap"> |
| 38 | {body} |
| 39 | </pre> |
| 40 | )} |
| 41 | </div> |
| 42 | <div className="flex shrink-0 items-center gap-1"> |
| 43 | <Link |
| 44 | to={commitHref} |
| 45 | className="rounded border border-border-muted px-2 py-0.5 font-mono text-xs text-accent hover:underline" |
| 46 | > |
| 47 | {shortOid(commit.oid)} |
| 48 | </Link> |
| 49 | <CopyButton text={commit.oid} /> |
| 50 | </div> |
| 51 | </div> |
| 52 | </li> |
| 53 | ); |
| 54 | }; |
| 55 | |
| 56 | export const CommitsTab = ({ context }: { context: RepoContext }) => { |
| 57 | const [commits, setCommits] = useState<CommitInfo[] | null>(null); |
| 58 | const [cursor, setCursor] = useState<string | null>(null); |
| 59 | const [error, setError] = useState<unknown>(null); |
| 60 | const { connection, repo, refName } = context; |
| 61 | |
| 62 | const load = async (nextCursor?: string) => { |
| 63 | try { |
| 64 | const page = await getLog(connection, repo.owner, repo.name, { |
| 65 | ref: refName, |
| 66 | limit: 50, |
| 67 | ...(nextCursor ? { cursor: nextCursor } : {}), |
| 68 | }); |
| 69 | setCommits((existing) => (nextCursor ? [...(existing ?? []), ...page.items] : page.items)); |
| 70 | setCursor(page.hasMore ? page.nextCursor : null); |
| 71 | } catch (cause) { |
| 72 | setError(cause); |
| 73 | } |
| 74 | }; |
| 75 | |
| 76 | useEffect(() => { |
| 77 | setCommits(null); |
| 78 | void load(); |
| 79 | // eslint-disable-next-line react-hooks/exhaustive-deps |
| 80 | }, [refName, repo.repoId]); |
| 81 | |
| 82 | if (error != null) return <ErrorBox error={error} />; |
| 83 | if (commits === null) return <Spinner />; |
| 84 | |
| 85 | return ( |
| 86 | <div> |
| 87 | <ul className="overflow-hidden rounded-md border border-border-muted"> |
| 88 | {commits.map((commit) => ( |
| 89 | <CommitRow key={commit.oid} context={context} commit={commit} /> |
| 90 | ))} |
| 91 | {commits.length === 0 && ( |
| 92 | <li className="px-4 py-8 text-center text-sm text-fg-muted">No commits on {refName}</li> |
| 93 | )} |
| 94 | </ul> |
| 95 | {cursor !== null && ( |
| 96 | <div className="mt-4 flex justify-center"> |
| 97 | <Button onClick={() => void load(cursor)}>Older commits</Button> |
| 98 | </div> |
| 99 | )} |
| 100 | </div> |
| 101 | ); |
| 102 | }; |