Files
FWS-DiscordBot/src/web/public/views/logs.js
T
nocodeandClaude Opus 5 06a9be0cba feat(web): serve the dashboard and its interface
The front-end is plain JavaScript served as is: no build step to run before
the bot starts, and no second toolchain to keep alive. A simple/advanced
switch hides the technical panels without ever standing in for a permission.

`npm run setup` starts the wizard alone, because the bot refuses to boot
without a token and the `.env` has to be writable from somewhere. When the
webhook and the dashboard share a port, a single HTTP server is started.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-23 20:09:14 +02:00

156 lines
4.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { api } from '../core/api.js';
import { formatBytes, formatDate, formatRelative, h, mount } from '../core/dom.js';
import { badge, button, card, renderAsync, select, table, toast } from '../core/ui.js';
/** Functional event log and daily technical log files. */
const LEVELS = { STAFF: 1, DEV: 2, OWNER: 3 };
function eventsCard() {
const host = h('div', { class: 'stack' });
let typeFilter = '';
const load = () =>
renderAsync(
host,
() => api.get('/api/logs/events', { type: typeFilter, limit: 150 }),
(data) =>
h(
'div',
{ class: 'stack' },
select(
[
{ value: '', label: 'Tous les événements' },
...data.types.map((row) => ({ value: row.type, label: `${row.type} (${row.count})` })),
],
{
value: typeFilter,
onChange: (event) => {
typeFilter = event.target.value;
load();
},
},
),
table(
[
{ label: 'Type', render: (event) => badge(event.type, 'info') },
{ label: 'Auteur', render: (event) => event.actor_id ?? '—' },
{
label: 'Détail',
render: (event) => (event.payload ? JSON.stringify(event.payload) : '—'),
},
{ label: 'Quand', className: 'nowrap', render: (event) => formatRelative(event.created_at) },
],
data.events,
{ empty: 'Aucun événement enregistré.' },
),
),
);
load();
return card('Événements fonctionnels', { subtitle: 'Ce que le bot a fait, et pour qui.' }, host);
}
function filesCard() {
const host = h('div', { class: 'stack' });
const viewer = h('div', { class: 'stack' });
let levelFilter = '';
const openFile = (name) =>
renderAsync(
viewer,
() => api.get(`/api/logs/files/${name}`, { lines: 400, level: levelFilter }),
(data) =>
card(
data.name,
{
actions: select(
[
{ value: '', label: 'Tous les niveaux' },
{ value: 'debug', label: 'debug' },
{ value: 'info', label: 'info' },
{ value: 'warn', label: 'warn' },
{ value: 'error', label: 'error' },
],
{
value: levelFilter,
onChange: (event) => {
levelFilter = event.target.value;
openFile(name);
},
},
),
},
h(
'div',
{ class: 'stack', style: { gap: '0.1rem', maxHeight: '28rem', overflowY: 'auto' } },
data.entries.length === 0
? h('p', { class: 'empty', text: 'Aucune ligne pour ce filtre.' })
: data.entries.map((entry) =>
h('div', {
class: `log-line log-${entry.level ?? 'info'}`,
text: `${entry.time ? formatDate(entry.time) : ''} [${entry.scope ?? '—'}] ${entry.message ?? ''}${
entry.meta ? ` ${JSON.stringify(entry.meta)}` : ''
}`,
}),
),
),
),
);
renderAsync(
host,
() => api.get('/api/logs/files'),
(data) =>
table(
[
{ label: 'Fichier', key: 'name' },
{ label: 'Taille', className: 'num', render: (file) => formatBytes(file.sizeBytes) },
{ label: 'Modifié', className: 'nowrap', render: (file) => formatRelative(file.modifiedAt) },
{ label: '', render: (file) => button('Ouvrir', () => openFile(file.name), { tone: 'ghost' }) },
],
data.files,
{ empty: 'Aucun fichier de log.' },
),
);
return h(
'div',
{ class: 'stack' },
card('Journaux techniques', { subtitle: 'Un fichier JSON par jour, secrets masqués.' }, host),
viewer,
);
}
export function render(host, { state }) {
mount(
host,
h(
'div',
{ class: 'stack' },
h(
'div',
{ class: 'page-head' },
h(
'div',
{},
h('h1', { text: 'Journaux' }),
h('p', { class: 'muted', text: 'Ce que le bot a fait et ce qu’il a rencontré.' }),
),
state.me.accessLevel >= LEVELS.OWNER &&
button(
'Purger les anciens événements',
async () => {
const result = await api.post('/api/settings/purge', {});
toast(`${result.events} événement(s) et ${result.sessions} session(s) supprimés.`);
render(host, { state });
},
{ tone: 'ghost' },
),
),
eventsCard(),
state.me.accessLevel >= LEVELS.DEV ? filesCard() : null,
),
);
}