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>
123 lines
4.2 KiB
JavaScript
123 lines
4.2 KiB
JavaScript
/**
|
|
* Minimal DOM helpers. No framework, no build step: the dashboard ships the
|
|
* files it serves (M16-T11).
|
|
*/
|
|
|
|
/**
|
|
* `h('div', { class: 'card' }, h('h2', { text: 'Titre' }))`
|
|
* Text always goes through `textContent`, so API values can never inject markup.
|
|
*/
|
|
export function h(tag, props = {}, ...children) {
|
|
const node = document.createElement(tag);
|
|
|
|
for (const [key, value] of Object.entries(props ?? {})) {
|
|
if (value === null || value === undefined || value === false) continue;
|
|
|
|
if (key === 'text') node.textContent = String(value);
|
|
else if (key === 'class') node.className = value;
|
|
else if (key === 'dataset') Object.assign(node.dataset, value);
|
|
else if (key === 'style') Object.assign(node.style, value);
|
|
else if (key.startsWith('on') && typeof value === 'function') {
|
|
node.addEventListener(key.slice(2).toLowerCase(), value);
|
|
} else if (key === 'value' || key === 'checked' || key === 'disabled' || key === 'selected') {
|
|
node[key] = value;
|
|
} else node.setAttribute(key, value === true ? '' : String(value));
|
|
}
|
|
|
|
for (const child of children.flat(Infinity)) {
|
|
if (child === null || child === undefined || child === false) continue;
|
|
node.append(child instanceof Node ? child : document.createTextNode(String(child)));
|
|
}
|
|
return node;
|
|
}
|
|
|
|
export const clear = (node) => {
|
|
while (node.firstChild) node.firstChild.remove();
|
|
return node;
|
|
};
|
|
|
|
export const mount = (node, ...children) => {
|
|
clear(node);
|
|
node.append(...children.flat(Infinity).filter(Boolean));
|
|
return node;
|
|
};
|
|
|
|
export const qs = (selector, scope = document) => scope.querySelector(selector);
|
|
|
|
/** Escapes then applies the small subset of Markdown used by Gitea bodies. */
|
|
export function markdown(text) {
|
|
const escaped = String(text ?? '')
|
|
.replaceAll('&', '&')
|
|
.replaceAll('<', '<')
|
|
.replaceAll('>', '>');
|
|
|
|
return escaped
|
|
.replace(/```([\s\S]*?)```/g, (_, code) => `<pre><code>${code.trim()}</code></pre>`)
|
|
.replace(/`([^`\n]+)`/g, '<code>$1</code>')
|
|
.replace(/^### (.+)$/gm, '<h4>$1</h4>')
|
|
.replace(/^## (.+)$/gm, '<h3>$1</h3>')
|
|
.replace(/^# (.+)$/gm, '<h3>$1</h3>')
|
|
.replace(/\*\*([^*\n]+)\*\*/g, '<strong>$1</strong>')
|
|
.replace(/^[-*] (.+)$/gm, '<li>$1</li>')
|
|
.replace(/(<li>[\s\S]*?<\/li>)/g, '<ul>$1</ul>')
|
|
.replace(/\n{2,}/g, '<br><br>');
|
|
}
|
|
|
|
/** Renders trusted Markdown built above; never call it with raw HTML. */
|
|
export function richText(text, className = 'rich') {
|
|
const node = h('div', { class: className });
|
|
node.innerHTML = markdown(text);
|
|
return node;
|
|
}
|
|
|
|
export const formatDate = (value) => {
|
|
if (!value) return '—';
|
|
const date = new Date(String(value).includes('T') ? value : `${value}Z`);
|
|
if (Number.isNaN(date.getTime())) return String(value);
|
|
return date.toLocaleString('fr-FR', { dateStyle: 'medium', timeStyle: 'short' });
|
|
};
|
|
|
|
export function formatRelative(value) {
|
|
if (!value) return '—';
|
|
const date = new Date(String(value).includes('T') ? value : `${value}Z`);
|
|
if (Number.isNaN(date.getTime())) return String(value);
|
|
|
|
const seconds = Math.round((date.getTime() - Date.now()) / 1000);
|
|
const units = [
|
|
['year', 31_536_000],
|
|
['month', 2_592_000],
|
|
['day', 86_400],
|
|
['hour', 3600],
|
|
['minute', 60],
|
|
];
|
|
const formatter = new Intl.RelativeTimeFormat('fr', { numeric: 'auto' });
|
|
|
|
for (const [unit, size] of units) {
|
|
if (Math.abs(seconds) >= size) return formatter.format(Math.round(seconds / size), unit);
|
|
}
|
|
return formatter.format(seconds, 'second');
|
|
}
|
|
|
|
export const formatBytes = (bytes) => {
|
|
const value = Number(bytes) || 0;
|
|
const units = ['o', 'Ko', 'Mo', 'Go'];
|
|
let index = 0;
|
|
let result = value;
|
|
while (result >= 1024 && index < units.length - 1) {
|
|
result /= 1024;
|
|
index += 1;
|
|
}
|
|
return `${result.toFixed(index === 0 ? 0 : 1)} ${units[index]}`;
|
|
};
|
|
|
|
export const formatDuration = (ms) => {
|
|
const seconds = Math.floor(Number(ms) / 1000);
|
|
const days = Math.floor(seconds / 86_400);
|
|
const hours = Math.floor((seconds % 86_400) / 3600);
|
|
const minutes = Math.floor((seconds % 3600) / 60);
|
|
if (days > 0) return `${days} j ${hours} h`;
|
|
if (hours > 0) return `${hours} h ${minutes} min`;
|
|
if (minutes > 0) return `${minutes} min`;
|
|
return `${seconds} s`;
|
|
};
|