import { api } from '../core/api.js'; import { formatRelative, h, mount } from '../core/dom.js'; import { autoRefresh } from '../core/live.js'; import { badge, card, notice, renderAsync, select, stat, toast } from '../core/ui.js'; /** * « Mes tâches » — every card one member carries, across every board (M19). * * A board answers « where does the work stand ». Nobody opens a dashboard to * ask that first: they ask what is on them today, and once the work is spread * over several boards no single board can say. */ const today = () => new Date().toISOString().slice(0, 10); const day = (value) => (value ? String(value).slice(0, 10) : null); const plus = (days) => { const date = new Date(); date.setDate(date.getDate() + days); return date.toISOString().slice(0, 10); }; /** * Buckets, in the order someone wants to be told about them: what is late, * what is due, then everything else. A card with no date is not urgent but is * not invisible either — it sits at the bottom rather than nowhere. */ function bucketOf(task) { if (task.isDone) return 'done'; const due = day(task.dueAt); if (!due) return 'undated'; if (due < today()) return 'overdue'; if (due === today()) return 'today'; return due <= plus(7) ? 'week' : 'later'; } const BUCKETS = [ { key: 'overdue', title: 'En retard' }, { key: 'today', title: 'Pour aujourd’hui' }, { key: 'week', title: 'Cette semaine' }, { key: 'later', title: 'Plus tard' }, { key: 'undated', title: 'Sans échéance' }, ]; /** Member names, asked once: the picker must not cost a roster fetch per render. */ let memberNames = null; async function names() { if (memberNames) return memberNames; const data = await api.get('/api/discord/members', { limit: 200 }).catch(() => null); memberNames = new Map((data?.members ?? []).map((member) => [member.id, member.displayName])); return memberNames; } async function finish(task, reload) { if (!task.doneColumnId) { toast('Ce tableau n’a pas de colonne « terminé ».', 'error'); return; } await api.post(`/api/projects/${task.boardId}/cards/${task.id}/move`, { columnId: task.doneColumnId, position: 0, }); toast(`« ${task.title} » est terminée.`); // The shell owns the sidebar counter; it has no reason to know this view. document.dispatchEvent(new CustomEvent('fws:tasks-changed')); reload(); } function taskRow(task, issues, reload) { const issue = task.issueRef ? issues[task.issueRef] : null; const due = day(task.dueAt); const late = !task.isDone && due && due < today(); return h( 'li', { class: task.isDone ? 'task task-done' : 'task', dataset: { bucket: bucketOf(task) } }, h( 'button', { class: 'task-check', type: 'button', title: task.isDone ? 'Déjà terminée' : 'Marquer comme terminée', disabled: task.isDone || !task.doneColumnId, onClick: () => finish(task, reload), }, h('span', { text: task.isDone ? '✓' : '' }), ), h( 'div', { class: 'task-body' }, h( 'a', { class: 'task-title', href: `#/projects/${task.boardId}`, title: 'Ouvrir le tableau' }, task.title, ), h( 'div', { class: 'task-meta' }, h('span', { class: 'muted', text: task.boardName }), badge(task.columnName, task.isDone ? 'success' : 'neutral'), task.labels.map((label) => badge(label, 'info')), issue && h( 'a', { href: issue.htmlUrl, target: '_blank', rel: 'noreferrer', class: 'task-issue' }, `${task.issueRef} · ${issue.state === 'closed' ? 'fermée' : 'ouverte'}`, ), !issue && task.issueRef && h('span', { class: 'muted', text: task.issueRef }), ), ), due && h( 'span', { class: late ? 'task-due task-due-late' : 'task-due', title: due }, formatRelative(`${due}T12:00:00Z`), ), ); } /** * Returns its node right away and fills it once the names arrive: waiting on * Discord would hold up the tasks, which are the point of the page. */ function picker(data, reload) { if (data.assignees.length < 2) return null; const host = h('span', { class: 'task-picker' }); const build = (resolved) => mount( host, select( [ { value: '', label: 'Moi' }, ...data.assignees.map((entry) => ({ value: entry.assigneeId, label: `${resolved.get(entry.assigneeId) ?? entry.assigneeId} (${entry.open})`, })), ], { value: data.self ? '' : data.assigneeId, 'aria-label': 'Voir les tâches de', onChange: (event) => reload(event.target.value), }, ), ); if (memberNames) build(memberNames); else names().then(build); return host; } export function render(host) { let assignee = ''; const load = () => api.get('/api/me/tasks', { assignee }); const reload = (next) => { if (next !== undefined) assignee = next; return draw(); }; const draw = () => renderAsync(host, load, (data) => { const grouped = new Map(BUCKETS.map((bucket) => [bucket.key, []])); const done = []; for (const task of data.cards) { const bucket = bucketOf(task); if (bucket === 'done') done.push(task); else grouped.get(bucket).push(task); } const owner = data.self ? 'toi' : (memberNames?.get(data.assigneeId) ?? `le membre ${data.assigneeId}`); return h( 'div', { class: 'stack' }, h( 'div', { class: 'page-head' }, h( 'div', {}, h('h1', { text: data.self ? 'Mes tâches' : 'Tâches assignées' }), h('p', { class: 'muted', text: `Toutes les cartes assignées à ${owner}, tous tableaux confondus, la plus urgente en premier.`, }), ), h('div', { class: 'row' }, picker(data, reload)), ), h( 'div', { class: 'grid' }, stat('À faire', data.counts.open, { tone: data.counts.open > 0 ? 'primary' : 'neutral' }), stat('En retard', data.counts.overdue, { tone: data.counts.overdue > 0 ? 'danger' : 'neutral', }), stat('Pour aujourd’hui', data.counts.dueToday, { tone: data.counts.dueToday > 0 ? 'warning' : 'neutral', }), stat('Terminées', data.counts.done), ), data.cards.length === 0 && notice( data.self ? 'Rien ne t’est assigné. Ouvre un tableau et mets ton nom sur une carte pour la voir apparaître ici.' : 'Aucune carte n’est assignée à ce membre.', 'info', ), BUCKETS.map((bucket) => { const tasks = grouped.get(bucket.key); if (tasks.length === 0) return null; return card( bucket.title, { subtitle: `${tasks.length} carte(s)` }, h( 'ul', { class: 'task-list' }, tasks.map((task) => taskRow(task, data.issues, () => reload())), ), ); }), done.length > 0 && h( 'details', { class: 'card task-archive' }, h('summary', { text: `Terminées (${done.length})` }), h( 'ul', { class: 'task-list' }, done.map((task) => taskRow(task, data.issues, () => reload())), ), ), ); }); autoRefresh(() => draw(), { topics: ['projects', 'tick'] }); return draw(); }