All files / src/components/lists TaskList.tsx

73.46% Statements 36/49
82.14% Branches 23/28
75% Functions 15/20
71.42% Lines 30/42

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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172                                                243x 243x 243x     243x               243x     243x 243x   243x   243x 78x       243x                                     243x 121x 78x 1x 1x 1x 1x 1x     1x           243x         243x   243x   243x 243x                                             243x               66x                   90x       32x               210x                                              
import { useEffect, useMemo, useState, type JSX } from "react";
import { List } from "lucide-react";
import { useMatch } from "react-router";
import type { Workspace, TaskData } from "../../hooks/types.js";
import { ICON_MD } from "../../utils/iconSize.js";
import { newTaskUrl, useAppNavigate } from "../../utils/navigation.js";
import { SectionHeader, type SectionHeaderAction } from "../display/SectionHeader.js";
import { buildTaskTree, groupTasksByStatus } from "./listHelpers.js";
import { StatusGroupAccordion } from "./StatusGroupAccordion.js";
import { TaskTreeNode } from "./TaskTreeNode.js";
import { useGroupByStatus } from "./useGroupByStatus.js";
import { useTaskSearch } from "./useTaskSearch.js";
import styles from "./TaskList.module.scss";
 
/** Props for the TaskList component. */
interface TaskListProps {
  /** All workspaces (used for workspace name lookup). */
  workspaces: Workspace[];
  /** All tasks to display. */
  tasks: TaskData[];
}
 
/** Global task tree sidebar view — shows all tasks across all workspaces. */
export function TaskList({ workspaces, tasks }: TaskListProps): JSX.Element {
  const navigate = useAppNavigate();
  const [expandedTasks, setExpandedTasks] = useState<Set<string>>(new Set());
  const [manuallyCollapsed, setManuallyCollapsed] = useState<Set<string>>(new Set());
 
  const { groupByStatus, toggleGroupByStatus, isGroupExpanded, toggleStatusGroup } =
    useGroupByStatus();
  const {
    searchQuery,
    setSearchQuery,
    directMatchTaskIds,
    treeMatchTaskIds,
    titleHighlights,
    isSearching,
  } = useTaskSearch(tasks);
 
  // Derive selected state from router
  const taskMatch = useMatch("/tasks/:taskId/*");
  const selectedTaskId = taskMatch?.params.taskId !== "new" ? taskMatch?.params.taskId : undefined;
 
  const taskStatusById = useMemo(() => new Map(tasks.map((t) => [t.id, t.status])), [tasks]);
 
  const workspaceNames = useMemo(
    () => new Map(workspaces.map((w) => [w.id, w.name])),
    [workspaces],
  );
 
  const toggleTask = (tid: string): void => {
    setExpandedTasks((prev) => {
      const next = new Set(prev);
      if (next.has(tid)) {
        next.delete(tid);
        setManuallyCollapsed((mc) => new Set(mc).add(tid));
      } else {
        next.add(tid);
        setManuallyCollapsed((mc) => {
          const updated = new Set(mc);
          updated.delete(tid);
          return updated;
        });
      }
      return next;
    });
  };
 
  // Auto-expand parent tasks that have children (skip manually collapsed ones)
  useEffect(() => {
    const parentIds = new Set(tasks.filter((t) => t.parentTaskId).map((t) => t.parentTaskId));
    if (parentIds.size > 0) {
      setExpandedTasks((prev) => {
        const next = new Set(prev);
        for (const pid of parentIds) {
          if (!manuallyCollapsed.has(pid)) {
            next.add(pid);
          }
        }
        return next;
      });
    }
  }, [tasks, manuallyCollapsed]);
 
  // Resolve which tasks are visible given the current search and grouping mode
  const activeMatchIds = isSearching
    ? groupByStatus
      ? directMatchTaskIds
      : treeMatchTaskIds
    : undefined;
  const visibleTasks = activeMatchIds ? tasks.filter((t) => activeMatchIds.has(t.id)) : tasks;
 
  const tree = !groupByStatus ? buildTaskTree(visibleTasks) : [];
 
  const headerActions = useMemo<SectionHeaderAction[]>(
    () => [
      {
        key: "group",
        icon: <List size={ICON_MD} />,
        tooltip: groupByStatus ? "Switch to tree view" : "Group tasks by status",
        ariaLabel: groupByStatus ? "Switch to tree view" : "Group tasks by status",
        onClick: toggleGroupByStatus,
        active: groupByStatus,
        ariaPressed: groupByStatus,
        testId: "task-group-by-status-toggle",
      },
      {
        key: "add",
        icon: <span>+</span>,
        tooltip: "New task",
        ariaLabel: "New task",
        onClick: () => navigate(newTaskUrl()),
        testId: "new-task-button",
      },
    ],
    [groupByStatus, toggleGroupByStatus, navigate],
  );
 
  return (
    <div className={styles.container}>
      <SectionHeader title="Tasks" actions={headerActions} data-testid="task-list-header" />
 
      {tasks.length > 0 && (
        <input
          type="text"
          value={searchQuery}
          onChange={(e) => setSearchQuery(e.target.value)}
          placeholder="Filter..."
          aria-label="Filter tasks"
          className={styles.searchInput}
          data-testid="sidebar-search"
        />
      )}
 
      {groupByStatus
        ? groupTasksByStatus(visibleTasks, taskStatusById).map((group) => (
            <StatusGroupAccordion
              key={group.status}
              group={group}
              isExpanded={isGroupExpanded(group.status)}
              onToggle={() => toggleStatusGroup(group.status)}
              selectedTaskId={selectedTaskId}
              navigate={navigate}
              titleHighlights={titleHighlights}
              workspaceNames={workspaceNames}
            />
          ))
        : tree.map((node) => (
            <TaskTreeNode
              key={node.id}
              node={node}
              depth={0}
              expandedTasks={expandedTasks}
              toggleTask={toggleTask}
              selectedTaskId={selectedTaskId}
              navigate={navigate}
              taskStatusById={taskStatusById}
              titleHighlights={titleHighlights}
              workspaceNames={workspaceNames}
            />
          ))}
 
      {visibleTasks.length === 0 && !isSearching && (
        <div className={styles.emptyState}>No tasks yet. Click + to create one.</div>
      )}
      {visibleTasks.length === 0 && isSearching && (
        <div className={styles.emptyState}>No matching tasks</div>
      )}
    </div>
  );
}