All files / src/utils breadcrumbs.ts

87.83% Statements 65/74
63.63% Branches 28/44
66.66% Functions 16/24
86.56% Lines 58/67

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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245                                                        7x 7x 7x   7x 13x 1x   12x 12x 12x 12x   7x       1x       1x         2x 1x           1x       1x     1x     1x       1x         2x                                                                                         1x         1x                   1x 1x 1x                               2x 2x 2x 2x 2x 2x   2x   2x 2x 2x     2x 2x             2x 2x             2x 2x           2x                       1x 1x 1x   1x   1x 1x 1x     1x 1x                       1x 1x 1x 1x             1x 1x    
import type { Environment, TaskData, Workspace } from "../hooks/types.js";
import {
  ENVIRONMENTS_URL,
  environmentUrl,
  HOME_URL,
  PERSONAS_URL,
  SCHEDULES_URL,
  SETTINGS_URL,
  taskUrl,
  workspaceUrl,
} from "./navigation.js";
 
/** A single segment in the breadcrumb trail. */
export interface BreadcrumbSegment {
  /** Display label for this segment. */
  label: string;
  /** URL to navigate to when clicked, or undefined for the current (non-clickable) segment. */
  url: string | undefined;
}
 
/**
 * Walks up the `parentTaskId` chain from a task to build an ordered list
 * of ancestor tasks (root-first). Includes the task itself as the last element.
 */
export function buildTaskAncestorChain(
  taskId: string,
  tasksById: Map<string, TaskData>,
): TaskData[] {
  const ancestors: TaskData[] = [];
  let currentId: string | undefined = taskId;
  const visited = new Set<string>();
 
  while (currentId && tasksById.has(currentId)) {
    if (visited.has(currentId)) {
      break; // guard against cycles
    }
    visited.add(currentId);
    const ancestor: TaskData = tasksById.get(currentId)!;
    ancestors.unshift(ancestor);
    currentId = ancestor.parentTaskId || undefined;
  }
  return ancestors;
}
 
/** Home breadcrumb segment. */
const HOME_SEGMENT: BreadcrumbSegment = { label: "Home", url: HOME_URL };
 
/** Build breadcrumbs for the home page. */
export function buildHomeBreadcrumbs(): BreadcrumbSegment[] {
  return [{ label: "Home", url: undefined }];
}
 
/** Build breadcrumbs for the settings page, optionally showing the active tab. */
export function buildSettingsBreadcrumbs(tabLabel?: string): BreadcrumbSegment[] {
  if (tabLabel) {
    return [
      HOME_SEGMENT,
      { label: "Settings", url: SETTINGS_URL },
      { label: tabLabel, url: undefined },
    ];
  }
  return [HOME_SEGMENT, { label: "Settings", url: undefined }];
}
 
/** Environments breadcrumb segment. */
const ENVIRONMENTS_SEGMENT: BreadcrumbSegment = { label: "Environments", url: ENVIRONMENTS_URL };
 
/** Personas breadcrumb segment (links back to the top-level Persona Library). */
const PERSONAS_SEGMENT: BreadcrumbSegment = { label: "Personas", url: PERSONAS_URL };
 
/** Schedules breadcrumb segment (links back to the top-level Schedules page). */
const SCHEDULES_SEGMENT: BreadcrumbSegment = { label: "Schedules", url: SCHEDULES_URL };
 
/** Build breadcrumbs for the top-level Persona Library page. */
export function buildPersonaLibraryBreadcrumbs(): BreadcrumbSegment[] {
  return [HOME_SEGMENT, { label: "Personas", url: undefined }];
}
 
/** Build breadcrumbs for a persona detail (create or edit) page. */
export function buildPersonaDetailBreadcrumbs(label: string): BreadcrumbSegment[] {
  return [HOME_SEGMENT, PERSONAS_SEGMENT, { label, url: undefined }];
}
 
/** Build breadcrumbs for the coordination page. */
export function buildCoordinationBreadcrumbs(): BreadcrumbSegment[] {
  return [HOME_SEGMENT, { label: "Coordination", url: undefined }];
}
 
/** Build breadcrumbs for the schedules list page. */
export function buildSchedulesBreadcrumbs(): BreadcrumbSegment[] {
  return [HOME_SEGMENT, { label: "Schedules", url: undefined }];
}
 
/** Build breadcrumbs for a schedule detail (create or edit) page. */
export function buildScheduleDetailBreadcrumbs(label: string): BreadcrumbSegment[] {
  return [HOME_SEGMENT, SCHEDULES_SEGMENT, { label, url: undefined }];
}
 
/** Build breadcrumbs for the sessions list page. */
export function buildSessionsListBreadcrumbs(): BreadcrumbSegment[] {
  return [HOME_SEGMENT, { label: "Sessions", url: undefined }];
}
 
/** Build breadcrumbs for an agent detail page. */
export function buildAgentBreadcrumbs(agentName: string): BreadcrumbSegment[] {
  return [HOME_SEGMENT, { label: agentName, url: undefined }];
}
 
/** Build breadcrumbs for the new agent page. */
export function buildAgentCreateBreadcrumbs(): BreadcrumbSegment[] {
  return [HOME_SEGMENT, { label: "New Agent", url: undefined }];
}
 
