const TASK_LIVE = t => !['done','closed','dropped','resolved'].includes((t.status || 'open').toLowerCase()) && !t.archived;
const TASK_QUEUES = [
{ id: 'blocked', label: 'blocked', color: 'var(--amber)',
why: 'waiting on someone or something named in the reason',
flag: '--status blocked',
test: t => TASK_LIVE(t) && (t.status || '').toLowerCase() === 'blocked' },
{ id: 'stalled', label: 'stalled', color: 'var(--accent)',
why: 'in progress, nothing changed for more than 3 days',
flag: '--status in_progress --stale 3d',
test: t => TASK_LIVE(t) && (t.status || '').toLowerCase() === 'in_progress' && (Date.now() - new Date(t.updated_at || 0).getTime()) > 3 * 86400e3 },
{ id: 'unowned', label: 'unowned', color: 'var(--text)',
why: 'open, no assignee - nobody will pick it up by accident',
flag: '--status open --unassigned',
test: t => TASK_LIVE(t) && (t.status || 'open').toLowerCase() === 'open' && !(t.assignee || '').trim() },
];
const TASK_PAGE = 40;
function taskSelectQueue(id) { App.taskShown = TASK_PAGE; App.taskQueue = App.taskQueue === id ? null : id; App.taskCursor = 0; renderTasks(); }
function taskSetStatus(st) { App.taskShown = TASK_PAGE; App.taskStatusFilter = st; App.taskCursor = 0; renderTasks(); }
function taskFiltered() {
let list = [...App.tasks];
const f = App.taskStatusFilter || 'live';
if (f === 'live') list = list.filter(TASK_LIVE);
else if (f === 'done') list = list.filter(t => !TASK_LIVE(t));
else list = list.filter(t => TASK_LIVE(t) && (t.status || 'open').toLowerCase() === f);
if (App.taskQueue) { const q = TASK_QUEUES.find(q => q.id === App.taskQueue); if (q) list = list.filter(q.test); }
const prio = { critical: 0, high: 1, medium: 2, normal: 2, low: 3 };
const sr = { blocked: 0, in_progress: 1, open: 2 };
list.sort((a, b) => (sr[(a.status||'open').toLowerCase()] ?? 2) - (sr[(b.status||'open').toLowerCase()] ?? 2)
|| (prio[a.priority] ?? 2) - (prio[b.priority] ?? 2)
|| new Date(b.updated_at || 0) - new Date(a.updated_at || 0));
return list;
}
function renderTasks(keepSelection) {
const top = document.getElementById('taskTop');
if (!top) return;
const live = App.tasks.filter(TASK_LIVE);
const by = st => live.filter(t => (t.status || 'open').toLowerCase() === st).length;
const f = App.taskStatusFilter || 'live';
const listNow = taskFiltered();
const tq = App.taskQueue && TASK_QUEUES.find(x => x.id === App.taskQueue);
top.innerHTML = `
<span class="segs">${[['live','live'],['open','open'],['in_progress','in progress'],['blocked','blocked'],['done','closed']].map(([k,l]) => `<button class="chip ${f === k ? 'active' : ''}" onclick="taskSetStatus('${k}')">${l}</button>`).join('')}</span>
<span class="bar-r">
<span class="cnt"><span id="taskCount">${listNow.length} of ${App.tasks.length}${f === 'live' ? ' · live only' : ''}</span> · ${live.length} live: ${by('open')} open, ${by('in_progress')} in progress, ${by('blocked')} blocked${tq ? ` · ${tq.label} · <a href="javascript:void(0)" onclick="taskSelectQueue(App.taskQueue)">clear</a>` : ''}</span>
<button class="btn sm" data-write onclick="openNewModal('task')">Add task</button>
</span>`;
document.getElementById('taskQueue').innerHTML = TASK_QUEUES.map(q => {
const n = App.tasks.filter(q.test).length;
return `<button type="button" class="b ${App.taskQueue === q.id ? 'on' : ''} ${n ? '' : 'zero'}" aria-pressed="${App.taskQueue === q.id}" onclick="taskSelectQueue('${q.id}')">
<div class="h"><b style="color:${n ? q.color : ''}">${n}</b><span>${q.label}</span></div>
<div class="w">${q.why}</div></button>`;
}).join('');
const list = taskFiltered();
if (keepSelection && App.taskCursorKey) { const at = list.findIndex(t => t.key === App.taskCursorKey); if (at >= 0) App.taskCursor = at; }
if (App.taskCursor == null || App.taskCursor >= list.length) App.taskCursor = 0;
App.taskCursorKey = list.length ? list[App.taskCursor].key : null;
const rows = document.getElementById('taskRows');
if (!list.length) { rows.innerHTML = `<div class="inc-empty">Nothing matches. <kbd>esc</kbd> clears the queue filter.</div>`; return; }
App.taskShown = Math.max(App.taskShown || TASK_PAGE, App.taskCursor + 1);
const shown = list.slice(0, App.taskShown);
rows.innerHTML = shown.map((t, idx) => {
const cur = idx === App.taskCursor;
const st = (t.status || 'open').toLowerCase();
const done = !TASK_LIVE(t);
const pr = (t.priority || '').toLowerCase();
const dot = done ? 'resolved' : pr === 'critical' ? 'critical' : pr === 'high' ? 'high' : '';
const age = Math.max(0, Math.floor((Date.now() - new Date(t.updated_at || 0).getTime()) / 86400e3));
const note = st === 'blocked' && t.blocked_reason
? `<div class="inc-note"><b>Blocked</b> · ${escapeHtml(t.blocked_reason)}${t.parent_task ? ` <span class="parent">· part of <a href="#/knowledge/${encodeURIComponent(t.parent_task)}" onclick="handleCardClick(event, ${jsArg(t.parent_task)}, 'knowledge')">${escapeHtml(t.parent_task)}</a></span>` : ''}</div>`
: done && t.resolution ? `<div class="inc-note res"><b>${st === 'dropped' ? 'Dropped' : 'Done'}</b> · ${escapeHtml(t.resolution)}</div>`
: t.parent_task ? `<div class="inc-note" style="border-left-color:var(--border)"><span class="parent">part of <a href="#/knowledge/${encodeURIComponent(t.parent_task)}" onclick="handleCardClick(event, ${jsArg(t.parent_task)}, 'knowledge')">${escapeHtml(t.parent_task)}</a></span></div>` : '';
const acts = cur ? `<div class="acts" onclick="event.stopPropagation()">
<a href="#/knowledge/${encodeURIComponent(t.key)}" class="btn sm primary" onclick="handleCardClick(event, ${jsArg(t.key)}, 'knowledge')">Open <kbd>⏎</kbd></a>
${!done ? `<select class="move-sel" data-write onchange="moveTaskTo(this.value, ${jsArg(t.key)}, ${jsArg(t.title)}); this.value='';">
<option value="" disabled selected>Move to… (m)</option>
<option value="open" ${st === 'open' ? 'disabled' : ''}>Open</option>
<option value="in_progress" ${st === 'in_progress' ? 'disabled' : ''}>In progress</option>
<option value="blocked" ${st === 'blocked' ? 'disabled' : ''}>Blocked</option></select>
<button class="btn sm success" data-write onclick="openResolveTaskModal(${jsArg(t.key)}, ${jsArg(t.title)})">Done <kbd>x</kbd></button>` : `<button class="btn sm" data-write onclick="moveTaskTo('open', ${jsArg(t.key)}, ${jsArg(t.title)})">Reopen</button>`}
<button class="btn sm" data-write onclick="openEditModal(${jsArg(t.key)})">Edit <kbd>e</kbd></button>
<button class="btn sm ghost" onclick="openHistoryForEntry(${jsArg(t.key)})">Versions <kbd>h</kbd></button>
<button class="btn sm ghost" onclick="openDiffForEntry(${jsArg(t.key)})">Diff <kbd>d</kbd></button>
</div>` : '';
return `<div class="inc-r task ${cur ? 'cur' : ''}" onclick="App.taskCursor=${idx}; renderTasks();" ondblclick="handleCardClick(event, ${jsArg(t.key)}, 'knowledge')">
<span class="n">${idx + 1}</span>
<span class="dot ${dot}"></span>
<div class="t"><b>${escapeHtml(t.title)}</b><span>${escapeHtml(t.key)}${pr && pr !== 'normal' && pr !== 'medium' ? ' · ' + escapeHtml(pr) : ''}</span></div>
<span class="st ${st}">${st.replace('_', ' ')}</span>
<span class="who ${t.assignee ? '' : 'none'}">${escapeHtml(t.assignee || 'unassigned')}</span>
<span class="age">${age}d</span>
${note}${acts}
</div>`;
}).join('') + (list.length > shown.length
? `<div class="inc-more" onclick="App.taskShown += ${TASK_PAGE}; renderTasks();">${list.length - shown.length} more · <span class="acc">show ${Math.min(TASK_PAGE, list.length - shown.length)}</span></div>` : '');
const curEl = rows.querySelector('.inc-r.cur');
if (curEl && App.taskKeyNav) { curEl.scrollIntoViewIfNeeded && curEl.scrollIntoViewIfNeeded(false); App.taskKeyNav = false; }
}
window.addEventListener('keydown', (e) => {
if (App.activeTab !== 'tasks') return;
if (['INPUT', 'TEXTAREA', 'SELECT'].includes(document.activeElement.tagName)) return;
if (document.querySelector('.modal-backdrop.open')) return;
const list = taskFiltered();
const cur = list[App.taskCursor || 0];
const claim = () => { e.preventDefault(); e.stopImmediatePropagation(); };
if (e.key === 'j' || e.key === 'ArrowDown') { claim(); App.taskCursor = Math.min((App.taskCursor || 0) + 1, list.length - 1); App.taskKeyNav = true; renderTasks(); }
else if (e.key === 'k' || e.key === 'ArrowUp') { claim(); App.taskCursor = Math.max((App.taskCursor || 0) - 1, 0); App.taskKeyNav = true; renderTasks(); }
else if (e.key === 'Escape' && App.taskQueue) { claim(); taskSelectQueue(App.taskQueue); }
else if (!cur) return;
else if (e.key === 'Enter') { claim(); location.hash = '#/knowledge/' + encodeURIComponent(cur.key); }
else if (e.key === 'x' && TASK_LIVE(cur)) { claim(); openResolveTaskModal(cur.key, cur.title); }
else if (e.key === 'm' && TASK_LIVE(cur)) { claim(); const sel = document.querySelector('.inc-r.cur select'); sel && sel.focus(); }
else if (e.key === 'e') { claim(); openEditModal(cur.key); }
else if (e.key === 'd') { claim(); openDiffForEntry(cur.key); }
else if (e.key === 'h') { claim(); openHistoryForEntry(cur.key); }
}, true);
function moveTaskTo(targetStatus, key, title) {
if (!targetStatus) return;
if (targetStatus === 'done') {
openResolveTaskModal(key, title);
} else if (targetStatus === 'blocked') {
promptBlockTask(key, title);
} else {
return transitionTask(key, targetStatus);
}
}
async function transitionTask(key, status, reason = null) {
if (refuseReadOnly()) return;
const payload = { status };
if (status === 'blocked' && reason) {
payload.blocked_reason = reason;
}
const res = await apiPost(`/tasks/${encodeURIComponent(key)}/transition`, payload);
if (res.ok) {
showToast(`Task ${key} transitioned to ${status}`, 'success');
App.detailCache.delete(key);
await refreshAll();
if (App.selectedKey === key) {
selectEntry(key, false);
}
} else {
showToast(`Transition failed: ${res.data?.error || res.error || 'Server error'}`, 'error');
}
}
function promptBlockTask(key, title = '') {
if (refuseReadOnly()) return;
App.activeBlockKey = key;
const t = App.tasks.find(x => x.key === key);
const displayTitle = title || t?.title || key;
const targetEl = document.getElementById('blockTaskTargetText');
if (targetEl) targetEl.textContent = key;
const titleEl = document.getElementById('blockTaskTitle');
if (titleEl) titleEl.textContent = `Block: ${displayTitle}`;
const reasonEl = document.getElementById('blockTaskReason');
if (reasonEl) reasonEl.value = '';
openModal('blockTaskModal');
}
async function submitBlockTask() {
const reasonEl = document.getElementById('blockTaskReason');
const reason = reasonEl ? reasonEl.value.trim() : '';
if (!markRequired(['blockTaskReason']) || !reason) {
showToast('Say what is blocking it', 'error');
return;
}
closeModal('blockTaskModal');
await transitionTask(App.activeBlockKey, 'blocked', reason);
}
function openResolveTaskModal(key, title) {
if (refuseReadOnly()) return;
App.activeResolveKey = key;
document.getElementById('resolveTaskTitle').textContent = `Finish: ${title || key}`;
document.getElementById('resolveTaskTargetText').textContent = key;
document.getElementById('resolveTaskOutcome').value = '';
openModal('resolveTaskModal');
}
async function submitResolveTask() {
if (!markRequired(['resolveTaskOutcome'])) {
showToast('Say what the outcome was', 'error');
return;
}
const outcome = document.getElementById('resolveTaskOutcome').value.trim();
const res = await apiPost(`/tasks/${encodeURIComponent(App.activeResolveKey)}/resolve`, {
resolution: outcome
});
if (res.ok) {
closeModal('resolveTaskModal');
showToast(`Task ${App.activeResolveKey} marked done!`, 'success');
App.detailCache.delete(App.activeResolveKey);
await refreshAll();
if (App.selectedKey === App.activeResolveKey) {
selectEntry(App.activeResolveKey, false);
}
} else {
showToast(`Failed: ${res.data?.error || res.error || 'Server error'}`, 'error');
}
}