// Shared bits for the views. import { el } from './ui.js'; import { ms } from './fmt.js'; /** The screen frame: toolbar, scrolling pane, status bar. Returns the parts. */ export function screen(root, toolbarItems) { const toolbar = el('div.toolbar', {}, ...toolbarItems); const body = el('div.content'); const pane = el('div.pane.fill', {}, body); const status = el('div.statusbar', {}, 'Loading…'); root.append(el('div.screen', {}, toolbar, pane, status)); return { toolbar, body, pane, status, setStatus(text, kind = '') { status.textContent = text; status.className = `statusbar${kind ? ` ${kind}` : ''}`; }, }; } /** replaceChildren without the "null" text nodes the DOM makes from empty slots. */ export function fill(node, ...children) { node.replaceChildren(...children.flat().filter((c) => c != null && c !== false)); return node; } export const section = (title, hint, ...extra) => el('div.section-head', {}, el('h2.section-title', {}, title), hint ? el('span.hint', {}, hint) : null, ...extra); export function table(headers, rows, { empty = 'Nothing to show.' } = {}) { if (!rows.length) return el('div.callout', {}, empty); return el('div.table-wrap', {}, el('table.table', {}, el('thead', {}, el('tr', {}, ...headers.map((h) => (typeof h === 'string' ? el('th', {}, h) : el('th', h.props || {}, h.text))))), el('tbody', {}, ...rows))); } export function errorBox(err, hint) { return el('div.callout.error', { role: 'alert' }, err.message || String(err), hint ? el('div.small.muted', {}, hint) : null); } /** A notice for AdGuard-backed views when it isn't set up or reachable. */ export function adguardProblem(err) { if (err.status === 503) { return el('div.empty', {}, el('p', {}, 'AdGuard Home isn’t connected yet.'), el('p.small', {}, 'Set ADGUARD_URL, ADGUARD_USERNAME and ADGUARD_PASSWORD in the hub’s .env, then restart the hub. Settings shows where that file is.'), el('a.btn', { href: '/settings' }, 'Open settings')); } return errorBox(err, err.status === 502 ? 'Check that AdGuard Home is running and the hub’s .env points at its web address.' : ''); } export const domainLink = (d) => el('a.domain', { href: `/domains/${encodeURIComponent(d)}` }, d); export function clientLink(ip, device, fallbackName) { const name = device?.name || fallbackName || ip; const href = device ? `/clients/${encodeURIComponent(device.key)}` : `/queries?ip=${encodeURIComponent(ip)}`; return el('span.chips', {}, el('a', { href, title: ip }, name), name !== ip ? el('span.muted.small', {}, ip) : null, device?.suspicious ? el('span.chip.danger', {}, 'Suspicious') : null); } export function resultChip(r) { if (r.blocked) return el('span.chip.danger', { title: r.rule || r.reason }, 'Blocked'); if (r.reason?.startsWith('Rewrite') || r.reason === 'FilteredSafeSearch') return el('span.chip.accent', { title: r.reason }, 'Rewritten'); if (r.reason === 'NotFilteredWhiteList') return el('span.chip.ok', { title: r.rule }, 'Allowed by rule'); if (r.status && r.status !== 'NOERROR') return el('span.chip.warn', {}, r.status); return el('span.chip', {}, 'OK'); } export const RANGES = [['1h', '1 h'], ['6h', '6 h'], ['24h', '24 h'], ['7d', '7 d'], ['30d', '30 d']]; export const SPANS = { '1h': 3600, '6h': 21600, '24h': 86400, '7d': 604800, '30d': 2592000 }; /** Re-run fn every `ms` while the tab is visible; returns a stop function. */ export function every(ms, fn) { const t = setInterval(() => { if (!document.hidden) fn(); }, ms); return () => clearInterval(t); } // Incident cells, shared by Network health and a probe's page. kind "report" rows are stretches // when a probe didn't report; the probe's local outage log confirms them when it reports again. const METHOD_NAMES = { icmp: 'Ping', http: 'HTTP', dns: 'DNS' }; export function incidentTarget(i) { if (i.kind === 'report') return el('span', {}, i.label); return el('span', {}, i.label, el('span.muted.small', {}, ` ${i.methods.map((m) => METHOD_NAMES[m] || m).join(', ')}`)); } export function incidentTypeText(i) { if (i.kind !== 'report') return undefined; return i.confirmed ? 'Hub unreachable' : 'Not reporting'; } export function incidentChecks(i) { if (i.kind === 'report') return i.confirmed ? `${i.failed} failed reports` : 'No reports'; return i.failed ? `${i.failed}/${i.samples} failed` : `${i.samples} slow`; } export function incidentDetail(i) { if (i.kind === 'report') { if (i.confirmed) return `The probe logged that it couldn’t reach the hub${i.error ? `: ${i.error}` : ''}`; return 'Kept if the probe’s log shows it couldn’t reach the hub, removed if it was off'; } return i.error || (i.max_latency ? `peak ${ms(i.max_latency)}` : ''); }