File size: 5,689 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
87
88
89
90
91
92
93
94
95
import { Menu, UnstyledButton } from '@mantine/core';
import { IconCheck, IconCloud, IconSelector, IconServer, IconShieldLock } from '@/components/icons';
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, iconOnly = false, className }: { compact?: boolean; iconOnly?: 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 || iconOnly ? '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 || iconOnly ? 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 className="fpt-env-dropdown">
        {groups.filter(([, list]) => list.length).map(([title, list]) => (
          <div key={title}>
            <Menu.Label className="fpt-env-group">{title}</Menu.Label>
            {list.map((p) => (
              <Menu.Item key={p.id} className="fpt-env-option" 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 className="fpt-env-divider" />
        <Menu.Item className="fpt-env-manage" leftSection={<IconServer size={15} />} onClick={() => nav('/pods')}>Manage test environments</Menu.Item>
      </Menu.Dropdown>
    </Menu>
  );
}