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.

183 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 { UserAvatar, 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, and as a workspace
39 // acting under its own identity (lib/mission.ts), never named after it.
40 const ids = [...new Set(lines.map((line) => line.actor).filter((actor): actor is string => !!actor && actor !== "g1t"))];
41 const names = ids.length > 0 ? await identity.usernames(ids.filter((id) => !id.startsWith("wsp_"))).catch(() => ({}) as Record<string, string>) : {};
42 const g1t = (actor: string | null) => actor === "g1t" || !!actor?.startsWith("wsp_");
43 return {
44 lines: lines.map((line) => ({ ...line, who: g1t(line.actor) ? "g1t" : line.actor ? (names[line.actor] ?? null) : null })),
45 next: found.length === PAGE ? found[found.length - 1].id : null,
46 defaultBranch: repo.defaultBranch,
47 branch: only,
48 };
49}
50
51function describe(line: ActivityLine, base: string): { icon: ReactNode; text: ReactNode } {
52 const branch = (name: string) => (
53 <Link to={`${base}/tree/${encodeTag(name)}`} className="font-mono text-[0.8125rem] font-medium text-fg hover:text-accent">
54 {name}
55 </Link>
56 );
57 const commit = (hash: string) => (
58 <Link to={`${base}/commit/${hash}`} className="font-mono text-[0.8125rem] text-muted hover:text-accent">
59 {hash.slice(0, 7)}
60 </Link>
61 );
62 switch (line.kind) {
63 case "push":
64 return line.created
65 ? { icon: <GitBranchPlus size={15} />, text: <>created {branch(line.branch)} at {commit(line.commit)}</> }
66 : {
67 icon: <GitCommitHorizontal size={15} />,
68 text: (
69 <>
70 pushed to {branch(line.branch)}{" "}
71 {line.before ? (
72 <Link to={`${base}/compare/${line.before}...${line.commit}`} className="font-mono text-[0.8125rem] text-muted hover:text-accent">
73 {line.before.slice(0, 7)}…{line.commit.slice(0, 7)}
74 </Link>
75 ) : (
76 commit(line.commit)
77 )}
78 </>
79 ),
80 };
81 case "deleted":
82 return {
83 icon: <GitBranchMinus size={15} />,
84 text: (
85 <>
86 deleted <span className="font-mono text-[0.8125rem] text-fg">{line.branch}</span> at {commit(line.commit)}
87 </>
88 ),
89 };
90 case "tag":
91 return { icon: <TagIcon size={15} />, text: <>tagged {branch(line.tag)} at {commit(line.commit)}</> };
92 case "merge":
93 return {
94 icon: <GitMerge size={15} />,
95 text: (
96 <>
97 merged{" "}
98 <Link to={`${base}/pull/${line.number}`} className="font-medium text-fg hover:text-accent">
99 #{line.number}
100 </Link>{" "}
101 as {commit(line.commit)}
102 </>
103 ),
104 };
105 case "renamed":
106 return { icon: <GitBranch size={15} />, text: <>renamed <span className="font-mono text-[0.8125rem]">{line.from}</span> to {branch(line.to)}</> };
107 case "default":
108 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></> };
109 }
110}
111
112export default function ActivityPage({ loaderData, params }: Route.ComponentProps) {
113 const { lines, next, branch } = loaderData;
114 const base = `/${params.owner}/${params.repo}`;
115 return (
116 <div className="space-y-4">
117 <div>
118 <h2 className="text-lg font-semibold tracking-tight">
119 Activity{branch && <> on <span className="font-mono text-base">{branch}</span></>}
120 </h2>
121 <p className="mt-1 text-sm text-muted">
122 {branch ? (
123 <>
124 Pushes to it, and when it was made, renamed or deleted, newest first.{" "}
125 <Link to={base + "/activity"} className="text-fg hover:underline">
126 All activity
127 </Link>
128 </>
129 ) : (
130 "Pushes, merges, new and deleted branches, tags, and branch renames, newest first, by person or agent."
131 )}
132 </p>
133 </div>
134 {lines.length === 0 ? (
135 <EmptyState title={branch ? (next ? "Nothing on this branch here" : "No activity on this branch") : "No activity yet"}>
136 {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."}
137 </EmptyState>
138 ) : (
139 <Card asChild divided className="overflow-hidden">
140 <ul>
141 {lines.map(({ id, time, who, line }) => {
142 const { icon, text } = describe(line, base);
143 return (
144 <li key={id} className="flex items-start gap-3 px-4 py-3 text-sm">
145 <span className="mt-0.5 shrink-0 text-faint">{icon}</span>
146 <span className="min-w-0 grow">
147 <span className="inline-flex items-center gap-1.5 align-middle">
148 {who && who !== "g1t" ? (
149 <UserAvatar username={who} size={18} />
150 ) : (
151 <Avatar name={who ?? "someone"} image={null} size={18} system={who === "g1t"} />
152 )}
153 {who && who !== "g1t" ? (
154 <UserCard username={who}>
155 <Link to={`/u/${who}`} className="font-medium hover:text-accent">
156 {who}
157 </Link>
158 </UserCard>
159 ) : (
160 <span className="font-medium">{who ?? "Someone"}</span>
161 )}
162 </span>{" "}
163 <span className="text-muted">{text}</span>
164 </span>
165 <span className="shrink-0 text-xs text-faint">
166 <TimeAgo at={time} />
167 </span>
168 </li>
169 );
170 })}
171 </ul>
172 </Card>
173 )}
174 {next && (
175 <div className="flex justify-center">
176 <ButtonLink variant="outline" to={`?${branch ? `branch=${encodeURIComponent(branch)}&` : ""}before=${encodeURIComponent(next)}`}>
177 Older
178 </ButtonLink>
179 </div>
180 )}
181 </div>
182 );
183}