# All files / src/components/lists EnvironmentNav.tsx

63.06% Statements 70/111
60% Branches 51/85
52.94% Functions 18/34
62.37% Lines 63/101

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

    7x

    7x

    7x

    44x
    44x

    44x
    44x
    44x

    44x

    44x

    44x

    44x
    44x

    44x
    81x
    81x
    28x

    81x

    69x

    44x
    28x

    44x
    28x
    28x

    44x
    28x
    28x

    44x
    44x

    44x

    16x
    16x

    27x
    16x

    16x
    16x

    16x
    5x
    5x
    11x
    5x
    5x
    6x
    3x
    3x
    3x
    3x
    3x

    16x
    16x
    16x

    16x

    44x
    44x
    44x

    44x

    44x

    44x

    129x
    129x
    129x
    129x
    129x

     /**
     * EnvironmentNav -- vertical sidebar navigation for the Environments page.
     *
     * @module
     */

    import { useCallback, useMemo, useRef, useState, type JSX, type KeyboardEvent } from "react";
    import { Circle, Filter, Layers, Plus } from "lucide-react";
    import { ICON_XS, ICON_MD } from "../../utils/iconSize.js";
    import { useMatch } from "react-router";
    import type { Environment } from "../../hooks/types.js";
    import { environmentUrl, NEW_ENVIRONMENT_URL, useAppNavigate } from "../../utils/navigation.js";
    import { SectionHeader, type SectionHeaderAction } from "../display/SectionHeader.js";
    import { FilterDropdown, type FilterDropdownGroup } from "../display/FilterDropdown.js";
    import { useFilterGroupSort } from "../../hooks/useFilterGroupSort.js";
    import styles from "./EnvironmentNav.module.scss";

    /** Status-dot color mapping using CSS custom properties. */
    const STATUS_COLORS: Record<string, string> = {
      connected: "var(--accent-green)",
      sleeping: "var(--accent-yellow)",
      error: "var(--accent-red)",
      disconnected: "var(--text-tertiary)",
      connecting: "var(--accent-blue)",
    };

    /** Human-readable labels for status values. */
    const STATUS_LABELS: Record<string, string> = {
      connected: "Connected",
      sleeping: "Sleeping",
      error: "Error",
      disconnected: "Disconnected",
      connecting: "Connecting",
    };

    /** Human-readable labels for adapter types. */
    const TYPE_LABELS: Record<string, string> = {
      local: "Local",
      ssh: "SSH",
      codespace: "Codespace",
      docker: "Docker",
    };

    /** Props for the EnvironmentNav component. */
    interface EnvironmentNavProps {
      /** List of all environments to display in the nav. */
      environments: Environment[];
    }

    /** Vertical nav rail listing environments with status dots, filter, and grouping. */
    export function EnvironmentNav({ environments }: EnvironmentNavProps): JSX.Element {
      const navigate = useAppNavigate();
      const tabListRef = useRef<HTMLElement>(null);

      const envMatch = useMatch("/environments/:environmentId");
      const workspaceMatch = useMatch("/environments/:environmentId/workspaces/:workspaceId");
      const workspaceSubMatch = useMatch("/environments/:environmentId/workspaces/:workspaceId/*");
      const rawId =
        envMatch?.params.environmentId ??
        workspaceMatch?.params.environmentId ??
        workspaceSubMatch?.params.environmentId;
      const activeId = rawId === "new" ? undefined : rawId;

      const {
        filterValues,
        filterActive,
        toggleFilter,
        clearFilter,
        groupBy,
        groupActive,
        toggleGroup,
        clearGroup,
      } = useFilterGroupSort({
        storagePrefix: "grackle-env-nav",
        validGroupKeys: ["status", "type"],
      });

      const [filterOpen, setFilterOpen] = useState(false);
      const [groupOpen, setGroupOpen] = useState(false);

      // ── Filter groups (single-level, all dimensions at once) ────────
      const filterGroups = useMemo<FilterDropdownGroup[]>(() => {
        const statuses = [...new Set(environments.map((e) => e.status))].sort();
        const types = [...new Set(environments.map((e) => e.adapterType))].sort();
        return [
          {
            label: "Status",
            options: statuses.map((s) => ({ key: `status:${s}`, label: STATUS_LABELS[s] || s })),
          },
          {
            label: "Type",
            options: types.map((t) => ({ key: `type:${t}`, label: TYPE_LABELS[t] || t })),
          },
        ];
      }, [environments]);

      const groupOptions = useMemo(
        () => [
          { key: "status", label: "By Status" },
          { key: "type", label: "By Type" },
        ],
        [],
      );

      // ── Filtered environments ───────────────────────────────────────
      const filtered = useMemo(() => {
        if (!filterActive) {
          return environments;
        }
        const statusFilters = new Set<string>();
        const typeFilters = new Set<string>();
        for (const k of filterValues) {
          if (k.startsWith("status:")) {
            statusFilters.add(k);
          I} else if (k.startsWith("type:")) {
            typeFilters.add(k);
          }
        }
        return environments.filter((env) => {
          const passesStatus = statusFilters.size === 0 || statusFilters.has(`status:${env.status}`);
          const passesType = typeFilters.size === 0 || typeFilters.has(`type:${env.adapterType}`);
          return passesStatus && passesType;
        });
      }, [environments, filterActive, filterValues]);

      // ── Grouped environments ────────────────────────────────────────
      const groups = useMemo(() => {
        if (!groupActive) {
          return [{ id: "__all__", label: "", environments: filtered }];
        }
        const map = new Map<string, Environment[]>();
        for (const env of filtered) {
          const key = groupBy === "status" ? env.status : env.adapterType;
          const existing = map.get(key);
          if (existing) {
            existing.push(env);
          } else {
            map.set(key, [env]);
          }
        }
        const labels = groupBy === "status" ? STATUS_LABELS : TYPE_LABELS;
        return [...map.entries()]
          .sort(([a], [b]) => a.localeCompare(b))
          .map(([key, envs]) => ({ id: key, label: labels[key] || key, environments: envs }));
      }, [filtered, groupActive, groupBy]);

      // ── Header actions ──────────────────────────────────────────────
      const headerActions = useMemo<SectionHeaderAction[]>(
        () => [
          {
            key: "filter",
            icon: <Filter size={ICON_MD} />,
            tooltip: filterActive ? "Filter active" : "Filter",
            ariaLabel: "Filter environments",
            onClick: () => {
              setFilterOpen((p) => !p);
              setGroupOpen(false);
            },
            active: filterActive,
            ariaHasPopup: true,
            ariaExpanded: filterOpen,
            testId: "env-nav-filter",
          },
          {
            key: "group",
            icon: <Layers size={ICON_MD} />,
            tooltip: groupActive ? `Grouped by ${groupBy}` : "Group",
            ariaLabel: "Group environments",
            onClick: () => {
              setGroupOpen((p) => !p);
              setFilterOpen(false);
            },
            active: groupActive,
            ariaHasPopup: true,
            ariaExpanded: groupOpen,
            testId: "env-nav-group",
          },
          {
            key: "add",
            icon: <Plus size={ICON_MD} />,
            tooltip: "Add environment",
            ariaLabel: "Add environment",
            onClick: () => navigate(NEW_ENVIRONMENT_URL),
            testId: "env-nav-add-header",
          },
        ],
        [filterActive, filterOpen, groupActive, groupOpen, groupBy, navigate],
      );

      // ── Keyboard nav ────────────────────────────────────────────────
      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 flatEnvs = groups.flatMap((g) => g.environments);
          const currentIndex =
            focusedIndex >= 0 ? focusedIndex : flatEnvs.findIndex((env) => env.id === activeId);
          let nextIndex = currentIndex;

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

          const flatList = groups.flatMap((g) => g.environments);
          if (nextIndex < flatList.length) {
            navigate(environmentUrl(flatList[nextIndex].id));
          }
          buttons[nextIndex].focus();
        },
        [activeId, groups, navigate],
      );

      const flatEnvs = groups.flatMap((g) => g.environments);
      const activeInList = activeId && flatEnvs.some((e) => e.id === activeId);
      const focusableId =
        (activeInList ? activeId : undefined) ?? (flatEnvs.length > 0 ? flatEnvs[0].id : undefined);

      const handleClick = useCallback(
        (envId: string) => {
          navigate(environmentUrl(envId));
        },
        [navigate],
      );

      return (
        <div className={styles.container} data-testid="environment-nav">
          <div className={styles.headerWrapper}>
            <SectionHeader title="Environments" actions={headerActions} data-testid="env-nav-header" />
            {filterOpen && (
              <FilterDropdown
                groups={filterGroups}
                selected={filterValues}
                onToggle={toggleFilter}
                onClear={clearFilter}
                onClose={() => setFilterOpen(false)}
                data-testid="env-nav-filter-dropdown"
              />
            )}
            {groupOpen && (
              <FilterDropdown
                options={groupOptions}
                selected={new Set(groupBy ? [groupBy] : [])}
                onToggle={(key) => {
                  toggleGroup(key);
                  setGroupOpen(false);
                }}
                onClear={() => {
                  clearGroup();
                  setGroupOpen(false);
                }}
                onClose={() => setGroupOpen(false)}
                data-testid="env-nav-group-dropdown"
              />
            )}
          </div>

          <nav
            ref={tabListRef}
            role="tablist"
            aria-orientation="vertical"
            aria-label="Environments"
            onKeyDown={handleKeyDown}
            className={styles.nav}
          >
            {groups.map((group) => (
              <div key={group.id}>
                {group.label && (
                  <div className={styles.groupLabel} data-testid={`env-nav-group-${group.id}`}>
                    {group.label}
                  </div>
                )}
                {group.environments.map((env) => {
                  const isActive = env.id === activeId;
                  const isFocusable = env.id === focusableId;
                  const statusColor = STATUS_COLORS[env.status] || "var(--text-tertiary)";
                  const isConnected = env.status === "connected";
                  return (
                    <button
                      key={env.id}
                      role="tab"
                      type="button"
                      aria-selected={isActive}
                      tabIndex={isFocusable ? 0 : -1}
                      className={`${styles.tab} ${isActive ? styles.tabActive : ""}`}
                      onClick={() => handleClick(env.id)}
                      data-testid="env-nav-item"
                    >
                      <span
                        className={`${styles.statusDot} ${isConnected ? styles.pulse : ""}`}
                        style={{ color: statusColor }}
                        aria-hidden="true"
                      >
                        <Circle size={ICON_XS} fill="currentColor" />
                      </span>
                      <span className={styles.tabLabel} title={env.displayName || env.id}>
                        {env.displayName || env.id}
                      </span>
                    </button>
                  );
                })}
              </div>
            ))}
          </nav>

          <button
            type="button"
            className={styles.addButton}
            onClick={() => navigate(NEW_ENVIRONMENT_URL)}
            title="Add environment"
            data-testid="env-nav-add"
          >
            + Add Environment
          </button>

          {filtered.length === 0 && environments.length > 0 && (
            <div className={styles.empty}>No matching environments.</div>
          )}
          {environments.length === 0 && <div className={styles.empty}>No environments yet.</div>}
        </div>
      );
    }