Files
codecraft/web/src/components/FactoryRelease.tsx
T

137 lines
4.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { useEffect, useState } from "react";
import { get, post } from "../api";
import { fmtRelative, useT } from "../i18n";
import { toast } from "../dialogs";
import { Icon } from "./Icon";
import { Spinner } from "./Activity";
export interface ReleaseRow {
id: string;
repo: string;
version: string;
state: "running" | "done" | "failed";
log: string;
url: string;
started_at: number;
ended_at: number | null;
}
interface Preview {
current: string;
suggested: string;
spec: string | null;
running: boolean;
}
const repoName = (url: string) => url.replace(/\.git$/, "").split("/").slice(-2).join("/");
/** Confirm and start a release: version, the recipe that will run, then tag + image + downloads. */
export function ReleaseDialog({ repo, onClose, onStarted }: { repo: string; onClose: () => void; onStarted: () => void }) {
const t = useT();
const [pv, setPv] = useState<Preview>();
const [version, setVersion] = useState("");
const [busy, setBusy] = useState(false);
useEffect(() => {
get<Preview>(`/factory/release/preview?repo=${encodeURIComponent(repo)}`)
.then((p) => {
setPv(p);
setVersion(p.suggested);
})
.catch((e) => {
toast(e.message);
onClose();
});
}, [repo]);
if (!pv)
return (
<div className="modal-backdrop">
<div className="modal">
<Spinner />
</div>
</div>
);
const start = async () => {
setBusy(true);
try {
await post("/factory/release", { repo, version });
toast(t("release.started", { v: version }), "success");
onStarted();
onClose();
} catch (e: any) {
toast(e.message);
setBusy(false);
}
};
return (
<div className="modal-backdrop" onClick={busy ? undefined : onClose}>
<div className="modal draft-modal" role="dialog" aria-label={t("release.title")} onClick={(e) => e.stopPropagation()}>
<h3>
{t("release.title")} – {repoName(repo)}
</h3>
<div className="hint">{t("release.current", { v: pv.current })}</div>
<label className="factory-form" style={{ display: "block", margin: "8px 0" }}>
<span>{t("release.version")}</span>
<input className="mono" value={version} onChange={(e) => setVersion(e.target.value.trim().replace(/^v/, ""))} />
</label>
<div className="notice">{t("release.what")}</div>
{pv.spec ? (
<details open>
<summary>{t("release.recipe")}</summary>
<pre className="draft-spec">{pv.spec}</pre>
<div className="notice err">{t("release.trust")}</div>
</details>
) : (
<div className="hint">{t("release.noRecipe")}</div>
)}
{pv.running && <div className="notice err">{t("release.running")}</div>}
<div className="row" style={{ justifyContent: "flex-end", gap: 8, marginTop: 12 }}>
<button onClick={onClose} disabled={busy}>
{t("common.cancel")}
</button>
<button className="primary" onClick={start} disabled={busy || pv.running || !/^\d+\.\d+\.\d+$/.test(version)}>
{busy ? <Spinner small /> : <Icon name="check" />} {t("release.go", { v: version })}
</button>
</div>
</div>
</div>
);
}
/** The latest releases with their log. */
export function ReleasesPanel({ releases }: { releases: ReleaseRow[] }) {
const t = useT();
if (releases.length === 0) return null;
return (
<div className="panel" style={{ marginTop: 16 }}>
<h3 style={{ margin: "0 0 8px" }}>{t("release.list")}</h3>
{releases.map((r) => (
<details key={r.id} className="release-row" open={r.state === "running"}>
<summary>
<b>
{repoName(r.repo)} v{r.version}
</b>{" "}
<span className={`chip ${r.state === "done" ? "ok" : r.state === "running" ? "" : "warn"}`}>
{r.state === "running" && <span className="factory-pulse" />}
{t(`release.state.${r.state}` as any)}
</span>{" "}
<span className="hint">{fmtRelative(r.started_at)}</span>
{r.url && (
<>
{" "}
<a href={r.url} target="_blank" rel="noreferrer" onClick={(e) => e.stopPropagation()}>
<Icon name="external" />
</a>
</>
)}
</summary>
<pre className="draft-spec">{r.log || "–"}</pre>
</details>
))}
</div>
);
}