File size: 4,884 Bytes
fb62a97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import './topnav.css';
import { Menu, useComputedColorScheme, useMantineColorScheme } from '@mantine/core';
import { IconChevronDown, IconHandStop, IconMenu2, IconMoon, IconSearch, IconSun } from '@/components/icons';
import { Link, NavLink as RouterLink, useLocation, useNavigate } from 'react-router-dom';
import { useBrand, useKill, useRuns } from '../../api/hooks';
import { useAuth } from '../../auth/AuthContext';
import { NotificationBell } from '../../notify/NotificationBell';
import { useWaitsOnMe } from '../../domain/waiting';
import { AccountMenu, AGENT_STUDIO, AI_MONITORING, INTEGRATIONS, LIBRARY, MAIN, SETTINGS, SETUP, type NavItem } from './AppSidebar';
import { EnvSwitcher } from './EnvSwitcher';
import { useShell } from './prefs';
import { isMac } from './TopBar';

const isActive = (item: NavItem, path: string) => (item.match ? item.match(path) : path === item.to || path.startsWith(`${item.to}/`));

/** The app's navigation: brand at left, main pages across the center, and environment and account tools at right.
 * Setup and admin pages are grouped under More; phones keep the menu drawer. */
export function TopNav() {
  const { can } = useAuth();
  const loc = useLocation();
  const nav = useNavigate();
  const brand = useBrand();
  const runs = useRuns(40, 0);
  const mine = useWaitsOnMe();
  const kill = useKill();
  const { size, openNav, openPalette } = useShell();
  const { setColorScheme } = useMantineColorScheme();
  const scheme = useComputedColorScheme('dark');
  const dark = scheme === 'dark';
  const waiting = (runs.data?.items ?? []).filter(mine).length;
  const name = brand.data?.product_name ?? 'Astra';
  const phone = size === 'phone';
  const main = [...MAIN.filter((i) => i.to !== '/assistant'), ...LIBRARY, ...MAIN.filter((i) => i.to === '/assistant')];
  const more = [...SETUP, ...(can('admin') ? [AGENT_STUDIO, AI_MONITORING, INTEGRATIONS, SETTINGS] : [])];
  const moreActive = more.find((i) => isActive(i, loc.pathname));
  return (
    <header className="pw-nav" aria-label="Main navigation" data-size={size}>
      {phone && <button type="button" className="pw-nav-menu" aria-label="Open the menu" onClick={openNav}><IconMenu2 size={19} aria-hidden /></button>}
      <Link to="/" className="pw-nav-brand" aria-label={`${name} home`} data-testid="brand">
        <img className="astra-logo" src="/astra-logo.png" alt="" height={28} />
        <span className="pw-nav-name"><b className="astra-wordmark">{name}</b></span>
      </Link>

      <nav className="pw-nav-pill" aria-label="Pages">
        {main.map((it) => {
          const active = isActive(it, loc.pathname);
          const badge = it.to === '/runs' ? waiting : 0;
          return (
            <RouterLink key={it.to} to={it.to} className="pw-nav-item" data-active={active || undefined} aria-current={active ? 'page' : undefined}
              aria-label={badge ? `${it.label}, ${badge} waiting for you` : it.label}>
              {it.icon}<span>{it.to === '/runs' ? 'Runs' : it.label}</span>
              {badge > 0 && <span className="pw-nav-badge" aria-hidden>{badge > 9 ? '9+' : badge}</span>}
            </RouterLink>
          );
        })}
        {more.length > 0 && (
          <Menu position="bottom-end" width={230} withinPortal>
            <Menu.Target>
              <button type="button" className="pw-nav-item" data-active={!!moreActive || undefined} aria-label={moreActive ? `More: ${moreActive.label}` : 'More pages'}>
                {moreActive?.icon}<span>{moreActive ? moreActive.label : 'More'}</span><IconChevronDown size={13} aria-hidden />
              </button>
            </Menu.Target>
            <Menu.Dropdown>
              {more.map((it) => <Menu.Item key={it.to} leftSection={it.icon} onClick={() => nav(it.to)}>{it.label}</Menu.Item>)}
            </Menu.Dropdown>
          </Menu>
        )}
      </nav>

      <div className="pw-nav-right">
        {kill.data?.engaged && <span className="pw-nav-warn" role="status" data-testid="kill-indicator" aria-label="Emergency stop is on"><IconHandStop size={16} aria-hidden />Stopped</span>}
        <EnvSwitcher iconOnly className="pw-nav-env" />
        <button type="button" className="pw-nav-icon" data-tool="search" onClick={openPalette} aria-label={`Search or ask ${isMac() ? '⌘K' : 'Ctrl K'}`} title="Search, go to a page or ask a question">
          <IconSearch size={17} aria-hidden />
        </button>
        <button type="button" className="pw-nav-icon" data-tool="theme" onClick={() => setColorScheme(dark ? 'light' : 'dark')}
          aria-label={dark ? 'Switch to light mode' : 'Switch to dark mode'} title={dark ? 'Light mode' : 'Dark mode'}>
          {dark ? <IconSun size={17} aria-hidden /> : <IconMoon size={17} aria-hidden />}
        </button>
        <NotificationBell />
        <AccountMenu compact />
      </div>
    </header>
  );
}