Skip to content

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

177 linesCodeBlameRaw
1import { GitBranch, GitBranchMinus, GitBranchPlus, GitCommitHorizontal, GitMerge, Tag as TagIcon } from "lucide-react";
2import type { ReactNode } from "react";
3import { Link } from "react-router";
4
5import type { EventType } from "@g1t/contracts";
6
7import type { Route } from "./+types/activity";
8import { encodeTag } from "../../components/releases";
9import { ButtonLink, EmptyState, TimeAgo } from "../../components/ui";
10import { Avatar } from "../../components/ui/avatar";
11import { Card } from "../../components/ui/card";
12import { requireRepo } from "../../lib/access.server";
13import { ACTIVITY_TYPES, type ActivityLine, aboutBranch, activityLine } from "../../lib/about";
14import { page } from "../../lib/meta";
15import { events, identity } from "../../lib/services.server";
16import { UserCard } from "../../components/user-card";
17
18export function meta({ params, ...args }: Route.MetaArgs) {
19 return page(args, { title: `Activity · ${params.owner}/${params.repo} · g1t` });
20}
21
22const PAGE = 50;
23
24export async function loader({ params, context, request }: Route.LoaderArgs) {
25 const { repo } = await requireRepo(context, params, "read");
26 const url = new URL(request.url);
27 const before = url.searchParams.get("before") ?? undefined;
28 // One branch's activity (the Branches page links here): each page of
29 // events is read whole and only that branch's lines are kept.
30 const only = url.searchParams.get("branch")?.trim() || null;
31 const found = await events
32 .list({ repoId: repo.id, types: [...ACTIVITY_TYPES] as EventType[], before, limit: PAGE })
33 .catch(() => []);
34 const lines = found.flatMap((event) => {
35 const line = activityLine(event);
36 return line && (!only || aboutBranch(line, only, repo.defaultBranch)) ? [{ id: event.id, time: event.time, actor: event.actor, line }] : [];
37 });
38 // Who: people by their name now; g1t as itself.
39 const ids = [...new Set(lines.map((line) => line.actor).filter((actor): actor is string => !!actor && actor !== "g1t"))];
40 const names = ids.length > 0 ? await identity.usernames(ids).catch(() => ({}) as Record<string, string>) : {};
41 return {
42 lines: lines.map((line) => ({ ...line, who: line.actor === "g1t" ? "g1t" : line.actor ? (names[line.actor] ?? null) : null })),
43 next: found.length === PAGE ? found[found.length - 1].id : null,
44 defaultBranch: repo.defaultBranch,
45 branch: only,
46 };
47}
48
49function describe(line: ActivityLine, base: string): { icon: ReactNode; text: ReactNode } {
50 const branch = (name: string) => (
51 <Link to={`${base}/tree/${encodeTag(name)}`} className="font-mono text-[0.8125rem] font-medium text-fg hover:text-accent">
52 {name}
53 </Link>
54 );
55 const commit = (hash: string) => (
56 <Link to={`${base}/commit/${hash}`} className="font-mono text-[0.8125rem] text-muted hover:text-accent">
57 {hash.slice(0, 7)}
58 </Link>
59 );
60 switch (line.kind) {
61 case "push":
62 return line.created
63 ? { icon: <GitBranchPlus size={15} />, text: <>created {branch(line.branch)} at {commit(line.commit)}</> }
64 : {
65 icon: <GitCommitHorizontal size={15} />,
66 text: (
67 <>
68 pushed to {branch(line.branch)}{" "}
69 {line.before ? (
70 <Link to={`${base}/compare/${line.before}...${line.commit}`} className="font-mono text-[0.8125rem] text-muted hover:text-accent">
71 {line.before.slice(0, 7)}…{line.commit.slice(0, 7)}
72 </Link>
73 ) : (
74 commit(line.commit)
75 )}
76 </>
77 ),
78 };
79 case "deleted":
80 return {
81 icon: <GitBranchMinus size={15} />,
82 text: (
83 <>
84 deleted <span className="font-mono text-[0.8125rem] text-fg">{line.branch}</span> at {commit(line.commit)}
85 </>
86 ),
87 };
88 case "tag":
89 return { icon: <TagIcon size={15} />, text: <>tagged {branch(line.tag)} at {commit(line.commit)}</> };
90 case "merge":
91 return {
92 icon: <GitMerge size={15} />,
93 text: (
94 <>
95 merged{" "}
96 <Link to={`${base}/pull/${line.number}`} className="font-medium text-fg hover:text-accent">
97 #{line.number}
98 </Link>{" "}
99 as {commit(line.commit)}
100 </>
101 ),
102 };
103 case "renamed":
104 return { icon: <GitBranch size={15} />, text: <>renamed <span className="font-mono text-[0.8125rem]">{line.from}</span> to {branch(line.to)}</> };
105 case "default":
106 return { icon: <GitBranch size={15} />, text: <>made {branch(line.to)} the default branch, in place of <span className="font-mono text-[0.8125rem]">{line.from}</span></> };
107 }
108}
109
110export default function ActivityPage({ loaderData, params }: Route.ComponentProps) {
111 const { lines, next, branch } = loaderData;
112 const base = `/${params.owner}/${params.repo}`;
113 return (
114 <div className="space-y-4">
115 <div>
116 <h2 className="text-lg font-semibold tracking-tight">
117 Activity{branch && <> on <span className="font-mono text-base">{branch}</span></>}
118 </h2>
119 <p className="mt-1 text-sm text-muted">
120 {branch ? (
121 <>
122 Pushes to it, and when it was made, renamed or deleted, newest first.{" "}
123 <Link to={base + "/activity"} className="text-fg hover:underline">
124 All activity
125 </Link>
126 </>
127 ) : (
128 "Pushes, merges, new and deleted branches, tags, and branch renames, newest first, by person or agent."
129 )}
130 </p>
131 </div>
132 {lines.length === 0 ? (
133 <EmptyState title={branch ? (next ? "Nothing on this branch here" : "No activity on this branch") : "No activity yet"}>
134 {branch && next ? "Nothing in this stretch of the repository's activity was about it. Look further back." : "Pushes and merges show here as they happen."}
135 </EmptyState>
136 ) : (
137 <Card asChild divided className="overflow-hidden">
138 <ul>
139 {lines.map(({ id, time, who, line }) => {
140 const { icon, text } = describe(line, base);
141 return (
142 <li key={id} className="flex items-start gap-3 px-4 py-3 text-sm">
143 <span className="mt-0.5 shrink-0 text-faint">{icon}</span>
144 <span className="min-w-0 grow">
145 <span className="inline-flex items-center gap-1.5 align-middle">
146 <Avatar name={who ?? "someone"} size={18} system={who === "g1t"} />
147 {who && who !== "g1t" ? (
148 <UserCard username={who}>
149 <Link to={`/u/${who}`} className="font-medium hover:text-accent">
150 {who}
151 </Link>
152 </UserCard>
153 ) : (
154 <span className="font-medium">{who ?? "Someone"}</span>
155 )}
156 </span>{" "}
157 <span className="text-muted">{text}</span>
158 </span>
159 <span className="shrink-0 text-xs text-faint">
160 <TimeAgo at={time} />
161 </span>
162 </li>
163 );
164 })}
165 </ul>
166 </Card>
167 )}
168 {next && (
169 <div className="flex justify-center">
170 <ButtonLink variant="outline" to={`?${branch ? `branch=${encodeURIComponent(branch)}&` : ""}before=${encodeURIComponent(next)}`}>
171 Older
172 </ButtonLink>
173 </div>
174 )}
175 </div>
176 );
177}