import { useEffect, useRef, useState } from "react";
import { Icon } from "../lib/icons.jsx";
import { fmt, KIND_LABEL } from "../lib/constants.js";
import { useSettings } from "../lib/settings.jsx";
import { tr } from "../lib/i18n.js";
// A clock time from this computer, shown in the time zone and format chosen in Settings.
export function Time({ value }) {
const { formatTime } = useSettings();
return ;
}
// Words are English keys, translated when shown.
const STATUS = {
pass: ["pass", "ok", "Passed"], fail: ["fail", "x", "Failed"], ok: ["pass", "ok", "OK"],
acted: ["pass", "ok", "Acted"], esc: ["esc", "alert", "Escalated"], none: ["none", "minus", "No action"],
};
export function Status({ kind }) {
// Unknown words (an older run's labels) show as "no action" instead of breaking the page.
const [cls, ic, word] = STATUS[kind] || STATUS[{ escalated: "esc", "no action": "none" }[kind]] || STATUS.none || Object.values(STATUS)[0];
return {tr(word)};
}
export function KindTag({ kind }) {
return {KIND_LABEL[kind] || kind};
}
// A segmented toggle. options: [[value, label], ...]
export function Toggle({ value, options, onChange, ...rest }) {
return (
{options.map(([v, label]) => (
))}
);
}
// A number that rises from 0 to its value over ~0.8 s when it first appears.
export function CountUp({ value }) {
const reduce = typeof matchMedia !== "undefined" && matchMedia("(prefers-reduced-motion: reduce)").matches;
const [shown, setShown] = useState(reduce ? value : 0);
const from = useRef(reduce ? value : 0);
useEffect(() => {
if (reduce) { setShown(value); return; }
const start = from.current, t0 = performance.now(), dur = 800;
let raf;
const step = (now) => {
const k = Math.min(1, (now - t0) / dur);
const v = Math.round(start + (value - start) * (1 - Math.pow(1 - k, 3)));
from.current = v;
setShown(v);
if (k < 1) raf = requestAnimationFrame(step);
};
raf = requestAnimationFrame(step);
return () => cancelAnimationFrame(raf);
}, [value, reduce]);
return {fmt(shown)};
}
// value: a number (counts up) or a string such as "12 / 15" (the leading number counts up).
export function StatCard({ label, value, delta, trend, icon }) {
const dcls = ["up", "down", "warn"].includes(trend) ? trend : "flat";
const arr = trend === "up" ? "▲" : trend === "down" ? "▼" : "";
let body = value;
const m = typeof value === "number" ? [null, String(value), ""] : String(value).match(/^([\d,]+)(.*)$/);
if (m) body = <>{m[2]}>;
return (
{label}
{body}
{delta &&
{arr && {arr}}{delta}
}
);
}
export function CardHead({ title, sub, children }) {
return (
);
}
export const Mono = ({ children }) => {children};