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.

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