One `EventSource` for the whole dashboard, held in a single slot: a view that re-renders itself would otherwise register a second listener each time and double the work on every event. A refresh landing under someone's fingers is worse than data a few seconds old, so it waits while a dialog is open or a field has focus, a burst of writes collapses into one reload, and a background tab closes its stream. The Discord page stays off the tick because listing members calls the Discord API. When the stream cannot be established a timer takes over. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
386 lines
12 KiB
JavaScript
386 lines
12 KiB
JavaScript
import { api } from '../core/api.js';
|
||
import { autoRefresh } from '../core/live.js';
|
||
import { formatRelative, h } from '../core/dom.js';
|
||
import {
|
||
badge,
|
||
button,
|
||
card,
|
||
confirmAction,
|
||
field,
|
||
input,
|
||
modal,
|
||
notice,
|
||
renderAsync,
|
||
select,
|
||
table,
|
||
toast,
|
||
} from '../core/ui.js';
|
||
|
||
/** Tickets, polls, temporary voice channels and the bot presence rotation. */
|
||
|
||
const LEVELS = { STAFF: 1, DEV: 2, OWNER: 3 };
|
||
|
||
const STATUS_TYPES = [
|
||
{ value: 'playing', label: 'joue à' },
|
||
{ value: 'watching', label: 'regarde' },
|
||
{ value: 'listening', label: 'écoute' },
|
||
{ value: 'competing', label: 'participe à' },
|
||
{ value: 'streaming', label: 'en direct sur' },
|
||
];
|
||
|
||
function ticketsCard(data, level, reload) {
|
||
const labels = Object.fromEntries(data.categories.map((category) => [category.id, category.label]));
|
||
|
||
return card(
|
||
'Tickets',
|
||
{ subtitle: `${data.tickets.filter((ticket) => ticket.status === 'open').length} ticket(s) ouvert(s)` },
|
||
table(
|
||
[
|
||
{ label: '#', className: 'num', key: 'id' },
|
||
{ label: 'Catégorie', render: (ticket) => labels[ticket.category_id] ?? ticket.category_id },
|
||
{ label: 'Auteur', render: (ticket) => ticket.author_id },
|
||
{
|
||
label: 'État',
|
||
render: (ticket) =>
|
||
ticket.status === 'open' ? badge('Ouvert', 'success') : badge('Clôturé', 'neutral'),
|
||
},
|
||
{
|
||
label: 'Prise en charge',
|
||
className: 'advanced-only',
|
||
render: (ticket) => ticket.claimed_by ?? '—',
|
||
},
|
||
{ label: 'Ouvert', className: 'nowrap', render: (ticket) => formatRelative(ticket.created_at) },
|
||
{
|
||
label: '',
|
||
render: (ticket) =>
|
||
level >= LEVELS.STAFF &&
|
||
ticket.status === 'open' &&
|
||
button(
|
||
'Clôturer',
|
||
async () => {
|
||
const reason = input({ placeholder: 'Motif de clôture' });
|
||
const confirmed = await modal(
|
||
`Clôturer le ticket #${ticket.id}`,
|
||
() => ({
|
||
node: h(
|
||
'div',
|
||
{ class: 'stack' },
|
||
h('p', { text: 'Le salon privé associé sera supprimé.' }),
|
||
field('Motif', reason),
|
||
),
|
||
submit: () => true,
|
||
}),
|
||
{ confirmLabel: 'Clôturer', tone: 'danger' },
|
||
);
|
||
if (!confirmed) return;
|
||
await api.post(`/api/community/tickets/${ticket.id}/close`, { reason: reason.value });
|
||
toast('Ticket clôturé.');
|
||
reload();
|
||
},
|
||
{ tone: 'ghost' },
|
||
),
|
||
},
|
||
],
|
||
data.tickets,
|
||
{ empty: 'Aucun ticket.' },
|
||
),
|
||
);
|
||
}
|
||
|
||
function pollsCard(polls, level, reload) {
|
||
return card(
|
||
'Sondages',
|
||
{ subtitle: `${polls.filter((poll) => poll.status === 'open').length} sondage(s) en cours` },
|
||
table(
|
||
[
|
||
{ label: '#', className: 'num', key: 'id' },
|
||
{ label: 'Question', key: 'question' },
|
||
{
|
||
label: 'Résultats',
|
||
render: (poll) => {
|
||
const counts = Object.fromEntries(poll.counts.map((row) => [row.choice_index, row.count]));
|
||
const total = poll.counts.reduce((sum, row) => sum + row.count, 0) || 1;
|
||
return h(
|
||
'div',
|
||
{ class: 'stack', style: { gap: '0.2rem' } },
|
||
poll.choices.map((choice, index) =>
|
||
h(
|
||
'div',
|
||
{},
|
||
h(
|
||
'div',
|
||
{ class: 'row', style: { justifyContent: 'space-between', fontSize: '0.8rem' } },
|
||
h('span', { text: choice }),
|
||
h('span', { class: 'muted', text: `${counts[index] ?? 0}` }),
|
||
),
|
||
h(
|
||
'div',
|
||
{ class: 'bar' },
|
||
h('span', { style: { width: `${((counts[index] ?? 0) / total) * 100}%` } }),
|
||
),
|
||
),
|
||
),
|
||
);
|
||
},
|
||
},
|
||
{
|
||
label: 'État',
|
||
render: (poll) => (poll.status === 'open' ? badge('Ouvert', 'success') : badge('Clos', 'neutral')),
|
||
},
|
||
{ label: 'Fin', className: 'nowrap advanced-only', render: (poll) => formatRelative(poll.ends_at) },
|
||
{
|
||
label: '',
|
||
render: (poll) =>
|
||
level >= LEVELS.STAFF &&
|
||
poll.status === 'open' &&
|
||
button(
|
||
'Clore',
|
||
async () => {
|
||
if (!(await confirmAction('Clore le sondage', poll.question))) return;
|
||
await api.post(`/api/community/polls/${poll.id}/close`);
|
||
toast('Sondage clos.');
|
||
reload();
|
||
},
|
||
{ tone: 'ghost' },
|
||
),
|
||
},
|
||
],
|
||
polls,
|
||
{ empty: 'Aucun sondage.' },
|
||
),
|
||
);
|
||
}
|
||
|
||
function voiceCard(voice, level, reload) {
|
||
return card(
|
||
'Salons vocaux temporaires',
|
||
{ subtitle: `${voice.channels.filter((channel) => channel.alive).length} salon(s) actif(s)` },
|
||
h('h3', { text: 'Déclencheurs' }),
|
||
table(
|
||
[
|
||
{ label: 'Salon déclencheur', render: (trigger) => trigger.label || trigger.triggerId },
|
||
{ label: 'Modèle de nom', render: (trigger) => trigger.template ?? '—' },
|
||
{ label: 'Limite', className: 'num', render: (trigger) => trigger.userLimit || '∞' },
|
||
{
|
||
label: 'Actif',
|
||
render: (trigger) => (trigger.enabled ? badge('Oui', 'success') : badge('En pause', 'neutral')),
|
||
},
|
||
{ label: 'Salons vivants', className: 'num', key: 'activeChannels' },
|
||
{
|
||
label: '',
|
||
render: (trigger) =>
|
||
level >= LEVELS.DEV &&
|
||
button(
|
||
trigger.enabled ? 'Mettre en pause' : 'Réactiver',
|
||
async () => {
|
||
await api.post(`/api/community/voice/triggers/${trigger.triggerId}/toggle`, {
|
||
enabled: !trigger.enabled,
|
||
});
|
||
toast('Déclencheur mis à jour.');
|
||
reload();
|
||
},
|
||
{ tone: 'ghost' },
|
||
),
|
||
},
|
||
],
|
||
voice.triggers,
|
||
{
|
||
empty:
|
||
'Aucun déclencheur configuré. Utilise `/voice setup` sur Discord ou renseigne CHANNEL_VOICE_TRIGGER_ID.',
|
||
},
|
||
),
|
||
|
||
h('h3', { text: 'Salons générés' }),
|
||
table(
|
||
[
|
||
{ label: 'Salon', key: 'name' },
|
||
{ label: 'Propriétaire', render: (channel) => channel.owner_id },
|
||
{ label: 'Occupants', className: 'num', key: 'members' },
|
||
{
|
||
label: 'État',
|
||
render: (channel) =>
|
||
h(
|
||
'div',
|
||
{ class: 'row' },
|
||
channel.alive ? badge('Vivant', 'success') : badge('Fantôme', 'warning'),
|
||
channel.locked && badge('Verrouillé', 'info'),
|
||
),
|
||
},
|
||
{ label: 'Créé', className: 'nowrap', render: (channel) => formatRelative(channel.created_at) },
|
||
{
|
||
label: '',
|
||
render: (channel) =>
|
||
level >= LEVELS.STAFF &&
|
||
button(
|
||
'Supprimer',
|
||
async () => {
|
||
if (!(await confirmAction('Supprimer le salon', channel.name))) return;
|
||
await api.delete(`/api/community/voice/channels/${channel.channel_id}`);
|
||
toast('Salon supprimé.');
|
||
reload();
|
||
},
|
||
{ tone: 'danger' },
|
||
),
|
||
},
|
||
],
|
||
voice.channels,
|
||
{ empty: 'Aucun salon temporaire pour l’instant.' },
|
||
),
|
||
);
|
||
}
|
||
|
||
function statusesCard(statuses, intervalSeconds, level, reload) {
|
||
const interval = input({ type: 'number', value: intervalSeconds, min: 5, style: { maxWidth: '8rem' } });
|
||
|
||
return card(
|
||
'Présence du bot',
|
||
{
|
||
subtitle: 'Les statuts défilent dans l’ordre d’ajout.',
|
||
actions:
|
||
level >= LEVELS.DEV &&
|
||
h(
|
||
'div',
|
||
{ class: 'row' },
|
||
button(
|
||
'Ajouter un statut',
|
||
async () => {
|
||
const text = input({ placeholder: 'les issues de FWS Arch' });
|
||
const type = select(STATUS_TYPES, { value: 'watching' });
|
||
const url = input({ placeholder: 'https://twitch.tv/… (type « en direct » uniquement)' });
|
||
|
||
const confirmed = await modal(
|
||
'Nouveau statut',
|
||
() => ({
|
||
node: h(
|
||
'div',
|
||
{ class: 'stack' },
|
||
field('Type', type),
|
||
field('Texte', text),
|
||
field('Lien', url),
|
||
),
|
||
submit: () => text.value.trim() !== '',
|
||
}),
|
||
{ confirmLabel: 'Ajouter', tone: 'primary' },
|
||
);
|
||
if (!confirmed) return;
|
||
|
||
await api.post('/api/community/statuses', {
|
||
text: text.value,
|
||
type: type.value,
|
||
url: url.value || undefined,
|
||
});
|
||
toast('Statut ajouté.');
|
||
reload();
|
||
},
|
||
{ tone: 'primary' },
|
||
),
|
||
),
|
||
},
|
||
|
||
level >= LEVELS.DEV &&
|
||
h(
|
||
'div',
|
||
{ class: 'row' },
|
||
field('Rythme de rotation (secondes)', interval),
|
||
button(
|
||
'Enregistrer',
|
||
async () => {
|
||
const result = await api.put('/api/community/statuses/interval', {
|
||
seconds: Number(interval.value),
|
||
});
|
||
toast(`Rotation réglée sur ${result.intervalSeconds} s. Effective au prochain redémarrage.`);
|
||
},
|
||
{ tone: 'ghost' },
|
||
),
|
||
),
|
||
|
||
table(
|
||
[
|
||
{ label: '#', className: 'num', key: 'id' },
|
||
{
|
||
label: 'Statut',
|
||
render: (status) => {
|
||
const type = STATUS_TYPES.find((item) => item.value === status.type);
|
||
return `${type?.label ?? status.type} ${status.text}`;
|
||
},
|
||
},
|
||
{
|
||
label: 'Actif',
|
||
render: (status) => (status.enabled ? badge('Oui', 'success') : badge('En pause', 'neutral')),
|
||
},
|
||
{
|
||
label: '',
|
||
render: (status) =>
|
||
level >= LEVELS.DEV &&
|
||
h(
|
||
'div',
|
||
{ class: 'row' },
|
||
button(
|
||
status.enabled ? 'Pause' : 'Activer',
|
||
async () => {
|
||
await api.patch(`/api/community/statuses/${status.id}`, { enabled: !status.enabled });
|
||
toast('Statut mis à jour.');
|
||
reload();
|
||
},
|
||
{ tone: 'ghost' },
|
||
),
|
||
button(
|
||
'Supprimer',
|
||
async () => {
|
||
if (!(await confirmAction('Supprimer le statut', status.text))) return;
|
||
await api.delete(`/api/community/statuses/${status.id}`);
|
||
toast('Statut supprimé.');
|
||
reload();
|
||
},
|
||
{ tone: 'danger' },
|
||
),
|
||
),
|
||
},
|
||
],
|
||
statuses,
|
||
{ empty: 'Aucun statut : le bot affiche sa ligne par défaut.' },
|
||
),
|
||
);
|
||
}
|
||
|
||
export function render(host, { state }) {
|
||
const level = state.me.accessLevel;
|
||
const reload = () => render(host, { state });
|
||
|
||
autoRefresh(reload, { topics: ['community', 'discord', 'tick'] });
|
||
|
||
return renderAsync(
|
||
host,
|
||
async () => {
|
||
const [tickets, polls, voice, statuses] = await Promise.all([
|
||
api.get('/api/community/tickets', { limit: 100 }),
|
||
api.get('/api/community/polls', { limit: 30 }),
|
||
api.get('/api/community/voice'),
|
||
api.get('/api/community/statuses'),
|
||
]);
|
||
return { tickets, polls: polls.polls, voice, statuses };
|
||
},
|
||
(data) =>
|
||
h(
|
||
'div',
|
||
{ class: 'stack' },
|
||
h(
|
||
'div',
|
||
{ class: 'page-head' },
|
||
h(
|
||
'div',
|
||
{},
|
||
h('h1', { text: 'Tickets & vocaux' }),
|
||
h('p', { class: 'muted', text: 'Le quotidien de la communauté, géré depuis le site.' }),
|
||
),
|
||
),
|
||
data.voice.triggers.length === 0 &&
|
||
notice('Aucun salon « rejoindre pour créer » : la fonctionnalité vocale reste inactive.', 'info'),
|
||
ticketsCard(data.tickets, level, reload),
|
||
pollsCard(data.polls, level, reload),
|
||
voiceCard(data.voice, level, reload),
|
||
statusesCard(data.statuses.statuses, data.statuses.intervalSeconds, level, reload),
|
||
),
|
||
);
|
||
}
|