iterate.com / cloud

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

cloud / core / lib / src / standalone-page.tsmain at aacbc74

// standalone-page.ts — a page served as plain HTML, outside any app's build: the browser-auth
// gate's own pages (app-server.ts), the platform's callback pages (core/secrets-worker/src/callback-page.ts) and
// the CLI's sign-in page (cli/oauth.ts). Its one stylesheet is inline (theme/plain-page.css over
// theme/tokens.css), so the page loads nothing to look like the issuer's pages, and a CSP allows
// that stylesheet by its hash, `plainPageCssHash`.
import { plainPageCss } from "./theme/plain-page-css.gen.ts";

export { plainPageCssHash } from "./theme/plain-page-css.gen.ts";

/** The glyph of iterate's logo (packages/ui's iterate-logo.svg), copied here because a plain page
 *  loads nothing: its policy allows no image. */
const GLYPH = `<g transform="translate(20 20) scale(0.92)"><path d="M264.649 170.149H289.821L286.092 186.904L276.303 233.444L270.709 259.971L263.717 293.015L258.124 320.008L251.131 352.586L249.267 364.687V371.668L249.733 372.133H253.462L259.522 369.806L266.048 365.617L275.371 357.24L282.829 349.328L286.558 345.14L288.888 346.071L294.948 350.725L308 360.498L307.068 362.36L303.339 367.944L296.813 376.322L291.685 382.837L286.558 388.422L282.363 393.076L275.837 399.592L272.108 402.849L267.446 406.573L262.785 409.83L256.725 413.554L247.869 417.742L238.08 420.535L231.554 421H224.096L216.637 420.069L211.51 418.673L206.382 416.811L201.255 413.088L196.594 408.434L192.865 400.988L191.466 394.938L191 389.818V383.768L193.797 365.152L199.857 335.832L207.315 301.392L224.096 223.205L225.028 216.224V206.916L224.562 205.054L222.231 204.123L219.434 203.193L206.382 203.658L196.127 204.589H193.331V178.526L194.263 175.734L258.59 170.615L264.649 170.149Z"/><path d="M264.649 78H268.844L275.836 78.9308L282.362 80.7924L287.49 83.5848L292.151 87.7734L295.414 92.8928L297.278 96.616L299.143 105.924L299.609 113.836L299.143 118.49L298.677 122.213L296.812 128.729L293.549 134.779L290.286 138.502L286.091 141.76L282.362 143.621L278.167 145.018L274.438 145.948L267.912 146.414H260.92L254.394 145.483L249.267 144.087L244.139 141.294L239.944 138.037L236.681 133.383L233.884 127.332L232.486 121.282L232.02 117.559V108.716L232.952 101.735L234.816 95.6852L237.613 90.1004L240.41 86.3772L246.936 82.1886L252.529 79.8616L259.522 78.4654L264.649 78Z"/></g>`;

/** iterate's mark, at the head of every plain page: a rounded square in the text's colour with
 *  the glyph in the card's (plain-page.css `.mark`). */
const MARK = `<div class="mark" aria-hidden="true"><svg viewBox="0 0 500 500" xmlns="http://www.w3.org/2000/svg"><rect width="500" height="500"/>${GLYPH}</svg></div>`;

/** The tab's icon: the logo, inline, so that it needs no route on the page's host. The page's CSP
 *  holds an icon as it holds an image: a page with a policy allows `img-src data:`
 *  (`PLAIN_PAGE_ICON_SRC`). A `data:` image reaches no server. */
export const PLAIN_PAGE_ICON_SRC = "data:";
const ICON = `<link rel="icon" type="image/svg+xml" href="data:image/svg+xml,${encodeURIComponent(
  `<svg viewBox="0 0 500 500" xmlns="http://www.w3.org/2000/svg"><rect width="500" height="500" rx="112" fill="#000"/>${GLYPH.replace("<g ", '<g fill="#fff" ')}</svg>`,
)}">`;

