Skip to content
refs
57 lines · 2.5 KBRaw
1import type { CiJobState, CiRunState, CiRunSummary } from "@refs/backend/contract";
2import { CheckCircleIcon, CircleDashedIcon, CircleNotchIcon, ClockIcon, HourglassIcon, MinusCircleIcon, StopCircleIcon, WarningCircleIcon, XCircleIcon } from "@refs/design-system/icons";
3
4type State = CiRunState | CiJobState;
5
6export const CI_STATE_LABEL: Record<State, string> = {
7 pending: "Pending",
8 queued: "Queued",
9 waiting_image: "Waiting for image",
10 waiting_approval: "Waiting for approval",
11 running: "Running",
12 succeeded: "Succeeded",
13 failed: "Failed",
14 skipped: "Skipped",
15 cancelled: "Cancelled",
16 timed_out: "Timed out",
17};
18
19export const CI_ENDED: ReadonlySet<State> = new Set(["succeeded", "failed", "skipped", "cancelled", "timed_out"]);
20
21export function CiStatusIcon({ state, size = 16 }: { state: State; size?: number }) {
22 const label = CI_STATE_LABEL[state];
23 switch (state) {
24 case "succeeded":
25 return <CheckCircleIcon size={size} weight="fill" className="shrink-0 text-success" aria-label={label} />;
26 case "failed":
27 case "timed_out":
28 return <XCircleIcon size={size} className="shrink-0 text-danger" aria-label={label} />;
29 case "waiting_approval":
30 return <WarningCircleIcon size={size} weight="fill" className="shrink-0 text-needs" aria-label={label} />;
31 case "running":
32 return <CircleNotchIcon size={size} className="shrink-0 animate-spin text-ink motion-reduce:animate-none" aria-label={label} />;
33 case "cancelled":
34 return <StopCircleIcon size={size} className="shrink-0 text-faint" aria-label={label} />;
35 case "skipped":
36 return <MinusCircleIcon size={size} className="shrink-0 text-faint" aria-label={label} />;
37 case "waiting_image":
38 return <HourglassIcon size={size} className="shrink-0 text-faint" aria-label={label} />;
39 case "queued":
40 return <ClockIcon size={size} className="shrink-0 text-faint" aria-label={label} />;
41 case "pending":
42 return <CircleDashedIcon size={size} className="shrink-0 text-faint" aria-label={label} />;
43 }
44}
45
46export function triggerText(run: CiRunSummary): string {
47 if (run.trigger === "pull_request") return `Pull request #${run.pull}`;
48 if (run.trigger === "manual") return "Started by hand";
49 return "Push";
50}
51
52export function duration(start: number | null, end: number | null, now: number): string | null {
53 if (start === null) return null;
54 const total = Math.max(0, (end ?? now) - start);
55 const minutes = Math.floor(total / 60);
56 return minutes ? `${minutes}m ${total % 60}s` : `${total}s`;
57}