import '../../shell.css';
import { Menu, Tooltip, useMantineColorScheme } from '@mantine/core';
import {
IconAlertTriangle, IconBug, IconCheck, IconDeviceDesktop, IconFileReport, IconHandStop, IconHome, IconListCheck, IconLogout, IconMessageCircle, IconMoon,
IconPlayerPlay, IconPlug, IconServer, IconSettings, IconSun,
} from '@tabler/icons-react';
import type { ReactNode } from 'react';
import { NavLink as RouterLink, useLocation } from 'react-router-dom';
import type { Schemas } from '../../api/client';
import { useBrand, useHealth, useKill, useRuns } from '../../api/hooks';
import { useAuth } from '../../auth/AuthContext';
import { desktop } from '../../desktop';
import { areaTone } from '../../agents';
import { runTitle } from '../../runName';
import { EnvSwitcher, envKind, useChosenEnv } from './EnvSwitcher';
import { isWaiting, useWaitsOnMe } from '../../waiting';
type RunSummary = Schemas['RunSummary'];
export interface NavItem { to: string; label: string; icon: ReactNode; match?: (p: string) => boolean; admin?: boolean }
const ICON = { size: 16, stroke: 1.7 };
export const MAIN: NavItem[] = [
{ to: '/', label: 'Home', icon: , match: (p) => p === '/' },
{ to: '/runs', label: 'Test runs', icon: , match: (p) => p === '/runs' || p.startsWith('/runs/') || p === '/schedules' },
{ to: '/assistant', label: 'Ask', icon: },
];
export const LIBRARY: NavItem[] = [
{ to: '/scenarios', label: 'Scenarios', icon: },
{ to: '/tickets', label: 'Defects', icon: },
{ to: '/reports', label: 'Reports', icon: },
];
export const SETUP: NavItem[] = [
{ to: '/pods', label: 'Test environments', icon: },
];
export const INTEGRATIONS: NavItem = { to: '/integrations', label: 'Integrations', icon: };
export const SETTINGS: NavItem = { to: '/admin', label: 'Settings', icon: ,
match: (p) => ['/admin', '/agents', '/heal', '/scripts', '/learner', '/audit'].some((x) => p === x || p.startsWith(`${x}/`)) };
/** Where "New test run" links go: they open the Start sheet (`/runs/new` redirects there). */
export const NEW_RUN_PATH = '/runs/new';
export const initials = (name?: string) => (name ?? 'Patchwise').split(/\s+/).slice(0, 2).map((w) => w[0]?.toUpperCase() ?? '').join('') || 'TC';
function NavLink({ item, badge, onNavigate }: { item: NavItem; badge?: number; onNavigate?: () => void }) {
const loc = useLocation();
const active = item.match ? item.match(loc.pathname) : loc.pathname === item.to || loc.pathname.startsWith(`${item.to}/`);
return (
{item.icon}{item.label}
{!!badge && {badge}, waiting for you}
);
}
type Group = { key: string; title: string; runs: RunSummary[] };
/** The latest test runs, newest first: a square in the area's colour and the run's readable name. */
function RecentRuns({ items, onNavigate }: { items: RunSummary[]; onNavigate?: () => void }) {
const loc = useLocation();
const latest = [...items].sort((a, b) => (a.created_at < b.created_at ? 1 : -1)).slice(0, 4);
if (!latest.length) return null;
return (
Recent test runs
{latest.map((r) => {
const to = `/runs/${r.run_id}`;
const active = loc.pathname === to || loc.pathname.startsWith(`${to}/`);
return (
-
{runTitle(r)}
);
})}
);
}
/** Needs you (all of them) first, then Running, then Recent to fill up to `max` rows. */
export function groupRuns(items: RunSummary[], max = 12, mine: (r: RunSummary) => boolean = isWaiting): Group[] {
const needs = items.filter(mine);
const running = items.filter((r) => !needs.includes(r) && ['queued', 'running'].includes(r.status));
const recent = items.filter((r) => !needs.includes(r) && !running.includes(r))
.sort((a, b) => (a.created_at < b.created_at ? 1 : -1));
let room = Math.max(0, max - needs.length);
const run2 = running.slice(0, room); room -= run2.length;
const rec2 = recent.slice(0, room);
return [{ key: 'needs', title: 'Waiting for you', runs: needs }, { key: 'running', title: 'Running', runs: run2 }, { key: 'recent', title: 'Recent', runs: rec2 }]
.filter((g) => g.runs.length);
}
const weekCount = (items: RunSummary[]) => items.filter((r) => Date.now() - new Date(r.created_at).getTime() < 7 * 86_400_000).length;
/** The sidebar: brand, the environment switcher (the honesty cue), one level of navigation with two small group labels,
* the emergency-stop flag, and only the account row at the bottom. */
export function AppSidebar({ onNavigate }: { onNavigate?: () => void }) {
const { can } = useAuth();
const health = useHealth();
const env = useChosenEnv().pod;
const kill = useKill();
const runs = useRuns(40, 0);
const brand = useBrand();
const mine = useWaitsOnMe();
const waiting = (runs.data?.items ?? []).filter(mine).length;
const name = brand.data?.product_name ?? 'Patchwise';
return (
{name}{brand.data?.org_name ? {brand.data.org_name} : null}
{(runs.data?.items.length ?? 0) > 0 && (
{weekCount(runs.data?.items ?? [])} test {weekCount(runs.data?.items ?? []) === 1 ? 'run' : 'runs'}
started in the last 7 days{waiting ? `, ${waiting} waiting for you` : ''}
)}
{kill.data?.engaged && (
Emergency stop is on
)}
{health.data && !health.data.mock_up && envKind(env?.kind) === 'mock' && !!env && (
Practice app not answering
)}
);
}
/** The account row's menu: your account, light or dark, sign out. `compact`: the avatar alone (the icon rail). */
export function AccountMenu({ compact = false }: { compact?: boolean }) {
const { user, logout } = useAuth();
const { colorScheme, setColorScheme } = useMantineColorScheme();
const whoami = `${user?.username} (${user?.role})`;
const scheme = [
{ v: 'auto', label: 'Match the system', aria: 'Match the system theme', icon: },
{ v: 'light', label: 'Light', aria: 'Switch to light theme', icon: },
{ v: 'dark', label: 'Dark', aria: 'Switch to dark theme', icon: },
] as const;
return (
);
}