/** The whole document. `title` is text, escaped here; `head` and `body` are HTML, so a caller
 *  escapes what it did not write in them. `head` is what the head carries beyond the title and the
 *  stylesheet; `body` goes inside `main`, the page's one card, under the mark. A card is narrow,
 *  for one question and one action (a sign-in, a consent); `wide` is for a page a person reads or
 *  works in (their keys, the members, the approvals). In the body, a `button` is the main action,
 *  `button.secondary` another way, `button.link` an action that reads as a link, `.actions` a row
 *  of them, `.stack` a column of a form's parts, `.or` the line between two ways and `.muted` a
 *  quieter line; headings, lists, fieldsets, labels and `pre` have plain rules of their own. A
 *  `centered` card (a sign-in, a consent) has its mark and heading in the middle. */
const escapedText = (text: string) =>
  text.replace(/[&<>"]/g, (character) => `&#${character.charCodeAt(0)};`);

/** ONE ROW OF WHAT A PRESS SIGNS OR ALLOWS (`factsTable`): a name, its value and a quieter line
 *  under it (`note`), both HTML that the caller escaped, and `help`, plain text behind the row's
 *  "?". A fact that changes what a person decides goes in `value` or `note`, where it is read
 *  with no press; `help` only explains. */
export type Fact = { label: string; value: string; note?: string; help?: string };

/** A row's "?": plain text behind the browser's own `details`, so a key press or a touch opens it
 *  as a click does, with no script. `about` names what it explains, for a screen reader. */
const helpMark = (about: string, help: string) =>
  `<details class="help"><summary aria-label="About ${escapedText(about)}">?</summary><p>${escapedText(help)}</p></details>`;

/** THE TABLE AT THE HEAD OF A PAGE WHERE A PERSON SIGNS, APPROVES OR GIVES A SECRET: the actual
 *  fields and their values, one row each (plain-page.css `.facts`). */
export function factsTable(rows: Fact[]): string {
  const row = ({ label, value, note, help }: Fact) =>
    // the "?" is beside the name it explains; open, its words take the row's whole width
    `<div><dt>${escapedText(label)}${help ? ` ${helpMark(label, help)}` : ""}</dt><dd>${value}${
      note ? `<span class="note">${note}</span>` : ""
    }</dd></div>`;
  return `<dl class="facts">${rows.map(row).join("")}</dl>`;
}

export function standalonePage(input: {
  title: string;
  head: string;
  body: string;
  wide?: boolean;
  /** Somebody asks for the mind (a consent, a device's sign-in): their tile, as HTML. The card's
   *  head is then the mind's mark and that tile side by side, and the card is `centered`. */
  asker?: string;
  /** The one mark at the card's head in place of iterate's, as HTML: an app's own icon. */
  mark?: string;
  centered?: boolean;
  /** The mind that the page is of, by its control-plane hostname: shown beside the mark, so that a
   *  person sees which mind they give a secret or an approval to. Text, escaped here. */
  mind?: string;
  /** The mind's own logo in place of iterate's mark, as HTML: a tile (plain-page.css `.tile`).
   *  It is the mind's side of the head where somebody asks for the mind, and the card's one mark
   *  where no `mark` is given. */
  mindMark?: string;
}) {
  const own = input.mindMark || MARK;
  const head = input.asker
    ? `<div class="asks">${own}<span class="joins" aria-hidden="true"></span>${input.asker}</div>`
    : input.mind
      ? `<div class="masthead">${own}<span class="mind">${escapedText(input.mind)}</span></div>`
      : input.mark || own;
  const card = input.wide
    ? ' class="wide"'
    : input.asker || input.centered
      ? ' class="centered"'
      : "";
  return `<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>${escapedText(input.title)}</title>${ICON}<style>${plainPageCss}</style>${input.head}</head><body><main${card}>${head}${input.body}</main></body></html>`;
}