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

354 lines15,258 bytesCodeBlame
1import { BookOpen, Box, Trash2 } from "lucide-react";
2import { Form, Link, data, redirect, useNavigation } from "react-router";
3
4import { ECOSYSTEMS, type Ecosystem, type PackageVersion } from "@g1t/contracts";
5
6import type { Route } from "./+types/package";
7import { Markdown } from "../../components/markdown";
8import { PackageIcon } from "../../components/package-icon";
9import { ConfirmDialog } from "../../components/repo-lifecycle";
10import { Button, CopyLine, ErrorText, TimeAgo } from "../../components/ui";
11import { Badge } from "../../components/ui/badge";
12import { page } from "../../lib/meta";
13import { ECOSYSTEM_LABEL, formatBytes, installCommands, shortDigest } from "../../lib/packages";
14import { packages } from "../../lib/services.server";
15import { assertSameOrigin, getViewer, requireUser } from "../../lib/session.server";
16
17function ecosystemOf(value: string | undefined): Ecosystem {
18 if (value && (ECOSYSTEMS as readonly string[]).includes(value)) return value as Ecosystem;
19 throw data(null, { status: 404 });
20}
21
22export function meta({ params, ...args }: Route.MetaArgs) {
23 return page(args, { title: `${params["*"]} · Packages · ${params.owner} · g1t` });
24}
25
26export async function loader({ params, context }: Route.LoaderArgs) {
27 const viewer = getViewer(context);
28 const ecosystem = ecosystemOf(params.ecosystem);
29 const name = params["*"] ?? "";
30 const found = await packages.get(params.owner, ecosystem, name, viewer);
31 // Not found and not allowed look the same.
32 if (!found.ok) throw data(null, { status: 404 });
33 return { detail: found.value, username: viewer?.username ?? "you" };
34}
35
36type Outcome = { error: string | null; message: string | null };
37
38export async function action({ request, params, context }: Route.ActionArgs): Promise<Outcome | Response> {
39 assertSameOrigin(request);
40 const user = requireUser(context, request);
41 const ecosystem = ecosystemOf(params.ecosystem);
42 const name = params["*"] ?? "";
43 const form = await request.formData();
44 const intent = String(form.get("intent") ?? "");
45 const fail = (message: string): Outcome => ({ error: message, message: null });
46 if (intent === "delete-version") {
47 const version = String(form.get("version") ?? "");
48 const done = await packages.deleteVersion(user, params.owner, ecosystem, name, version, "web");
49 return done.ok ? { error: null, message: `Deleted ${shortDigest(version)} and its tags.` } : fail(done.error.message);
50 }
51 if (intent === "delete-package") {
52 const done = await packages.deletePackage(user, params.owner, ecosystem, name, "web");
53 if (!done.ok) return fail(done.error.message);
54 return redirect(`/${params.owner}/-/packages`);
55 }
56 if (intent === "visibility") {
57 const visibility = form.get("visibility") === "public" ? "public" : "private";
58 const done = await packages.set(user, params.owner, ecosystem, name, { visibility }, "web");
59 return done.ok ? { error: null, message: `It is ${visibility} now.` } : fail(done.error.message);
60 }
61 if (intent === "link") {
62 const repo = String(form.get("repo") ?? "").trim();
63 const done = repo
64 ? await packages.set(user, params.owner, ecosystem, name, { link: repo }, "web")
65 : await packages.set(user, params.owner, ecosystem, name, { unlink: true }, "web");
66 return done.ok ? { error: null, message: repo ? `Linked to ${repo}.` : "Unlinked." } : fail(done.error.message);
67 }
68 return fail("That is not something this page does.");
69}
70
71export default function PackagePage({ loaderData, actionData }: Route.ComponentProps) {
72 const { detail, username } = loaderData;
73 const { package: pkg, versions, tags, permissions } = detail;
74 const outcome = actionData as Outcome | undefined;
75 const latest = tags.find((tag) => tag.tag === "latest")?.tag ?? tags[0]?.tag ?? null;
76 const commands = installCommands(pkg, latest, username);
77 // npm and Composer versions are numbers; images are digests and tags.
78 const npm = pkg.ecosystem !== "container";
79 // Composer's versions are the repository's tags and branches: they
80 // change in git, not here.
81 const fromGit = pkg.ecosystem === "composer";
82 // Signatures and attestations hang off the images they describe.
83 const images = versions.filter((version) => !version.subject);
84 const attached = (digest: string) => versions.filter((version) => version.subject === digest);
85 return (
86 <div className="space-y-8">
87 <header className="space-y-3">
88 <Link to={`/${pkg.workspace}/-/packages`} className="text-sm text-muted hover:text-fg">
89 Packages
90 </Link>
91 <div className="flex flex-wrap items-center gap-3">
92 <PackageIcon ecosystem={pkg.ecosystem} size={32} />
93 <h1 className="text-2xl font-semibold tracking-tight">{pkg.name}</h1>
94 <Badge>{ECOSYSTEM_LABEL[pkg.ecosystem]}</Badge>
95 <Badge>{pkg.visibility === "private" ? "Private" : "Public"}</Badge>
96 </div>
97 <p className="flex flex-wrap items-center gap-x-4 gap-y-1 text-sm text-muted tabular-nums">
98 {pkg.repo && (
99 <Link to={`/${pkg.repo.namespace}/${pkg.repo.name}`} className="inline-flex items-center gap-1 hover:text-fg">
100 <Box size={13} />
101 {pkg.repo.namespace}/{pkg.repo.name}
102 </Link>
103 )}
104 <span>{formatBytes(pkg.size)}</span>
105 <span>
106 {pkg.downloads.toLocaleString("en-US")}{" "}
107 {npm ? (pkg.downloads === 1 ? "download" : "downloads") : pkg.downloads === 1 ? "pull" : "pulls"}
108 </span>
109 <span>
110 Updated <TimeAgo at={pkg.updated_at} />
111 </span>
112 </p>
113 {pkg.description && <p className="max-w-2xl text-sm text-fg-soft">{pkg.description}</p>}
114 </header>
115
116 {outcome?.error && <ErrorText>{outcome.error}</ErrorText>}
117 {outcome?.message && <p className="text-sm text-accent">{outcome.message}</p>}
118
119 <section className="space-y-2">
120 <h2 className="text-sm font-semibold">{npm ? "Install it" : "Pull it"}</h2>
121 {commands.registry && <CopyLine prompt text={commands.registry} />}
122 {pkg.visibility === "private" && <CopyLine prompt text={commands.login} />}
123 <CopyLine prompt text={commands.install} />
124 {commands.registry && pkg.visibility === "private" && pkg.ecosystem === "cargo" && (
125 <p className="text-xs text-faint">
126 <code className="font-mono">cargo login</code> asks for an{" "}
127 <Link to="/settings/tokens" className="text-muted hover:text-fg">
128 access token
129 </Link>{" "}
130 with <code className="font-mono">packages:read</code>.
131 </p>
132 )}
133 {commands.registry && pkg.visibility === "private" && pkg.ecosystem !== "cargo" && (
134 <p className="text-xs text-faint">
135 Put an{" "}
136 <Link to="/settings/tokens" className="text-muted hover:text-fg">
137 access token
138 </Link>{" "}
139 with <code className="font-mono">packages:read</code> in place of YOUR_TOKEN.
140 </p>
141 )}
142 </section>
143
144 {detail.readme && (
145 <section className="overflow-hidden rounded-xl border border-line">
146 <h2 className="flex items-center gap-2 border-b border-line bg-surface px-4 py-2.5 text-sm font-medium">
147 <BookOpen size={15} className="text-faint" />
148 README
149 </h2>
150 <div className="p-6">
151 <Markdown
152 source={detail.readme}
153 repo={pkg.repo ? { namespace: pkg.repo.namespace, name: pkg.repo.name } : undefined}
154 />
155 </div>
156 </section>
157 )}
158
159 <section className="space-y-3">
160 <h2 className="text-sm font-semibold">
161 Versions <span className="font-normal text-faint">{images.length}</span>
162 </h2>
163 {fromGit && (
164 <p className="text-xs text-faint">
165 Each tag of {pkg.repo ? `${pkg.repo.namespace}/${pkg.repo.name}` : "its repository"} that reads as a version, and each
166 branch as <code className="font-mono">dev-</code>, from its composer.json. Push a tag to publish one; delete it to take it away.
167 </p>
168 )}
169 {images.length === 0 ? (
170 <p className="text-sm text-muted">No versions are left.</p>
171 ) : (
172 <ul className="divide-y divide-line overflow-hidden rounded-xl border border-line bg-surface">
173 {images.map((version) => (
174 <VersionRow
175 key={version.id}
176 version={version}
177 attached={attached(version.digest)}
178 canDelete={permissions.delete && !fromGit}
179 npm={npm}
180 />
181 ))}
182 </ul>
183 )}
184 </section>
185
186 {permissions.admin && <Settings detail={detail} />}
187 </div>
188 );
189}
190
191function VersionRow({
192 version,
193 attached,
194 canDelete,
195 npm,
196}: {
197 version: PackageVersion;
198 attached: PackageVersion[];
199 canDelete: boolean;
200 npm: boolean;
201}) {
202 return (
203 <li className="flex flex-wrap items-start gap-x-4 gap-y-2 px-4 py-3">
204 <div className="min-w-0 grow space-y-1">
205 <div className="flex flex-wrap items-center gap-1.5">
206 {npm && <span className="font-mono text-sm font-medium">{version.version}</span>}
207 {npm ? (
208 version.tags.map((tag) => (
209 <Badge key={tag} tone={tag === "latest" ? "accent" : "neutral"} className="font-mono">
210 {tag}
211 </Badge>
212 ))
213 ) : version.tags.length > 0 ? (
214 version.tags.map((tag) => (
215 <Badge key={tag} tone={tag === "latest" ? "accent" : "neutral"} className="font-mono">
216 {tag}
217 </Badge>
218 ))
219 ) : (
220 <span className="text-xs text-faint">Untagged</span>
221 )}
222 {!npm && (
223 <code className="font-mono text-xs text-muted" title={version.digest}>
224 {shortDigest(version.digest)}
225 </code>
226 )}
227 {version.deprecated && (
228 <Badge tone="neutral" title={version.deprecated}>
229 Deprecated
230 </Badge>
231 )}
232 </div>
233 {version.deprecated && <p className="text-xs text-muted">{version.deprecated}</p>}
234 <p className="flex flex-wrap gap-x-3 text-xs text-faint tabular-nums">
235 <span>{formatBytes(version.size)}</span>
236 {version.platforms.length > 0 && <span>{version.platforms.join(", ")}</span>}
237 {attached.length > 0 && (
238 <span title={attached.map((a) => a.artifact_type ?? a.media_type ?? "artifact").join(", ")}>
239 {attached.length} attached ({attached.map((a) => artifactWord(a)).join(", ")})
240 </span>
241 )}
242 <span>
243 {version.published_by ? `${version.published_by} · ` : ""}
244 <TimeAgo at={version.published_at} />
245 </span>
246 </p>
247 </div>
248 {canDelete && (
249 <ConfirmDialog
250 intent="delete-version"
251 fields={{ version: npm ? version.version : version.digest }}
252 title={`Delete ${npm ? version.version : (version.tags[0] ?? shortDigest(version.digest))}?`}
253 description={
254 npm
255 ? "Anyone installing this version gets an error from then on."
256 : "Anyone pulling it by this tag or digest gets an error from then on."
257 }
258 submit="Delete version"
259 busy="Deleting…"
260 trigger={(open) => (
261 <button type="button" onClick={open} aria-label="Delete version" className="rounded-md p-1.5 text-faint hover:bg-raised hover:text-danger">
262 <Trash2 size={14} />
263 </button>
264 )}
265 >
266 <li>Its tags go with it.</li>
267 <li>Files no other version uses are removed within a day.</li>
268 </ConfirmDialog>
269 )}
270 </li>
271 );
272}
273
274/** "signature", "SBOM", "attestation", or what the artifact says it is. */
275function artifactWord(version: PackageVersion): string {
276 const type = version.artifact_type ?? version.media_type ?? "";
277 if (/signature|cosign|notary/i.test(type)) return "signature";
278 if (/spdx|cyclonedx|sbom/i.test(type)) return "SBOM";
279 if (/in-toto|attestation|provenance/i.test(type)) return "attestation";
280 return "artifact";
281}
282
283function Settings({ detail }: { detail: Route.ComponentProps["loaderData"]["detail"] }) {
284 const { package: pkg } = detail;
285 const busy = useNavigation().state !== "idle";
286 return (
287 <section className="space-y-4 rounded-xl border border-line bg-surface p-5">
288 <h2 className="text-sm font-semibold">Settings</h2>
289 {pkg.repo ? (
290 <p className="text-sm text-muted">
291 Linked to{" "}
292 <Link to={`/${pkg.repo.namespace}/${pkg.repo.name}`} className="text-fg-soft hover:text-fg">
293 {pkg.repo.namespace}/{pkg.repo.name}
294 </Link>
295 : it has the repository's visibility and roles.
296 </p>
297 ) : (
298 <Form method="post" className="flex flex-wrap items-center gap-2 text-sm">
299 <input type="hidden" name="intent" value="visibility" />
300 <span className="text-muted">Visibility</span>
301 <select
302 name="visibility"
303 defaultValue={pkg.visibility}
304 className="h-8 rounded-md border border-line bg-bg px-2 text-sm"
305 aria-label="Visibility"
306 >
307 <option value="private">Private: workspace members</option>
308 <option value="public">Public: anyone can pull</option>
309 </select>
310 <Button type="submit" variant="quiet" disabled={busy}>
311 Save
312 </Button>
313 </Form>
314 )}
315 <Form method="post" className="flex flex-wrap items-center gap-2 text-sm">
316 <input type="hidden" name="intent" value="link" />
317 <label className="text-muted" htmlFor="link-repo">
318 Repository
319 </label>
320 <input
321 id="link-repo"
322 name="repo"
323 defaultValue={pkg.repo?.name ?? ""}
324 placeholder="none"
325 className="h-8 w-56 rounded-md border border-line bg-bg px-2 font-mono text-sm placeholder:text-faint"
326 />
327 <Button type="submit" variant="quiet" disabled={busy}>
328 {pkg.repo ? "Change link" : "Link"}
329 </Button>
330 <span className="text-xs text-faint">Empty to unlink. A linked package takes the repository's access.</span>
331 </Form>
332 {detail.permissions.delete && (
333 <div className="border-t border-line pt-4">
334 <ConfirmDialog
335 intent="delete-package"
336 title={`Delete ${pkg.name}?`}
337 description="Every version and tag goes, and anyone pulling it gets an error."
338 confirm={pkg.name}
339 submit="Delete package"
340 busy="Deleting…"
341 trigger={(open) => (
342 <Button type="button" variant="danger" onClick={open}>
343 Delete package
344 </Button>
345 )}
346 >
347 <li>{detail.versions.length} versions and their tags.</li>
348 <li>The name can be pushed again afterwards.</li>
349 </ConfirmDialog>
350 </div>
351 )}
352 </section>
353 );
354}