Skip to content

g1t/apps/web/app/routes/repo/activity.tsx

141 lines6,057 bytesCodeBlame
1import { GitBranch, 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 { Avatar, ButtonLink, EmptyState, TimeAgo } from "../../components/ui";
10import { requireRepo } from "../../lib/access.server";
11import { ACTIVITY_TYPES, type ActivityLine, activityLine } from "../../lib/about";
12import { page } from "../../lib/meta";
13import { events, identity } from "../../lib/services.server";
14
15export function meta({ params, ...args }: Route.MetaArgs) {
16 return page(args, { title: `Activity · ${params.owner}/${params.repo} · g1t` });
17}
18
19const PAGE = 50;
20
21export async function loader({ params, context, request }: Route.LoaderArgs) {
22 const { repo } = await requireRepo(context, params, "read");
23 const before = new URL(request.url).searchParams.get("before") ?? undefined;
24 const found = await events
25 .list({ repoId: repo.id, types: [...ACTIVITY_TYPES] as EventType[], before, limit: PAGE })
26 .catch(() => []);
27 const lines = found.flatMap((event) => {
28 const line = activityLine(event);
29 return line ? [{ id: event.id, time: event.time, actor: event.actor, line }] : [];
30 });
31 // Who: people by their name now; g1t as itself.
32 const ids = [...new Set(lines.map((line) => line.actor).filter((actor): actor is string => !!actor && actor !== "g1t"))];
33 const names = ids.length > 0 ? await identity.usernames(ids).catch(() => ({}) as Record<string, string>) : {};
34 return {
35 lines: lines.map((line) => ({ ...line, who: line.actor === "g1t" ? "g1t" : line.actor ? (names[line.actor] ?? null) : null })),
36 next: found.length === PAGE ? found[found.length - 1].id : null,
37 defaultBranch: repo.defaultBranch,
38 };
39}
40
41function describe(line: ActivityLine, base: string): { icon: ReactNode; text: ReactNode } {
42 const branch = (name: string) => (
43 <Link to={`${base}/tree/${encodeTag(name)}`} className="font-mono text-[0.8125rem] font-medium text-fg hover:text-accent">
44 {name}
45 </Link>
46 );
47 const commit = (hash: string) => (
48 <Link to={`${base}/commit/${hash}`} className="font-mono text-[0.8125rem] text-muted hover:text-accent">
49 {hash.slice(0, 7)}
50 </Link>
51 );
52 switch (line.kind) {
53 case "push":
54 return line.created
55 ? { icon: <GitBranchPlus size={15} />, text: <>created {branch(line.branch)} at {commit(line.commit)}</> }
56 : {
57 icon: <GitCommitHorizontal size={15} />,
58 text: (
59 <>
60 pushed to {branch(line.branch)}{" "}
61 {line.before ? (
62 <Link to={`${base}/compare/${line.before}...${line.commit}`} className="font-mono text-[0.8125rem] text-muted hover:text-accent">
63 {line.before.slice(0, 7)}…{line.commit.slice(0, 7)}
64 </Link>
65 ) : (
66 commit(line.commit)
67 )}
68 </>
69 ),
70 };
71 case "tag":
72 return { icon: <TagIcon size={15} />, text: <>tagged {branch(line.tag)} at {commit(line.commit)}</> };
73 case "merge":
74 return {
75 icon: <GitMerge size={15} />,
76 text: (
77 <>
78 merged{" "}
79 <Link to={`${base}/pull/${line.number}`} className="font-medium text-fg hover:text-accent">
80 #{line.number}
81 </Link>{" "}
82 as {commit(line.commit)}
83 </>
84 ),
85 };
86 case "renamed":
87 return { icon: <GitBranch size={15} />, text: <>renamed <span className="font-mono text-[0.8125rem]">{line.from}</span> to {branch(line.to)}</> };
88 case "default":
89 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></> };
90 }
91}
92
93export default function ActivityPage({ loaderData, params }: Route.ComponentProps) {
94 const { lines, next } = loaderData;
95 const base = `/${params.owner}/${params.repo}`;
96 return (
97 <div className="space-y-4">
98 <div>
99 <h2 className="text-lg font-semibold tracking-tight">Activity</h2>
100 <p className="mt-1 text-sm text-muted">Pushes, merges, new branches and tags, and branch renames, newest first, by person or agent.</p>
101 </div>
102 {lines.length === 0 ? (
103 <EmptyState title="No activity yet">Pushes and merges show here as they happen.</EmptyState>
104 ) : (
105 <ul className="divide-y divide-line overflow-hidden rounded-xl border border-line bg-surface">
106 {lines.map(({ id, time, who, line }) => {
107 const { icon, text } = describe(line, base);
108 return (
109 <li key={id} className="flex items-start gap-3 px-4 py-3 text-sm">
110 <span className="mt-0.5 shrink-0 text-faint">{icon}</span>
111 <span className="min-w-0 grow">
112 <span className="inline-flex items-center gap-1.5 align-middle">
113 <Avatar name={who ?? "someone"} size={18} system={who === "g1t"} />
114 {who && who !== "g1t" ? (
115 <Link to={`/u/${who}`} className="font-medium hover:text-accent">
116 {who}
117 </Link>
118 ) : (
119 <span className="font-medium">{who ?? "Someone"}</span>
120 )}
121 </span>{" "}
122 <span className="text-muted">{text}</span>
123 </span>
124 <span className="shrink-0 text-xs text-faint">
125 <TimeAgo at={time} />
126 </span>
127 </li>
128 );
129 })}
130 </ul>
131 )}
132 {next && (
133 <div className="flex justify-center">
134 <ButtonLink variant="quiet" to={`?before=${encodeURIComponent(next)}`}>
135 Older
136 </ButtonLink>
137 </div>
138 )}
139 </div>
140 );
141}