| 1 | import { GitBranch, GitBranchMinus, GitBranchPlus, GitCommitHorizontal, GitMerge, Tag as TagIcon } from "lucide-react"; |
| 2 | import type { ReactNode } from "react"; |
| 3 | import { Link } from "react-router"; |
| 4 | |
| 5 | import type { EventType } from "@g1t/contracts"; |
| 6 | |
| 7 | import type { Route } from "./+types/activity"; |
| 8 | import { encodeTag } from "../../components/releases"; |
| 9 | import { ButtonLink, EmptyState, TimeAgo } from "../../components/ui"; |
| 10 | import { Avatar } from "../../components/ui/avatar"; |
| 11 | import { Card } from "../../components/ui/card"; |
| 12 | import { requireRepo } from "../../lib/access.server"; |
| 13 | import { ACTIVITY_TYPES, type ActivityLine, aboutBranch, activityLine } from "../../lib/about"; |
| 14 | import { page } from "../../lib/meta"; |
| 15 | import { isFailed, part } from "../../lib/partial"; |
| 16 | import { Unavailable } from "../../components/boundary"; |
| 17 | import { events, identity } from "../../lib/services.server"; |
| 18 | import { UserAvatar, UserCard } from "../../components/user-card"; |
| 19 | |
| 20 | export function meta({ params, ...args }: Route.MetaArgs) { |
| 21 | return page(args, { title: `Activity · ${params.owner}/${params.repo} · g1t` }); |
| 22 | } |
| 23 | |
| 24 | const PAGE = 50; |
| 25 | |
| 26 | export 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 | |
| 59 | function 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 | ? { |
| 74 | icon: <GitBranchPlus size={15} />, |
| 75 | text: ( |
| 76 | <> |
| 77 | created {branch(line.branch)} at {commit(line.commit)} |
| 78 | </> |
| 79 | ), |
| 80 | } |
| 81 | : { |
| 82 | icon: <GitCommitHorizontal size={15} />, |
| 83 | text: ( |
| 84 | <> |
| 85 | pushed to {branch(line.branch)}{" "} |
| 86 | {line.before ? ( |
| 87 | <Link to={`${base}/compare/${line.before}...${line.commit}`} className="font-mono text-[0.8125rem] text-muted hover:text-accent"> |
| 88 | {line.before.slice(0, 7)}…{line.commit.slice(0, 7)} |
| 89 | </Link> |
| 90 | ) : ( |
| 91 | commit(line.commit) |
| 92 | )} |
| 93 | </> |
| 94 | ), |
| 95 | }; |
| 96 | case "deleted": |
| 97 | return { |
| 98 | icon: <GitBranchMinus size={15} />, |
| 99 | text: ( |
| 100 | <> |
| 101 | deleted <span className="font-mono text-[0.8125rem] text-fg">{line.branch}</span> at {commit(line.commit)} |
| 102 | </> |
| 103 | ), |
| 104 | }; |
| 105 | case "tag": |
| 106 | return { |
| 107 | icon: <TagIcon size={15} />, |
| 108 | text: ( |
| 109 | <> |
| 110 | tagged {branch(line.tag)} at {commit(line.commit)} |
| 111 | </> |
| 112 | ), |
| 113 | }; |
| 114 | case "merge": |
| 115 | return { |
| 116 | icon: <GitMerge size={15} />, |
| 117 | text: ( |
| 118 | <> |
| 119 | merged{" "} |
| 120 | <Link to={`${base}/pull/${line.number}`} className="font-medium text-fg hover:text-accent"> |
| 121 | #{line.number} |
| 122 | </Link>{" "} |
| 123 | as {commit(line.commit)} |
| 124 | </> |
| 125 | ), |
| 126 | }; |
| 127 | case "renamed": |
| 128 | return { |
| 129 | icon: <GitBranch size={15} />, |
| 130 | text: ( |
| 131 | <> |
| 132 | renamed <span className="font-mono text-[0.8125rem]">{line.from}</span> to {branch(line.to)} |
| 133 | </> |
| 134 | ), |
| 135 | }; |
| 136 | case "default": |
| 137 | return { |
| 138 | icon: <GitBranch size={15} />, |
| 139 | text: ( |
| 140 | <> |
| 141 | made {branch(line.to)} the default branch, in place of <span className="font-mono text-[0.8125rem]">{line.from}</span> |
| 142 | </> |
| 143 | ), |
| 144 | }; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | export default function ActivityPage({ loaderData, params }: Route.ComponentProps) { |
| 149 | const { lines, next, branch, failed, namesFailed } = loaderData; |
| 150 | const base = `/${params.owner}/${params.repo}`; |
| 151 | return ( |
| 152 | <div className="space-y-4"> |
| 153 | <div> |
| 154 | <h2 className="text-lg font-semibold tracking-tight"> |
| 155 | Activity |
| 156 | {branch && ( |
| 157 | <> |
| 158 | {" "} |
| 159 | on <span className="font-mono text-base">{branch}</span> |
| 160 | </> |
| 161 | )} |
| 162 | </h2> |
| 163 | <p className="mt-1 text-sm text-muted"> |
| 164 | {branch ? ( |
| 165 | <> |
| 166 | Pushes to it, and when it was made, renamed or deleted, newest first.{" "} |
| 167 | <Link to={base + "/activity"} className="text-fg hover:underline"> |
| 168 | All activity |
| 169 | </Link> |
| 170 | </> |
| 171 | ) : ( |
| 172 | "Pushes, merges, new and deleted branches, tags, and branch renames, newest first, by person or agent." |
| 173 | )} |
| 174 | </p> |
| 175 | </div> |
| 176 | {namesFailed && <Unavailable compact what={namesFailed.what} />} |
| 177 | {failed ? ( |
| 178 | <Unavailable what={failed.what} /> |
| 179 | ) : lines.length === 0 ? ( |
| 180 | <EmptyState title={branch ? (next ? "Nothing on this branch here" : "No activity on this branch") : "No activity yet"}> |
| 181 | {branch && next |
| 182 | ? "Nothing in this stretch of the repository's activity was about it. Look further back." |
| 183 | : "Pushes and merges show here as they happen."} |
| 184 | </EmptyState> |
| 185 | ) : ( |
| 186 | <Card asChild divided className="overflow-hidden"> |
| 187 | <ul> |
| 188 | {lines.map(({ id, time, who, line }) => { |
| 189 | const { icon, text } = describe(line, base); |
| 190 | return ( |
| 191 | <li key={id} className="flex items-start gap-3 px-4 py-3 text-sm"> |
| 192 | <span className="mt-0.5 shrink-0 text-faint">{icon}</span> |
| 193 | <span className="min-w-0 grow"> |
| 194 | <span className="inline-flex items-center gap-1.5 align-middle"> |
| 195 | {who && who !== "g1t" ? ( |
| 196 | <UserAvatar username={who} size={18} /> |
| 197 | ) : ( |
| 198 | <Avatar name={who ?? "someone"} image={null} size={18} system={who === "g1t"} /> |
| 199 | )} |
| 200 | {who && who !== "g1t" ? ( |
| 201 | <UserCard username={who}> |
| 202 | <Link to={`/u/${who}`} className="font-medium hover:text-accent"> |
| 203 | {who} |
| 204 | </Link> |
| 205 | </UserCard> |
| 206 | ) : ( |
| 207 | <span className="font-medium">{who ?? "Someone"}</span> |
| 208 | )} |
| 209 | </span>{" "} |
| 210 | <span className="text-muted">{text}</span> |
| 211 | </span> |
| 212 | <span className="shrink-0 text-xs text-faint"> |
| 213 | <TimeAgo at={time} /> |
| 214 | </span> |
| 215 | </li> |
| 216 | ); |
| 217 | })} |
| 218 | </ul> |
| 219 | </Card> |
| 220 | )} |
| 221 | {next && ( |
| 222 | <div className="flex justify-center"> |
| 223 | <ButtonLink variant="outline" to={`?${branch ? `branch=${encodeURIComponent(branch)}&` : ""}before=${encodeURIComponent(next)}`}> |
| 224 | Older |
| 225 | </ButtonLink> |
| 226 | </div> |
| 227 | )} |
| 228 | </div> |
| 229 | ); |
| 230 | } |