feat(админка): переходы между разделами без перезагрузки
Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
+42
-7
@@ -1,4 +1,5 @@
|
||||
import { h, render } from '/static/admin2/vendor/preact.module.js';
|
||||
import { useEffect, useRef, useState } from '/static/admin2/vendor/hooks.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';
|
||||
@@ -15,20 +16,53 @@ const TITLES = {
|
||||
work: ["Работа", "Очередь, прогоны и сбои"],
|
||||
system: ["Система", "Здоровье службы и настройки"],
|
||||
};
|
||||
const section = sectionFromPath(location.pathname);
|
||||
const [title, subtitle] = TITLES[section]
|
||||
|| [SECTIONS.find(([key]) => key === section)?.[1] || 'Раздел', ''];
|
||||
const App = () => {
|
||||
const [section, setSection] = useState(sectionFromPath(location.pathname));
|
||||
const firstRender = useRef(true);
|
||||
const [title, subtitle] = TITLES[section]
|
||||
|| [SECTIONS.find(([key]) => key === section)?.[1] || 'Раздел', ''];
|
||||
|
||||
const App = () => html`
|
||||
// Назад/вперёд в браузере меняют адрес без клика по меню — забираем тот же переход.
|
||||
useEffect(() => {
|
||||
const onPop = () => setSection(sectionFromPath(location.pathname));
|
||||
window.addEventListener('popstate', onPop);
|
||||
return () => window.removeEventListener('popstate', onPop);
|
||||
}, []);
|
||||
|
||||
// Заголовок вкладки и фокус на заголовок раздела — только при переходе, не при
|
||||
// первой загрузке: иначе фокус отбирается у адресной строки на пустом месте.
|
||||
useEffect(() => {
|
||||
document.title = title + ' — Кокпит';
|
||||
if (firstRender.current) { firstRender.current = false; return; }
|
||||
const h1 = document.querySelector('main h1');
|
||||
if (h1) h1.focus();
|
||||
window.scrollTo(0, 0);
|
||||
}, [section, title]);
|
||||
|
||||
// Оболочка остаётся, меняется только правая часть: обычный переход перезагружал
|
||||
// документ и пересоздавал меню. Средний клик и Ctrl/Cmd/Shift оставляем браузеру —
|
||||
// человек вправе открыть раздел в новой вкладке.
|
||||
const go = (event, href) => {
|
||||
if (event.defaultPrevented || event.button !== 0
|
||||
|| event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return;
|
||||
event.preventDefault();
|
||||
if (location.pathname === href) return;
|
||||
history.pushState({}, '', href);
|
||||
setSection(sectionFromPath(href));
|
||||
};
|
||||
|
||||
return html`
|
||||
<div class="shell">
|
||||
<nav class="side" aria-label="Разделы">
|
||||
<a class="brand" href=${hrefFor('today')}>smeta<span>·</span>кокпит</a>
|
||||
<a class="brand" href=${hrefFor('today')}
|
||||
onClick=${(e) => go(e, hrefFor('today'))}>smeta<span>·</span>кокпит</a>
|
||||
${SECTIONS.map(([key, label, href]) => html`
|
||||
<a key=${key} href=${href} class=${'nav-link' + (key === section ? ' active' : '')}
|
||||
<a key=${key} href=${href} onClick=${(e) => go(e, 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>
|
||||
<header class="top"><div><h1 tabIndex="-1">${title}</h1><p>${subtitle}</p></div></header>
|
||||
${section === 'today' ? html`<${DayPage} />`
|
||||
: section === 'customers' ? html`<${CustomersPage} />`
|
||||
: section === 'money' ? html`<${MoneyPage} />`
|
||||
@@ -40,6 +74,7 @@ const App = () => html`
|
||||
</section>`}
|
||||
</main>
|
||||
</div>`;
|
||||
};
|
||||
|
||||
render(html`<${App} />`, document.getElementById('app'));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user