iterate.com / cloud

git clone https://git.iterate.com/repos/cloud

cloud / internal-packages / installer / worker / installation-pages.jsmain at aacbc74

// installation-pages.js — THE PAGES OF A CHAT INSTALLATION ON THE CLOUD'S OWN ORIGIN (index.js
// `/i/<id>/<kind>`), as plain HTML: what the chat links to with a single-use link when the person
// must act in their own browser. The flow is in index.js; here is what each page says.

const escape = (value) =>
  String(value ?? "").replace(
    /[&<>"']/g,
    (c) => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;" })[c],
  );

const page = (title, body) =>
  `<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><meta name="robots" content="noindex"><title>${escape(title)} · iterate Cloud</title><style>${`
:root { color-scheme: light dark; --bg: #fafaf9; --fg: #1c1917; --muted: #57534e; --line: #e7e5e4; --accent: #1d4ed8; --bad: #b91c1c; }
@media (prefers-color-scheme: dark) { :root { --bg: #0c0a09; --fg: #f5f5f4; --muted: #a8a29e; --line: #292524; --accent: #93c5fd; --bad: #fca5a5; } }
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--fg); font: 16px/1.5 system-ui, sans-serif; }
main { max-width: 36rem; margin: 0 auto; padding: 2rem 1rem 4rem; }
h1 { font-size: 1.4rem; margin: 0 0 1rem; }
p { margin: 0 0 1rem; color: var(--muted); }
p.bad { color: var(--bad); }
form { margin: 0 0 1rem; }
label { display: block; margin: 0.5rem 0 0.2rem; font-size: 0.9rem; color: var(--muted); }
input { width: 100%; padding: 0.55rem 0.7rem; border: 1px solid var(--line); border-radius: 0.5rem; background: transparent; color: inherit; font: inherit; }
button { margin-top: 0.75rem; padding: 0.6rem 1rem; border: 0; border-radius: 0.5rem; background: var(--accent); color: var(--bg); font: inherit; cursor: pointer; }
ul { list-style: none; padding: 0; margin: 0 0 1rem; }
li { border: 1px solid var(--line); border-radius: 0.6rem; padding: 0.8rem; margin: 0 0 0.6rem; }
.row { display: flex; justify-content: space-between; gap: 1rem; flex-wrap: wrap; }
.small { font-size: 0.85rem; }
`}</style></head><body><main><h1>${escape(title)}</h1>${body}</main></body></html>`;

export const connected = () =>
  page(
    "Cloudflare is connected",
    "<p>iterate is getting your mind ready in your Cloudflare account. You can close this tab and go back to the chat.</p>",
  );

export const message = (title, text) => page(title, `<p>${escape(text)}</p>`);

export const used = () =>
  message(
    "This link was used, or it ran out",
    "Each link works once, for 30 minutes. Ask in the chat for a new one.",
  );

/** A consent from a Cloudflare login that is not the owner's: who it must be, and how to get there. */
export const wrongUser = ({ expected, signOut, again }) =>
  page(
    "That is another Cloudflare login",
    `<p>This installation needs the Cloudflare login${expected ? ` of <strong>${escape(expected)}</strong>` : " that connected it first"}. iterate gave up the access you just granted.</p>
<p>Sign out of Cloudflare, sign in as ${expected ? escape(expected) : "that login"}, then connect again.</p>
<p><a href="${escape(signOut)}" target="_blank" rel="noopener">Sign out of Cloudflare</a></p>
${again ? `<p><a href="${escape(again)}">Connect again as ${escape(expected || "that login")}</a></p>` : "<p>Then ask in the chat for a new link.</p>"}`,
  );

export const notSignedIn = () =>
  message(
    "Open this from the chat",
    "This page needs the browser that connected Cloudflare for this installation. Ask in the chat for a new link.",
  );

export function accounts({ list = [], chosen = null, done = false }) {
  if (done)
    return message("Account chosen", "Go back to the chat: iterate goes on in that account.");
  const items = list
    .map(
      (a) =>
        `<li><form method="post"><div class="row"><span>${escape(a.name)}${a.id === chosen ? " (chosen)" : ""}</span><input type="hidden" name="account" value="${escape(a.id)}"><button type="submit">Use this account</button></div></form></li>`,
    )
    .join("");
  return page(
    "Which Cloudflare account?",
    `<p>Your consent covered several accounts. iterate puts your mind into one of them.</p><ul>${items}</ul>`,
  );
}

const CONTACT = [
  ["name", "Full name", "text"],
  ["email", "Email", "email"],
  ["phone", "Phone, as +44.7700900000", "tel"],
  ["street", "Street", "text"],
  ["city", "City", "text"],
  ["postal", "Postcode", "text"],
  ["country", "Country code, as GB", "text"],
];

/** The contact that Cloudflare's registrar takes, from the form; null when the form has none. */
export function contactsOf(form) {
  const v = (k) => String(form.get(k) || "").trim();
  if (!v("name")) return null;
  const address = {
    street: v("street"),
    city: v("city"),
    state: v("city"),
    postal_code: v("postal"),
    country_code: v("country").toUpperCase(),
  };
  return {
    registrant: { email: v("email"), phone: v("phone"), postal_info: { name: v("name"), address } },
  };
}

/** @param {{ q?: string, found?: { domains: any[], contactOnFile: boolean } | null, said?: string, bought?: string }} parts */
export function domain({ q = "", found = null, said = "", bought = "" }) {
  if (bought)
    return message(
      `${bought} is yours`,
      `Cloudflare is registering ${bought} in your account. iterate goes on with it by itself: go back to the chat.`,
    );
  const search = `<form method="get"><label for="q">A name, or words for one</label><input id="q" name="q" value="${escape(q)}" autocomplete="off"><button type="submit">Search</button></form>`;
  const note = said ? `<p class="bad">${escape(said)}</p>` : "";
  if (!found)
    return page(
      "Buy a domain",
      `<p>Search for a domain. You buy it from Cloudflare, in your own account, and Cloudflare charges your card.</p>${note}${search}`,
    );
  const contact = found.contactOnFile
    ? ""
    : `<p class="small">Cloudflare has no contact on file for this account: who the domain is registered to.</p>${CONTACT.map(([k, label, type]) => `<label>${label}<input name="${k}" type="${type}" required></label>`).join("")}`;
  const items = found.domains
    .map((d) =>
      d.available
        ? `<li><form method="post"><div class="row"><strong>${escape(d.name)}</strong><span>${escape(d.currency)} ${escape(d.price ?? "?")} the first year, ${escape(d.renewal ?? "?")} a year after</span></div><input type="hidden" name="domain" value="${escape(d.name)}"><label>To buy it, type ${escape(d.name)}</label><input name="confirm" autocomplete="off" required>${contact}<button type="submit">Buy ${escape(d.name)}</button></form></li>`
        : `<li><div class="row"><span>${escape(d.name)}</span><span class="small">taken</span></div></li>`,
    )
    .join("");
  return page(
    "Buy a domain",
    `${note}${search}<ul>${items || "<li>Nothing found. Try other words.</li>"}</ul>`,
  );
}