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

74 lines
4.0 KiB
JavaScript

// Team: members, roles, invitations.
import * as api from "../api.js";
import * as S from "../session.js";
import { badge, button, confirmAction, el, errorBox, fullTime, loading, pageHead, table, toast } from "../ui.js";
const ROLES = ["viewer", "developer", "admin", "owner"];
const ROLE_HELP = {
viewer: "Read everything, change nothing.",
developer: "Also send email and manage webhooks.",
admin: "Also manage keys, domains, suppressions and members.",
owner: "Everything, including granting owner.",
};
export async function teamView(ctx) {
const acct = S.accountId();
const myRole = ctx.account?.role || "viewer";
const canManage = ["owner", "admin"].includes(myRole);
const list = el("div.card.tight");
const pending = el("div.card.tight");
const msg = el("div");
async function load() {
list.replaceChildren(loading());
try {
const page = await api.listMembers(acct);
const me = S.session()?.sub;
list.replaceChildren(table(
["Member", "Role", "Joined", ""],
(page.data || []).map((m) => {
const sel = el("select.input", { style: "width:auto", disabled: !canManage || (m.role === "owner" && myRole !== "owner") },
ROLES.map((r) => el("option", { value: r, selected: r === m.role, disabled: r === "owner" && myRole !== "owner" }, r)));
sel.onchange = async () => { try { await api.setRole(acct, m.user_id, sel.value); toast("Role updated"); load(); } catch (err) { alert(err.message); load(); } };
return el("tr",
el("td", m.email, m.user_id === me ? el("span.muted.small", " (you)") : null),
el("td", canManage ? sel : badge(m.role)),
el("td.muted", fullTime(m.created_at)),
el("td", canManage ? button("Remove", async () => {
if (!confirmAction(`Remove ${m.email} from this account?`)) return;
try { await api.removeMember(acct, m.user_id); toast("Removed"); load(); } catch (err) { alert(err.message); }
}, "btn danger sm") : null));
}), "No members."));
const inv = page.pending_invites || [];
pending.hidden = !inv.length;
pending.replaceChildren(table(["Invited", "Role", "Expires", ""],
inv.map((i) => el("tr", el("td", i.email), el("td", badge(i.role)), el("td.muted", fullTime(i.expires_at)),
el("td", canManage ? button("Revoke", async () => { try { await api.revokeInvite(acct, i.id); toast("Revoked"); load(); } catch (err) { alert(err.message); } }, "btn secondary sm") : null))), ""));
} catch (e) { list.replaceChildren(errorBox(e)); }
}
await load();
const email = el("input.input", { type: "email", placeholder: "teammate@company.com", required: true });
const role = el("select.input", ROLES.filter((r) => r !== "owner").map((r) => el("option", { value: r, selected: r === "developer" }, r)));
const help = el("p.help", ROLE_HELP[role.value]);
role.onchange = () => (help.textContent = ROLE_HELP[role.value]);
const form = el("form", { onsubmit: async (e) => {
e.preventDefault(); msg.replaceChildren();
try {
const r = await api.invite(acct, email.value.trim(), role.value);
toast(r.email_sent === false ? "Invite created (email not sent)" : "Invite sent");
if (r.email_sent === false) msg.append(el("p.help", "The invitation exists but no email went out (system sender not configured). The invitee joins automatically when they sign in with this address."));
email.value = ""; load();
} catch (err) { msg.append(errorBox(err)); }
} },
el("div.inline-form", el("div.field", el("label", "Email"), email), el("div.field", { style: "flex:0 0 160px" }, el("label", "Role"), role), el("button.btn", { type: "submit" }, "Invite")),
help);
return el("div",
pageHead("Team", `${ctx.account?.name || "This account"}. You are ${myRole}.`),
canManage ? el("div.card", form, msg) : el("div.card", el("p.muted", "Owners and admins invite people and change roles.")),
list,
el("h2", { style: "margin-top:20px" }, "Pending invitations"), pending);
}