91 lines · 4.4 KBRaw
| 1 | import { Ago, Banner, Button, Empty, LayerCard, LineBadge, Select } from "@refs/design-system"; |
| 2 | import { ListChecksIcon, PlayIcon } from "@refs/design-system/icons"; |
| 3 | import { Form, Link, redirect, useNavigation } from "react-router"; |
| 4 | import type { Route } from "./+types/repo-runs"; |
| 5 | import { CiStatusIcon, CI_STATE_LABEL, duration, triggerText } from "../components/ci-status"; |
| 6 | import { failed, formText, found, fullName, nowSeconds, refs } from "../lib/session"; |
| 7 | |
| 8 | export const meta: Route.MetaFunction = ({ params }) => [{ title: `Runs · ${fullName(params)} · refs` }]; |
| 9 | |
| 10 | export async function loader({ params, context }: Route.LoaderArgs) { |
| 11 | const repo = await found(refs(context).repo(fullName(params))); |
| 12 | const ci = repo.ci(); |
| 13 | const [runs, branches] = await Promise.all([ci.runs(), ci.canRun ? repo.refs().then((list) => ({ all: list.branches, main: list.defaultRef })).catch(() => null) : null]); |
| 14 | return { repo: repo.summary.name, runs, branches, canRun: ci.canRun, now: nowSeconds() }; |
| 15 | } |
| 16 | |
| 17 | export async function action({ request, params, context }: Route.ActionArgs) { |
| 18 | const repo = await found(refs(context).repo(fullName(params))); |
| 19 | const form = await request.formData(); |
| 20 | const outcome = await repo.ci().start(formText(form, "branch")); |
| 21 | if (!outcome.ok) return failed(outcome.problem, {}); |
| 22 | throw redirect(`/${repo.summary.name}/runs/${outcome.value.runId}`); |
| 23 | } |
| 24 | |
| 25 | function StartRun({ branches, main }: { branches: readonly string[]; main: string | null }) { |
| 26 | const submitting = useNavigation().state === "submitting"; |
| 27 | return ( |
| 28 | <Form method="post" className="flex flex-wrap items-end gap-2"> |
| 29 | <Select label="Branch" name="branch" defaultValue={main ?? branches[0]} items={Object.fromEntries(branches.map((branch) => [branch, branch]))} className="w-56" /> |
| 30 | <Button type="submit" variant="primary" icon={<PlayIcon />} loading={submitting}> |
| 31 | Start run |
| 32 | </Button> |
| 33 | </Form> |
| 34 | ); |
| 35 | } |
| 36 | |
| 37 | export default function RepoRuns({ loaderData, actionData }: Route.ComponentProps) { |
| 38 | const { repo, runs, branches, canRun, now } = loaderData; |
| 39 | return ( |
| 40 | <div className="flex flex-col gap-4"> |
| 41 | {actionData?.error && <Banner variant="error" description={actionData.error} />} |
| 42 | {canRun && branches && branches.all.length > 0 && ( |
| 43 | <LayerCard className="p-4"> |
| 44 | <StartRun branches={branches.all} main={branches.main} /> |
| 45 | </LayerCard> |
| 46 | )} |
| 47 | {runs.length === 0 ? ( |
| 48 | <LayerCard> |
| 49 | <Empty |
| 50 | icon={<ListChecksIcon size={40} />} |
| 51 | title="No runs yet" |
| 52 | description={ |
| 53 | <> |
| 54 | Add <code className="font-mono">.refs/ci.yml</code> to a branch. Each push, pull request or start by hand then runs its jobs here. |
| 55 | </> |
| 56 | } |
| 57 | /> |
| 58 | </LayerCard> |
| 59 | ) : ( |
| 60 | <LayerCard> |
| 61 | <ul className="m-0 list-none p-0"> |
| 62 | {runs.map((run) => ( |
| 63 | <li key={run.id} className="flex items-start gap-3 px-4 py-3 shadow-[inset_0_-1px_0_var(--hairline)] transition-colors duration-150 last:shadow-none hover:bg-hover"> |
| 64 | <span className="mt-0.5"> |
| 65 | <CiStatusIcon state={run.state} /> |
| 66 | </span> |
| 67 | <div className="flex min-w-0 flex-1 flex-col gap-1"> |
| 68 | <div className="flex flex-wrap items-baseline gap-x-2"> |
| 69 | <Link to={`/${repo}/runs/${run.id}`} className="font-semibold text-ink hover:text-accent-text"> |
| 70 | {triggerText(run)} |
| 71 | </Link> |
| 72 | <span className="font-mono text-xs text-faint">{run.sha.slice(0, 7)}</span> |
| 73 | <span className="text-compact text-muted">{CI_STATE_LABEL[run.state]}</span> |
| 74 | </div> |
| 75 | <span className="flex flex-wrap items-center gap-x-2 gap-y-1 text-compact text-muted"> |
| 76 | <LineBadge name={run.branch} /> |
| 77 | {run.actor.name && <span>{run.actor.kind === "agent" ? `${run.actor.name}'s agent` : run.actor.name}</span>} |
| 78 | <Ago at={run.createdAt} now={now} /> |
| 79 | {!run.trusted && <span className="text-caution">Untrusted</span>} |
| 80 | {run.supersededBy && <span>Replaced by a newer push</span>} |
| 81 | </span> |
| 82 | </div> |
| 83 | <span className="shrink-0 text-compact text-faint tabular-nums">{duration(run.createdAt, run.endedAt, now)}</span> |
| 84 | </li> |
| 85 | ))} |
| 86 | </ul> |
| 87 | </LayerCard> |
| 88 | )} |
| 89 | </div> |
| 90 | ); |
| 91 | } |