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

345 lines14,792 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" && (
125 <p className="text-xs text-faint">
126 Put 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> in place of YOUR_TOKEN.
131 </p>
132 )}
133 </section>
134
135 {detail.readme && (
136 <section className="overflow-hidden rounded-xl border border-line">
137 <h2 className="flex items-center gap-2 border-b border-line bg-surface px-4 py-2.5 text-sm font-medium">
138 <BookOpen size={15} className="text-faint" />
139 README
140 </h2>
141 <div className="p-6">
142 <Markdown
143 source={detail.readme}
144 repo={pkg.repo ? { namespace: pkg.repo.namespace, name: pkg.repo.name } : undefined}
145 />
146 </div>
147 </section>
148 )}
149
150 <section className="space-y-3">
151 <h2 className="text-sm font-semibold">
152 Versions <span className="font-normal text-faint">{images.length}</span>
153 </h2>
154 {fromGit && (
155 <p className="text-xs text-faint">
156 Each tag of {pkg.repo ? `${pkg.repo.namespace}/${pkg.repo.name}` : "its repository"} that reads as a version, and each
157 branch as <code className="font-mono">dev-</code>, from its composer.json. Push a tag to publish one; delete it to take it away.
158 </p>
159 )}
160 {images.length === 0 ? (
161 <p className="text-sm text-muted">No versions are left.</p>
162 ) : (
163 <ul className="divide-y divide-line overflow-hidden rounded-xl border border-line bg-surface">
164 {images.map((version) => (
165 <VersionRow
166 key={version.id}
167 version={version}
168 attached={attached(version.digest)}
169 canDelete={permissions.delete && !fromGit}
170 npm={npm}
171 />
172 ))}
173 </ul>
174 )}
175 </section>
176
177 {permissions.admin && <Settings detail={detail} />}
178 </div>
179 );
180}
181
182function VersionRow({
183 version,
184 attached,
185 canDelete,
186 npm,
187}: {
188 version: PackageVersion;
189 attached: PackageVersion[];
190 canDelete: boolean;
191 npm: boolean;
192}) {
193 return (
194 <li className="flex flex-wrap items-start gap-x-4 gap-y-2 px-4 py-3">
195 <div className="min-w-0 grow space-y-1">
196 <div className="flex flex-wrap items-center gap-1.5">
197 {npm && <span className="font-mono text-sm font-medium">{version.version}</span>}
198 {npm ? (
199 version.tags.map((tag) => (
200 <Badge key={tag} tone={tag === "latest" ? "accent" : "neutral"} className="font-mono">
201 {tag}
202 </Badge>
203 ))
204 ) : version.tags.length > 0 ? (
205 version.tags.map((tag) => (
206 <Badge key={tag} tone={tag === "latest" ? "accent" : "neutral"} className="font-mono">
207 {tag}
208 </Badge>
209 ))
210 ) : (
211 <span className="text-xs text-faint">Untagged</span>
212 )}
213 {!npm && (
214 <code className="font-mono text-xs text-muted" title={version.digest}>
215 {shortDigest(version.digest)}
216 </code>
217 )}
218 {version.deprecated && (
219 <Badge tone="neutral" title={version.deprecated}>
220 Deprecated
221 </Badge>
222 )}
223 </div>
224 {version.deprecated && <p className="text-xs text-muted">{version.deprecated}</p>}
225 <p className="flex flex-wrap gap-x-3 text-xs text-faint tabular-nums">
226 <span>{formatBytes(version.size)}</span>
227 {version.platforms.length > 0 && <span>{version.platforms.join(", ")}</span>}
228 {attached.length > 0 && (
229 <span title={attached.map((a) => a.artifact_type ?? a.media_type ?? "artifact").join(", ")}>
230 {attached.length} attached ({attached.map((a) => artifactWord(a)).join(", ")})
231 </span>
232 )}
233 <span>
234 {version.published_by ? `${version.published_by} · ` : ""}
235 <TimeAgo at={version.published_at} />
236 </span>
237 </p>
238 </div>
239 {canDelete && (
240 <ConfirmDialog
241 intent="delete-version"
242 fields={{ version: npm ? version.version : version.digest }}
243 title={`Delete ${npm ? version.version : (version.tags[0] ?? shortDigest(version.digest))}?`}
244 description={
245 npm
246 ? "Anyone installing this version gets an error from then on."
247 : "Anyone pulling it by this tag or digest gets an error from then on."
248 }
249 submit="Delete version"
250 busy="Deleting…"
251 trigger={(open) => (
252 <button type="button" onClick={open} aria-label="Delete version" className="rounded-md p-1.5 text-faint hover:bg-raised hover:text-danger">
253 <Trash2 size={14} />
254 </button>
255 )}
256 >
257 <li>Its tags go with it.</li>
258 <li>Files no other version uses are removed within a day.</li>
259 </ConfirmDialog>
260 )}
261 </li>
262 );
263}
264
265/** "signature", "SBOM", "attestation", or what the artifact says it is. */
266function artifactWord(version: PackageVersion): string {
267 const type = version.artifact_type ?? version.media_type ?? "";
268 if (/signature|cosign|notary/i.test(type)) return "signature";
269 if (/spdx|cyclonedx|sbom/i.test(type)) return "SBOM";
270 if (/in-toto|attestation|provenance/i.test(type)) return "attestation";
271 return "artifact";
272}
273
274function Settings({ detail }: { detail: Route.ComponentProps["loaderData"]["detail"] }) {
275 const { package: pkg } = detail;
276 const busy = useNavigation().state !== "idle";
277 return (
278 <section className="space-y-4 rounded-xl border border-line bg-surface p-5">
279 <h2 className="text-sm font-semibold">Settings</h2>
280 {pkg.repo ? (
281 <p className="text-sm text-muted">
282 Linked to{" "}
283 <Link to={`/${pkg.repo.namespace}/${pkg.repo.name}`} className="text-fg-soft hover:text-fg">
284 {pkg.repo.namespace}/{pkg.repo.name}
285 </Link>
286 : it has the repository's visibility and roles.
287 </p>
288 ) : (
289 <Form method="post" className="flex flex-wrap items-center gap-2 text-sm">
290 <input type="hidden" name="intent" value="visibility" />
291 <span className="text-muted">Visibility</span>
292 <select
293 name="visibility"
294 defaultValue={pkg.visibility}
295 className="h-8 rounded-md border border-line bg-bg px-2 text-sm"
296 aria-label="Visibility"
297 >
298 <option value="private">Private: workspace members</option>
299 <option value="public">Public: anyone can pull</option>
300 </select>
301 <Button type="submit" variant="quiet" disabled={busy}>
302 Save
303 </Button>
304 </Form>
305 )}
306 <Form method="post" className="flex flex-wrap items-center gap-2 text-sm">
307 <input type="hidden" name="intent" value="link" />
308 <label className="text-muted" htmlFor="link-repo">
309 Repository
310 </label>
311 <input
312 id="link-repo"
313 name="repo"
314 defaultValue={pkg.repo?.name ?? ""}
315 placeholder="none"
316 className="h-8 w-56 rounded-md border border-line bg-bg px-2 font-mono text-sm placeholder:text-faint"
317 />
318 <Button type="submit" variant="quiet" disabled={busy}>
319 {pkg.repo ? "Change link" : "Link"}
320 </Button>
321 <span className="text-xs text-faint">Empty to unlink. A linked package takes the repository's access.</span>
322 </Form>
323 {detail.permissions.delete && (
324 <div className="border-t border-line pt-4">
325 <ConfirmDialog
326 intent="delete-package"
327 title={`Delete ${pkg.name}?`}
328 description="Every version and tag goes, and anyone pulling it gets an error."
329 confirm={pkg.name}
330 submit="Delete package"
331 busy="Deleting…"
332 trigger={(open) => (
333 <Button type="button" variant="danger" onClick={open}>
334 Delete package
335 </Button>
336 )}
337 >
338 <li>{detail.versions.length} versions and their tags.</li>
339 <li>The name can be pushed again afterwards.</li>
340 </ConfirmDialog>
341 </div>
342 )}
343 </section>
344 );
345}