mail-server serves apps/mail/dashboard at /dashboard/ (tower-http fs,
DASHBOARD_DIR, mock/ and MOUNT.md never served). New account-scoped,
Resend-shaped endpoints the dashboard reads: GET /emails/{id}/events
(status history with timestamps and a one-line detail from the stored
payload), GET /emails/{id}/webhook-deliveries and
GET /webhooks/{id}/deliveries (delivery log joined with event + endpoint),
GET /emails?test=true|false and GET /suppressions?q= server filters.
GET /billing now carries description, next_plan, upgrade_label,
current_period_end/renews_at; POST /billing/checkout accepts success_url
(own origin only) and returns url. mail-auth: a bad, used or expired link
opened in a browser redirects to <redirect_to>#error=invalid_token (or a
plain HTML page when the token was never ours); the dashboard shows the
message on the login form. Dashboard uses the server-side test filter,
portal via POST /billing/portal, docs updated.
Verified live against local Postgres mail_e2e + fake relay + webhook
listener: curl of every endpoint (404 for another account's ids, 401
without auth, 422 on test=maybe), and a headless-browser pass through the
real magic-link login, email timeline, webhook delivery log, usage card
and the expired-link redirect.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
154 lines
5.2 KiB
JavaScript
154 lines
5.2 KiB
JavaScript
// Bootstrap: session, account, shell (sidebar + topbar) and the hash router.
|
|
|
|
import * as api from "./api.js";
|
|
import * as S from "./session.js";
|
|
import { handler, navigate, parse, route } from "./router.js";
|
|
import { button, clear, el, errorBox, loading } from "./ui.js";
|
|
import { loginView } from "./views/login.js";
|
|
import { startView } from "./views/start.js";
|
|
import { emailsView, emailDetailView } from "./views/emails.js";
|
|
import { domainsView, domainDetailView } from "./views/domains.js";
|
|
import { keysView } from "./views/keys.js";
|
|
import { webhooksView, webhookDetailView } from "./views/webhooks.js";
|
|
import { suppressionsView } from "./views/suppressions.js";
|
|
import { teamView } from "./views/team.js";
|
|
import { usageView } from "./views/usage.js";
|
|
|
|
const root = document.getElementById("app");
|
|
|
|
route("emails", (ctx) => (ctx.id ? emailDetailView(ctx) : emailsView(ctx)));
|
|
route("domains", (ctx) => (ctx.id ? domainDetailView(ctx) : domainsView(ctx)));
|
|
route("keys", keysView);
|
|
route("webhooks", (ctx) => (ctx.id ? webhookDetailView(ctx) : webhooksView(ctx)));
|
|
route("suppressions", suppressionsView);
|
|
route("team", teamView);
|
|
route("usage", usageView);
|
|
route("start", startView);
|
|
|
|
const NAV = [
|
|
["emails", "Emails"],
|
|
["domains", "Domains"],
|
|
["keys", "API keys"],
|
|
["webhooks", "Webhooks"],
|
|
["suppressions", "Suppressions"],
|
|
["team", "Team"],
|
|
["usage", "Usage and plan"],
|
|
];
|
|
|
|
/** Accounts the signed-in user belongs to; cached until invalidated. */
|
|
let accountsCache = null;
|
|
const invalidateAccounts = () => (accountsCache = null);
|
|
|
|
async function loadAccounts() {
|
|
if (!accountsCache) accountsCache = (await api.listAccounts()).data || [];
|
|
return accountsCache;
|
|
}
|
|
|
|
const currentAccount = () => (accountsCache || []).find((a) => a.id === S.accountId()) || null;
|
|
|
|
function modeSwitch() {
|
|
const live = el("button", { type: "button", onclick: () => { S.setTest(false); render(); } }, "Live");
|
|
const test = el("button", { type: "button", onclick: () => { S.setTest(true); render(); } }, "Test");
|
|
(S.isTest() ? test : live).classList.add("on");
|
|
return el("div.seg", { title: "Test mode shows sk_test_ keys and the emails they sent" }, live, test);
|
|
}
|
|
|
|
function accountPicker(accounts) {
|
|
const sel = el("select.acct", {
|
|
onchange: (e) => {
|
|
if (e.target.value === "__new") {
|
|
e.target.value = S.accountId();
|
|
navigate("/start?new=1");
|
|
return;
|
|
}
|
|
S.setAccountId(e.target.value);
|
|
render();
|
|
},
|
|
});
|
|
for (const a of accounts) sel.append(el("option", { value: a.id, selected: a.id === S.accountId() }, a.name));
|
|
sel.append(el("option", { value: "__new" }, "New account"));
|
|
return sel;
|
|
}
|
|
|
|
function shell(ctx, accounts, content) {
|
|
const nav = el("nav.nav");
|
|
for (const [name, text] of NAV) nav.append(el("a", { href: `#/${name}`, className: ctx.name === name ? "active" : "" }, text));
|
|
const signOut = (e) => {
|
|
e.preventDefault();
|
|
S.signOut();
|
|
invalidateAccounts();
|
|
location.hash = "#/";
|
|
render();
|
|
};
|
|
const side = el(
|
|
"aside.side",
|
|
el("div.brand", el("span.mark"), "Scribase Mail"),
|
|
nav,
|
|
el("div.grow"),
|
|
el(
|
|
"div.foot",
|
|
el("span", S.userEmail()),
|
|
el("a", { href: window.MAIL_DASHBOARD?.docsUrl || "#", target: "_blank", rel: "noopener" }, "Docs"),
|
|
el("a", { href: "#/start" }, "Setup guide"),
|
|
el("a", { href: "#", onclick: signOut }, "Sign out")
|
|
)
|
|
);
|
|
const top = el("div.topbar", accountPicker(accounts), el("div.spacer"), modeSwitch());
|
|
return el("div.shell", side, el("main.main", top, el("div.content", content)));
|
|
}
|
|
|
|
let rendering = 0;
|
|
/** Set once from a failed magic link (`#error=`), shown on the next login render. */
|
|
let loginError = null;
|
|
async function render() {
|
|
const pass = ++rendering;
|
|
clear(root);
|
|
if (!S.token()) {
|
|
root.append(loginView(loginError));
|
|
loginError = null;
|
|
return;
|
|
}
|
|
const ctx = parse();
|
|
root.append(el("div.center", loading()));
|
|
let accounts;
|
|
try {
|
|
accounts = await loadAccounts();
|
|
} catch (e) {
|
|
if (pass !== rendering) return;
|
|
if (!S.token()) return render(); // 401 signed us out
|
|
clear(root).append(el("div.center", el("div", errorBox(e), button("Retry", render, "btn secondary"))));
|
|
return;
|
|
}
|
|
if (pass !== rendering) return;
|
|
if (!accounts.find((a) => a.id === S.accountId())) S.setAccountId(accounts[0]?.id || null);
|
|
|
|
const onCreated = (acct) => {
|
|
invalidateAccounts();
|
|
S.setAccountId(acct.id);
|
|
navigate("/start");
|
|
};
|
|
if (!accounts.length || (ctx.name === "start" && ctx.query.new)) {
|
|
clear(root).append(el("div.center", await startView({ ...ctx, accounts, createOnly: true, onCreated })));
|
|
return;
|
|
}
|
|
if (!ctx.name) {
|
|
navigate(S.onboarded(S.accountId()) ? "/emails" : "/start");
|
|
return;
|
|
}
|
|
const view = handler(ctx.name) || (() => el("div.empty", "Page not found."));
|
|
const body = el("div");
|
|
clear(root).append(shell(ctx, accounts, body));
|
|
try {
|
|
const node = await view({ ...ctx, account: currentAccount(), rerender: render });
|
|
if (pass === rendering) body.append(node);
|
|
} catch (e) {
|
|
if (!S.token()) return render();
|
|
body.append(errorBox(e));
|
|
}
|
|
}
|
|
|
|
S.consumeLoginFragment();
|
|
loginError = S.consumeErrorFragment();
|
|
window.addEventListener("hashchange", render);
|
|
render();
|