Add optional sync: SyncClient with optimistic concurrency, settings UI, Django DELETE and 409, dev proxy, e2e with real backend

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
denis
2026-10-16 17:24:58 +02:00
parent aa2fd84352
commit 4689d25700
20 changed files with 949 additions and 16 deletions
+58
View File
@@ -7,6 +7,9 @@ import { el, uniqueId } from './dom.js';
export class SettingsPanel extends EventTarget {
#i18n;
#root;
#syncRoot = el('fieldset', { class: 'setting-group sync-group' });
#syncState = { enabled: false, token: null, status: 'off', error: null, lastSync: null };
#syncMessage = '';
/** @param {import('../i18n/i18n.js').I18n} i18n */
constructor(i18n) {
@@ -43,8 +46,17 @@ export class SettingsPanel extends EventTarget {
this.#field('settings.voiceRate', 'range', { value: settings.voiceRate, min: 0.5, max: 2, step: 0.1, key: 'voiceRate', number: true }),
this.#field('settings.author', 'checkbox', { value: settings.authorMode, key: 'authorMode' }),
this.#backupSection(),
this.#syncRoot,
settings.authorMode ? el('a', { class: 'btn author-link', href: 'author.html', text: t('settings.authorOpen') }) : null,
);
this.#renderSync();
}
/** Met à jour la section de synchronisation (état du SyncClient ; `message` : résultat d'une action). */
setSync(state, message = this.#syncMessage) {
this.#syncState = state;
this.#syncMessage = message;
this.#renderSync();
}
/** Redonne le focus à un réglage après un nouveau rendu (ex. changement de langue). */
@@ -80,6 +92,52 @@ export class SettingsPanel extends EventTarget {
);
}
#renderSync() {
const t = (key, params) => this.#i18n.t(key, params);
const state = this.#syncState;
const emit = (action, extra = {}) => this.dispatchEvent(new CustomEvent('sync', { detail: { action, ...extra } }));
const button = (key, action, extra) => el('button', { type: 'button', class: 'btn', 'data-action': action, text: t(key), onclick: () => emit(action, extra) });
const activeAction = this.#syncRoot.contains(document.activeElement) ? (document.activeElement.dataset.action ?? 'token') : null;
const children = [el('legend', { text: t('sync.title') }), el('p', { class: 'hint', text: t('sync.help') })];
if (!state.enabled) {
const id = uniqueId('sync-token');
const input = el('input', { id, type: 'text', autocomplete: 'off', spellcheck: 'false', class: 'token-input' });
children.push(
button('sync.enable', 'enable'),
el('div', { class: 'setting stacked' }, el('label', { for: id, text: t('sync.linkLabel') }), input),
el('button', { type: 'button', class: 'btn', 'data-action': 'link', text: t('sync.link'), onclick: () => emit('link', { token: input.value }) }),
);
} else {
const id = uniqueId('sync-mytoken');
const when = state.lastSync ? new Date(state.lastSync).toLocaleString(this.#i18n.lang) : null;
const status =
state.status === 'syncing'
? t('sync.status.syncing')
: state.status === 'error'
? t(`sync.error.${state.error}`)
: when
? t('sync.status.last', { when })
: t('sync.status.never');
children.push(
el('p', { class: 'sync-status', role: 'status', 'data-status': state.status, text: status }),
el('div', { class: 'setting stacked' }, el('label', { for: id, text: t('sync.tokenLabel') }), el('input', { id, type: 'text', readonly: true, class: 'token-input', value: state.token })),
el('p', { class: 'hint', text: t('sync.tokenHelp') }),
button('sync.copy', 'copy'),
button('sync.now', 'now'),
button('sync.disable', 'disable'),
button('sync.erase', 'erase'),
);
}
// Résultat de la dernière action (copie, jeton invalide…) : annoncé aux lecteurs d'écran.
children.push(el('p', { class: 'sync-message', role: 'status', text: this.#syncMessage }));
this.#syncRoot.replaceChildren(...children);
// Le rendu remplace les boutons : on redonne le focus à l'équivalent (ou au jeton) pour le clavier.
if (activeAction) {
(this.#syncRoot.querySelector(`[data-action="${activeAction}"]`) ?? this.#syncRoot.querySelector('.token-input'))?.focus();
}
}
#testButton(labelKey, kind) {
const button = el('button', {
type: 'button',