|
5 | 5 | */ |
6 | 6 |
|
7 | 7 | import React, { Fragment } from 'react'; |
| 8 | +import { Option, none, some, fold } from 'fp-ts/lib/Option'; |
| 9 | +import { pipe } from 'fp-ts/lib/pipeable'; |
8 | 10 | import { FormattedMessage } from '@kbn/i18n/react'; |
9 | 11 |
|
10 | 12 | import { EuiLink, EuiLoadingSpinner } from '@elastic/eui'; |
@@ -33,41 +35,35 @@ export const HealthCheck: React.FunctionComponent<Props> = ({ |
33 | 35 | waitForCheck = true, |
34 | 36 | }) => { |
35 | 37 | const { setLoadingHealthCheck } = useHealthContext(); |
36 | | - const [alertingHealth, setAlertingHealth] = React.useState<AlertingFrameworkHealth | null>(null); |
| 38 | + const [alertingHealth, setAlertingHealth] = React.useState<Option<AlertingFrameworkHealth>>(none); |
37 | 39 |
|
38 | | - /** |
39 | | - * if waitForCheck && no response from server, loading |
40 | | - if waitForcheck && response from server, go through logic |
41 | | - if !waitForCheck && no response from server, return child |
42 | | - if !waitForCheck && response from server, go through logic |
43 | | - */ |
44 | 40 | React.useEffect(() => { |
45 | 41 | (async function () { |
46 | 42 | setLoadingHealthCheck(true); |
47 | | - setAlertingHealth(await health({ http })); |
| 43 | + setAlertingHealth(some(await health({ http }))); |
48 | 44 | setLoadingHealthCheck(false); |
49 | 45 | })(); |
50 | 46 | }, [http, setLoadingHealthCheck]); |
51 | 47 |
|
52 | 48 | const className = inFlyout ? 'alertingFlyoutHealthCheck' : 'alertingHealthCheck'; |
53 | 49 |
|
54 | | - const healthLoadedButNotSecure = |
55 | | - alertingHealth && |
56 | | - (!alertingHealth.isSufficientlySecure || !alertingHealth.hasPermanentEncryptionKey); |
57 | | - |
58 | | - if (healthLoadedButNotSecure) { |
59 | | - return !alertingHealth?.isSufficientlySecure && !alertingHealth?.hasPermanentEncryptionKey ? ( |
60 | | - <TlsAndEncryptionError docLinks={docLinks} className={className} /> |
61 | | - ) : !alertingHealth?.hasPermanentEncryptionKey ? ( |
62 | | - <EncryptionError docLinks={docLinks} className={className} /> |
63 | | - ) : ( |
64 | | - <TlsError docLinks={docLinks} className={className} /> |
65 | | - ); |
66 | | - } else if (waitForCheck && !alertingHealth) { |
67 | | - return <EuiLoadingSpinner size="m" />; |
68 | | - } |
69 | | - |
70 | | - return <Fragment>{children}</Fragment>; |
| 50 | + return pipe( |
| 51 | + alertingHealth, |
| 52 | + fold( |
| 53 | + () => (waitForCheck ? <EuiLoadingSpinner size="m" /> : <Fragment>{children}</Fragment>), |
| 54 | + (healthCheck) => { |
| 55 | + return healthCheck?.isSufficientlySecure && healthCheck?.hasPermanentEncryptionKey ? ( |
| 56 | + <Fragment>{children}</Fragment> |
| 57 | + ) : !healthCheck.isSufficientlySecure && !healthCheck.hasPermanentEncryptionKey ? ( |
| 58 | + <TlsAndEncryptionError docLinks={docLinks} className={className} /> |
| 59 | + ) : !healthCheck.hasPermanentEncryptionKey ? ( |
| 60 | + <EncryptionError docLinks={docLinks} className={className} /> |
| 61 | + ) : ( |
| 62 | + <TlsError docLinks={docLinks} className={className} /> |
| 63 | + ); |
| 64 | + } |
| 65 | + ) |
| 66 | + ); |
71 | 67 | }; |
72 | 68 |
|
73 | 69 | type PromptErrorProps = Pick<Props, 'docLinks'> & { |
|
0 commit comments