NT-Connections/public/admin.js
Aidan Dona b2f8e879ab Add puzzle hints and per-word descriptions with sources
Hint is revealed on demand below the board while playing. After the game
ends, each word's description and source link are listed below the
puzzle, and hovering a solved word highlights its entry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-13 16:44:07 -05:00

118 lines
6.7 KiB
JavaScript

const COLORS = ['yellow', 'green', 'blue', 'purple'];
const $app = document.getElementById('app');
let games = [];
const icon = (name) => el('i', { class: `ph-bold ph-${name}`, 'aria-hidden': 'true' });
const errorList = (errors) => el('ul', { class: 'errors', role: 'alert' }, errors.map((e) => el('li', {}, e)));
async function showList() {
const { ok, status, data } = await api('/api/admin/games');
if (status === 401) return showLogin();
if (!ok) return $app.replaceChildren(el('div', { class: 'empty' }, 'Could not load puzzles. Try refreshing.'));
games = data;
const items = games.length
? games.map((g) => el('li', {}, el('div', { class: 'item' },
el('div', {}, el('strong', {}, g.title), el('div', { class: 'meta' }, g.date ? fmtDate(g.date) : 'No date (list only)')),
el('div', { class: 'row-actions' },
el('a', { class: 'btn icon-btn', href: `/play/${g.id}`, 'aria-label': `Play ${g.title}` }, icon('arrow-right')),
el('button', { class: 'btn icon-btn', 'aria-label': `Edit ${g.title}`, onclick: () => showEditor(g) }, icon('pencil-simple')),
el('button', { class: 'btn icon-btn danger', 'aria-label': `Delete ${g.title}`, onclick: () => remove(g) }, icon('trash'))))))
: [el('li', { class: 'empty' }, 'No puzzles yet. Create your first one.')];
$app.replaceChildren(
el('h1', {}, 'Your puzzles'),
el('p', { class: 'sub' }, 'Dated puzzles appear on their day. Future ones stay hidden until then.'),
el('div', { class: 'actions', style: 'justify-content:flex-start;margin-bottom:20px' },
el('button', { class: 'btn primary', onclick: () => showEditor() }, icon('plus'), 'New puzzle'),
el('button', { class: 'btn', onclick: logout }, icon('sign-out'), 'Log out')),
el('ul', { class: 'list' }, items),
);
}
function showLogin(error) {
const input = el('input', { id: 'pw', type: 'password', autocomplete: 'current-password', required: true });
$app.replaceChildren(
el('h1', {}, 'Admin login'),
el('p', { class: 'sub' }, 'Only the puzzle editor needs this.'),
el('form', { onsubmit: async (e) => {
e.preventDefault();
const btn = e.submitter; btn.disabled = true;
const { ok, data } = await api('/api/login', { method: 'POST', body: { password: input.value } });
ok ? showList() : showLogin(data.error || 'Login failed.');
} },
el('div', { class: 'field' }, el('label', { for: 'pw' }, 'Password'), input,
error ? el('p', { class: 'help', style: 'color:var(--error)', role: 'alert' }, error) : null),
el('button', { class: 'btn primary', type: 'submit' }, 'Log in')),
);
input.focus();
}
function showEditor(game) {
const g = game || { title: '', date: '', hint: '', groups: COLORS.map((color) => ({ name: '', color, words: ['', '', '', ''] })) };
const field = (id, label, input, help) =>
el('div', { class: 'field' }, el('label', { for: id }, label), input, help ? el('p', { class: 'help' }, help) : null);
const title = el('input', { id: 'title', value: g.title, maxlength: 100, required: true });
const date = el('input', { id: 'date', type: 'date', value: g.date || '' });
const hint = el('input', { id: 'hint', value: g.hint || '', maxlength: 200 });
const errBox = el('div');
const groupEls = g.groups.map((grp, i) => {
const n = i + 1;
const name = el('input', { id: `g${n}-name`, value: grp.name, maxlength: 80, required: true });
const color = el('select', { id: `g${n}-color` }, COLORS.map((c) => el('option', { value: c, selected: c === grp.color }, c[0].toUpperCase() + c.slice(1))));
const words = grp.words.map((w, j) => el('input', { value: w, maxlength: 30, required: true, 'aria-label': `Group ${n} word ${j + 1}` }));
const descs = grp.words.map((_, j) => el('input', { value: grp.details?.[j]?.description || '', maxlength: 500, placeholder: 'Description (optional)', 'aria-label': `Group ${n} word ${j + 1} description` }));
const sources = grp.words.map((_, j) => el('input', { type: 'url', value: grp.details?.[j]?.source || '', maxlength: 500, placeholder: 'https://source (optional)', 'aria-label': `Group ${n} word ${j + 1} source link` }));
const box = el('fieldset', { class: 'group-edit' },
el('legend', { class: 'sr' }, `Group ${n}`),
el('div', { class: 'head' },
field(`g${n}-name`, el('span', {}, el('span', { class: `swatch ${grp.color}` }), `Group ${n} name`), name),
field(`g${n}-color`, 'Color', color)),
el('label', {}, 'Words'),
el('p', { class: 'help', style: 'margin-bottom:8px' }, 'Descriptions and sources show below the puzzle once it ends.'),
el('div', { class: 'words' }, words.map((w, j) => el('div', { class: 'word-row' }, w, descs[j], sources[j]))));
color.addEventListener('change', () => { box.querySelector('.swatch').className = `swatch ${color.value}`; });
return { box, read: () => ({
name: name.value, color: color.value, words: words.map((w) => w.value),
details: words.map((_, j) => ({ description: descs[j].value, source: sources[j].value })),
}) };
});
$app.replaceChildren(
el('a', { href: '/admin', class: 'btn', style: 'margin-bottom:20px', onclick: (e) => { e.preventDefault(); showList(); } }, icon('arrow-left'), 'Back'),
el('h1', {}, game ? 'Edit puzzle' : 'New puzzle'),
el('p', { class: 'sub' }, 'Yellow is the easiest group, purple the trickiest.'),
el('form', { novalidate: true, onsubmit: async (e) => {
e.preventDefault();
const btn = e.submitter; btn.disabled = true;
const body = { title: title.value, date: date.value || null, hint: hint.value, groups: groupEls.map((x) => x.read()) };
const { ok, data } = await api(game ? `/api/admin/games/${game.id}` : '/api/admin/games', { method: game ? 'PUT' : 'POST', body });
btn.disabled = false;
if (ok) { toast('Saved'); return showList(); }
if (data.error) return showLogin(data.error);
errBox.replaceChildren(errorList(data.errors || ['Could not save.']));
errBox.scrollIntoView({ block: 'center' });
} },
errBox,
field('title', 'Title', title),
field('date', 'Date', date, 'Optional. The puzzle becomes the daily puzzle on this date.'),
field('hint', 'Hint', hint, 'Optional. Players can reveal it below the puzzle while playing.'),
groupEls.map((x) => x.box),
el('div', { class: 'actions', style: 'justify-content:flex-start' }, el('button', { class: 'btn primary', type: 'submit' }, icon('check'), 'Save puzzle'))),
);
}
async function remove(g) {
if (!confirm(`Delete "${g.title}"? This cannot be undone.`)) return;
const { ok } = await api(`/api/admin/games/${g.id}`, { method: 'DELETE' });
toast(ok ? 'Deleted' : 'Delete failed');
showList();
}
async function logout() {
await api('/api/logout', { method: 'POST' });
showLogin();
}
showList();