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.

436 linesCodeBlameRaw
1import { ArrowRight, ArrowUpRight, Box, CircleDot, GitBranch, GitPullRequest, KeyRound, Lock, Pin, Plus, Rocket } from "lucide-react";
2import { Link, redirect } from "react-router";
3
4import type { PackageSummary, Project, ProjectDeploys } from "@g1t/contracts";
5
6import type { Route } from "./+types/overview";
7import { DeployLink, host, StatusDot } from "../../components/deploy";
8import { ButtonLink, EmptyState, TimeAgo } from "../../components/ui";
9import { Avatar } from "../../components/ui/avatar";
10import { Badge } from "../../components/ui/badge";
11import { Card } from "../../components/ui/card";
12import { UsageCard } from "../../components/usage-card";
13import { PullIcon } from "../../components/work-icons";
14import { openedBy } from "../../lib/opened-by";
15import { kindLabel, libraryPackages, packageLine, packagePath, primaryLink } from "../../lib/project-kind";
16import { PinButton } from "../../components/pin-button";
17import { sortProjects } from "../../lib/project-list";
18import { usageFor } from "../../lib/workspace-usage.server";
19import { deployments, identity, packages, projects as projectsApi, work } from "../../lib/services.server";
20import { getViewer, roleIn } from "../../lib/session.server";
21import { workspaceProjects } from "../../lib/workspace-projects.server";
22import { Unavailable } from "../../components/boundary";
23import { isFailed, part } from "../../lib/partial";
24
25/** Projects whose open work items and pull requests are counted: the most active. */
26const MAX_COUNTED = 30;
27/** Projects shown under Recently active, after the pins. */
28const MAX_ACTIVE = 6;
29/** Projects whose pull requests are listed under "In progress". */
30const MAX_LISTED = 8;
31const MAX_PULLS = 8;
32const MAX_FACES = 8;
33export async function loader({ params, context }: Route.LoaderArgs) {
34 const viewer = getViewer(context);
35 const slug = params.owner.toLowerCase();
36 const role = roleIn(viewer, slug);
37 // A member's way in is Home; this page
38 // is the workspace as a visitor sees it.
39 if (role) throw redirect(`/${slug}/-/home`);
40 const [listed, members, deploys, usage, published, shortcuts, items] = await Promise.all([
41 // The listing the sidebar and the Projects tab share.
42 workspaceProjects(slug, viewer),
43 role ? identity.listMembers(slug, viewer) : null,
44 role ? deployments.overview(slug, viewer) : null,
45 role ? usageFor(slug, viewer) : null,
46 // One listing for every card: a library's card shows its package where an app's shows production.
47 packages.list(slug, viewer).catch(() => null),
48 // The viewer's pins lead the page, as they lead the sidebar.
49 role ? projectsApi.shortcuts(slug, viewer).catch(() => null) : null,
50 // Its open work items the viewer may see: the workspace's, counted once whatever they are linked to.
51 part(work.listWorkItems(slug, viewer, { state: "open" }), "Open work items"),
52 ]);
53 const projects = listed.ok ? listed.value : [];
54 const pinnedIds = new Set((shortcuts?.pinned ?? []).map((project) => project.id));
55 const byId = new Map(projects.map((project) => [project.id, project]));
56 const pinned = (shortcuts?.pinned ?? []).map((pin) => byId.get(pin.id)).filter((project): project is Project => project != null);
57 // However many there are: the most active first, archived ones last.
58 const byActivity = sortProjects(
59 // Deploying does not order the list, so it is not read here.
60 projects.map((project) => ({ ...project, pushedAt: project.pushedAt ?? null, activity: project.activity ?? 0, deploying: false })),
61 "active",
62 )
63 .sort((a, b) => Number(a.archived) - Number(b.archived))
64 .map((listedProject) => byId.get(listedProject.id)!);
65 const active = byActivity.filter((project) => !pinnedIds.has(project.id)).slice(0, MAX_ACTIVE);
66
67 const pathOf = (project: Project) => (project.source.kind === "hosted" ? project.source.repo : { namespace: project.workspace, name: project.slug });
68 const counted = [...new Map([...pinned, ...active, ...byActivity].map((project) => [project.id, project])).values()].slice(
69 0,
70 Math.max(MAX_COUNTED, pinned.length + active.length),
71 );
72 // The projects are what the page is about. What is open in each, and the
73 // pull requests in progress, are beside them: a project whose counts
74 // could not be read shows none, and the page says the totals are short.
75 const counts = await Promise.all(counted.map((project) => part(work.counts(pathOf(project), viewer), "Open work items and pull requests")));
76 const open: Record<string, { items: number; pulls: number }> = {};
77 counted.forEach((project, i) => {
78 const result = counts[i];
79 if (!isFailed(result) && result.ok) open[project.id] = result.value;
80 });
81 const countsFailed = counts.find(isFailed) ?? null;
82
83 const busy = counted.filter((project) => (open[project.id]?.pulls ?? 0) > 0).slice(0, MAX_LISTED);
84 const lists = await Promise.all(busy.map((project) => part(work.listPulls(pathOf(project), viewer, "open"), "Pull requests in progress")));
85 const pullsFailed = lists.find(isFailed) ?? null;
86 const pulls = lists
87 .flatMap((result, i) => (!isFailed(result) && result.ok ? result.value.map((pull) => ({ pull, project: busy[i] })) : []))
88 .sort((a, b) => b.pull.updatedAt.localeCompare(a.pull.updatedAt))
89 .slice(0, MAX_PULLS);
90
91 const bySlug: Record<string, ProjectDeploys> = {};
92 for (const entry of deploys?.ok ? deploys.value : []) bySlug[entry.slug] = entry;
93
94 const byRepo = new Map<string, PackageSummary[]>();
95 for (const pkg of published?.ok ? published.value : []) {
96 if (pkg.repo) byRepo.set(pkg.repo.id, [...(byRepo.get(pkg.repo.id) ?? []), pkg]);
97 }
98 const packageOf: Record<string, PackageSummary> = {};
99 for (const project of [...pinned, ...active]) {
100 if ((project.kind !== "library" && project.kind !== "tool") || project.source.kind !== "hosted") continue;
101 const first = libraryPackages(byRepo.get(project.source.repoId) ?? [])[0];
102 if (first) packageOf[project.id] = first;
103 }
104
105 return {
106 slug,
107 role,
108 total: projects.length,
109 elsewhere: projects.filter((project) => project.runs === "elsewhere" && project.productionUrl).length,
110 pinned,
111 active,
112 open,
113 deploys: bySlug,
114 packageOf,
115 pulls: pulls.map(({ pull, project }) => ({ pull, slug: project.slug, name: project.name })),
116 countsFailed: countsFailed ?? (isFailed(items) ? items : null),
117 openItems: !isFailed(items) && items.ok ? items.value.length : 0,
118 pullsFailed,
119 members: members?.ok ? members.value : null,
120 usage,
121 };
122}
123
124function Stat({ value, label }: { value: number; label: string }) {
125 return (
126 <Card className="px-4 py-3">
127 <p className="text-xl font-semibold tabular-nums tracking-tight">{value}</p>
128 <p className="text-xs text-muted">{label}</p>
129 </Card>
130 );
131}
132
133/** What a member sees in a workspace with no projects yet. */
134function NoProjects({ slug }: { slug: string }) {
135 return (
136 <EmptyState title="No projects yet">
137 <ButtonLink to={`/new?workspace=${slug}`} variant="accent" className="mt-3">
138 <Plus size={14} />
139 New project
140 </ButtonLink>
141 </EmptyState>
142 );
143}
144
145function ProjectCard({
146 project,
147 open,
148 deploys,
149 pkg,
150 member,
151 pinned,
152}: {
153 project: Project;
154 open: { items: number; pulls: number } | undefined;
155 deploys: ProjectDeploys | undefined;
156 /** For a library, the package it publishes. */
157 pkg: PackageSummary | undefined;
158 member: boolean;
159 /** Whether the viewer pinned it; null when they cannot pin. */
160 pinned: boolean | null;
161}) {
162 // A library or a tool shows the package it publishes.
163 const library = project.kind === "library" || project.kind === "tool";
164 const base = `/${project.workspace}/${project.slug}`;
165 const production = deploys?.production ?? null;
166 // Production where g1t serves it or elsewhere, else its homepage or docs.
167 const link = primaryLink(project, production?.url);
168 const latest = deploys?.latest ?? null;
169 const source = project.source.kind === "hosted" ? project.source : null;
170 return (
171 <li className="group relative flex flex-col rounded-2xl border border-line bg-surface p-5 transition-colors hover:border-line-strong">
172 <div className="flex items-start gap-3">
173 <span className="flex size-9 shrink-0 items-center justify-center rounded-lg bg-raised text-muted ring-1 ring-line">
174 {project.private ? <Lock size={15} /> : <Box size={15} />}
175 </span>
176 <div className="min-w-0 grow">
177 <Link to={base} prefetch="intent" className="font-medium after:absolute after:inset-0 hover:underline">
178 {project.name}
179 </Link>
180 {link && !(library && pkg && !production) ? (
181 <DeployLink href={link} className="relative z-10 mt-0.5 flex items-center gap-1 truncate font-mono text-xs text-muted hover:text-accent">
182 {host(link)}
183 <ArrowUpRight size={11} className="shrink-0" />
184 </DeployLink>
185 ) : library && pkg ? (
186 <Link to={packagePath(pkg)} className="relative z-10 mt-0.5 block truncate font-mono text-xs text-muted hover:text-accent">
187 {packageLine(pkg)}
188 </Link>
189 ) : (
190 <p className="mt-0.5 truncate text-xs text-faint">
191 {project.description ??
192 (library ? "Not published yet" : project.runs === "g1t" ? (deploys?.enabled ? "Not deployed yet" : "Deployments are off") : kindLabel(project))}
193 </p>
194 )}
195 </div>
196 {(project.private || project.archived) && (
197 <span className="flex shrink-0 gap-1.5">
198 {project.private && <Badge size="md">private</Badge>}
199 {project.archived && <Badge size="md">archived</Badge>}
200 </span>
201 )}
202 {pinned != null && (
203 <PinButton workspace={project.workspace} slug={project.slug} name={project.name} pinned={pinned} compact className="-mt-1.5 -mr-2" />
204 )}
205 </div>
206
207 {project.description && (link || (library && pkg)) && <p className="mt-3 line-clamp-2 text-sm text-muted">{project.description}</p>}
208
209 <div className="mt-auto pt-5">
210 {member && latest && (
211 <p className="mb-3 flex items-center gap-2 text-xs text-muted">
212 <StatusDot status={latest.status} />
213 <span className="truncate">{latest.kind === "production" ? "Production" : `Preview of ${latest.branch}`}</span>
214 <span className="ml-auto shrink-0 text-faint">
215 <TimeAgo at={latest.createdAt} />
216 </span>
217 </p>
218 )}
219 <div className="flex items-center gap-4 border-t border-line pt-3 text-xs text-muted">
220 {source && (
221 <span className="flex min-w-0 items-center gap-1.5 font-mono">
222 <GitBranch size={12} className="shrink-0 text-faint" />
223 <span className="truncate">{source.defaultBranch}</span>
224 </span>
225 )}
226 <span className="flex items-center gap-1">
227 <CircleDot size={12} className="text-faint" />
228 {open?.items ?? 0}
229 </span>
230 <span className="flex items-center gap-1">
231 <GitPullRequest size={12} className="text-faint" />
232 {open?.pulls ?? 0}
233 </span>
234 {member && deploys?.enabled && (deploys.previews ?? 0) > 0 && (
235 <span className="ml-auto flex items-center gap-1 text-success">
236 <Rocket size={12} />
237 {deploys.previews} {deploys.previews === 1 ? "preview" : "previews"}
238 </span>
239 )}
240 </div>
241 </div>
242 </li>
243 );
244}
245
246export default function WorkspaceOverview({ loaderData }: Route.ComponentProps) {
247 const { slug, role, total, pinned, active, open, deploys, pulls, members, usage } = loaderData;
248 const card = (project: Project, isPinned: boolean) => (
249 <ProjectCard
250 key={project.id}
251 project={project}
252 open={open[project.id]}
253 deploys={deploys[project.slug]}
254 pkg={loaderData.packageOf[project.id]}
255 member={role != null}
256 pinned={role ? isPinned : null}
257 />
258 );
259 const totals = Object.values(open).reduce((sum, counts) => ({ pulls: sum.pulls + counts.pulls }), { pulls: 0 });
260 // Live on g1t, or deployed elsewhere with an address.
261 const liveApps = Object.values(deploys).filter((entry) => entry.production).length + loaderData.elsewhere;
262 return (
263 <div className="grid gap-10 lg:grid-cols-[1fr_18rem]">
264 <div className="min-w-0 space-y-10">
265 {total === 0 && role ? (
266 <NoProjects slug={slug} />
267 ) : (
268 <>
269 <div className="grid grid-cols-2 gap-3 sm:grid-cols-4">
270 <Stat value={total} label={total === 1 ? "Project" : "Projects"} />
271 <Stat value={liveApps} label="In production" />
272 <Stat value={loaderData.openItems} label="Open work items" />
273 <Stat value={totals.pulls} label="Pull requests in progress" />
274 </div>
275 {loaderData.countsFailed && <Unavailable what={loaderData.countsFailed.what} compact />}
276
277 {role && (
278 <section aria-labelledby="pinned">
279 <h2 id="pinned" className="text-sm font-medium text-muted">
280 Pinned
281 </h2>
282 {pinned.length > 0 ? (
283 <ul className="mt-3 grid gap-4 sm:grid-cols-2">{pinned.map((project) => card(project, true))}</ul>
284 ) : (
285 <Card asChild tone="plain" className="mt-3 flex items-center gap-2 border-dashed px-4 py-3 text-sm text-muted">
286 <p>
287 <Pin size={14} className="shrink-0 text-faint" />
288 Pin the projects you use most, from their page or from Projects, to keep them here and in your sidebar.
289 </p>
290 </Card>
291 )}
292 </section>
293 )}
294
295 {active.length > 0 && (
296 <section aria-labelledby="active">
297 <div className="flex items-center justify-between">
298 <h2 id="active" className="text-sm font-medium text-muted">
299 {role ? "Recently active" : "Projects"}
300 </h2>
301 {role && (
302 <Link to={`/new?workspace=${slug}`} className="inline-flex items-center gap-1 text-xs text-muted hover:text-fg">
303 <Plus size={13} />
304 New project
305 </Link>
306 )}
307 </div>
308 <ul className="mt-3 grid gap-4 sm:grid-cols-2">{active.map((project) => card(project, false))}</ul>
309 </section>
310 )}
311
312 <Link
313 to={`/${slug}/-/projects`}
314 prefetch="intent"
315 className="flex items-center justify-between rounded-xl border border-line bg-surface px-4 py-3 text-sm transition-colors hover:border-line-strong"
316 >
317 <span>
318 All projects <span className="tabular-nums text-muted">({total})</span>
319 </span>
320 <span className="flex items-center gap-1 text-xs text-muted">
321 Search, filter and sort
322 <ArrowRight size={13} />
323 </span>
324 </Link>
325
326 {loaderData.pullsFailed && pulls.length === 0 && (
327 <section>
328 <h2 className="text-sm font-medium text-muted">In progress</h2>
329 <div className="mt-3">
330 <Unavailable what={loaderData.pullsFailed.what} />
331 </div>
332 </section>
333 )}
334 {pulls.length > 0 && (
335 <section>
336 <h2 className="text-sm font-medium text-muted">In progress</h2>
337 <Card asChild divided className="mt-3 overflow-hidden">
338 <ul>
339 {pulls.map(({ pull, slug: project, name }) => (
340 <li key={pull.id}>
341 <Link
342 prefetch="intent"
343 to={`/${slug}/${project}/pull/${pull.number}`}
344 className="flex items-center gap-3 px-4 py-3 transition-colors hover:bg-raised"
345 >
346 <PullIcon status={pull.status} />
347 <span className="min-w-0 grow">
348 <span className="block truncate font-medium">{pull.title}</span>
349 <span className="text-xs text-muted">
350 {name} <span className="font-mono">#{pull.number}</span>
351 {pull.item != null && ` · for #${pull.item}`} · {openedBy(pull).name}
352 </span>
353 </span>
354 <span className="hidden shrink-0 text-xs text-muted sm:block">
355 {pull.status === "draft" ? "Being worked on" : "Ready for review"}
356 </span>
357 <span className="w-14 shrink-0 text-right text-xs text-faint">
358 <TimeAgo at={pull.updatedAt} />
359 </span>
360 </Link>
361 </li>
362 ))}
363 </ul>
364 </Card>
365 </section>
366 )}
367 </>
368 )}
369 </div>
370
371 <aside className="space-y-6">
372 {role && usage && <UsageCard slug={slug} glance={usage} owner={role === "owner"} />}
373
374 {members ? (
375 <section>
376 <div className="flex items-center justify-between">
377 <h2 className="text-sm font-medium">Members</h2>
378 <Link to={role === "owner" ? `/${slug}/-/members` : `/${slug}/-/people`} className="text-xs text-muted hover:text-fg">
379 {role === "owner" ? "Manage" : "See all"}
380 </Link>
381 </div>
382 <ul className="mt-3 space-y-1.5">
383 {members.slice(0, MAX_FACES).map((member) => (
384 <li key={member.username} className="flex items-center gap-2 text-sm">
385 <Avatar name={member.username} />
386 <span className="grow truncate font-mono">{member.username}</span>
387 {member.role === "owner" && <Badge size="md">owner</Badge>}
388 </li>
389 ))}
390 </ul>
391 {members.length > MAX_FACES && <p className="mt-2 text-xs text-faint">and {members.length - MAX_FACES} more</p>}
392 </section>
393 ) : (
394 <p className="text-sm text-muted">
395 Members of this workspace build its projects. Anyone can file a work item or open a pull request on a public one.
396 </p>
397 )}
398
399 {role && (
400 <Card asChild className="p-5">
401 <section>
402 <h2 className="flex items-center gap-2 font-medium">
403 <Rocket size={15} className="text-faint" />
404 Deploy on g1t.page
405 </h2>
406 <p className="mt-1.5 text-sm text-muted">
407 Off until you turn them on for a project, under its Settings. Then production builds from its default branch, with a live preview for every pull
408 request. Apps cost nothing while no one visits.
409 </p>
410 <Link prefetch="intent" to={`/${slug}/-/billing`} className="mt-3 inline-flex items-center gap-1 text-sm text-accent hover:underline">
411 Billing and plans <ArrowRight size={13} />
412 </Link>
413 </section>
414 </Card>
415 )}
416
417 {role && (
418 <Card asChild className="p-5">
419 <section>
420 <h2 className="flex items-center gap-2 font-medium">
421 <KeyRound size={15} className="text-faint" />
422 Automate without a service account
423 </h2>
424 <p className="mt-1.5 text-sm text-muted">
425 A workspace has access tokens of its own for CI, integrations and agents. They act as the workspace, not as a person.
426 </p>
427 <Link prefetch="intent" to={`/${slug}/-/tokens`} className="mt-3 inline-flex items-center gap-1 text-sm text-accent hover:underline">
428 Access tokens <ArrowRight size={13} />
429 </Link>
430 </section>
431 </Card>
432 )}
433 </aside>
434 </div>
435 );
436}