import { LANGS, type Lang, type PluralKey, type TextKey, t, tPlural } from "./i18n.ts"; const ESCAPES: Record = { "&": "&", "<": "<", ">": ">", '"': """, "'": "'", }; export const escapeHtml = (text: string) => text.replace(/[&<>"']/g, (ch) => ESCAPES[ch] ?? ch); export const STYLESHEET = ` :root { color-scheme: light dark; --bg: #f4f5f7; --card: #fff; --text: #1c1e21; --muted: #5f6672; --line: #d5d9e0; --accent: #2457d6; --accent-text: #fff; --error-bg: #fdecec; --error-text: #a11a1a; --lock-bg: #fff3d1; --lock-text: #5c3d00; --lock-line: #b97800; } @media (prefers-color-scheme: dark) { :root { --bg: #15171a; --card: #1f2226; --text: #eceef1; --muted: #a3aab5; --line: #3a3f47; --accent: #6f95f5; --accent-text: #10131a; --error-bg: #3b1c1c; --error-text: #ffb4b4; --lock-bg: #3a2e10; --lock-text: #ffdd99; --lock-line: #e0a422; } } * { box-sizing: border-box; } body { margin: 0; min-height: 100vh; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 16px; padding: 24px 16px; background: var(--bg); color: var(--text); font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; } main { width: 100%; max-width: 400px; padding: 32px 28px; background: var(--card); border: 1px solid var(--line); border-radius: 14px; } h1 { margin: 0 0 8px; font-size: 26px; line-height: 1.2; overflow-wrap: anywhere; } p { margin: 0 0 16px; } .lead { color: var(--muted); } .lead:last-child { margin-bottom: 0; } .error { padding: 10px 12px; border-radius: 8px; font-size: 15px; } .error { background: var(--error-bg); color: var(--error-text); } .error.locked { background: var(--lock-bg); color: var(--lock-text); font-weight: 600; border-left: 5px solid var(--lock-line); } .field-error { margin: 0 0 16px; font-size: 14px; color: var(--error-text); } label { display: block; margin: 0 0 6px; font-weight: 600; font-size: 15px; } input { width: 100%; margin: 0 0 16px; padding: 11px 12px; font: inherit; color: inherit; background: transparent; border: 1px solid var(--line); border-radius: 8px; } input[aria-invalid] { margin-bottom: 6px; border-color: var(--error-text); } input:focus-visible, button:focus-visible, a:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; } button, .button { display: block; width: 100%; padding: 12px; font: inherit; font-weight: 600; text-align: center; text-decoration: none; color: var(--accent-text); background: var(--accent); border: 0; border-radius: 8px; cursor: pointer; } form { margin: 0; } nav { display: flex; gap: 14px; font-size: 14px; color: var(--muted); } nav a { color: var(--accent); } nav [aria-current] { color: var(--text); font-weight: 600; text-decoration: none; } `; interface PageOptions { lang: Lang; path: string; title: string; body: string; /** Через сколько секунд браузер сам откроет страницу `path` заново. */ refreshSeconds?: number; } function page({ lang, path, title, body, refreshSeconds }: PageOptions): string { const refresh = refreshSeconds === undefined ? "" : `\n`; const switcher = LANGS.map(({ code, name }) => code === lang ? `${name}` : `${name}`, ).join(""); return ` ${refresh} ${escapeHtml(title)}
${body}
`; } export interface LoginPageOptions { lang: Lang; login?: string; /** Сообщение над формой; `plural` — текст с числом, форма слова зависит от `count`. */ error?: | { key: TextKey; params?: Record } | { plural: PluralKey; param: string; count: number }; /** Незаполненные поля: сообщение показывается под каждым из них, а не над формой. */ emptyFields?: { login: boolean; password: boolean }; /** Время, когда блокировка снимется («05:15 GMT+5»): дописывается к сообщению над формой. */ unlockAt?: string; /** Через сколько секунд блокировка снимется: страница сама откроется заново без сообщения. */ refreshSeconds?: number; } export function loginPage(options: LoginPageOptions): string { const { lang, login = "", error, emptyFields, unlockAt, refreshSeconds } = options; const e = (key: TextKey, params?: Record) => escapeHtml(t(lang, key, params)); const errorMessage = error && ("plural" in error ? tPlural(lang, error.plural, error.param, error.count) : t(lang, error.key, error.params)); const until = unlockAt ? ` ${t(lang, "login.errorLockedUntil", { time: unlockAt })}` : ""; const errorText = errorMessage && escapeHtml(errorMessage + until); // Блокировка выглядит иначе, чем неверный пароль: другой цвет, полоса слева, жирный текст. const errorClass = error && "plural" in error ? "error locked" : "error"; // Поле с сообщением под ним: рамка выделена, сообщение привязано к полю для чтения с экрана. const invalid = (id: string) => ` aria-invalid="true" aria-describedby="${id}-error"`; const fieldError = (id: string, key: TextKey) => `\n

${e(key)}

`; const loginEmpty = emptyFields?.login ?? false; const passwordEmpty = emptyFields?.password ?? false; const body = `

${e("login.title")}

${e("login.lead")}

${errorText ? `` : ""}
${loginEmpty ? fieldError("login", "login.errorLoginEmpty") : ""} ${passwordEmpty ? fieldError("password", "login.errorPasswordEmpty") : ""}
`; return page({ lang, path: "/login", title: t(lang, "login.title"), body, refreshSeconds }); } export function homePage({ lang, login }: { lang: Lang; login: string }): string { const e = (key: TextKey, params?: Record) => escapeHtml(t(lang, key, params)); const body = `

${e("home.signedInAs", { login })}

${e("home.lead")}

`; return page({ lang, path: "/", title: t(lang, "home.title"), body }); } /** Страница с одним сообщением: «страницы нет» или «что-то пошло не так». */ export function messagePage(options: { lang: Lang; path: string; title: TextKey; text: TextKey; link?: TextKey; }): string { const { lang } = options; const body = `

${escapeHtml(t(lang, options.title))}

${escapeHtml(t(lang, options.text))}

${options.link ? `${escapeHtml(t(lang, options.link))}` : ""}`; return page({ lang, path: options.path, title: t(lang, options.title), body }); }