Files
FWS-DiscordBot/src/web/public/core/ui.js
T
nocodeandClaude Opus 5 d439b1b3e8 feat(web): make deploying reachable from where it is looked for
The « Déployer » button only existed on the row of an already created
target, so an empty list was a dead end whose only way out sat in the page
header. The empty state now carries the action that fills it.

A release also gains its own deploy button: wanting to ship a given version
is the moment people look for it, and sending them to another page to retype
the tag was needless.

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

234 lines
7.2 KiB
JavaScript

import { clear, h, mount } from './dom.js';
/** Shared widgets: cards, tables, badges, toasts, modals (M16-T11). */
export const ACCESS_LABELS = { 0: 'Membre', 1: 'Staff', 2: 'Dev', 3: 'Owner' };
export function card(title, { subtitle, actions, advanced = false } = {}, ...children) {
return h(
'section',
{ class: advanced ? 'card advanced-only' : 'card' },
(title || actions) &&
h(
'header',
{ class: 'card-head' },
h(
'div',
{},
title && h('h2', { text: title }),
subtitle && h('p', { class: 'muted', text: subtitle }),
),
actions && h('div', { class: 'card-actions' }, actions),
),
h('div', { class: 'card-body' }, ...children),
);
}
export const stat = (label, value, { hint, tone = 'neutral' } = {}) =>
h(
'div',
{ class: `stat stat-${tone}` },
h('span', { class: 'stat-label', text: label }),
h('strong', { class: 'stat-value', text: value ?? '—' }),
hint && h('span', { class: 'stat-hint', text: hint }),
);
export const badge = (text, tone = 'neutral') => h('span', { class: `badge badge-${tone}`, text });
export const button = (label, onClick, { tone = 'ghost', disabled = false, title, type = 'button' } = {}) =>
h('button', { class: `btn btn-${tone}`, onClick, disabled, title, type, text: label });
export function field(label, control, { help, error } = {}) {
return h(
'label',
{ class: error ? 'field field-error' : 'field' },
h('span', { class: 'field-label', text: label }),
control,
help && h('span', { class: 'field-help', text: help }),
error && h('span', { class: 'field-error-text', text: error }),
);
}
export const input = (props = {}) => h('input', { class: 'input', ...props });
export const textarea = (props = {}) => h('textarea', { class: 'input textarea', ...props });
export function select(options, props = {}) {
return h(
'select',
{ class: 'input', ...props },
options.map((option) =>
h('option', {
value: option.value,
text: option.label,
selected: option.value === props.value,
}),
),
);
}
export function toggle(checked, onChange, label) {
return h(
'label',
{ class: 'toggle' },
h('input', { type: 'checkbox', checked, onChange: (event) => onChange(event.target.checked) }),
h('span', { class: 'toggle-track' }),
label && h('span', { text: label }),
);
}
/**
* Renders a table. Each column is `{ key, label, render?, className? }`;
* `render` returns a Node or a string.
*/
export function table(columns, rows, { empty = 'Rien à afficher pour le moment.' } = {}) {
if (!rows || rows.length === 0) {
// An empty state is where people look for the action that fills it, so it
// accepts a node and not only a sentence.
return empty instanceof Node
? h('div', { class: 'empty' }, empty)
: h('p', { class: 'empty', text: empty });
}
return h(
'div',
{ class: 'table-wrap' },
h(
'table',
{ class: 'table' },
h(
'thead',
{},
h(
'tr',
{},
columns.map((column) => h('th', { class: column.className ?? '', text: column.label })),
),
),
h(
'tbody',
{},
rows.map((row) =>
h(
'tr',
{},
columns.map((column) => {
const cell = h('td', { class: column.className ?? '' });
const value = column.render ? column.render(row) : row[column.key];
if (value instanceof Node) cell.append(value);
else cell.textContent = value === null || value === undefined ? '—' : String(value);
return cell;
}),
),
),
),
),
);
}
export const spinner = (label = 'Chargement…') =>
h('div', { class: 'loading' }, h('span', { class: 'spinner' }), h('span', { text: label }));
export const errorBox = (message, { retry } = {}) =>
h(
'div',
{ class: 'notice notice-error' },
h('p', { text: message }),
retry && button('Réessayer', retry, { tone: 'ghost' }),
);
export const notice = (message, tone = 'info') =>
h('div', { class: `notice notice-${tone}` }, h('p', { text: message }));
let toastHost;
export function toast(message, tone = 'success') {
toastHost ??= document.body.appendChild(h('div', { class: 'toast-host' }));
const node = h('div', { class: `toast toast-${tone}`, text: message });
toastHost.append(node);
setTimeout(() => node.classList.add('toast-out'), 3600);
setTimeout(() => node.remove(), 4200);
}
/** Promise-based modal. Resolves with the value passed to `close`. */
export function modal(title, buildBody, { confirmLabel = 'Confirmer', tone = 'primary' } = {}) {
return new Promise((resolve) => {
const overlay = h('div', { class: 'overlay' });
const close = (value) => {
overlay.remove();
document.removeEventListener('keydown', onKey);
resolve(value);
};
const onKey = (event) => {
if (event.key === 'Escape') close(null);
};
const body = buildBody(close);
const footer = h(
'footer',
{ class: 'modal-foot' },
button('Annuler', () => close(null), { tone: 'ghost' }),
button(confirmLabel, () => close(body.submit ? body.submit() : true), { tone }),
);
overlay.append(
h(
'div',
{ class: 'modal' },
h('header', { class: 'modal-head' }, h('h2', { text: title })),
h('div', { class: 'modal-body' }, body.node ?? body),
footer,
),
);
overlay.addEventListener('click', (event) => {
if (event.target === overlay) close(null);
});
document.addEventListener('keydown', onKey);
document.body.append(overlay);
const firstInput = overlay.querySelector('input, textarea, select');
firstInput?.focus();
});
}
/** Confirmation that requires typing an exact phrase, for irreversible actions. */
export function confirmDangerous(title, message, phrase) {
return modal(
title,
() => {
const control = input({ placeholder: phrase, autocomplete: 'off' });
const node = h(
'div',
{},
h('p', { text: message }),
h('p', { class: 'muted', text: `Tape « ${phrase} » pour confirmer.` }),
control,
);
return { node, submit: () => control.value.trim() === phrase };
},
{ confirmLabel: 'Supprimer', tone: 'danger' },
);
}
export const confirmAction = (title, message) =>
modal(title, () => ({ node: h('p', { text: message }), submit: () => true }));
/**
* Renders an async view into a host node, showing a loader then the result.
* Any thrown error becomes a retry box instead of a blank page.
*/
export async function renderAsync(host, load, build) {
mount(host, spinner());
try {
const data = await load();
mount(host, await build(data));
} catch (error) {
mount(host, errorBox(error.message, { retry: () => renderAsync(host, load, build) }));
}
return host;
}
export const section = (...children) => h('div', { class: 'stack' }, ...children);
export const row = (...children) => h('div', { class: 'row' }, ...children);
export const grid = (...children) => h('div', { class: 'grid' }, ...children);
export { clear, mount };