All files / src/components/lists TaskTreeNode.tsx

50% Statements 13/26
64.1% Branches 25/39
33.33% Functions 3/9
50% Lines 13/26

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 173 174 175 176 177 178 179 180 181 182                          12x   12x                                                                       211x   211x   211x 211x 211x 211x 211x   211x     211x                                                                                                                         2x                                                                         1x                                      
import { type CSSProperties, type JSX } from "react";
import { ChevronRight } from "lucide-react";
import { AnimatePresence, motion } from "motion/react";
import { MAX_TASK_DEPTH, type MatchIndex } from "@grackle-ai/common";
import { ICON_SM } from "../../utils/iconSize.js";
import { taskUrl, newTaskUrl } from "../../utils/navigation.js";
import type { useAppNavigate } from "../../utils/navigation.js";
import { getStatusStyle, resolveStatus } from "../../utils/taskStatus.js";
import { Tooltip } from "../display/Tooltip.js";
import { HighlightedText, type TaskNode } from "./listHelpers.js";
import styles from "./TaskList.module.scss";
 
/** Base left-padding for task rows. */
const TASK_BASE_INDENT_PX: number = 16;
/** Additional left-padding per depth level. */
const TASK_DEPTH_INDENT_PX: number = 16;
 
/** Props for the recursive TaskTreeNode component. */
export interface TaskTreeNodeProps {
  /** The tree node to render. */
  node: TaskNode;
  /** Current nesting depth (0 = root). */
  depth: number;
  /** Set of task IDs that are currently expanded. */
  expandedTasks: Set<string>;
  /** Toggle expand/collapse for a task node. */
  toggleTask: (taskId: string) => void;
  /** ID of the currently selected task, if any. */
  selectedTaskId: string | undefined;
  /** Navigate function from useAppNavigate. */
  navigate: ReturnType<typeof useAppNavigate>;
  /** Status lookup by task ID, used for blocked-dependency detection. */
  taskStatusById: Map<string, string>;
  /** Per-task title highlight indices for matched search queries. */
  titleHighlights: Map<string, readonly MatchIndex[]>;
  /** Workspace name lookup by workspace ID. */
  workspaceNames: Map<string, string>;
}
 
/** Renders a single task tree node and recursively renders its expanded children. */
export function TaskTreeNode({
  node,
  depth,
  expandedTasks,
  toggleTask,
  selectedTaskId,
  navigate,
  taskStatusById,
  titleHighlights,
  workspaceNames,
}: TaskTreeNodeProps): JSX.Element {
  const statusStyle = getStatusStyle(node.status);
  const isBlocked =
    node.dependsOn.length > 0 &&
    node.dependsOn.some((depId) => taskStatusById.get(depId) !== "complete");
  const isExpanded = expandedTasks.has(node.id);
  const hasChildren = node.children.length > 0;
  const isSelected = selectedTaskId === node.id;
  const indent = TASK_BASE_INDENT_PX + depth * TASK_DEPTH_INDENT_PX;
  const isRoot = depth === 0;
  const wsName =
    isRoot && !node.parentTaskId && node.workspaceId
      ? workspaceNames.get(node.workspaceId)
      : undefined;
  return (
    <>
      <div
        onClick={() => navigate(taskUrl(node.id))}
        role="button"
        tabIndex={0}
        aria-label={node.title}
        onKeyDown={(e) => {
          Iif (e.currentTarget === e.target && (e.key === "Enter" || e.key === " ")) {
            e.preventDefault();
            navigate(taskUrl(node.id));
          }
        }}
        className={`${styles.taskRow} ${isSelected ? styles.selected : ""}`}
        style={{ "--task-indent": `${indent}px` } as CSSProperties}
        data-task-id={node.id}
      >
        {hasChildren && (
          <span
            className={`${styles.expandArrow} ${isExpanded ? styles.expanded : ""}`}
            role="button"
            tabIndex={0}
            aria-label={isExpanded ? "Collapse task" : "Expand task"}
            onClick={(e) => {
              e.stopPropagation();
              toggleTask(node.id);
            }}
            onKeyDown={(e) => {
              Iif (e.key === "Enter" || e.key === " ") {
                e.preventDefault();
                e.stopPropagation();
                toggleTask(node.id);
              }
            }}
          >
            <ChevronRight size={ICON_SM} aria-hidden="true" />
          </span>
        )}
        {!hasChildren && <span className={styles.leafSpacer} />}
        <span
          className={styles.taskStatusIcon}
          style={{ color: statusStyle.color }}
          aria-hidden="true"
          data-testid={`task-status-${resolveStatus(node.status)}`}
        >
          {statusStyle.icon}
        </span>
        <span className={styles.taskTitle} title={node.title}>
          <HighlightedText
            text={node.title}
            indices={titleHighlights.get(node.id)}
            highlightClass={styles.searchHighlight}
          />
        </span>
        {wsName && (
          <span className={styles.workspaceBadge} title={wsName}>
            {wsName}
          </span>
        )}
        {hasChildren && (
          <span className={styles.childCountBadge}>
            {node.children.filter((c) => c.status === "complete").length}/{node.children.length}
          </span>
        )}
        {node.dependsOn.length > 0 && (
          <span
            className={`${styles.dependencyBadge} ${isBlocked ? styles.blockedBadge : ""}`}
            title={`Depends on: ${node.dependsOn.join(", ")}`}
          >
            {isBlocked ? "blocked" : "dep"}
          </span>
        )}
        {depth < MAX_TASK_DEPTH && (
          <Tooltip text="Add child task">
            <button
              onClick={(e) => {
                e.stopPropagation();
                navigate(newTaskUrl(node.workspaceId, node.id));
              }}
              aria-label="Add child task"
              className={styles.addChildButton}
            >
              +
            </button>
          </Tooltip>
        )}
      </div>
 
      <AnimatePresence>
        {hasChildren && isExpanded && (
          <motion.div
            initial={{ height: 0, opacity: 0 }}
            animate={{ height: "auto", opacity: 1 }}
            exit={{ height: 0, opacity: 0 }}
            transition={{ duration: 0.15 }}
            style={{ overflow: "hidden" }}
          >
            {node.children.map((child) => (
              <TaskTreeNode
                key={child.id}
                node={child}
                depth={depth + 1}
                expandedTasks={expandedTasks}
                toggleTask={toggleTask}
                selectedTaskId={selectedTaskId}
                navigate={navigate}
                taskStatusById={taskStatusById}
                titleHighlights={titleHighlights}
                workspaceNames={workspaceNames}
              />
            ))}
          </motion.div>
        )}
      </AnimatePresence>
    </>
  );
}