# All files / src/components/layout AppNav.tsx

60.45% Statements 133/220
100% Branches 23/23
50% Functions 1/2
60.45% Lines 133/220

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
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    3431x
    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
    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
    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
    20x
    1x
    1x
    20x
    3x
    3x
    20x
    1x
    1x
    20x
    3x
    3x
    20x
    3x
    3x
    20x
    2x
    2x
    20x

    2x
    2x
    20x
    1x
    1x
    20x
    1x
    1x
    20x
    1x
    1x
    2x
    2x

    1x

     import { useCallback, useMemo, useRef, type JSX, type KeyboardEvent, type ReactNode } from "react";
    import { useLocation } from "react-router";
    import {
      Activity,
      Brain,
      CalendarClock,
      ClipboardList,
      Home,
      MessageSquare,
      Monitor,
      Network,
      Settings,
      User,
    } from "lucide-react";
    import {
      CHAT_URL,
      COORDINATION_URL,
      ENVIRONMENTS_URL,
      HOME_URL,
      KNOWLEDGE_URL,
      PERSONAS_URL,
      SCHEDULES_URL,
      SESSIONS_URL,
      SETTINGS_URL,
      SETTINGS_CREDENTIALS_URL,
      TASKS_URL,
      useAppNavigate,
    } from "../../utils/navigation.js";
    import { ICON_LG } from "../../utils/iconSize.js";
    import { Tooltip } from "../display/Tooltip.js";
    import styles from "./AppNav.module.scss";

    /** Application view identifiers. */
    export type AppView =
      | "dashboard"
      | "chat"
      | "tasks"
      | "personas"
      | "environments"
      | "sessions"
      | "knowledge"
      | "coordination"
      | "schedules"
      | "settings";

    /**
     * Conceptual axis a nav tab belongs to, introduced with the context-axis
     * workbench shell (#1414). Tabs are grouped so the view bar can reason about
     * them by altitude rather than as one flat list:
     *
     * - `workbench` — the Code context's primary views (chat, sessions, tasks,
     *   knowledge, dashboard).
     * - `fleet` — cross-context overview surfaces (coordination). Relocated to a
     *   dedicated fleet altitude in #1415; rendered inline in the view bar for now.
     * - `global` — infrastructure / settings that sit outside the workbench
     *   (environments, settings); rendered as an end-aligned cluster.
     */
    export type NavGroup = "workbench" | "global" | "fleet";

    /** Tab definition for the application navigation bar. */
    export interface AppTab {
      /** View identifier. */
      view: AppView;
      /** Display label. */
      label: string;
      /** Icon element displayed before the label. */
      icon: ReactNode;
      /** Route to navigate to when clicked. */
      route: string;
      /** data-testid suffix. */
      testId: string;
      /**
       * Display order within the nav bar (lower numbers appear first). Applied
       * across all plugins so tab order is explicit rather than dependent on plugin
       * load order. End-aligned tabs ignore this and are always pinned right.
       */
      order?: number;
      /** Horizontal alignment within the nav bar. `"end"` pins the tab to the right edge. */
      align?: "end";
      /**
       * Conceptual axis this tab belongs to. Used by the shell to reason about tabs
       * by altitude (and, later, to relocate `fleet`/`global` tabs out of the view
       * bar). Optional so external tab lists keep working; defaults to `workbench`.
       */
      group?: NavGroup;
    }

    /** Ordered list of all app navigation tabs. Exported for plugin registry use. */
    export const TABS: AppTab[] = [
      {
        view: "dashboard",
        label: "Dashboard",
        icon: <Home size={ICON_LG} />,
        route: HOME_URL,
        testId: "sidebar-tab-dashboard",
        order: 0,
        group: "workbench",
      },
      {
        view: "tasks",
        label: "Tasks",
        icon: <ClipboardList size={ICON_LG} />,
        route: TASKS_URL,
        testId: "sidebar-tab-tasks",
        order: 1,
        group: "workbench",
      },
      {
        view: "personas",
        label: "Personas",
        icon: <User size={ICON_LG} />,
        route: PERSONAS_URL,
        testId: "sidebar-tab-personas",
        order: 6.2,
        group: "fleet",
      },
      {
        view: "chat",
        label: "Root",
        icon: <MessageSquare size={ICON_LG} />,
        route: CHAT_URL,
        testId: "sidebar-tab-chat",
        order: 3,
        group: "workbench",
      },
      {
        view: "sessions",
        label: "Sessions",
        icon: <Activity size={ICON_LG} />,
        route: SESSIONS_URL,
        testId: "sidebar-tab-sessions",
        order: 4,
        group: "workbench",
      },
      {
        view: "knowledge",
        label: "Knowledge",
        icon: <Brain size={ICON_LG} />,
        route: KNOWLEDGE_URL,
        testId: "sidebar-tab-knowledge",
        order: 5,
        group: "workbench",
      },
      {
        view: "coordination",
        label: "Coordination",
        icon: <Network size={ICON_LG} />,
        route: COORDINATION_URL,
        testId: "sidebar-tab-coordination",
        order: 6,
        group: "fleet",
      },
      // Schedules is a cross-context "fleet" surface (an agent's heartbeat), not a
      // per-context view — relocated out of Settings (#1416) as a holding home until
      // per-agent ownership (#1418). Tagged `group: "fleet"` so it rides the same
      // altitude as Coordination and is swept into the fleet rail by #1415 with no
      // further shell changes.
      {
        view: "schedules",
        label: "Schedules",
        icon: <CalendarClock size={ICON_LG} />,
        route: SCHEDULES_URL,
        testId: "sidebar-tab-schedules",
        order: 6.5,
        group: "fleet",
      },
      {
        view: "environments",
        label: "Environments",
        icon: <Monitor size={ICON_LG} />,
        route: ENVIRONMENTS_URL,
        testId: "sidebar-tab-environments",
        order: 6.4,
        group: "fleet",
      },
      {
        view: "settings",
        label: "Settings",
        icon: <Settings size={ICON_LG} />,
        route: SETTINGS_CREDENTIALS_URL,
        testId: "sidebar-tab-settings",
        align: "end",
        group: "global",
      },
    ];

    /** Derive the active application view from a URL pathname. */
    export function getActiveView(pathname: string): AppView {
      if (pathname === HOME_URL || pathname === "/") {
        return "dashboard";
      }
      if (pathname.startsWith(PERSONAS_URL)) {
        return "personas";
      }
      if (pathname.startsWith(COORDINATION_URL)) {
        return "coordination";
      }
      if (pathname.startsWith(SCHEDULES_URL)) {
        return "schedules";
      }
      if (pathname.startsWith(SESSIONS_URL)) {
        return "sessions";
      }
      if (pathname.startsWith("/chat")) {
        return "chat";
      }
      if (pathname.startsWith("/workspaces") || /^\/environments\/[^/]+\/workspaces\//.test(pathname)) {
        // Workspace routes (including environment-scoped workspaces) are workbench-level,
        // not fleet. Return "tasks" so AppNav stays visible (#1419).
        return "tasks";
      }
      if (pathname.startsWith("/environments")) {
        return "environments";
      }
      if (pathname.startsWith(KNOWLEDGE_URL)) {
        return "knowledge";
      }
      if (pathname.startsWith(SETTINGS_URL)) {
        return "settings";
      }
      return "tasks";
    }

    /** Props for the {@link AppNav} component. */
    export interface AppNavProps {
      /** Tabs to render. Defaults to the canonical {@link TABS}. */
      tabs?: AppTab[];
      /**
       * When provided, only tabs whose {@link AppTab.group} is in this list are
       * rendered (tabs without a `group` are treated as `workbench`). Omit to
       * render every tab. This is the lever the fleet/overview relocation (#1415)
       * uses to pull `fleet` tabs out of the view bar without changing `TABS`.
       */
      groups?: NavGroup[];
    }

    /** Full-width navigation bar below the StatusBar for switching between app views. */
    export function AppNav({ tabs = TABS, groups }: AppNavProps): JSX.Element {
      const location = useLocation();
      const navigate = useAppNavigate();
      const tabListRef = useRef<HTMLElement>(null);

      const activeView = getActiveView(location.pathname);

      // Sort by explicit `order`, then render end-aligned tabs (e.g. Settings) last
      // regardless of order, so they stay pinned to the right edge no matter which
      // plugins contribute tabs. Tabs without an `order` keep their incoming order
      // (stable sort) and fall after explicitly-ordered ones. When `groups` is set,
      // restrict to those axes first (a missing `group` counts as `workbench`).
      const orderedTabs = useMemo(() => {
        const byOrder = (a: AppTab, b: AppTab): number =>
          (a.order ?? Number.MAX_SAFE_INTEGER) - (b.order ?? Number.MAX_SAFE_INTEGER);
        const visible =
          groups === undefined ? tabs : tabs.filter((t) => groups.includes(t.group ?? "workbench"));
        return [
          ...visible.filter((t) => t.align !== "end").sort(byOrder),
          ...visible.filter((t) => t.align === "end"),
        ];
      }, [tabs, groups]);
      const firstEndAlignedView = orderedTabs.find((t) => t.align === "end")?.view;

      const handleClick = useCallback(
        (tab: AppTab) => {
          navigate(tab.route);
        },
        [navigate],
      );

      const handleKeyDown = useCallback(
        (e: KeyboardEvent<HTMLElement>) => {
          const buttons = tabListRef.current?.querySelectorAll<HTMLButtonElement>('[role="tab"]');
          if (!buttons) {
            return;
          }
          const focusedIndex = Array.from(buttons).findIndex((b) => b === document.activeElement);
          const currentIndex =
            focusedIndex >= 0 ? focusedIndex : orderedTabs.findIndex((t) => t.view === activeView);
          let nextIndex = currentIndex;

          if (e.key === "ArrowRight" || e.key === "j" || e.key === "J") {
            e.preventDefault();
            nextIndex = (currentIndex + 1) % orderedTabs.length;
          } else if (e.key === "ArrowLeft" || e.key === "k" || e.key === "K") {
            e.preventDefault();
            nextIndex = (currentIndex - 1 + orderedTabs.length) % orderedTabs.length;
          } else if (e.key === "Home") {
            e.preventDefault();
            nextIndex = 0;
          } else if (e.key === "End") {
            e.preventDefault();
            nextIndex = orderedTabs.length - 1;
          } else {
            return;
          }

          navigate(orderedTabs[nextIndex].route);
          buttons[nextIndex]?.focus(); // eslint-disable-line @typescript-eslint/no-unnecessary-condition -- index may be out of bounds
        },
        [activeView, navigate, orderedTabs],
      );

      return (
        <nav
          className={styles.nav}
          ref={tabListRef}
          role="tablist"
          aria-orientation="horizontal"
          aria-label="App navigation"
          onKeyDown={handleKeyDown}
          data-testid="sidebar-nav"
        >
          {orderedTabs.map((tab) => {
            const isActive = tab.view === activeView;
            const isFirstEndAligned = tab.view === firstEndAlignedView;
            return (
              <Tooltip
                key={tab.view}
                text={tab.label}
                placement="bottom"
                className={isFirstEndAligned ? styles.tabEnd : undefined}
              >
                <button
                  role="tab"
                  type="button"
                  aria-selected={isActive}
                  tabIndex={isActive ? 0 : -1}
                  className={`${styles.tab} ${isActive ? styles.tabActive : ""}`}
                  onClick={() => handleClick(tab)}
                  data-testid={tab.testId}
                  aria-label={tab.label}
                >
                  <span className={styles.tabIcon} aria-hidden="true">
                    {tab.icon}
                  </span>
                  <span className={styles.tabLabel}>{tab.label}</span>
                </button>
              </Tooltip>
            );
          })}
        </nav>
      );
    }