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

89.74% Statements 35/39
83.33% Branches 25/30
80% Functions 8/10
91.17% Lines 31/34

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

    21x

    21x
    68x

    68x

    21x

    8x
    8x

    14x

    8x
    8x

    8x
    2x
    2x
    6x
    2x
    2x
    4x
    2x
    2x
    2x
    2x
    2x

    8x
    8x

    21x

    68x
    68x
    68x

    6x

    6x

    6x

     /**
     * ContextDetailShell — shared header + tab bar for context detail views.
     * Used by both the Code context and Agent detail pages so the
     * back-arrow / icon / name / tab-bar pattern is rendered from one component.
     *
     * @module
     */

    import { useCallback, useMemo, useRef, type JSX, type KeyboardEvent, type ReactNode } from "react";
    import { Activity, ArrowLeft, CalendarClock, Code2, MessageSquare, Settings } from "lucide-react";
    import { Tooltip } from "../display/Tooltip.js";
    import type { AgentTab } from "../../utils/navigation.js";
    import navStyles from "./AppNav.module.scss";
    import styles from "./ContextDetailShell.module.scss";

    /** A single tab in the {@link ContextDetailShell} tab bar. */
    export interface ContextDetailTab {
      /** Stable identifier used as the `activeTab` value. */
      id: string;
      /** Display label. */
      label: string;
      /** Icon element rendered before the label. */
      icon: ReactNode;
      /** Optional `data-testid` for the tab button. */
      testId?: string;
      /** When `"end"`, the tab is pinned to the right edge of the bar. */
      align?: "end";
    }

    /** Props for {@link ContextDetailShell}. */
    export interface ContextDetailShellProps {
      /** Icon element displayed in the header (avatar, Lucide icon, etc.). */
      icon: ReactNode;
      /** Context name displayed next to the icon. */
      name: string;
      /** Navigate back (e.g. to the home page). */
      onNavigateBack: () => void;
      /** Optional content rendered below the name (e.g. heartbeat status). */
      statusContent?: ReactNode;
      /** Tabs rendered in the tab bar. */
      tabs: ContextDetailTab[];
      /** Identifier of the currently active tab. */
      activeTab: string;
      /** Called with a tab id when the user selects a tab. */
      onSelectTab: (tabId: string) => void;
      /** Accessible label for the tab bar (`aria-label`). Defaults to `"Navigation"`. */
      ariaLabel?: string;
      /** `data-testid` for the header element. */
      headerTestId?: string;
      /** `data-testid` for the tab bar `<nav>` element. */
      tabBarTestId?: string;
    }

    /**
     * Shared layout shell for context detail pages. Renders a header row
     * (back arrow, icon, name, optional status) followed by a horizontal
     * tab bar with keyboard navigation and end-alignment support.
     */
    export function ContextDetailShell({
      icon,
      name,
      onNavigateBack,
      statusContent,
      tabs,
      activeTab,
      onSelectTab,
      ariaLabel = "Navigation",
      headerTestId,
      tabBarTestId,
    }: ContextDetailShellProps): JSX.Element {
      const tabListRef = useRef<HTMLElement>(null);

      const orderedTabs = useMemo(
        () => [...tabs.filter((t) => t.align !== "end"), ...tabs.filter((t) => t.align === "end")],
        [tabs],
      );
      const firstEndAlignedId = orderedTabs.find((t) => t.align === "end")?.id;

      const handleKeyDown = useCallback(
        (e: KeyboardEvent<HTMLElement>) => {
          const buttons = tabListRef.current?.querySelectorAll<HTMLButtonElement>('[role="tab"]');
          Iif (!buttons || buttons.length === 0) {
            return;
          }

          const focusedIndex = Array.from(buttons).findIndex((b) => b === document.activeElement);
          const currentIndex =
            focusedIndex >= 0 ? focusedIndex : orderedTabs.findIndex((t) => t.id === activeTab);
          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 E{
            return;
          }

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

      return (
        <>
          <header className={styles.header} data-testid={headerTestId}>
            <button
              type="button"
              className={styles.backButton}
              onClick={onNavigateBack}
              aria-label="Back"
              data-testid={headerTestId ? `${headerTestId}-back` : undefined}
            >
              <ArrowLeft size={18} />
            </button>
            <span className={styles.icon} aria-hidden="true">
              {icon}
            </span>
            <div className={styles.info}>
              <h2
                className={styles.name}
                data-testid={headerTestId ? `${headerTestId}-name` : undefined}
              >
                {name}
              </h2>
              {statusContent}
            </div>
          </header>
          <nav
            ref={tabListRef}
            className={navStyles.nav}
            role="tablist"
            aria-label={ariaLabel}
            aria-orientation="horizontal"
            onKeyDown={handleKeyDown}
            data-testid={tabBarTestId}
          >
            {orderedTabs.map((tab) => {
              const isActive = tab.id === activeTab;
              const isFirstEndAligned = tab.id === firstEndAlignedId;
              return (
                <Tooltip
                  key={tab.id}
                  text={tab.label}
                  placement="bottom"
                  className={isFirstEndAligned ? navStyles.tabEnd : undefined}
                >
                  <button
                    role="tab"
                    type="button"
                    aria-selected={isActive}
                    tabIndex={isActive ? 0 : -1}
                    className={`${navStyles.tab} ${isActive ? navStyles.tabActive : ""}`}
                    onClick={() => onSelectTab(tab.id)}
                    data-testid={tab.testId}
                    aria-label={tab.label}
                  >
                    <span className={navStyles.tabIcon} aria-hidden="true">
                      {tab.icon}
                    </span>
                    <span className={navStyles.tabLabel}>{tab.label}</span>
                  </button>
                </Tooltip>
              );
            })}
          </nav>
        </>
      );
    }

    // ---------------------------------------------------------------------------
    // Pre-built tab arrays and icons for the two stock contexts (Code, Agent).
    // Consumers pass these to ContextDetailShell to avoid duplicating lucide
    // imports or re-defining the same tab lists.
    // ---------------------------------------------------------------------------

    const TAB_ICON_SIZE: number = 18;

    /** Icon element for the Code context header (matches the ContextNav rail icon). */
    export const CODE_HEADER_ICON: JSX.Element = <Code2 size={24} />;

    /** Tab definitions for the Agent detail view, with Settings right-aligned. */
    export const AGENT_DETAIL_TABS: (ContextDetailTab & { id: AgentTab })[] = [
      {
        id: "chat",
        label: "Chat",
        icon: <MessageSquare size={TAB_ICON_SIZE} />,
        testId: "agent-tab-chat",
      },
      {
        id: "sessions",
        label: "Sessions",
        icon: <Activity size={TAB_ICON_SIZE} />,
        testId: "agent-tab-sessions",
      },
      {
        id: "schedules",
        label: "Schedules",
        icon: <CalendarClock size={TAB_ICON_SIZE} />,
        testId: "agent-tab-schedules",
      },
      {
        id: "settings",
        label: "Settings",
        icon: <Settings size={TAB_ICON_SIZE} />,
        testId: "agent-tab-settings",
        align: "end",
      },
    ];