/** Build breadcrumbs for the environments landing page. */
export function buildEnvironmentsBreadcrumbs(): BreadcrumbSegment[] {
  return [HOME_SEGMENT, { label: "Environments", url: undefined }];
}
 
/** Build breadcrumbs for the new environment page. */
export function buildNewEnvironmentBreadcrumbs(): BreadcrumbSegment[] {
  return [HOME_SEGMENT, ENVIRONMENTS_SEGMENT, { label: "New Environment", url: undefined }];
}
 
/** Build breadcrumbs for the new chat page. */
export function buildNewChatBreadcrumbs(): BreadcrumbSegment[] {
  return [HOME_SEGMENT, { label: "New Chat", url: undefined }];
}
 
/** Build breadcrumbs for a session page. */
export function buildSessionBreadcrumbs(sessionId: string): BreadcrumbSegment[] {
  return [HOME_SEGMENT, { label: `Session ${sessionId.slice(0, 8)}`, url: undefined }];
}
 
/** Build breadcrumbs for a workspace page: Home > Environments > [Env] > [Workspace]. */
export function buildWorkspaceBreadcrumbs(
  workspaceId: string,
  environmentId: string,
  workspaces: Workspace[],
  environments: Environment[],
): BreadcrumbSegment[] {
  const workspace = workspaces.find((p) => p.id === workspaceId);
  const environment = environments.find((e) => e.id === environmentId);
  return [
    HOME_SEGMENT,
    ENVIRONMENTS_SEGMENT,
    { label: environment?.displayName ?? "Environment", url: environmentUrl(environmentId) },
    { label: workspace?.name ?? "Workspace", url: undefined },
  ];
}
 
/** Build breadcrumbs for a task page: Home > Environments > [Env] > [Workspace] > [ancestors...] > [Task]. */
export function buildTaskBreadcrumbs(
  taskId: string,
  routeEnvironmentId: string | undefined,
  workspaces: Workspace[],
  environments: Environment[],
  tasksById: Map<string, TaskData>,
): BreadcrumbSegment[] {
  const ancestors = buildTaskAncestorChain(taskId, tasksById);
  const task = tasksById.get(taskId);
  const taskWorkspaceId = task?.workspaceId;
  const workspace = taskWorkspaceId ? workspaces.find((p) => p.id === taskWorkspaceId) : undefined;
  const environmentId = routeEnvironmentId ?? workspace?.linkedEnvironmentIds[0];
  const environment = environmentId ? environments.find((e) => e.id === environmentId) : undefined;
 
  const segments: BreadcrumbSegment[] = [HOME_SEGMENT];
 
  Eif (environment && environmentId) {
    segments.push(ENVIRONMENTS_SEGMENT);
    segments.push({ label: environment.displayName, url: environmentUrl(environmentId) });
  }
 
  Eif (workspace && environmentId) {
    segments.push({
      label: workspace.name,
      url: workspaceUrl(workspace.id, environmentId),
    });
  }
 
  // Add ancestor tasks (all except the last, which is the current task)
  for (let i = 0; i < ancestors.length - 1; i++) {
    segments.push({
      label: ancestors[i].title,
      url: taskUrl(ancestors[i].id, undefined, taskWorkspaceId, environmentId),
    });
  }
 
  // Current task (non-clickable)
  const currentTask = ancestors[ancestors.length - 1];
  segments.push({
    // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- currentTask may be undefined if ancestors is empty
    label: currentTask?.title ?? taskId,
    url: undefined,
  });
 
  return segments;
}
 
/** Build breadcrumbs for the new task page. */
export function buildNewTaskBreadcrumbs(
  workspaceIdParam: string,
  environmentId: string | undefined,
  parentTaskId: string | undefined,
  workspaces: Workspace[],
  environments: Environment[],
  tasksById: Map<string, TaskData>,
): BreadcrumbSegment[] {
  const workspace = workspaces.find((p) => p.id === workspaceIdParam);
  const envId = environmentId ?? workspace?.linkedEnvironmentIds[0];
  const environment = envId ? environments.find((e) => e.id === envId) : undefined;
 
  const segments: BreadcrumbSegment[] = [HOME_SEGMENT];
 
  Eif (environment && envId) {
    segments.push(ENVIRONMENTS_SEGMENT);
    segments.push({ label: environment.displayName, url: environmentUrl(envId) });
  }
 
  if (envId) {
    segments.push({
      label: workspace?.name ?? "Workspace",
      url: workspaceUrl(workspaceIdParam, envId),
    });
  } else E{
    segments.push({
      label: workspace?.name ?? "Workspace",
      url: undefined,
    });
  }
 
  // If creating a child task, show parent ancestors
  Eif (parentTaskId) {
    const ancestors = buildTaskAncestorChain(parentTaskId, tasksById);
    for (const ancestor of ancestors) {
      segments.push({
        label: ancestor.title,
        url: taskUrl(ancestor.id, undefined, workspaceIdParam, envId),
      });
    }
  }
 
  segments.push({ label: "New Task", url: undefined });
  return segments;
}