fix(rules): show the rules field with a save button and edit the message

The rules lived in a card of their own that never showed up next to the
settings, so the section read as an empty toggle nobody could fill in. The
markdown now renders as a real field with an "Enregistrer" button, and
saving edits the message already pinned instead of deleting it and
posting a new one: same link, same place, pin kept.
This commit is contained in:
BlackAngelTV committed 2026-09-25 21:14:00 +02:00
1 parent 22e9723700
commit e6ce594b34
7 files changed
+156 -168

No files matched your search

+39 -88
View File
@@ -56,6 +56,30 @@ function settingControl(setting, value, apply) {
);
}
// Markdown is a long text: a single-line input would hide what is written.
if (setting.type === 'markdown') {
const editor = textarea({
value: value ?? '',
rows: 12,
maxlength: setting.maxLength,
placeholder: '## Règlement\n\n1. …\n2. …',
});
return field(
setting.label,
h(
'div',
{ class: 'stack' },
editor,
h(
'div',
{ class: 'row' },
button('Enregistrer', () => apply(editor.value), { tone: 'primary' }),
),
),
{ help: setting.help },
);
}
const control = input({
value: value ?? '',
type: setting.type === 'int' ? 'number' : 'text',
@@ -77,11 +101,7 @@ function settingControl(setting, value, apply) {
function behaviourCard(data, level, reload) {
const editable = level >= LEVELS.DEV;
// The rules get their own card: a plain text field would not carry the
// markdown editor nor the publishing button.
const schema = (Array.isArray(data.schema) ? data.schema : []).filter(
(setting) => setting.key !== 'rules.markdown',
);
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
@@ -115,6 +135,20 @@ function behaviourCard(data, level, reload) {
const apply = async (next) => {
try {
// The rules are a message on Discord, not a stored value: saving
// them has to publish them.
if (setting.key === 'rules.markdown') {
const published = await api.post('/api/discord/rules', { markdown: next });
values[setting.key] = next;
toast(
published.edited
? 'Règlement mis à jour dans le salon des règles.'
: 'Règlement publié et épinglé dans le salon des règles.',
published.pinned ? 'success' : 'info',
);
return;
}
const saved = await api.put(`/api/settings/values/${setting.key}`, { value: next });
toast(`« ${setting.label} » enregistré.`);
values[setting.key] = saved.value;
@@ -497,88 +531,6 @@ function referenceCard(data) {
);
}
/**
* Writes the rules in markdown and republishes them: the previous message is
* deleted, the new one is sent and pinned in `CHANNEL_RULES_ID`.
*/
function rulesCard(level, reload) {
const host = h('div', { class: 'stack' });
const editable = level >= LEVELS.DEV;
renderAsync(
host,
() => api.get('/api/discord/rules'),
(data) => {
const editor = textarea({
value: data.markdown ?? '',
placeholder: '## Règlement\n\n1. …\n2. …',
rows: 12,
maxlength: 4000,
disabled: !editable,
});
const counter = h('span', { class: 'field-help' });
const refresh = () => {
counter.textContent = `${editor.value.length} / 4000 caractères`;
};
editor.addEventListener('input', refresh);
refresh();
return h(
'div',
{ class: 'stack' },
!data.configured &&
notice(
'Aucun salon des règles dans le .env : renseigne `CHANNEL_RULES_ID` puis redémarre le bot.',
'error',
),
field('Règlement (markdown)', editor, {
help: 'Markdown de Discord : **gras**, *italique*, `code`, listes, titres. Publié et épinglé dans le salon des règles.',
}),
counter,
h(
'div',
{ class: 'row' },
editable &&
button(
'Publier le règlement',
async (event) => {
// The button stays disabled until the answer arrives: a second
// click would publish the rules twice.
event.currentTarget.disabled = true;
try {
const result = await api.post('/api/discord/rules', { markdown: editor.value });
toast(
result.pinned
? 'Règlement publié et épinglé.'
: 'Règlement publié, mais l’épinglage a échoué.',
result.pinned ? 'success' : 'info',
);
reload();
} catch (error) {
toast(error.message, 'error');
event.currentTarget.disabled = false;
}
},
{ tone: 'primary', disabled: !data.configured },
),
data.publishedAt
? h('span', {
class: 'muted',
text: `Dernier règlement publié ${formatRelative(data.publishedAt)}.`,
})
: h('span', { class: 'muted', text: 'Aucun règlement publié pour l’instant.' }),
),
);
},
);
return card(
'Règlement du serveur',
{ subtitle: 'Un seul message, épinglé dans le salon des règles, remis à jour à chaque publication.' },
host,
);
}
export function render(host, { state }) {
const level = state.me.accessLevel;
const reload = () => render(host, { state });
@@ -619,7 +571,6 @@ export function render(host, { state }) {
),
behaviourCard(data, level, reload),
rulesCard(level, reload),
connectionsCard(data),
level >= LEVELS.OWNER ? sessionsCard(reload) : null,
level >= LEVELS.OWNER ? tokensCard(reload) : null,