import { useEffect, useRef, useState, type ReactNode } from "react";
import { createPortal } from "react-dom";
import { ApiError } from "../api/client";
import type { Job } from "../api/types";
export function errorMessage(e: unknown, fallback = "Something went wrong. Try again."): string {
return e instanceof ApiError ? e.message : e instanceof Error && e.message ? e.message : fallback;
}
/** Launch progress bar for a job. */
export function JobProgress({ job, compact = false }: { job: Job; compact?: boolean }) {
const pct = Math.round((job.status === "done" ? 1 : job.progress) * 100);
const failed = job.status === "failed";
const label = failed
? job.error ?? "Failed"
: job.status === "queued"
? job.message ?? "Queued for launch"
: job.message ?? "Working";
return (
{label}
{!failed && {pct}%}
);
}
export function Modal({
title,
onClose,
children,
wide = false,
actions,
}: {
title: string;
onClose: () => void;
children: ReactNode;
wide?: boolean;
/** Buttons for the header's right side, in place of the close button. */
actions?: ReactNode;
}) {
const ref = useRef(null);
useEffect(() => {
const onKey = (e: KeyboardEvent) => e.key === "Escape" && onClose();
document.addEventListener("keydown", onKey);
ref.current?.focus();
return () => document.removeEventListener("keydown", onKey);
}, [onClose]);
// Rendered into : a parent with backdrop-filter or transform (cards have one) would otherwise become the
// containing block of this fixed backdrop and trap the modal inside that card.
return createPortal(