Login via our own magic links, 3-step onboarding (domain, DNS records, first send), emails with status timeline, domains, API keys (live/test), webhooks, suppressions, team, usage + plan. Live/Test switch everywhere. Dev mock server + MOUNT.md listing the one-line axum mount and missing API pieces. Verified in a headless browser against the mock (24 screenshots). Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
153 lines
4.6 KiB
JavaScript
153 lines
4.6 KiB
JavaScript
// Small DOM helpers. No templates, no framework.
|
|
|
|
/** el("div.card", {onclick}, child, "text", [more]) */
|
|
export function el(spec, attrs, ...children) {
|
|
if (attrs && (attrs instanceof Node || typeof attrs !== "object" || Array.isArray(attrs))) {
|
|
children.unshift(attrs);
|
|
attrs = {};
|
|
}
|
|
const [tag, ...classes] = spec.split(".");
|
|
const node = document.createElement(tag || "div");
|
|
if (classes.length) node.className = classes.join(" ");
|
|
for (const [k, v] of Object.entries(attrs || {})) {
|
|
if (v === undefined || v === null || v === false) continue;
|
|
if (k.startsWith("on") && typeof v === "function") node.addEventListener(k.slice(2), v);
|
|
else if (k === "html") node.innerHTML = v;
|
|
else if (k === "dataset") Object.assign(node.dataset, v);
|
|
else if (k in node && k !== "list" && k !== "form") node[k] = v;
|
|
else node.setAttribute(k, v === true ? "" : v);
|
|
}
|
|
append(node, children);
|
|
return node;
|
|
}
|
|
|
|
export function append(node, children) {
|
|
for (const c of children.flat(Infinity)) {
|
|
if (c === null || c === undefined || c === false) continue;
|
|
node.append(c instanceof Node ? c : document.createTextNode(String(c)));
|
|
}
|
|
return node;
|
|
}
|
|
|
|
export const clear = (node) => {
|
|
while (node.firstChild) node.removeChild(node.firstChild);
|
|
return node;
|
|
};
|
|
|
|
let toastTimer;
|
|
export function toast(message) {
|
|
const t = document.getElementById("toast");
|
|
t.textContent = message;
|
|
t.hidden = false;
|
|
clearTimeout(toastTimer);
|
|
toastTimer = setTimeout(() => (t.hidden = true), 1800);
|
|
}
|
|
|
|
export async function copyText(text) {
|
|
try {
|
|
await navigator.clipboard.writeText(text);
|
|
return true;
|
|
} catch {
|
|
const ta = el("textarea", { value: text, style: "position:fixed;opacity:0" });
|
|
document.body.append(ta);
|
|
ta.select();
|
|
const ok = document.execCommand("copy");
|
|
ta.remove();
|
|
return ok;
|
|
}
|
|
}
|
|
|
|
export function copyButton(text, label = "Copy") {
|
|
const b = el("button.btn.copy", { type: "button" }, label);
|
|
b.addEventListener("click", async () => {
|
|
if (await copyText(typeof text === "function" ? text() : text)) {
|
|
b.textContent = "Copied";
|
|
b.classList.add("done");
|
|
setTimeout(() => {
|
|
b.textContent = label;
|
|
b.classList.remove("done");
|
|
}, 1200);
|
|
} else toast("Copy failed");
|
|
});
|
|
return b;
|
|
}
|
|
|
|
export const errorBox = (e) => el("p.err", e?.message || String(e));
|
|
|
|
export function button(label, onclick, cls = "btn") {
|
|
const b = el(`button.${cls.split(" ").join(".")}`, { type: "button" }, label);
|
|
b.addEventListener("click", async (ev) => {
|
|
ev.preventDefault();
|
|
if (b.disabled) return;
|
|
b.disabled = true;
|
|
try {
|
|
await onclick(ev, b);
|
|
} finally {
|
|
b.disabled = false;
|
|
}
|
|
});
|
|
return b;
|
|
}
|
|
|
|
export const confirmAction = (text) => window.confirm(text);
|
|
|
|
export function timeAgo(iso) {
|
|
if (!iso) return "";
|
|
const d = new Date(iso);
|
|
if (Number.isNaN(d.getTime())) return iso;
|
|
const s = Math.round((Date.now() - d.getTime()) / 1000);
|
|
if (Math.abs(s) < 60) return "just now";
|
|
const m = Math.round(s / 60);
|
|
if (Math.abs(m) < 60) return `${m} min ago`;
|
|
const h = Math.round(m / 60);
|
|
if (Math.abs(h) < 48) return `${h} h ago`;
|
|
return d.toLocaleDateString(undefined, { month: "short", day: "numeric" });
|
|
}
|
|
|
|
export const fullTime = (iso) => (iso ? new Date(iso).toLocaleString() : "");
|
|
|
|
const STATUS_CLASS = {
|
|
delivered: "ok",
|
|
verified: "ok",
|
|
sent: "",
|
|
queued: "",
|
|
scheduled: "",
|
|
pending: "warn",
|
|
not_started: "",
|
|
delivery_delayed: "warn",
|
|
bounced: "bad",
|
|
complained: "bad",
|
|
failed: "bad",
|
|
canceled: "",
|
|
suppressed: "bad",
|
|
enabled: "ok",
|
|
disabled: "",
|
|
active: "ok",
|
|
paused: "warn",
|
|
suspended: "bad",
|
|
};
|
|
|
|
export const label = (s) => String(s || "").replace(/_/g, " ");
|
|
|
|
export const badge = (status, extra = "") =>
|
|
el(`span.badge${STATUS_CLASS[status] ? "." + STATUS_CLASS[status] : ""}${extra ? "." + extra : ""}`, label(status));
|
|
|
|
export const testBadge = () => el("span.badge.test", "test");
|
|
|
|
export function table(headers, rows, emptyText) {
|
|
if (!rows.length) return el("div.empty", emptyText || "Nothing here yet.");
|
|
return el(
|
|
"table",
|
|
el("thead", el("tr", headers.map((h) => el(`th${h.num ? ".num" : ""}`, h.label ?? h)))),
|
|
el("tbody", rows)
|
|
);
|
|
}
|
|
|
|
export const loading = () => el("div.loading", "Loading");
|
|
|
|
export const pageHead = (title, sub, ...actions) =>
|
|
el("div.page-head", el("div.grow", el("h1", title), sub ? el("p", sub) : null), actions);
|
|
|
|
/** Value display with a copy button to the right. */
|
|
export const copyRow = (value) => el("div.row", el("code", { style: "word-break:break-all" }, value), copyButton(value));
|