fix(web): stop the settings page breaking on an unexpected payload

Every list the page walks was read straight off the response, so a single
missing field took the whole view down with `undefined.map` — which reads as
a broken feature rather than as what it is, a page and an API disagreeing on
the shape of the data.

The page now normalises what it receives: absent lists render empty, an enum
without options falls back to a text field, and settings that arrive without
sections are shown under one heading instead of not at all.

Errors of that family also say so, and point at the reload that fixes them.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
nocodeandClaude Opus 5 committed 2026-09-23 22:31:44 +02:00
1 parent 2b4e6f356a
commit a1fb846d51
3 files changed
+58 -21

No files matched your search

+2 -1
View File
@@ -310,7 +310,8 @@ async function renderView(host) {
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 })));
const { errorBox } = await import('./core/ui.js');
mount(host, errorBox(error.message, { retry: () => renderView(host) }));
}
}
+18 -1
View File
@@ -146,11 +146,28 @@ export function table(columns, rows, { empty = 'Rien à afficher pour le moment.
export const spinner = (label = 'Chargement…') =>
h('div', { class: 'loading' }, h('span', { class: 'spinner' }), h('span', { text: label }));
/**
* A `undefined is not a function` reaching the screen almost always means the
* page and the API disagree on the shape of the data — which reads as a broken
* feature unless the box says otherwise.
*/
const looksLikeVersionMismatch = (message) =>
/Cannot read propert|undefined is not|is not a function|of undefined/i.test(String(message));
export const errorBox = (message, { retry } = {}) =>
h(
'div',
{ class: 'notice notice-error' },
h('p', { text: message }),
h(
'div',
{},
h('p', { text: message }),
looksLikeVersionMismatch(message) &&
h('p', {
class: 'muted',
text: 'Cette erreur vient en général d’une interface plus ancienne que l’API : recharge avec Ctrl+Shift+R, et si elle persiste, re-téléverse le dossier src/web/public.',
}),
),
retry && button('Réessayer', retry, { tone: 'ghost' }),
);
+38 -19
View File
@@ -44,7 +44,7 @@ function settingControl(setting, value, apply) {
);
}
if (setting.type === 'enum') {
if (setting.type === 'enum' && Array.isArray(setting.options)) {
return field(
setting.label,
select(
@@ -76,20 +76,27 @@ function settingControl(setting, value, apply) {
function behaviourCard(data, level, reload) {
const editable = level >= LEVELS.DEV;
const sections = data.sections ?? [];
const schema = Array.isArray(data.schema) ? data.schema : [];
const values = data.values ?? {};
// Falling back to one section keeps the page useful against an API that does
// not group its settings, rather than rendering nothing at all.
const sections = Array.isArray(data.sections)
? data.sections
: [{ id: null, title: 'Comportement du bot', summary: null }];
return h(
'div',
{ class: 'stack' },
...sections.map((section) => {
const settings = data.schema.filter((setting) => setting.section === section.id);
const settings = section.id ? schema.filter((setting) => setting.section === section.id) : schema;
if (settings.length === 0) return null;
return card(
section.title,
{ subtitle: section.summary },
...settings.map((setting) => {
const value = data.values[setting.key];
const value = values[setting.key];
if (!editable) {
return h(
@@ -105,7 +112,7 @@ function behaviourCard(data, level, reload) {
try {
const saved = await api.put(`/api/settings/values/${setting.key}`, { value: next });
toast(`« ${setting.label} » enregistré.`);
data.values[setting.key] = saved.value;
values[setting.key] = saved.value;
} catch (error) {
toast(error.message, 'error');
}
@@ -131,7 +138,13 @@ const testButton = (service, label) =>
{ tone: 'ghost' },
);
function connectionsCard(data) {
function connectionsCard(raw) {
const data = {
discord: raw.discord ?? {},
gitea: raw.gitea ?? {},
webhook: raw.webhook ?? {},
web: raw.web ?? {},
};
return h(
'div',
{ class: 'stack' },
@@ -149,13 +162,13 @@ function connectionsCard(data) {
h(
'div',
{ class: 'advanced-only' },
keyValue(Object.entries(data.discord.roles).map(([label, value]) => ({ label, value }))),
keyValue(Object.entries(data.discord.roles ?? {}).map(([label, value]) => ({ label, value }))),
),
h('h3', { class: 'advanced-only', text: 'Salons' }),
h(
'div',
{ class: 'advanced-only' },
keyValue(Object.entries(data.discord.channels).map(([label, value]) => ({ label, value }))),
keyValue(Object.entries(data.discord.channels ?? {}).map(([label, value]) => ({ label, value }))),
),
),
@@ -397,14 +410,15 @@ function tokensCard(reload) {
}
function maintenanceCard(data) {
const runtime = data.runtime ?? {};
return card(
'Maintenance',
{ subtitle: `Base : ${data.runtime.databasePath}` },
{ subtitle: `Base : ${runtime.databasePath ?? '—'}` },
grid(
stat('Environnement', data.runtime.nodeEnv),
stat('Niveau de log', data.runtime.logLevel),
stat('Rétention', `${data.runtime.logRetentionDays} jours`),
stat('PID', data.runtime.pid),
stat('Environnement', runtime.nodeEnv),
stat('Niveau de log', runtime.logLevel),
stat('Rétention', `${runtime.logRetentionDays ?? '—'} jours`),
stat('PID', runtime.pid),
),
h(
'div',
@@ -425,7 +439,7 @@ function maintenanceCard(data) {
},
{ tone: 'ghost' },
),
data.web.allowRestart &&
data.web?.allowRestart &&
button(
'Redémarrer le bot',
async () => {
@@ -441,12 +455,13 @@ function maintenanceCard(data) {
{ tone: 'danger' },
),
),
!data.web.allowRestart &&
!data.web?.allowRestart &&
h('p', { class: 'muted', text: 'Le redémarrage depuis le site est désactivé (`WEB_ALLOW_RESTART`).' }),
);
}
function referenceCard(data) {
const reference = data.reference ?? {};
return card(
'Référence',
{ advanced: true, subtitle: 'Ce que le bot connaît, pour se repérer.' },
@@ -457,7 +472,7 @@ function referenceCard(data) {
{ label: 'Valeur', className: 'num', key: 'value' },
{ label: 'Libellé Discord', key: 'label' },
],
data.reference.accessLevels,
reference.accessLevels ?? [],
),
h('h3', { text: 'Catégories de tickets' }),
table(
@@ -466,13 +481,13 @@ function referenceCard(data) {
{ label: 'Libellé', key: 'label' },
{ label: 'Visible par', render: (category) => category.scope.join(', ') },
],
data.reference.ticketCategories,
reference.ticketCategories ?? [],
),
h('h3', { text: 'Types de notification' }),
h(
'div',
{ class: 'row' },
data.reference.notificationTypes.map((type) => badge(type, 'info')),
(reference.notificationTypes ?? []).map((type) => badge(type, 'info')),
),
);
}
@@ -503,7 +518,11 @@ export function render(host, { state }) {
}),
),
level >= LEVELS.OWNER &&
h('a', { class: 'btn btn-primary', href: '#/setup', text: 'Assistant de configuration .env' }),
h('a', {
class: 'btn btn-primary',
href: '#/setup',
text: 'Assistant de configuration .env',
}),
),
level >= LEVELS.OWNER &&