/* TMD Portal — Tasks as their own thing (backed by public.tmd_tasks). A task is a small piece of work with one owner. It can stand alone or hang off a deliverable. Tasks move through one simple pipeline: Not started → In progress → Waiting → Done. Deliverables keep their richer, per-type pipelines (see Pipelines). */ const { useState: uSx, useEffect: uEx, useMemo: uMx } = React; const TASK_STAGES = [ { v: 'todo', l: 'Not started', sem: 'todo' }, { v: 'doing', l: 'In progress', sem: 'doing' }, { v: 'waiting', l: 'Waiting', sem: 'waiting' }, { v: 'done', l: 'Done', sem: 'done' } ]; const taskStage = v => TASK_STAGES.find(s => s.v === v) || TASK_STAGES[0]; function TStatus({ t, size }) { const s = taskStage(t.state); return ; } const taskOpen = t => t.state !== 'done'; const taskOverdue = t => taskOpen(t) && t.due && dayDiff(t.due) < 0; async function updateTask(app, t, patch) { if (patch.state === 'done' && !patch.done_at) patch.done_at = new Date().toISOString(); if (patch.state && patch.state !== 'done') patch.done_at = null; const { error } = await SB.from('tmd_tasks').update(patch).eq('id', t.id); if (error) { app.toast('Not saved: ' + error.message); return false; } await app.work.reload(); return true; } /* ================= New task (standalone or on a deliverable) ================= */ function NewTask({ open, app, onClose, deliverable }) { const brands = app.work.brands.filter(b => b.status !== 'Ended'); const myBrands = (app.staff && app.staff.brands) || []; const allowed = app.role === 'Brand manager' ? brands.filter(b => myBrands.includes(b.slug)) : brands; const staff = (app.staffList || []).filter(s => s.active); const [f, setF] = uSx({}); const [busy, setBusy] = uSx(false); const [err, setErr] = uSx(''); uEx(() => { if (open) { setF({ title: '', brand_slug: deliverable ? deliverable.brand_slug : '', assignee: deliverable ? (deliverable.assignee || app.me) : app.me, due: '', priority: 'Normal', recurrence: '', notes: '' }); setErr(''); setBusy(false); } }, [open]); if (!open) return null; const set = k => e => setF(x => Object.assign({}, x, { [k]: e.target.value })); const save = async () => { setErr(''); if (!f.title.trim()) { setErr('Give it a title.'); return; } setBusy(true); const row = { title: f.title.trim(), brand_slug: f.brand_slug || null, assignee: f.assignee || null, due: f.due || null, priority: f.priority, recurrence: f.recurrence || null, notes: f.notes.trim() || null }; if (deliverable) row.deliverable_id = deliverable.id; const { error } = await SB.from('tmd_tasks').insert(row); setBusy(false); if (error) { setErr(error.message); return; } if (deliverable) await logActivity(deliverable.id, app.me, 'added task “' + row.title + '”' + (row.assignee ? ' for ' + person(row.assignee).name : '')); await app.work.reload(); app.toast('Task added' + (row.assignee && row.assignee !== app.me ? ' and ' + person(row.assignee).name + ' emailed' : '')); onClose(); }; return ( Cancel{busy ? 'Working' : 'Add task'}}>