/* 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'}>}>
Tasks start at “Not started” and move through In progress and Waiting to Done. The person you assign gets an email. A repeating task creates the next one when it is finished.
A deliverable is a thing that ships: a reel, a static post, a blog, an ad. Each type has its own stages because the work is different (a reel needs a shoot, a blog needs an outline). Every stage maps to one of five states, which is what colours the status chips and decides what counts as overdue, waiting or done.
Tasksone simple pipeline
A task is a step someone has to do, on its own or as part of a deliverable. Tasks only need four stages: Not started, In progress, Waiting, Done. Finishing a repeating task creates the next one.
Not startedIn progressWaitingDone
{tasksOpen} open tasks right now.
Deliverable pipelines
{T.types.map(x => )}
{inUse} open {t.name.toLowerCase()}{inUse === 1 ? '' : 's'} on this pipeline.Stages run left to right. “Blocked” can be chosen at any point and means stopped by something outside the queue.
Stage names and their order are part of the portal’s configuration. If a type needs a different set of stages, ask for it to be changed and existing work is mapped across.
{{
todo: 'Requested, queued, briefed. Real work has not started.',
doing: 'Someone at the Desk is working on it right now.',
waiting: 'Nothing can move until a person outside the work responds. The loudest state on purpose.',
done: 'Published, delivered, live. It leaves the board.',
blocked: 'Stopped by something that is not a queue: no consent, no budget, no rate card.'
}[k]}