# All files / src/utils breadcrumbs.ts

93.75% Statements 135/144
68.75% Branches 22/32
83.33% Functions 10/12
93.75% Lines 135/144

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
    1x

    1x
    7x
    7x
    7x
    7x
    7x
    7x

    7x
    13x
    1x
    1x
    12x
    12x
    12x
    13x
    13x
    7x
    7x

    1x

    1x
    1x
    1x

    1x
    2x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x

    1x

    1x

    1x
    1x
    1x

    1x
    2x
    2x

    1x

    1x

    1x
    1x
    1x

    1x
    1x
    1x

    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x

    1x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x

    2x

    2x
    2x
    2x
    2x

    2x
    2x
    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
    1x

    1x
    1x
    1x
    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,
      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 };

    /** 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 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];

      if (environment && environmentId) {
        segments.push(ENVIRONMENTS_SEGMENT);
        segments.push({ label: environment.displayName, url: environmentUrl(environmentId) });
      }

      if (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];

      if (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 {
        segments.push({
          label: workspace?.name ?? "Workspace",
          url: undefined,
        });
      }

      // If creating a child task, show parent ancestors
      if (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;
    }