-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathDatasourceTestConnectionButton.tsx
More file actions
60 lines (57 loc) · 1.83 KB
/
Copy pathDatasourceTestConnectionButton.tsx
File metadata and controls
60 lines (57 loc) · 1.83 KB
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
import { ReactElement, useCallback } from 'react';
import { Button, ButtonProps } from '@mui/material';
import { useUnsavedDatasourceStore } from '@perses-dev/plugin-system';
import { useSnackbar } from '@perses-dev/components';
import { DatasourceSpec } from '@perses-dev/core';
type DatasourceTestConnectionButtonProps = (
| {
connectionType: 'proxy';
spec: DatasourceSpec;
directUrl?: undefined;
healthCheckPath: string;
}
| {
connectionType: 'direct';
spec?: undefined;
directUrl: string;
healthCheckPath: string;
}
) &
Omit<ButtonProps, 'onClick'>;
export const DatasourceTestConnectionButton = ({
healthCheckPath,
connectionType,
spec,
directUrl,
...buttonProps
}: DatasourceTestConnectionButtonProps): ReactElement => {
const datasourceStore = useUnsavedDatasourceStore();
const { successSnackbar, exceptionSnackbar } = useSnackbar();
const testConnection = useCallback(
async function isHealthy(): Promise<boolean> {
switch (connectionType) {
case 'direct':
return datasourceStore.testDirectConnection(directUrl, healthCheckPath);
case 'proxy':
return datasourceStore.testProxyConnection(spec, healthCheckPath);
}
},
[connectionType, datasourceStore, directUrl, healthCheckPath, spec]
);
const handleTestConnection = useCallback(
async function handleTestConnection(): Promise<void> {
const isHealthy = await testConnection();
if (isHealthy) {
successSnackbar('Datasource is healthy');
} else {
exceptionSnackbar(new Error('Datasource is not healthy'));
}
},
[exceptionSnackbar, testConnection, successSnackbar]
);
return (
<Button onClick={handleTestConnection} color="info" variant="outlined" {...buttonProps}>
Test Connection
</Button>
);
};