All files / src/utils boardColumns.ts

0% Statements 0/34
0% Branches 0/20
0% Functions 0/6
0% Lines 0/31

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146                                                                                                                                                                                                                                                                                                   
/**
 * Pure helpers for bucketing tasks into Kanban board columns.
 *
 * Unlike the sidebar `groupTasksByStatus()` (which adds a virtual "blocked"
 * group), the board keeps blocked tasks in their actual-status column and
 * overlays a badge.
 */
 
import type { TaskData } from "../hooks/types.js";
import {
  BOARD_COLUMN_ORDER,
  getStatusStyle,
  resolveStatus,
  type TaskStatusKey,
  type TaskStatusStyle,
} from "./taskStatus.js";
 
// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------
 
/** Represents a single column on the Kanban board. */
export interface BoardColumn {
  /** Canonical status key (e.g. "working"). */
  status: TaskStatusKey;
  /** Human-readable column heading. */
  label: string;
  /** Visual style for the column header. */
  style: TaskStatusStyle;
  /** Tasks in this column, sorted by `sortOrder`. */
  tasks: BoardTask[];
}
 
/** A task decorated with board-specific computed properties. */
export interface BoardTask {
  /** Original task data. */
  task: TaskData;
  /** True when the task has unresolved dependencies. */
  isBlocked: boolean;
  /** Number of direct child tasks. */
  childCount: number;
  /** Number of direct child tasks that are complete. */
  doneChildCount: number;
  /** Paused sub-badge label derived from latest session status. */
  pausedSubBadge?: "Needs input" | "Ready to complete";
}
 
// ---------------------------------------------------------------------------
// Bucketing
// ---------------------------------------------------------------------------
 
/** Options for building board columns. */
interface BuildColumnsOptions {
  /** Flat list of tasks belonging to one workspace. */
  tasks: TaskData[];
  /** Map of taskId → status for all tasks (used for dependency checking). */
  taskStatusById: Map<string, string>;
  /** Map of taskId → latest session status (used for paused sub-badges). */
  sessionStatusByTaskId?: Map<string, string>;
}
 
/**
 * Bucket tasks into fixed board columns.
 *
 * - Always returns all five columns (empty ones get an empty `tasks` array).
 * - Resolves legacy status aliases to canonical keys.
 * - Sorts tasks within each column by `sortOrder`.
 * - Computes blocked state and child-progress counts.
 * - Derives paused sub-badges from the latest session status.
 */
export function buildBoardColumns({
  tasks,
  taskStatusById,
  sessionStatusByTaskId,
}: BuildColumnsOptions): BoardColumn[] {
  // Index children by parent
  const childrenByParent = new Map<string, TaskData[]>();
  for (const t of tasks) {
    if (t.parentTaskId) {
      const list = childrenByParent.get(t.parentTaskId);
      if (list) {
        list.push(t);
      } else {
        childrenByParent.set(t.parentTaskId, [t]);
      }
    }
  }
 
  // Pre-build empty buckets keyed by status
  const buckets = new Map<TaskStatusKey, BoardTask[]>(BOARD_COLUMN_ORDER.map((s) => [s, []]));
 
  for (const task of tasks) {
    const column = resolveStatus(task.status);
    const isBlocked =
      task.dependsOn.length > 0 &&
      task.dependsOn.some((depId) => taskStatusById.get(depId) !== "complete");
 
    const children = childrenByParent.get(task.id) ?? [];
    const childCount = children.length;
    const doneChildCount = children.filter((c) => c.status === "complete").length;
 
    let pausedSubBadge: BoardTask["pausedSubBadge"];
    if (column === "paused" && sessionStatusByTaskId) {
      const sessionStatus = sessionStatusByTaskId.get(task.id);
      if (sessionStatus === "idle") {
        pausedSubBadge = "Needs input";
      } else if (sessionStatus === "completed") {
        pausedSubBadge = "Ready to complete";
      }
    }
 
    const boardTask: BoardTask = {
      task,
      isBlocked,
      childCount,
      doneChildCount,
      pausedSubBadge,
    };
 
    const bucket = buckets.get(column);
    if (bucket) {
      bucket.push(boardTask);
    } else {
      // Unknown status → fall back to not_started column
      buckets.get("not_started")!.push(boardTask);
    }
  }
 
  // Sort tasks in each bucket by sortOrder
  for (const columnTasks of buckets.values()) {
    columnTasks.sort((a, b) => a.task.sortOrder - b.task.sortOrder);
  }
 
  // Build final column array in display order
  return BOARD_COLUMN_ORDER.map((status) => {
    const style = getStatusStyle(status);
 
    return {
      status,
      label: style.label,
      style,
      tasks: buckets.get(status) ?? [],
    };
  });
}