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