Files
smeta-web/static/admin2/app.js
T

63 lines
3.2 KiB
JavaScript

import { h, render } from '/static/admin2/vendor/preact.module.js';
import htm from '/static/admin2/vendor/htm.module.js';
import { SECTIONS, sectionFromPath, hrefFor } from '/static/admin2/router.js';
import { DayPage } from '/static/admin2/pages/day.js';
import { CustomersPage } from '/static/admin2/pages/customers.js';
import { MoneyPage } from '/static/admin2/pages/money.js';
import { WorkPage } from '/static/admin2/pages/work.js';
import { SystemPage } from '/static/admin2/pages/system.js';
const html = htm.bind(h);
const TITLES = {
today: ["Мой день", "Что требует внимания сегодня"],
customers: ["Клиенты", "Кто платит, кто ушёл и кого надо вернуть"],
money: ["Деньги", "Поступления, поток и обязательства"],
work: ["Работа", "Очередь, прогоны и сбои"],
system: ["Система", "Здоровье службы и настройки"],
};
const section = sectionFromPath(location.pathname);
const [title, subtitle] = TITLES[section]
|| [SECTIONS.find(([key]) => key === section)?.[1] || 'Раздел', ''];
const App = () => html`
<div class="shell">
<nav class="side" aria-label="Разделы">
<a class="brand" href=${hrefFor('today')}>smeta<span>·</span>кокпит</a>
${SECTIONS.map(([key, label, href]) => html`
<a key=${key} href=${href} class=${'nav-link' + (key === section ? ' active' : '')}
aria-current=${key === section ? 'page' : null}>${label}</a>`)}
</nav>
<main class="main">
<header class="top"><div><h1>${title}</h1><p>${subtitle}</p></div></header>
${section === 'today' ? html`<${DayPage} />`
: section === 'customers' ? html`<${CustomersPage} />`
: section === 'money' ? html`<${MoneyPage} />`
: section === 'work' ? html`<${WorkPage} />`
: section === 'system' ? html`<${SystemPage} />` : html`
<section class="card" aria-labelledby="placeholder">
<h2 id="placeholder">Раздел готовится</h2>
<p>Данные и действия подключит следующий этап.</p>
</section>`}
</main>
</div>`;
render(html`<${App} />`, document.getElementById('app'));
// P4.8: ошибки браузера — без PII, не мешают работе страницы.
function reportClientError(message, stack) {
if (!localStorage.getItem('token')) return;
try {
fetch('/api/client-error', {
method: 'POST',
headers: {'Content-Type': 'application/json',
Authorization: 'Bearer ' + localStorage.getItem('token')},
body: JSON.stringify({message: String(message).slice(0, 2000),
stack: String(stack || '').slice(0, 2000),
path: location.pathname}),
}).catch(() => {});
} catch (e) { /* отчёт об ошибке не должен ронять страницу */ }
}
window.addEventListener('error', (e) => reportClientError(e.message, e.error && e.error.stack));
window.addEventListener('unhandledrejection', (e) =>
reportClientError(e.reason && e.reason.message || e.reason, e.reason && e.reason.stack));