EnergyVenom's picture
Deploy 7831dd1: layout fixes, sidebar folds to icons
f56442a verified
Raw History Blame Contribute Delete
3.58 kB
import './rail.css';
import { Tooltip } from '@mantine/core';
import { IconAlertTriangle, IconHandStop } from '@tabler/icons-react';
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 '../../waiting';
import { AccountMenu, INTEGRATIONS, LIBRARY, MAIN, SETTINGS, SETUP, type NavItem } from './AppSidebar';
import { envKind, useChosenEnv } from './EnvSwitcher';
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}/`);
const name = badge ? `${item.label}, ${badge} waiting for you` : item.label;
return (
<Tooltip label={item.label} position="right" withArrow openDelay={150}>
<RouterLink to={item.to} className="pw-rail-item" data-active={active} aria-current={active ? 'page' : undefined} aria-label={name}>
{item.icon}
{!!badge && <span className="pw-rail-badge" aria-hidden>{badge > 9 ? '9+' : badge}</span>}
</RouterLink>
</Tooltip>
);
}
/** The app's navigation on a desktop: a narrow rail of icons (each named 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 lives 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 ?? 'Patchwise';
const items: NavItem[] = [...MAIN, ...LIBRARY, ...SETUP, ...(can('admin') ? [INTEGRATIONS, SETTINGS] : [])];
return (
<div className="pw-rail">
<RouterLink to="/" className="pw-rail-brand" aria-label={`${name} home`} data-testid="brand">
<img className="trio-logo" data-for="light" src="/favicon-graphite.svg" alt="" width={32} height={32} />
<img className="trio-logo" data-for="dark" src="/favicon-paper.svg" alt="" width={32} height={32} />
</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>
);
}