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