Download web/src/components/shell/EnvSwitcher.tsx from EnergyVenom/patch-testing-coworker: direct link, hf CLI and curl.
- Browser
- Download file 5.59 kB
-
https://huggingface.co/spaces/EnergyVenom/patch-testing-coworker/resolve/main/web/src/components/shell/EnvSwitcher.tsx
- Command line
-
hf download hf://spaces/EnergyVenom/patch-testing-coworker/web/src/components/shell/EnvSwitcher.tsx
-
curl -L -o EnvSwitcher.tsx https://huggingface.co/spaces/EnergyVenom/patch-testing-coworker/resolve/main/web/src/components/shell/EnvSwitcher.tsx
5.59 kB
| 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 ( | |
| <span className="fpt-pill fpt-envbadge" data-tone={fusion ? 'fusion' : 'amber'} data-size={size} | |
| title={fusion ? `Oracle Fusion pod${name ? `: ${name}` : ''}` : `Practice environment${name ? `: ${name}` : ''} (${MOCK_TEXT})`}> | |
| {fusion ? <IconCloud size={13} aria-hidden /> : <IconShieldLock size={13} aria-hidden />} | |
| {fusion ? `Oracle Fusion pod${name ? `: ${name}` : ''}` : <><span className="fpt-env-long">Practice environment: </span>{MOCK_TEXT}</>} | |
| </span> | |
| ); | |
| } | |
| 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 = ( | |
| <span role="note" aria-label="target system" data-testid="mock-banner" className={compact ? 'fpt-sr-only' : 'fpt-env-label'}> | |
| <b>{name}</b> | |
| <span className="fpt-env-sub">{fusion ? 'Oracle Fusion pod' : MOCK_TEXT}</span> | |
| </span> | |
| ); | |
| const groups: [string, Pod[]][] = [['Practice', pods.filter((p) => envKind(p.kind) === 'mock')], ['Oracle Fusion', pods.filter((p) => envKind(p.kind) === 'fusion')]]; | |
| return ( | |
| <Menu position={compact ? 'right-end' : 'bottom-start'} width={280} withinPortal> | |
| <Menu.Target> | |
| <UnstyledButton className={className ? `fpt-env ${className}` : 'fpt-env'} data-kind={kind} data-compact={compact || undefined} title={compact ? tip : undefined}> | |
| <span className="fpt-env-tile" aria-hidden>{fusion ? <IconCloud size={16} stroke={1.8} /> : <IconShieldLock size={16} stroke={1.8} />}</span> | |
| {label} | |
| <span className="fpt-sr-only">{`, the test environment for new test runs (${fusion ? 'Oracle Fusion pod' : 'practice environment'}). Change it`}</span> | |
| {!compact && <IconSelector size={14} aria-hidden className="fpt-env-chev" />} | |
| </UnstyledButton> | |
| </Menu.Target> | |
| <Menu.Dropdown> | |
| {groups.filter(([, list]) => list.length).map(([title, list]) => ( | |
| <div key={title}> | |
| <Menu.Label>{title}</Menu.Label> | |
| {list.map((p) => ( | |
| <Menu.Item key={p.id} onClick={() => set({ env: p.id })} | |
| leftSection={envKind(p.kind) === 'fusion' ? <IconCloud size={15} /> : <IconShieldLock size={15} />} | |
| rightSection={p.id === pod?.id ? <IconCheck size={14} aria-label="chosen" /> : undefined}> | |
| <div style={{ fontSize: 13.5 }}>{p.name}{p.is_default ? ' (default)' : ''}</div> | |
| <div style={{ fontSize: 12, opacity: 0.8, display: 'flex', alignItems: 'center', gap: 5 }}> | |
| <span className="fpt-sdot" data-state={p.last_check_ok === false ? 'failed' : p.last_check_at ? 'done' : 'idle'} aria-hidden style={{ width: 7, height: 7, flexBasis: 7 }} /> | |
| {p.last_check_ok === false ? `not reachable, ${ago(p.last_check_at)}` : ago(p.last_check_at)} | |
| </div> | |
| </Menu.Item> | |
| ))} | |
| </div> | |
| ))} | |
| {!pods.length && <Menu.Label>No test environment can run tests yet</Menu.Label>} | |
| <Menu.Divider /> | |
| <Menu.Item leftSection={<IconServer size={15} />} onClick={() => nav('/pods')}>Manage test environments</Menu.Item> | |
| </Menu.Dropdown> | |
| </Menu> | |
| ); | |
| } | |