File size: 4,099 Bytes
af1bee9
 
fb43301
af1bee9
 
 
fb43301
1de224d
af1bee9
 
0d811c3
 
 
af1bee9
 
 
0d811c3
 
 
af1bee9
0d811c3
af1bee9
 
0d811c3
af1bee9
 
 
 
 
 
0d811c3
 
 
af1bee9
 
 
 
 
 
 
 
 
ea59f21
1de224d
af1bee9
 
 
ea59f21
af1bee9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import './rail.css';
import { Tooltip } from '@mantine/core';
import { IconAlertTriangle, IconHandStop } from '@/components/icons';
import { NavLink as RouterLink, useLocation } from 'react-router-dom';
import { useBrand, useHealth, useKill, useRuns } from '../../api/hooks';
import { useAuth } from '../../auth/AuthContext';
import { useWaitsOnMe } from '../../domain/waiting';
import { AccountMenu, AGENT_STUDIO, AI_MONITORING, INTEGRATIONS, LIBRARY, MAIN, SETTINGS, SETUP, type NavItem } from './AppSidebar';
import { envKind, useChosenEnv } from './EnvSwitcher';

/** The name under each icon: short, and always part of the page's full name (so what is read out matches what is shown). */
const SHORT: Record<string, string> = { '/runs': 'Runs', '/pods': 'Pods', '/agent-studio': 'Studio' };

function RailLink({ item, badge }: { item: NavItem; badge?: number }) {
  const loc = useLocation();
  const active = item.match ? item.match(loc.pathname) : loc.pathname === item.to || loc.pathname.startsWith(`${item.to}/`);
  // the shown short name is always part of the name read out (Test environments: "Pods")
  const full = SHORT[item.to] && !item.label.toLowerCase().includes(SHORT[item.to].toLowerCase()) ? `${item.label} (${SHORT[item.to]})` : item.label;
  const name = badge ? `${full}, ${badge} waiting for you` : full;
  return (
    <Tooltip label={item.label} position="right" withArrow openDelay={400}>

      <RouterLink to={item.to} className="pw-rail-item" data-active={active} aria-current={active ? 'page' : undefined} aria-label={name}>

        {item.icon}

        <span className="pw-rail-label" aria-hidden>{SHORT[item.to] ?? item.label}</span>

        {!!badge && <span className="pw-rail-badge" aria-hidden>{badge > 9 ? '9+' : badge}</span>}

      </RouterLink>

    </Tooltip>
  );
}

/** The app's navigation on a desktop: the toolbar (an icon and a short name per page, the full name on hover and to

 * screen readers), the runs waiting for you as a badge on Test runs, the emergency stop and a silent practice app as

 * warnings, the account at the bottom. The environment switcher is the project switcher in the top bar. */
export function IconRail() {
  const { can } = useAuth();
  const brand = useBrand();
  const runs = useRuns(40, 0);
  const mine = useWaitsOnMe();
  const kill = useKill();
  const health = useHealth();
  const env = useChosenEnv().pod;
  const waiting = (runs.data?.items ?? []).filter(mine).length;
  const name = brand.data?.product_name ?? 'Astra';
  const items: NavItem[] = [...MAIN, ...LIBRARY, ...SETUP, ...(can('admin') ? [AGENT_STUDIO, AI_MONITORING, INTEGRATIONS, SETTINGS] : [])];
  return (
    <div className="pw-rail">

      <RouterLink to="/" className="pw-rail-brand" aria-label={`${name} home`} data-testid="brand">

        <img className="astra-logo" src="/astra-logo.png" alt="" height={26} />

      </RouterLink>

      <nav className="pw-rail-nav" aria-label="Pages">

        {items.map((it, i) => (

          <span key={it.to} className="pw-rail-slot" data-gap={i === MAIN.length || i === MAIN.length + LIBRARY.length || undefined}>

            <RailLink item={it} badge={it.to === '/runs' ? waiting : undefined} />

          </span>

        ))}

      </nav>

      <div className="pw-rail-foot">

        {kill.data?.engaged && (

          <Tooltip label="Emergency stop is on" position="right" withArrow>

            <span className="pw-rail-warn" data-tone="red" role="status" data-testid="kill-indicator" aria-label="Emergency stop is on"><IconHandStop size={18} aria-hidden /></span>

          </Tooltip>

        )}

        {health.data && !health.data.mock_up && envKind(env?.kind) === 'mock' && !!env && (

          <Tooltip label="The practice app is not answering" position="right" withArrow>

            <span className="pw-rail-warn" data-tone="orange" role="status" aria-label="practice app down"><IconAlertTriangle size={18} aria-hidden /></span>

          </Tooltip>

        )}

        <AccountMenu compact />

      </div>

    </div>
  );
}