caelum0x 2299eb1ba5 feat(mail): customer dashboard (no-build HTML + vanilla JS)
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>
2026-11-13 04:24:04 +03:00

102 lines
5.0 KiB
JavaScript

// API keys: create (shown once), list, revoke. Live sk_ or test sk_test_.
import * as api from "../api.js";
import * as S from "../session.js";
import { badge, button, confirmAction, copyButton, el, errorBox, fullTime, loading, pageHead, table, testBadge, timeAgo, toast } from "../ui.js";
/** The one-time reveal box for a freshly minted key. */
export const keyReveal = (token, mode) =>
el(
"div.secret",
{ style: "margin-top:12px" },
el("div", { style: "flex-basis:100%" }, el("b", "Copy this key now."), " It is shown once; we only keep a hash.", mode === "test" ? [" ", testBadge()] : null),
el("code", token),
copyButton(token)
);
export async function keysView(ctx) {
const list = el("div.card.tight");
const reveal = el("div");
const msg = el("div");
let domains = [];
async function load() {
list.replaceChildren(loading());
try {
const [keys, doms] = await Promise.all([api.listKeys(), api.listDomains()]);
domains = doms.data || [];
const test = S.isTest();
const rows = (keys.data || []).filter((k) => (k.mode === "test") === test);
list.replaceChildren(
table(
["Name", "Key", "Permission", "Domain", "Last used", "Created", ""],
rows.map((k) =>
el("tr",
el("td", k.name),
el("td.mono", `${k.key_prefix || "sk_"}...`, k.mode === "test" ? [" ", testBadge()] : null),
el("td", badge(k.permission === "full_access" ? "full access" : "sending only")),
el("td.muted", k.domain_id ? domains.find((d) => d.id === k.domain_id)?.name || k.domain_id : "any"),
el("td.muted", k.last_used_at ? timeAgo(k.last_used_at) : "never"),
el("td.muted", fullTime(k.created_at)),
el("td", button("Revoke", async () => {
if (!confirmAction(`Revoke "${k.name}"? Requests using it stop working immediately.`)) return;
try { await api.deleteKey(k.id); toast("Revoked"); load(); } catch (err) { alert(err.message); }
}, "btn danger sm")))
),
test ? "No test keys. Create one to send simulated emails that never leave the system." : "No live keys yet."
)
);
domainSel.replaceChildren(el("option", { value: "" }, "Any domain"), domains.map((d) => el("option", { value: d.id }, d.name)));
} catch (e) {
list.replaceChildren(errorBox(e));
}
}
const name = el("input.input", { placeholder: "production", required: true, maxLength: 50 });
const perm = el("select.input", el("option", { value: "full_access" }, "Full access"), el("option", { value: "sending_access" }, "Sending only"));
const domainSel = el("select.input", { disabled: true });
perm.onchange = () => { domainSel.disabled = perm.value !== "sending_access"; if (domainSel.disabled) domainSel.value = ""; };
const modeLive = el("button", { type: "button", onclick: () => setMode(false) }, "sk_ live");
const modeTest = el("button", { type: "button", onclick: () => setMode(true) }, "sk_test_");
const setMode = (t) => { modeLive.classList.toggle("on", !t); modeTest.classList.toggle("on", t); form.dataset.mode = t ? "test" : "live"; };
const form = el(
"form",
{ onsubmit: async (e) => {
e.preventDefault(); msg.replaceChildren();
try {
const body = { name: name.value.trim(), permission: perm.value, mode: form.dataset.mode };
if (perm.value === "sending_access" && domainSel.value) body.domain_id = domainSel.value;
const k = await api.createKey(body);
reveal.replaceChildren(keyReveal(k.token, k.mode));
name.value = "";
if ((k.mode === "test") !== S.isTest()) {
S.setTest(k.mode === "test");
sessionStorage.setItem("sm.pendingKey", JSON.stringify(k));
ctx.rerender();
} else load();
} catch (err) { msg.append(errorBox(err)); }
} },
el("div.inline-form",
el("div.field", el("label", "Name"), name),
el("div.field", el("label", "Permission"), perm),
el("div.field", el("label", "Restrict to domain"), domainSel),
el("div.field", el("label", "Mode"), el("div.seg", modeLive, modeTest)),
el("button.btn", { type: "submit" }, "Create key")),
el("p.help", { style: "margin-top:8px" }, "Test keys (sk_test_) simulate delivery, bounces and complaints without sending anything; they never count toward your quota.")
);
setMode(S.isTest());
// A key minted in the other mode flips the switch; show it once after the re-render.
const pendingKey = sessionStorage.getItem("sm.pendingKey");
if (pendingKey) {
sessionStorage.removeItem("sm.pendingKey");
const k = JSON.parse(pendingKey);
reveal.replaceChildren(keyReveal(k.token, k.mode));
}
await load();
return el("div",
pageHead("API keys", "Keys authenticate the API and SMTP (user scribase, password = key)."),
S.isTest() ? el("div.testbanner", el("b", "Test mode"), el("span.muted", "Showing sk_test_ keys.")) : null,
el("div.card", form, msg, reveal),
list);
}