# Tasks

> The task model of the Gantt, covering task, milestone and summary types, the task tree, durations, progress, colors, scheduled fields and editing tasks from code.

A project is a list of tasks in a tree. Each task is a plain object; the engine reads it, schedules it and gives you back a copy with computed dates. Your original objects are never mutated.

## The task object

```ts
import type { GanttTask } from '@birchui/gantt';

const tasks: GanttTask[] = [
  { id: 'relaunch', name: 'Website relaunch' },
  { id: 'discovery', parentId: 'relaunch', name: 'Discovery workshops', duration: 3 },
  { id: 'design', parentId: 'relaunch', name: 'Visual design', duration: 8, percentDone: 40, color: '#7c3aed' },
  { id: 'content', parentId: 'relaunch', name: 'Content migration', duration: 48, durationUnit: 'hour' },
  { id: 'golive', parentId: 'relaunch', name: 'Go live', type: 'milestone' },
];
```

| Field | Type | Description |
| --- | --- | --- |
| `id` | `string` | Unique identifier. Dependencies refer to it. |
| `name` | `string` | Shown in the grid and on the bar, and used as the accessible name. |
| `parentId` | `string \| null` | Places the task under another task, which becomes a summary. |
| `duration` | `number` | Working time in `durationUnit`. `0` makes a milestone. |
| `durationUnit` | `'day' \| 'hour'` | Defaults to `'day'`. A day is `hoursPerDay` working hours (8 by default). |
| `start` | `Date \| string \| number` | Where an unlinked task starts. Linked tasks start where their links allow. |
| `end` | `Date \| string \| number` | An instant. Used when there is no `duration`; when both are given, `duration` wins. |
| `percentDone` | `number` | Progress from 0 to 100. |
| `type` | `'task' \| 'milestone' \| 'summary'` | `'milestone'` makes a zero-duration task. Summaries come from having subtasks. |
| `constraint` | `{ type, date? }` | One of the eight scheduling constraints. See [Scheduling](https://docs.birchui.com/gantt/scheduling.md#constraints). |
| `manuallyScheduled` | `boolean` | Keep the task exactly at its `start` and `end`. See [Manual scheduling](https://docs.birchui.com/gantt/scheduling.md#manually-scheduled-tasks). |
| `calendarId` | `string \| null` | A task calendar that overrides the project calendar. See [Calendars](https://docs.birchui.com/gantt/calendars.md). |
| `baselines` | `{ start, end }[]` | Saved plans to compare against. See [Baselines](https://docs.birchui.com/gantt/baselines.md). |
| `color` | `string` | Bar color, any CSS color. |
| `expanded` | `boolean` | Initial expanded state of a summary in the grid. |
| `resourceIds` | `string[]` | Kept with the task for your own use. Resources are not levelled or exported; see [Limitations](https://docs.birchui.com/gantt/limitations.md). |
| any other key | `unknown` | Your own fields, kept on the task and passed back in every payload. |

Tasks with a missing or duplicate `id` are skipped with a warning. A `parentId` that points to a missing task, or that would create a loop, places the task at the top level with a warning.

## Task types

| Type | How you get it | Drawn as |
| --- | --- | --- |
| Task | The default. | A bar with a progress fill and handles for resizing and progress. |
| Milestone | `type: 'milestone'` or `duration: 0`. | A diamond at its date. |
| Summary | Any task that has subtasks. | A bracket from its earliest subtask start to its latest subtask end. |

A summary's dates, duration and progress are rolled up from its subtasks: it spans the earliest start to the latest end, its duration is the working time in between on the project calendar, and its progress is the duration-weighted average of its subtasks. Values you set on a summary for these fields are ignored. Moving a summary moves all of its subtasks; it cannot be resized or given progress directly.

## The tree

`parentId` builds the hierarchy, and **sibling order is array order**. Each task gets a WBS code from its position, such as `1.3.2`, shown in the WBS column and used in predecessor text. Nesting can go as deep as you need.

```ts
const tasks: GanttTask[] = [
  { id: 'phase-1', name: 'Phase 1: Structure' },
  { id: 'piling', parentId: 'phase-1', name: 'Piling', duration: 12 },
  { id: 'frame', parentId: 'phase-1', name: 'Steel frame', duration: 20 },
  { id: 'phase-2', name: 'Phase 2: Envelope' },
  { id: 'cladding', parentId: 'phase-2', name: 'Cladding', duration: 25 },
  { id: 'roof', parentId: 'phase-2', name: 'Roofing', duration: 10 },
];
```

## Durations and dates

- Durations are **working time**. `duration: 5` with the default units is 40 working hours, laid out on the task's calendar, so it can span a weekend or a holiday.
- Set `hoursPerDay` (default 8) and `daysPerWeek` (default 5) to match your calendar. With a 7.5-hour working day, set `hoursPerDay: 7.5`, or durations in days will not line up with whole days.
- `end` is an instant. A task that runs from Monday to Friday ends at `'…T17:00'` on Friday; an `end` of the following midnight (`'…T00:00'` or a date only) snaps back to the end of the last working day.
- Strings without an offset, such as `'2027-01-04T08:00'`, are wall-clock time in the `timeZone` option.

## Scheduled fields

`getTask(id)` and `getTasks()` return your task merged with the scheduled values:

| Field | Description |
| --- | --- |
| `start`, `end` | Scheduled dates, as `Date` objects. |
| `duration`, `durationUnit` | Scheduled duration. For summaries, the rolled-up working time. |
| `percentDone` | Progress; rolled up for summaries. |
| `type` | `'task'`, `'milestone'` or `'summary'`. |
| `wbs`, `outlineLevel` | WBS code and depth (1 for top-level tasks). |
| `earlyStart`, `earlyEnd`, `lateStart`, `lateEnd` | The CPM dates. |
| `totalSlack`, `freeSlack` | Slack in working days. See [Slack](https://docs.birchui.com/gantt/scheduling.md#slack-and-the-critical-path). |
| `isCritical` | On the critical path. |
| `hasConflict` | A constraint overrode a link. |

```ts
const task = gantt.getTask('design');
console.info(`${task?.wbs} ${task?.name}: ${task?.start.toDateString()} to ${task?.end.toDateString()}, slack ${task?.totalSlack} days`);
```

`createGantt<TTask>` is generic over your task type, so your own fields stay typed in `getTask`, payloads and column renderers.

## Editing tasks from code

| Method | Description |
| --- | --- |
| `addTask(task, position?)` | Add a task and return it scheduled. `position` is `{ above }`, `{ below }` (task ids) or `{ parentId, index }`. Without `duration` or `end`, a new task lasts one day; without `start`, it starts where the task it is placed next to starts, or at its parent's start, or at the project start. Without `id`, one is generated. |
| `updateTask(id, patch)` | Merge `patch` into the task and reschedule. Changing `parentId` moves the task under another parent. |
| `removeTask(id)` | Remove the task, its subtasks and their links. |
| `indent(id)`, `outdent(id)` | Change the task's level. See [Indent and outdent](https://docs.birchui.com/gantt/task-grid.md#indent-and-outdent). |
| `setTasks(tasks)` | Replace all tasks and reschedule. Clears the undo history. |
| `getTask(id)`, `getTasks()` | Read scheduled copies. |

```ts
const qa = gantt.addTask({ name: 'Accessibility audit', duration: 3 }, { below: 'design' });
gantt.updateTask('design', { duration: 10, percentDone: 55 });
gantt.removeTask('content');
```

API edits are undoable and emit `taskAdd`, `taskChange` or `taskRemove` with `source: 'api'`, followed by `tasksScheduled` for everything the change moved.

> **Warning**
>
> `updateTask(id, { start })` only moves a task the engine is free to place: an unlinked task keeps its new start, but a task with predecessors still starts where its links allow. To pin a linked task the way a drag does, set a constraint as well: `updateTask(id, { start, constraint: { type: 'snet', date: start } })`. See [Dragging sets a constraint](https://docs.birchui.com/gantt/scheduling.md#dragging-sets-a-constraint).

## Next steps

- [Dependencies](https://docs.birchui.com/gantt/dependencies.md) to link tasks.
- [Scheduling](https://docs.birchui.com/gantt/scheduling.md) for how dates are computed.
- [Task grid](https://docs.birchui.com/gantt/task-grid.md) for columns and inline editing.
