All files / src/pages/flowsheet-page/flowsheet/LeftSideBar taskItemLabels.ts

100% Statements 14/14
64.28% Branches 9/14
100% Functions 2/2
100% Lines 14/14

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      104520x                         34840x 34840x             3222x 3222x   3222x 34840x 34840x     3222x 34840x 34840x     34840x     34840x 34840x      
import type { TaskRead } from "@/api/apiStore.gen.ts";
 
function slugifyTaskType(taskType: TaskRead["task_type"]): string {
  return (
    (taskType ?? "Unknown")
      .toLowerCase()
      .replace(/[^a-z0-9]+/g, "-")
      .replace(/^-+|-+$/g, "") || "unknown"
  );
}
 
export function buildTaskItemAriaLabel(
  taskType: TaskRead["task_type"],
  index: number,
  subtask = false,
): string {
  const itemKind = subtask ? "subtask" : "log";
  return `${slugifyTaskType(taskType)}-${itemKind}-${index}`;
}
 
export function getTaskItemAriaLabels(
  tasks: TaskRead[],
  subtask = false,
): string[] {
  const typeTotals = new Map<string, number>();
  const typeSeenCounts = new Map<string, number>();
 
  tasks.forEach((task) => {
    const taskTypeKey = slugifyTaskType(task.task_type);
    typeTotals.set(taskTypeKey, (typeTotals.get(taskTypeKey) ?? 0) + 1);
  });
 
  return tasks.map((task) => {
    const taskTypeKey = slugifyTaskType(task.task_type);
    const tasksSeenSoFar = typeSeenCounts.get(taskTypeKey) ?? 0;
    // Tasks are rendered newest-first, so reverse the per-type counter to keep
    // the oldest visible task at index 0.
    const taskTypeIndex =
      (typeTotals.get(taskTypeKey) ?? 1) - tasksSeenSoFar - 1;
 
    typeSeenCounts.set(taskTypeKey, tasksSeenSoFar + 1);
    return buildTaskItemAriaLabel(task.task_type, taskTypeIndex, subtask);
  });
}