forked from opensearch-project/dashboards-flow-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport_modal.tsx
130 lines (123 loc) · 3.54 KB
/
export_modal.tsx
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
import React, { useEffect, useState } from 'react';
import yaml from 'js-yaml';
import {
EuiCodeBlock,
EuiFlexGroup,
EuiFlexItem,
EuiCompressedRadioGroup,
EuiText,
EuiLink,
EuiModal,
EuiModalHeader,
EuiModalHeaderTitle,
EuiModalBody,
EuiModalFooter,
EuiSmallButtonEmpty,
} from '@elastic/eui';
import {
CREATE_WORKFLOW_LINK,
Workflow,
customStringify,
getCharacterLimitedString,
} from '../../../../common';
import { reduceToTemplate } from '../../../utils';
interface ExportModalProps {
workflow?: Workflow;
setIsExportModalOpen(isOpen: boolean): void;
}
enum EXPORT_OPTION {
JSON = 'json',
YAML = 'yaml',
}
const exportOptions = [
{
id: EXPORT_OPTION.JSON,
label: 'JSON',
},
{
id: EXPORT_OPTION.YAML,
label: 'YAML',
},
];
/**
* Modal containing all of the export options
*/
export function ExportModal(props: ExportModalProps) {
// format type state
const [selectedOption, setSelectedOption] = useState<EXPORT_OPTION>(
EXPORT_OPTION.JSON
);
// formatted string state
const [formattedConfig, setFormattedConfig] = useState<string>('');
useEffect(() => {
if (props.workflow) {
const workflowTemplate = reduceToTemplate(props.workflow);
if (selectedOption === EXPORT_OPTION.JSON) {
setFormattedConfig(customStringify(workflowTemplate));
} else if (selectedOption === EXPORT_OPTION.YAML) {
setFormattedConfig(yaml.dump(workflowTemplate));
}
}
}, [props.workflow, selectedOption]);
return (
<EuiModal onClose={() => props.setIsExportModalOpen(false)}>
<EuiModalHeader>
<EuiModalHeaderTitle>
<p>{`Export ${getCharacterLimitedString(
props.workflow?.name || '',
25
)}`}</p>
</EuiModalHeaderTitle>
</EuiModalHeader>
<EuiModalBody>
<EuiFlexGroup direction="column">
<EuiFlexItem grow={false}>
<EuiText>
{`To build out identical resources in other environments, create and provision a workflow using the below template.`}{' '}
<EuiLink href={CREATE_WORKFLOW_LINK} target="_blank">
Learn more
</EuiLink>
</EuiText>
<EuiText
size="s"
color="subdued"
>{`Note: certain resource IDs in the template, such as model IDs, may be cluster-specific and not work out-of-the-box
in other environments. Ensure these values are updated before attempting to provision in other environments.`}</EuiText>
</EuiFlexItem>
<EuiFlexItem grow={false}>
<EuiCompressedRadioGroup
options={exportOptions}
idSelected={selectedOption}
onChange={(option) => {
setSelectedOption(option as EXPORT_OPTION);
}}
/>
</EuiFlexItem>
{props.workflow !== undefined && (
<EuiFlexItem grow={false}>
<EuiCodeBlock
language={selectedOption}
fontSize="m"
isCopyable={true}
>
{formattedConfig}
</EuiCodeBlock>
</EuiFlexItem>
)}
</EuiFlexGroup>
</EuiModalBody>
<EuiModalFooter>
<EuiSmallButtonEmpty
onClick={() => props.setIsExportModalOpen(false)}
data-testid="exportCloseButton"
>
Close
</EuiSmallButtonEmpty>
</EuiModalFooter>
</EuiModal>
);
}