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