import { Menu, UnstyledButton } from '@mantine/core'; import { IconCheck, IconCloud, IconSelector, IconServer, IconShieldLock } from '@tabler/icons-react'; import { useNavigate } from 'react-router-dom'; import { usePods, type Pod } from '../../api/hooks'; import { MOCK_TEXT } from '../common'; import { useUiPrefs } from './prefs'; export type EnvKind = 'mock' | 'fusion'; export const envKind = (kind?: string | null): EnvKind => (kind === 'fusion' ? 'fusion' : 'mock'); /** "Practice environment" (amber) or "Oracle Fusion pod" (graphite outline, cloud icon), with the pod name. */ export function EnvBadge({ kind, name, size = 'xs' }: { kind: EnvKind; name?: string | null; size?: 'xs' | 'sm' }) { const fusion = kind === 'fusion'; return ( {fusion ? : } {fusion ? `Oracle Fusion pod${name ? `: ${name}` : ''}` : <>Practice environment: {MOCK_TEXT}} ); } const ago = (iso?: string | null) => { if (!iso) return 'not checked'; const s = (Date.now() - new Date(iso).getTime()) / 1000; if (!Number.isFinite(s)) return 'not checked'; if (s < 90) return 'checked just now'; if (s < 3600) return `checked ${Math.round(s / 60)} min ago`; if (s < 86400 * 2) return `checked ${Math.round(s / 3600)} h ago`; return `checked ${Math.round(s / 86400)} days ago`; }; /** The environment chosen in the switcher (else the server default), and the runnable list it is chosen from. */ export function useChosenEnv(): { pod: Pod | undefined; pods: Pod[] } { const { prefs } = useUiPrefs(); const all = usePods().data ?? []; const runnable = all.filter((p) => p.runnable); const pod = runnable.find((p) => p.id === prefs.env) ?? runnable.find((p) => p.is_default) ?? runnable[0] ?? all.find((p) => p.is_default) ?? all[0]; return { pod, pods: runnable }; } /** The honesty cue at the top of the sidebar: which environment new test runs go to, in words ("Mock app, not Oracle Fusion" * on a practice pod, "Oracle Fusion pod" otherwise). Choosing one only preselects it for you; the server default pod stays an * admin setting on Test environments. */ export function EnvSwitcher({ compact = false, className }: { compact?: boolean; className?: string }) { const { set } = useUiPrefs(); const nav = useNavigate(); const { pod, pods } = useChosenEnv(); const kind = envKind(pod?.kind); const fusion = kind === 'fusion'; const name = pod?.name ?? (fusion ? 'Oracle Fusion pod' : 'Local mock pod'); const tip = fusion ? `Oracle Fusion pod: ${name}` : `Practice environment: ${name} (${MOCK_TEXT})`; const label = ( {name} {fusion ? 'Oracle Fusion pod' : MOCK_TEXT} ); const groups: [string, Pod[]][] = [['Practice', pods.filter((p) => envKind(p.kind) === 'mock')], ['Oracle Fusion', pods.filter((p) => envKind(p.kind) === 'fusion')]]; return ( {fusion ? : } {label} {`, the test environment for new test runs (${fusion ? 'Oracle Fusion pod' : 'practice environment'}). Change it`} {!compact && } {groups.filter(([, list]) => list.length).map(([title, list]) => (
{title} {list.map((p) => ( set({ env: p.id })} leftSection={envKind(p.kind) === 'fusion' ? : } rightSection={p.id === pod?.id ? : undefined}>
{p.name}{p.is_default ? ' (default)' : ''}
{p.last_check_ok === false ? `not reachable, ${ago(p.last_check_at)}` : ago(p.last_check_at)}
))}
))} {!pods.length && No test environment can run tests yet} } onClick={() => nav('/pods')}>Manage test environments
); }