g1t/apps/web/app/routes/workspace/overview.tsx

371 lines16,043 bytesCodeBlame
1import { ArrowRight, ArrowUpRight, Box, CircleDot, GitBranch, GitPullRequest, KeyRound, Lock, Plus, Rocket } from "lucide-react";
2import { Link } from "react-router";
3
4import type { 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 { planStatus, type UsageGlance, usageGlance } from "../../lib/billing";
12import { openedBy } from "../../lib/opened-by";
13import { billing, deployments, identity, projects as projectsApi, work } from "../../lib/services.server";
14import { getViewer, roleIn } from "../../lib/session.server";
15
16/** Projects whose open issues and pull requests are counted. */
17const MAX_COUNTED = 30;
18/** Projects whose pull requests are listed under "In progress". */
19const MAX_LISTED = 8;
20const MAX_PULLS = 8;
21const MAX_FACES = 8;
22/** The trial as published, when the price book cannot be read. */
23const DEFAULT_TRIAL_MICROS = 5_000_000;
24
25/**
26 * The month for the Usage card, for members only: what was spent, what
27 * pays first and what it went on. Fetched here, not with the sidebar, so
28 * only this page pays for it; a billing service that cannot answer leaves
29 * the card out rather than the page.
30 */
31async function usageFor(slug: string, viewer: User | null): Promise<UsageGlance | null> {
32 const now = new Date();
33 const monthStart = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), 1)).toISOString();
34 const [account, usage, features, entitlements, limit, book] = await Promise.all([
35 billing.account(slug, viewer).catch(() => null),
36 billing.usage(slug, viewer, monthStart).catch(() => null),
37 billing.features(slug, viewer).catch(() => null),
38 billing.entitlements(slug).catch(() => null),
39 billing.limit(slug, viewer).catch(() => null),
40 billing.prices().catch(() => null),
41 ]);
42 if (!account?.ok || !usage?.ok) return null;
43 // Without payments set up (a g1t run without billing), there is no plan to show.
44 if (!account.value.status.enabled && !usage.value.free) return null;
45 const plan = features?.ok ? (features.value.find((state) => state.plan.feature === "plan") ?? null) : null;
46 return usageGlance({
47 usage: usage.value,
48 status: planStatus(plan, entitlements),
49 entitlements,
50 limit: limit?.ok ? limit.value : null,
51 trialMicros: book?.free?.trialWorkspaceMicros ?? DEFAULT_TRIAL_MICROS,
52 });
53}
54
55export async function loader({ params, context }: Route.LoaderArgs) {
56 const viewer = getViewer(context);
57 const slug = params.owner.toLowerCase();
58 const role = roleIn(viewer, slug);
59 const [listed, members, deploys, usage] = await Promise.all([
60 projectsApi.list(slug, viewer),
61 role ? identity.listMembers(slug, viewer) : null,
62 role ? deployments.overview(slug, viewer) : null,
63 role ? usageFor(slug, viewer) : null,
64 ]);
65 const projects = listed.ok ? listed.value : [];
66
67 const pathOf = (project: Project) =>
68 project.source.kind === "hosted" ? project.source.repo : { namespace: project.workspace, name: project.slug };
69 const counted = projects.slice(0, MAX_COUNTED);
70 const counts = await Promise.all(counted.map((project) => work.counts(pathOf(project), viewer)));
71 const open: Record<string, { issues: number; pulls: number }> = {};
72 counted.forEach((project, i) => {
73 const result = counts[i];
74 if (result.ok) open[project.id] = result.value;
75 });
76
77 const busy = counted.filter((project) => (open[project.id]?.pulls ?? 0) > 0).slice(0, MAX_LISTED);
78 const lists = await Promise.all(busy.map((project) => work.listPulls(pathOf(project), viewer, "open")));
79 const pulls = lists
80 .flatMap((result, i) => (result.ok ? result.value.map((pull) => ({ pull, project: busy[i] })) : []))
81 .sort((a, b) => b.pull.updatedAt.localeCompare(a.pull.updatedAt))
82 .slice(0, MAX_PULLS);
83
84 const bySlug: Record<string, ProjectDeploys> = {};
85 for (const entry of deploys?.ok ? deploys.value : []) bySlug[entry.slug] = entry;
86
87 return {
88 slug,
89 role,
90 projects,
91 open,
92 deploys: bySlug,
93 pulls: pulls.map(({ pull, project }) => ({ pull, slug: project.slug, name: project.name })),
94 members: members?.ok ? members.value : null,
95 usage,
96 };
97}
98
99function Stat({ value, label }: { value: number; label: string }) {
100 return (
101 <div className="rounded-xl border border-line bg-surface px-4 py-3">
102 <p className="text-xl font-semibold tabular-nums tracking-tight">{value}</p>
103 <p className="text-xs text-muted">{label}</p>
104 </div>
105 );
106}
107
108/** What a member sees in a workspace with no projects yet. */
109function GetStarted({ slug }: { slug: string }) {
110 return (
111 <div className="rounded-2xl border border-line bg-surface p-8">
112 <span className="flex size-10 items-center justify-center rounded-xl bg-accent/10 text-accent ring-1 ring-accent/30">
113 <Box size={18} />
114 </span>
115 <h2 className="mt-4 text-lg font-semibold tracking-tight">Create your first project</h2>
116 <p className="mt-1.5 max-w-xl text-sm text-muted">
117 A project is what you build and run: its code, the issues that say what should change, the agents and
118 people who change it, and where it is deployed.
119 </p>
120 <div className="mt-6 flex flex-wrap gap-3">
121 <ButtonLink to={`/new?workspace=${slug}`} variant="accent">
122 <Plus size={14} />
123 New project
124 </ButtonLink>
125 </div>
126 <p className="mt-6 text-sm text-muted">Or push code you already have, and it becomes a project:</p>
127 <div className="mt-2 max-w-xl">
128 <CopyLine prompt text={`git push https://g1t.sh/${slug}/my-project.git main`} />
129 </div>
130 </div>
131 );
132}
133
134function ProjectCard({
135 project,
136 open,
137 deploys,
138 member,
139}: {
140 project: Project;
141 open: { issues: number; pulls: number } | undefined;
142 deploys: ProjectDeploys | undefined;
143 member: boolean;
144}) {
145 const base = `/${project.workspace}/${project.slug}`;
146 const production = deploys?.production ?? null;
147 const latest = deploys?.latest ?? null;
148 const source = project.source.kind === "hosted" ? project.source : null;
149 return (
150 <li className="group relative flex flex-col rounded-2xl border border-line bg-surface p-5 transition-colors hover:border-line-strong">
151 <div className="flex items-start gap-3">
152 <span className="flex size-9 shrink-0 items-center justify-center rounded-lg bg-raised text-muted ring-1 ring-line">
153 {project.private ? <Lock size={15} /> : <Box size={15} />}
154 </span>
155 <div className="min-w-0 grow">
156 <Link to={base} prefetch="intent" className="font-medium after:absolute after:inset-0 hover:underline">
157 {project.name}
158 </Link>
159 {production ? (
160 <a
161 href={production.url}
162 className="relative z-10 mt-0.5 flex items-center gap-1 truncate font-mono text-xs text-muted hover:text-accent"
163 >
164 {host(production.url)}
165 <ArrowUpRight size={11} className="shrink-0" />
166 </a>
167 ) : (
168 <p className="mt-0.5 truncate text-xs text-faint">
169 {project.description ?? (deploys?.enabled ? "Not deployed yet" : "Deployments are off")}
170 </p>
171 )}
172 </div>
173 {(project.private || project.archived) && (
174 <span className="flex shrink-0 gap-1.5">
175 {project.private && <Pill>private</Pill>}
176 {project.archived && <Pill>archived</Pill>}
177 </span>
178 )}
179 </div>
180
181 {project.description && production && <p className="mt-3 line-clamp-2 text-sm text-muted">{project.description}</p>}
182
183 <div className="mt-auto pt-5">
184 {member && latest && (
185 <p className="mb-3 flex items-center gap-2 text-xs text-muted">
186 <StatusDot status={latest.status} />
187 <span className="truncate">
188 {latest.kind === "production" ? "Production" : `Preview of ${latest.branch}`}
189 </span>
190 <span className="ml-auto shrink-0 text-faint">
191 <TimeAgo at={latest.createdAt} />
192 </span>
193 </p>
194 )}
195 <div className="flex items-center gap-4 border-t border-line pt-3 text-xs text-muted">
196 {source && (
197 <span className="flex min-w-0 items-center gap-1.5 font-mono">
198 <GitBranch size={12} className="shrink-0 text-faint" />
199 <span className="truncate">{source.defaultBranch}</span>
200 </span>
201 )}
202 <span className="flex items-center gap-1">
203 <CircleDot size={12} className="text-faint" />
204 {open?.issues ?? 0}
205 </span>
206 <span className="flex items-center gap-1">
207 <GitPullRequest size={12} className="text-faint" />
208 {open?.pulls ?? 0}
209 </span>
210 {member && deploys?.enabled && (deploys.previews ?? 0) > 0 && (
211 <span className="ml-auto flex items-center gap-1 text-accent">
212 <Rocket size={12} />
213 {deploys.previews} {deploys.previews === 1 ? "preview" : "previews"}
214 </span>
215 )}
216 </div>
217 </div>
218 </li>
219 );
220}
221
222export default function WorkspaceOverview({ loaderData }: Route.ComponentProps) {
223 const { slug, role, projects, open, deploys, pulls, members, usage } = loaderData;
224 const totals = Object.values(open).reduce(
225 (sum, counts) => ({ issues: sum.issues + counts.issues, pulls: sum.pulls + counts.pulls }),
226 { issues: 0, pulls: 0 },
227 );
228 const liveApps = Object.values(deploys).filter((entry) => entry.production).length;
229 return (
230 <div className="grid gap-10 lg:grid-cols-[1fr_18rem]">
231 <div className="min-w-0 space-y-10">
232 {projects.length === 0 && role ? (
233 <GetStarted slug={slug} />
234 ) : (
235 <>
236 <div className="grid grid-cols-2 gap-3 sm:grid-cols-4">
237 <Stat value={projects.length} label={projects.length === 1 ? "Project" : "Projects"} />
238 <Stat value={liveApps} label="In production" />
239 <Stat value={totals.issues} label="Open issues" />
240 <Stat value={totals.pulls} label="Pull requests in progress" />
241 </div>
242
243 <section>
244 <div className="flex items-center justify-between">
245 <h2 className="text-sm font-medium text-muted">Projects</h2>
246 {role && (
247 <Link to={`/new?workspace=${slug}`} className="inline-flex items-center gap-1 text-xs text-muted hover:text-fg">
248 <Plus size={13} />
249 New project
250 </Link>
251 )}
252 </div>
253 <ul className="mt-3 grid gap-4 sm:grid-cols-2">
254 {projects.map((project) => (
255 <ProjectCard
256 key={project.id}
257 project={project}
258 open={open[project.id]}
259 deploys={deploys[project.slug]}
260 member={role != null}
261 />
262 ))}
263 </ul>
264 </section>
265
266 {pulls.length > 0 && (
267 <section>
268 <h2 className="text-sm font-medium text-muted">In progress</h2>
269 <ul className="mt-3 divide-y divide-line overflow-hidden rounded-xl border border-line bg-surface">
270 {pulls.map(({ pull, slug: project, name }) => (
271 <li key={pull.id}>
272 <Link
273 prefetch="intent"
274 to={`/${slug}/${project}/pull/${pull.number}`}
275 className="flex items-center gap-3 px-4 py-3 transition-colors hover:bg-raised"
276 >
277 <PullIcon status={pull.status} />
278 <span className="min-w-0 grow">
279 <span className="block truncate font-medium">{pull.title}</span>
280 <span className="text-xs text-muted">
281 {name} <span className="font-mono">#{pull.number}</span>
282 {pull.issue != null && ` · for #${pull.issue}`} · {openedBy(pull).name}
283 </span>
284 </span>
285 <span className="hidden shrink-0 text-xs text-muted sm:block">
286 {pull.status === "draft" ? "Being worked on" : "Ready for review"}
287 </span>
288 <span className="w-14 shrink-0 text-right text-xs text-faint">
289 <TimeAgo at={pull.updatedAt} />
290 </span>
291 </Link>
292 </li>
293 ))}
294 </ul>
295 </section>
296 )}
297 </>
298 )}
299 </div>
300
301 <aside className="space-y-6">
302 {role && usage && <UsageCard slug={slug} glance={usage} owner={role === "owner"} />}
303
304 {members ? (
305 <section>
306 <div className="flex items-center justify-between">
307 <h2 className="text-sm font-medium">Members</h2>
308 <Link to={`/${slug}/-/people`} className="text-xs text-muted hover:text-fg">
309 {role === "owner" ? "Manage" : "See all"}
310 </Link>
311 </div>
312 <ul className="mt-3 space-y-1.5">
313 {members.slice(0, MAX_FACES).map((member) => (
314 <li key={member.username} className="flex items-center gap-2 text-sm">
315 <Avatar name={member.username} />
316 <span className="grow truncate font-mono">{member.username}</span>
317 {member.role === "owner" && <Pill>owner</Pill>}
318 </li>
319 ))}
320 </ul>
321 {members.length > MAX_FACES && <p className="mt-2 text-xs text-faint">and {members.length - MAX_FACES} more</p>}
322 </section>
323 ) : (
324 <p className="text-sm text-muted">
325 Members of this workspace build its projects. Anyone can open an issue or a pull request on a public one.
326 </p>
327 )}
328
329 {role && (
330 <section className="rounded-xl border border-line bg-surface p-5">
331 <h2 className="flex items-center gap-2 font-medium">
332 <Rocket size={15} className="text-faint" />
333 Deploy on g1t.page
334 </h2>
335 <p className="mt-1.5 text-sm text-muted">
336 Off until you turn them on for a project, under its Settings. Then production builds from its default
337 branch, with a live preview for every pull request. Apps cost nothing while no one visits.
338 </p>
339 <Link
340 prefetch="intent"
341 to={`/${slug}/-/billing`}
342 className="mt-3 inline-flex items-center gap-1 text-sm text-accent hover:underline"
343 >
344 Billing and plans <ArrowRight size={13} />
345 </Link>
346 </section>
347 )}
348
349 {role && (
350 <section className="rounded-xl border border-line bg-surface p-5">
351 <h2 className="flex items-center gap-2 font-medium">
352 <KeyRound size={15} className="text-faint" />
353 Automate without a service account
354 </h2>
355 <p className="mt-1.5 text-sm text-muted">
356 A workspace has access tokens of its own for CI, integrations and agents. They act as the workspace,
357 not as a person.
358 </p>
359 <Link
360 prefetch="intent"
361 to={`/${slug}/-/tokens`}
362 className="mt-3 inline-flex items-center gap-1 text-sm text-accent hover:underline"
363 >
364 Access tokens <ArrowRight size={13} />
365 </Link>
366 </section>
367 )}
368 </aside>
369 </div>
370 );
371}