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

186 lines
7.5 KiB
JavaScript

// Onboarding, outcome first: 1 add domain -> 2 publish DNS -> 3 send one email.
// Also doubles as "create your first account" when the user has none.
import * as api from "../api.js";
import * as S from "../session.js";
import { navigate } from "../router.js";
import { badge, button, el, errorBox, loading, toast } from "../ui.js";
import { recordsList } from "./domains.js";
import { keyReveal } from "./keys.js";
import { curlSnippet, nodeSnippet, sdkSnippet } from "../snippets.js";
function createAccountForm({ onCreated, accounts }) {
const name = el("input.input", { placeholder: "Acme Inc", required: true, autofocus: true, maxLength: 100 });
const msg = el("div");
const form = el(
"form",
{
onsubmit: async (e) => {
e.preventDefault();
msg.replaceChildren();
try {
const acct = await api.createAccount(name.value.trim());
S.setAccountId(acct.id);
onCreated?.(acct);
} catch (err) {
msg.append(errorBox(err));
}
},
},
el("h1", accounts?.length ? "New account" : "Name your account"),
el("p.muted", "Usually your company or product. You can invite teammates later."),
el("div.field", el("label", "Account name"), name),
el("div.row", el("button.btn", { type: "submit" }, "Create account"), accounts?.length ? el("a.btn.secondary", { href: "#/emails" }, "Cancel") : null),
msg
);
return el("div.login", form);
}
const stepsBar = (current, done) =>
el(
"div.steps",
["Add your domain", "Publish DNS records", "Send your first email"].map((t, i) =>
el(`div${i + 1 === current ? ".on" : ""}${i + 1 < current || done ? ".done" : ""}`, el("small", `Step ${i + 1}`), t)
)
);
function addDomainStep(refresh) {
const name = el("input.input", { placeholder: "yourdomain.com", required: true, autofocus: true, autocomplete: "off" });
const msg = el("div");
return el(
"div.card",
el("h2", "Which domain will your emails come from?"),
el("p.muted", "Use the domain your product already lives on. We generate the DKIM key for it right away."),
el(
"form.inline-form",
{
onsubmit: async (e) => {
e.preventDefault();
msg.replaceChildren();
try {
await api.createDomain({ name: name.value.trim().toLowerCase() });
refresh();
} catch (err) {
msg.append(errorBox(err));
}
},
},
el("div.field", el("label", "Domain"), name),
el("button.btn", { type: "submit" }, "Add domain")
),
msg
);
}
function dnsStep(domain, refresh) {
const status = el("span");
const paint = (d) => status.replaceChildren(badge(d.status));
paint(domain);
const msg = el("div");
const check = button("Check now", async () => {
msg.replaceChildren();
try {
const d = await api.verifyDomain(domain.id);
paint(d);
recs.replaceChildren(recordsList(d.records || []));
if (d.status === "verified") {
toast("Domain verified");
refresh();
} else msg.append(el("p.help", "Not all records resolve yet. DNS changes can take a few minutes to show up. Check again when you have published them."));
} catch (err) {
msg.append(errorBox(err));
}
});
const recs = el("div", recordsList(domain.records || []));
return el(
"div.card",
el("div.row", el("h2", { style: "margin:0" }, `Add these records at your DNS provider for ${domain.name}`), status),
el("p.muted", "Copy each host and value into your registrar (Cloudflare, Namecheap, GoDaddy, Route 53). Host is relative to your domain; use the full name if your provider asks for it."),
recs,
el("div.row", check, button("Skip for now", () => refresh({ skipDns: true }), "btn secondary"), el("a.btn.link", { href: `#/domains/${domain.id}` }, "Open domain")),
msg
);
}
function sendStep(domain, keys, refresh) {
const to = S.userEmail() || "you@example.com";
const from = `hello@${domain.name}`;
const subject = "Hello from Scribase Mail";
const state = { key: keys.length ? `${keys[0].key_prefix || "sk_"}...` : "sk_..." , hasKey: keys.length > 0 };
const code = el("div");
const tabs = el("div.tabs");
const paintCode = (which) => {
const args = { key: state.key, from, to, subject };
const text = which === "node" ? nodeSnippet(args) : which === "sdk" ? sdkSnippet(args) : curlSnippet(args);
code.replaceChildren(el("div.row", { style: "justify-content:space-between;margin-bottom:6px" }, tabs, copyBtn(text)), el("pre", text));
[...tabs.children].forEach((b) => b.classList.toggle("on", b.dataset.k === which));
};
const copyBtn = (text) => {
const b = el("button.btn.copy", { type: "button" }, "Copy");
b.onclick = async () => { await navigator.clipboard.writeText(text).catch(() => {}); b.textContent = "Copied"; setTimeout(() => (b.textContent = "Copy"), 1200); };
return b;
};
for (const [k, t] of [["curl", "curl"], ["node", "Node"], ["sdk", "@scribase/mail"]]) tabs.append(el("button", { type: "button", dataset: { k }, onclick: () => paintCode(k) }, t));
const keyBox = el("div");
const msg = el("div");
const mkKey = button(state.hasKey ? "Create another key" : "Create an API key", async () => {
msg.replaceChildren();
try {
const k = await api.createKey({ name: "first-key", permission: "full_access", mode: S.isTest() ? "test" : "live" });
state.key = k.token;
keyBox.replaceChildren(keyReveal(k.token, k.mode));
paintCode("curl");
} catch (err) {
msg.append(errorBox(err));
}
});
const sendBtn = button("Send it to me", async () => {
msg.replaceChildren();
try {
const r = await api.sendEmail({ from, to: [to], subject, html: "<p>It works.</p>", text: "It works." });
msg.append(el("p", "Sent. ", el("a", { href: `#/emails/${r.id}` }, "Watch it go out"), "."));
S.setOnboarded(S.accountId());
} catch (err) {
msg.append(errorBox(err));
if (err.status === 403) msg.append(el("p.help", "Sending needs a verified domain. Finish step 2 first."));
}
});
paintCode("curl");
return el(
"div.card",
el("h2", "Send one email"),
el("p.muted", `From ${from} to ${to}. Create a key, paste the snippet in your terminal, or let us send it from here.`),
el("div.row", mkKey, sendBtn, el("span.muted.small", domain.status === "verified" ? "" : "Domain not verified yet: live sends are refused until the records resolve.")),
keyBox,
el("div", { style: "margin-top:14px" }, code),
msg,
el("div.row", { style: "margin-top:14px" }, button("Finish", () => { S.setOnboarded(S.accountId()); navigate("/emails"); }, "btn secondary"))
);
}
export async function startView(ctx) {
if (ctx.createOnly) return createAccountForm(ctx);
const wrap = el("div");
const flags = { skipDns: false };
const refresh = async (opts = {}) => {
Object.assign(flags, opts);
wrap.replaceChildren(loading());
try {
const [domains, keys] = await Promise.all([api.listDomains(), api.listKeys()]);
const d = domains.data?.[0];
let step = 1;
if (d) step = d.status === "verified" || flags.skipDns ? 3 : 2;
wrap.replaceChildren(
el("div.page-head", el("div.grow", el("h1", "Get your first email out"), el("p", "Three steps. Most teams are done in ten minutes."))),
stepsBar(step, false),
step === 1 ? addDomainStep(refresh) : step === 2 ? dnsStep(await api.getDomain(d.id), refresh) : sendStep(d, keys.data || [], refresh)
);
} catch (e) {
wrap.replaceChildren(errorBox(e));
}
};
await refresh();
return wrap;
}