diff --git a/src/web/api/index.js b/src/web/api/index.js index f656efa..2f50d88 100644 --- a/src/web/api/index.js +++ b/src/web/api/index.js @@ -1,3 +1,6 @@ +import { readFileSync, readdirSync, statSync } from 'node:fs'; +import path from 'node:path'; +import { fileURLToPath } from 'node:url'; import { ACCESS_LEVELS } from '../../config/constants.js'; import { registerAuthRoutes } from '../auth/routes.js'; import { openEventStream } from '../events.js'; @@ -12,6 +15,42 @@ import { registerReleaseRoutes } from './releases.js'; import { registerRepositoryRoutes } from './repositories.js'; import { registerSettingsRoutes } from './settings.js'; +const here = path.dirname(fileURLToPath(import.meta.url)); + +const VERSION = (() => { + try { + return JSON.parse(readFileSync(path.join(here, '../../../package.json'), 'utf8')).version; + } catch { + return 'inconnue'; + } +})(); + +/** + * Newest file of the served interface. + * + * Deployments that ship an archive make it easy to look at a stale copy and + * wonder why a fix is missing; this gives the page something to state. + */ +const BUILD_AT = (() => { + const root = path.join(here, '../public'); + let newest = 0; + + const walk = (directory) => { + for (const entry of readdirSync(directory, { withFileTypes: true })) { + const target = path.join(directory, entry.name); + if (entry.isDirectory()) walk(target); + else newest = Math.max(newest, statSync(target).mtimeMs); + } + }; + + try { + walk(root); + } catch { + return null; + } + return newest > 0 ? new Date(newest).toISOString() : null; +})(); + /** * Route registration order matters only for readability: the router matches on * the full pattern, not on declaration order. @@ -25,6 +64,8 @@ export function registerApiRoutes(router, { setupOnly = false } = {}) { status: context.client?.isReady?.() ? 'ok' : 'starting', setupOnly: context.setupOnly, uptimeMs: Date.now() - context.startedAt, + version: VERSION, + buildAt: BUILD_AT, }), { public: true }, ); diff --git a/src/web/public/app.js b/src/web/public/app.js index ca2dc5b..b4f7de5 100644 --- a/src/web/public/app.js +++ b/src/web/public/app.js @@ -139,14 +139,17 @@ const currentRoute = () => { * Navigation only swaps the main area, so the highlight has to be refreshed on * its own — otherwise it keeps pointing at the page you arrived on. */ -function highlightNav() { +function highlightNav(scope = document) { const { segments } = currentRoute(); const active = segments[0] ?? ''; const route = ROUTES.find((entry) => entry.path === active); - for (const link of document.querySelectorAll('.nav a')) { - link.classList.toggle('active', link.getAttribute('href') === `#/${active}`); - if (link.getAttribute('href') === `#/${active}`) link.setAttribute('aria-current', 'page'); + for (const link of scope.querySelectorAll('.nav a')) { + // Compare the route the link carries, not its href: a trailing slash or a + // query string in the hash must not make an entry look inactive. + const current = link.dataset.route === active; + link.classList.toggle('active', current); + if (current) link.setAttribute('aria-current', 'page'); else link.removeAttribute('aria-current'); } document.title = route ? `${route.label} · FWS-Bot` : 'FWS-Bot'; @@ -209,7 +212,7 @@ function sidebar() { group.routes.map((route) => h( 'a', - { href: `#/${route.path}` }, + { href: `#/${route.path}`, dataset: { route: route.path } }, h('span', { class: 'nav-icon', text: route.icon }), h('span', { text: route.label }), ), @@ -226,6 +229,7 @@ function sidebar() { button('Avancé', () => setMode('advanced'), {}), ), liveIndicator(), + h('div', { class: 'build muted', text: '…' }), h( 'div', { class: 'viewer' }, @@ -320,9 +324,23 @@ export function render() { const shell = h('div', { class: 'shell' }, sidebar(), main); mount(app, shell); applyModeButtons(shell); + highlightNav(shell); renderView(main); } +/** Names the interface actually being served, archive deployments being easy to get wrong. */ +async function showBuild() { + const health = await api.get('/api/health').catch(() => null); + const node = qs('.build'); + if (!node || !health) return; + + const built = health.buildAt + ? new Date(health.buildAt).toLocaleString('fr-FR', { dateStyle: 'short', timeStyle: 'short' }) + : 'date inconnue'; + node.textContent = `v${health.version} · interface du ${built}`; + node.title = "Date du fichier d'interface le plus récent servi par le bot."; +} + async function boot() { document.body.dataset.mode = state.mode; document.documentElement.dataset.theme = state.theme; @@ -335,6 +353,7 @@ async function boot() { state.config = config; startLive(); + showBuild(); window.addEventListener('hashchange', () => { const main = qs('.main'); diff --git a/src/web/public/styles.css b/src/web/public/styles.css index 94b8da0..3fd602c 100644 --- a/src/web/public/styles.css +++ b/src/web/public/styles.css @@ -690,6 +690,12 @@ pre code { padding-left: 1.2rem; } +.build { + font-size: 0.7rem; + padding: 0 0.2rem; + opacity: 0.75; +} + .live { display: flex; align-items: center;