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>
This commit is contained in:
23 files changed
+5850
-2
No files matched your search
@@ -0,0 +1,301 @@
|
||||
import { api } from './core/api.js';
|
||||
import { h, mount, qs } from './core/dom.js';
|
||||
import { ACCESS_LABELS, button, spinner, toast } from './core/ui.js';
|
||||
|
||||
/** Shell, navigation and hash routing of the dashboard (M16-T11). */
|
||||
|
||||
const LEVELS = { USER: 0, STAFF: 1, DEV: 2, OWNER: 3 };
|
||||
|
||||
/**
|
||||
* `module` is imported on demand, so a view is only downloaded when visited.
|
||||
* `level` hides a page the visitor could not use anyway; the server enforces it too.
|
||||
*/
|
||||
const ROUTES = [
|
||||
{
|
||||
path: '',
|
||||
icon: '◎',
|
||||
label: 'Vue d’ensemble',
|
||||
group: 'Pilotage',
|
||||
module: './views/overview.js',
|
||||
level: LEVELS.STAFF,
|
||||
},
|
||||
{
|
||||
path: 'org',
|
||||
icon: '⬡',
|
||||
label: 'Organisation',
|
||||
group: 'Pilotage',
|
||||
module: './views/organization.js',
|
||||
level: LEVELS.STAFF,
|
||||
},
|
||||
{
|
||||
path: 'repos',
|
||||
icon: '◫',
|
||||
label: 'Dépôts',
|
||||
group: 'Code',
|
||||
module: './views/repositories.js',
|
||||
level: LEVELS.STAFF,
|
||||
},
|
||||
{
|
||||
path: 'releases',
|
||||
icon: '⬀',
|
||||
label: 'Releases',
|
||||
group: 'Code',
|
||||
module: './views/releases.js',
|
||||
level: LEVELS.STAFF,
|
||||
},
|
||||
{
|
||||
path: 'deploy',
|
||||
icon: '⚙',
|
||||
label: 'Déploiements',
|
||||
group: 'Code',
|
||||
module: './views/deployments.js',
|
||||
level: LEVELS.STAFF,
|
||||
},
|
||||
{
|
||||
path: 'discord',
|
||||
icon: '☰',
|
||||
label: 'Serveur Discord',
|
||||
group: 'Communauté',
|
||||
module: './views/discord.js',
|
||||
level: LEVELS.STAFF,
|
||||
},
|
||||
{
|
||||
path: 'community',
|
||||
icon: '✦',
|
||||
label: 'Tickets & vocaux',
|
||||
group: 'Communauté',
|
||||
module: './views/community.js',
|
||||
level: LEVELS.STAFF,
|
||||
},
|
||||
{
|
||||
path: 'logs',
|
||||
icon: '≡',
|
||||
label: 'Journaux',
|
||||
group: 'Système',
|
||||
module: './views/logs.js',
|
||||
level: LEVELS.STAFF,
|
||||
advanced: true,
|
||||
},
|
||||
{
|
||||
path: 'settings',
|
||||
icon: '⚒',
|
||||
label: 'Paramètres',
|
||||
group: 'Système',
|
||||
module: './views/settings.js',
|
||||
level: LEVELS.STAFF,
|
||||
},
|
||||
{
|
||||
path: 'setup',
|
||||
icon: '✎',
|
||||
label: 'Configuration .env',
|
||||
group: 'Système',
|
||||
module: './views/setup.js',
|
||||
level: LEVELS.OWNER,
|
||||
},
|
||||
];
|
||||
|
||||
export const state = {
|
||||
me: null,
|
||||
config: null,
|
||||
mode: localStorage.getItem('fws.mode') ?? 'simple',
|
||||
theme: localStorage.getItem('fws.theme') ?? 'dark',
|
||||
/** Repository selected in the Releases and Deployments pages. */
|
||||
repository: localStorage.getItem('fws.repository') ?? '',
|
||||
};
|
||||
|
||||
export function setMode(mode) {
|
||||
state.mode = mode;
|
||||
localStorage.setItem('fws.mode', mode);
|
||||
document.body.dataset.mode = mode;
|
||||
render();
|
||||
}
|
||||
|
||||
export function setTheme(theme) {
|
||||
state.theme = theme;
|
||||
localStorage.setItem('fws.theme', theme);
|
||||
document.documentElement.dataset.theme = theme;
|
||||
}
|
||||
|
||||
export function setRepository(fullName) {
|
||||
state.repository = fullName;
|
||||
localStorage.setItem('fws.repository', fullName);
|
||||
}
|
||||
|
||||
export const navigate = (path) => {
|
||||
window.location.hash = `#/${path}`;
|
||||
};
|
||||
|
||||
const currentRoute = () => {
|
||||
const raw = window.location.hash.replace(/^#\/?/, '');
|
||||
const [path] = raw.split('?');
|
||||
const segments = path.split('/').filter(Boolean);
|
||||
return { segments, path };
|
||||
};
|
||||
|
||||
function sidebar() {
|
||||
const { segments } = currentRoute();
|
||||
const active = segments[0] ?? '';
|
||||
const visible = ROUTES.filter(
|
||||
(route) => state.me.accessLevel >= route.level && (!route.advanced || state.mode === 'advanced'),
|
||||
);
|
||||
|
||||
const groups = [];
|
||||
for (const route of visible) {
|
||||
let group = groups.find((entry) => entry.name === route.group);
|
||||
if (!group) groups.push((group = { name: route.group, routes: [] }));
|
||||
group.routes.push(route);
|
||||
}
|
||||
|
||||
return h(
|
||||
'aside',
|
||||
{ class: 'sidebar' },
|
||||
h(
|
||||
'a',
|
||||
{ class: 'brand', href: '#/' },
|
||||
h('img', { src: 'favicon.svg', alt: '' }),
|
||||
h('div', {}, h('strong', { text: 'FWS-Bot' }), h('span', { text: 'Tableau de bord' })),
|
||||
),
|
||||
h(
|
||||
'nav',
|
||||
{ class: 'nav' },
|
||||
groups.map((group) => [
|
||||
h('div', { class: 'nav-group', text: group.name }),
|
||||
group.routes.map((route) =>
|
||||
h(
|
||||
'a',
|
||||
{ href: `#/${route.path}`, class: route.path === active ? 'active' : '' },
|
||||
h('span', { class: 'nav-icon', text: route.icon }),
|
||||
h('span', { text: route.label }),
|
||||
),
|
||||
),
|
||||
]),
|
||||
),
|
||||
h(
|
||||
'div',
|
||||
{ class: 'sidebar-foot' },
|
||||
h(
|
||||
'div',
|
||||
{ class: 'mode-switch', role: 'group', 'aria-label': 'Mode d’affichage' },
|
||||
button('Simple', () => setMode('simple'), {}),
|
||||
button('Avancé', () => setMode('advanced'), {}),
|
||||
),
|
||||
h(
|
||||
'div',
|
||||
{ class: 'viewer' },
|
||||
state.me.avatar && h('img', { src: state.me.avatar, alt: '' }),
|
||||
h(
|
||||
'div',
|
||||
{},
|
||||
h('strong', { text: state.me.displayName ?? state.me.username ?? 'Invité' }),
|
||||
h('span', { class: 'muted', text: ACCESS_LABELS[state.me.accessLevel] ?? '—' }),
|
||||
),
|
||||
),
|
||||
h(
|
||||
'div',
|
||||
{ class: 'row' },
|
||||
button(
|
||||
state.theme === 'dark' ? '☀ Clair' : '☾ Sombre',
|
||||
() => {
|
||||
setTheme(state.theme === 'dark' ? 'light' : 'dark');
|
||||
render();
|
||||
},
|
||||
{ tone: 'ghost' },
|
||||
),
|
||||
button(
|
||||
'Déconnexion',
|
||||
async () => {
|
||||
await api.post('/api/auth/logout');
|
||||
window.location.reload();
|
||||
},
|
||||
{ tone: 'ghost' },
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
function applyModeButtons(node) {
|
||||
for (const control of node.querySelectorAll('.mode-switch button')) {
|
||||
control.classList.toggle(
|
||||
'active',
|
||||
control.textContent === (state.mode === 'simple' ? 'Simple' : 'Avancé'),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async function renderView(host) {
|
||||
const { segments } = currentRoute();
|
||||
const route = ROUTES.find((entry) => entry.path === (segments[0] ?? ''));
|
||||
|
||||
if (!route) {
|
||||
mount(host, h('div', { class: 'notice notice-warning' }, h('p', { text: 'Page inconnue.' })));
|
||||
return;
|
||||
}
|
||||
if (state.me.accessLevel < route.level) {
|
||||
mount(
|
||||
host,
|
||||
h(
|
||||
'div',
|
||||
{ class: 'notice notice-error' },
|
||||
h('p', { text: 'Cette page demande un niveau d’accès supérieur.' }),
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
mount(host, spinner());
|
||||
try {
|
||||
const module = await import(route.module);
|
||||
await module.render(host, { params: segments.slice(1), state });
|
||||
} catch (error) {
|
||||
mount(host, h('div', { class: 'notice notice-error' }, h('p', { text: error.message })));
|
||||
}
|
||||
}
|
||||
|
||||
export function render() {
|
||||
const app = qs('#app');
|
||||
|
||||
if (!state.me?.authenticated) {
|
||||
import('./views/login.js').then((module) => module.render(app, { state }));
|
||||
return;
|
||||
}
|
||||
|
||||
// The bootstrap session may only reach the wizard, so it never gets a shell.
|
||||
if (state.me.source === 'setup') {
|
||||
import('./views/setup.js').then((module) => module.render(app, { state, standalone: true }));
|
||||
return;
|
||||
}
|
||||
|
||||
const main = h('main', { class: 'main' });
|
||||
const shell = h('div', { class: 'shell' }, sidebar(), main);
|
||||
mount(app, shell);
|
||||
applyModeButtons(shell);
|
||||
renderView(main);
|
||||
}
|
||||
|
||||
async function boot() {
|
||||
document.body.dataset.mode = state.mode;
|
||||
document.documentElement.dataset.theme = state.theme;
|
||||
|
||||
const [me, config] = await Promise.all([
|
||||
api.get('/api/me').catch(() => ({ authenticated: false })),
|
||||
api.get('/api/auth/config').catch(() => ({})),
|
||||
]);
|
||||
state.me = me;
|
||||
state.config = config;
|
||||
|
||||
window.addEventListener('hashchange', () => {
|
||||
if (state.me?.authenticated && state.me.source !== 'setup') renderView(qs('.main'));
|
||||
else render();
|
||||
});
|
||||
window.addEventListener('unhandledrejection', (event) => {
|
||||
if (event.reason?.status === 401) {
|
||||
state.me = { authenticated: false };
|
||||
render();
|
||||
} else if (event.reason instanceof Error) toast(event.reason.message, 'error');
|
||||
});
|
||||
|
||||
render();
|
||||
}
|
||||
|
||||
boot();
|
||||
Reference in new issue
Block a user