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