feat(settings): make the server settings real, and widen them
The dashboard offered seven switches that nothing read: they promised a behaviour the bot did not have, which is worse than offering nothing. Every setting is now declared once, with its type, its bounds and its explanation, and the services read it: the welcome message and its wording, the automatic role, departure notices, the private notice sent to a sanctioned member, the default mute duration, how much history a ban erases, whether a closed ticket is deleted or archived, and an optional role mention on the Gitea notifications. A test walks the sources and fails when a declared key is read nowhere, so the list cannot drift back into decoration. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
13 files changed
+576
-146
No files matched your search
@@ -33,47 +33,91 @@ const keyValue = (rows) =>
|
||||
rows,
|
||||
);
|
||||
|
||||
function behaviourCard(data, level, reload) {
|
||||
if (level < LEVELS.DEV) {
|
||||
return card(
|
||||
'Comportement du bot',
|
||||
{ subtitle: 'Lecture seule : le niveau Dev est requis pour modifier ces réglages.' },
|
||||
keyValue(
|
||||
data.schema.map((setting) => ({
|
||||
label: setting.label,
|
||||
value: String(data.values[setting.key]),
|
||||
})),
|
||||
),
|
||||
/** One control per setting, typed after its declaration. */
|
||||
function settingControl(setting, value, apply) {
|
||||
if (setting.type === 'bool') {
|
||||
return h(
|
||||
'div',
|
||||
{},
|
||||
toggle(Boolean(value), apply, setting.label),
|
||||
h('div', { class: 'field-help', text: setting.help }),
|
||||
);
|
||||
}
|
||||
|
||||
return card(
|
||||
'Comportement du bot',
|
||||
{ subtitle: 'Appliqué immédiatement, sans redémarrage.' },
|
||||
...data.schema.map((setting) => {
|
||||
const apply = async (value) => {
|
||||
await api.put(`/api/settings/values/${setting.key}`, { value });
|
||||
toast(`« ${setting.label} » enregistré.`);
|
||||
};
|
||||
if (setting.type === 'enum') {
|
||||
return field(
|
||||
setting.label,
|
||||
select(
|
||||
setting.options.map((option) => ({ value: option, label: option })),
|
||||
{ value, onChange: (event) => apply(event.target.value) },
|
||||
),
|
||||
{ help: setting.help },
|
||||
);
|
||||
}
|
||||
|
||||
if (setting.type === 'bool') {
|
||||
return h(
|
||||
'div',
|
||||
{},
|
||||
toggle(Boolean(data.values[setting.key]), apply, setting.label),
|
||||
h('div', { class: 'field-help', text: setting.help }),
|
||||
);
|
||||
}
|
||||
return field(
|
||||
setting.label,
|
||||
select(
|
||||
setting.options.map((option) => ({ value: option, label: option })),
|
||||
{ value: data.values[setting.key], onChange: (event) => apply(event.target.value) },
|
||||
),
|
||||
{ help: setting.help },
|
||||
const control = input({
|
||||
value: value ?? '',
|
||||
type: setting.type === 'int' ? 'number' : 'text',
|
||||
min: setting.min,
|
||||
max: setting.max,
|
||||
maxlength: setting.maxLength,
|
||||
placeholder: String(setting.default ?? ''),
|
||||
});
|
||||
|
||||
// Text fields save on blur rather than on every keystroke.
|
||||
const commit = () => apply(setting.type === 'int' ? Number(control.value) : control.value);
|
||||
control.addEventListener('change', commit);
|
||||
control.addEventListener('keydown', (event) => {
|
||||
if (event.key === 'Enter') control.blur();
|
||||
});
|
||||
|
||||
return field(setting.label, control, { help: setting.help });
|
||||
}
|
||||
|
||||
function behaviourCard(data, level, reload) {
|
||||
const editable = level >= LEVELS.DEV;
|
||||
const sections = data.sections ?? [];
|
||||
|
||||
return h(
|
||||
'div',
|
||||
{ class: 'stack' },
|
||||
...sections.map((section) => {
|
||||
const settings = data.schema.filter((setting) => setting.section === section.id);
|
||||
if (settings.length === 0) return null;
|
||||
|
||||
return card(
|
||||
section.title,
|
||||
{ subtitle: section.summary },
|
||||
...settings.map((setting) => {
|
||||
const value = data.values[setting.key];
|
||||
|
||||
if (!editable) {
|
||||
return h(
|
||||
'div',
|
||||
{ class: setting.advanced ? 'advanced-only' : '' },
|
||||
h('strong', { text: `${setting.label} : ` }),
|
||||
h('span', { text: String(value) }),
|
||||
h('div', { class: 'field-help', text: setting.help }),
|
||||
);
|
||||
}
|
||||
|
||||
const apply = async (next) => {
|
||||
try {
|
||||
const saved = await api.put(`/api/settings/values/${setting.key}`, { value: next });
|
||||
toast(`« ${setting.label} » enregistré.`);
|
||||
data.values[setting.key] = saved.value;
|
||||
} catch (error) {
|
||||
toast(error.message, 'error');
|
||||
}
|
||||
};
|
||||
|
||||
const node = settingControl(setting, value, apply);
|
||||
if (setting.advanced) node.classList.add('advanced-only');
|
||||
return node;
|
||||
}),
|
||||
);
|
||||
}),
|
||||
button('Recharger', reload, { tone: 'ghost' }),
|
||||
editable && button('Recharger les réglages', reload, { tone: 'ghost' }),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user