/
index.tsx
247 lines (231 loc) · 8.08 KB
/
index.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import MyIcon from '@/components/Icon';
import { editModeMap } from '@/constants/editApp';
import { useLoading } from '@/hooks/useLoading';
import { useToast } from '@/hooks/useToast';
import { GET } from '@/services/request';
import { YamlItemType } from '@/types';
import { TemplateType, YamlSourceType } from '@/types/app';
import { serviceSideProps } from '@/utils/i18n';
import {
developGenerateYamlList,
getTemplateDataSource,
parseTemplateString
} from '@/utils/json-yaml';
import { getTemplateDefaultValues } from '@/utils/template';
import { Box, Flex, Text } from '@chakra-ui/react';
import { StreamLanguage } from '@codemirror/language';
import { yaml } from '@codemirror/legacy-modes/mode/yaml';
import { useQuery } from '@tanstack/react-query';
import CodeMirror from '@uiw/react-codemirror';
import JsYaml from 'js-yaml';
import { debounce, has, isObject, mapValues } from 'lodash';
import { useTranslation } from 'next-i18next';
import { useMemo, useState } from 'react';
import { useForm } from 'react-hook-form';
import ErrorModal from '../deploy/components/ErrorModal';
import BreadCrumbHeader from './components/BreadCrumbHeader';
import Form from './components/Form';
import YamlList from './components/YamlList';
import { postDeployApp } from '@/api/app';
export default function Develop() {
const { t } = useTranslation();
const [yamlValue, setYamlValue] = useState('');
const { toast } = useToast();
const [yamlSource, setYamlSource] = useState<YamlSourceType>();
const [yamlList, setYamlList] = useState<YamlItemType[]>([]);
const { Loading, setIsLoading } = useLoading();
const [errorMessage, setErrorMessage] = useState('');
const { title, applyBtnText, applyMessage, applySuccess, applyError } = editModeMap(false);
const detailName = useMemo(
() => yamlSource?.source?.defaults?.app_name?.value || '',
[yamlSource?.source?.defaults?.app_name?.value]
);
const { data: platformEnvs } = useQuery(['getPlatformEnvs'], () => GET('/api/platform/getEnv'));
const onYamlChange = (value: string) => {
setYamlValue(value);
parseTemplate(value);
};
const getYamlSource = (str: string): YamlSourceType => {
const yamlData = JsYaml.loadAll(str);
const templateYaml: TemplateType = yamlData.find(
(item: any) => item.kind === 'Template'
) as TemplateType;
const yamlList = yamlData.filter((item: any) => item.kind !== 'Template');
const dataSource = getTemplateDataSource(templateYaml);
const result: YamlSourceType = {
source: {
...dataSource,
...platformEnvs
},
yamlList: yamlList
};
return result;
};
const generateCorrectYaml = (yamlSource: YamlSourceType, inputsForm = {}) => {
const yamlString = yamlSource?.yamlList?.map((item) => JsYaml.dump(item)).join('---\n');
const output = mapValues(yamlSource?.source.defaults, (value) => value.value);
const generateStr = parseTemplateString(yamlString, /\$\{\{\s*(.*?)\s*\}\}/g, {
...yamlSource?.source,
inputs: inputsForm,
defaults: output
});
return generateStr;
};
const parseTemplate = (str: string) => {
try {
const result = getYamlSource(str);
setYamlSource(result);
const correctYaml = generateCorrectYaml(result);
setYamlList(developGenerateYamlList(correctYaml, detailName));
} catch (error: any) {
toast({
title: 'Parsing Yaml Error',
status: 'error',
description: <pre style={{ whiteSpace: 'pre-wrap' }}>{error?.message}</pre>,
duration: 4000,
isClosable: true
});
}
};
// form
const formHook = useForm({
defaultValues: getTemplateDefaultValues(yamlSource)
});
// watch form change, compute new yaml
formHook.watch((data: any) => {
data && formOnchangeDebounce(data);
});
const formOnchangeDebounce = debounce((data: any) => {
try {
if (yamlSource) {
const correctYaml = generateCorrectYaml(yamlSource, data);
setYamlList(developGenerateYamlList(correctYaml, detailName));
}
} catch (error) {
console.log(error);
}
}, 1000);
const submitSuccess = async () => {
setIsLoading(true);
try {
const result: string[] = await postDeployApp(
yamlList.map((item) => item.value),
'dryrun'
);
toast({
title: t(applySuccess),
status: 'success',
description: result?.toString()
});
} catch (error) {
console.log(error, 'sadasdas');
setErrorMessage(JSON.stringify(error));
}
setIsLoading(false);
};
const submitError = () => {
formHook.getValues();
function deepSearch(obj: any): string {
if (has(obj, 'message')) {
return obj.message;
}
for (let key in obj) {
if (isObject(obj[key])) {
let message = deepSearch(obj[key]);
if (message) {
return message;
}
}
}
return t('Submit Error');
}
toast({
title: deepSearch(formHook.formState.errors),
status: 'error',
position: 'top',
duration: 3000,
isClosable: true
});
};
return (
<Flex flexDirection={'column'} p={'0px 34px 20px 34px '} h="100vh" maxW={'1440px'} mx="auto">
<BreadCrumbHeader applyCb={() => formHook.handleSubmit(submitSuccess, submitError)()} />
<Flex
border={'1px solid #DEE0E2'}
borderRadius={'8px'}
overflowY={'hidden'}
overflowX={'scroll'}
flex={1}>
{/* left */}
<Flex flexDirection={'column'} flex={'0 1 500px'} borderRight={'1px solid #EFF0F1'}>
<Flex
flexShrink={0}
borderBottom={'1px solid #EFF0F1'}
h="48px"
alignItems={'center'}
backgroundColor={'#F8FAFB'}
px="36px"
borderRadius={'8px 8px 0px 0px '}>
<MyIcon name="dev" color={'#24282C'} w={'24px'} h={'24px'}></MyIcon>
<Text fontWeight={'500'} fontSize={'16px'} color={'#24282C'} ml="8px">
{t('develop.Development')}
</Text>
<Text color={'#5A646E'} fontSize={'14px'} fontWeight={400} ml="auto">
{t('develop.Please enter YAML code')}
</Text>
</Flex>
<Box h="100%" w="100%" position={'relative'} overflow={'auto'}>
<CodeMirror
extensions={[StreamLanguage.define(yaml)]}
width="500px"
height="100%"
minHeight="1200px"
onChange={debounce(onYamlChange, 1000)}
/>
</Box>
</Flex>
{/* right */}
<Flex flex={2} flexDirection={'column'}>
<Flex
flexShrink={0}
borderBottom={'1px solid #EFF0F1'}
h="48px"
alignItems={'center'}
backgroundColor={'#F8FAFB'}
pl="42px"
borderRadius={'8px 8px 0px 0px '}>
<MyIcon name="eyeShow" color={'#24282C'} w={'24px'} h={'24px'}></MyIcon>
<Text fontWeight={'500'} fontSize={'16px'} color={'#24282C'} ml="8px">
{t('develop.Preview')}
</Text>
</Flex>
<Flex flexDirection={'column'} h="0" flex={1} overflowY={'scroll'}>
<Flex pl="42px" pt="26px" borderBottom={'1px solid #EFF0F1'} flexDirection={'column'}>
<Text fontWeight={'500'} fontSize={'18px'} color={'#24282C'}>
{t('develop.Configure Form')}
</Text>
<Form formSource={yamlSource!} formHook={formHook} />
</Flex>
<Flex flex={1} pl="42px" pt="26px" flexDirection={'column'} position={'relative'}>
<Text fontWeight={'500'} fontSize={'18px'} color={'#24282C'}>
{t('develop.YAML File')}
</Text>
<YamlList yamlList={yamlList} />
</Flex>
</Flex>
</Flex>
</Flex>
<Loading />
{!!errorMessage && (
<ErrorModal title={applyError} content={errorMessage} onClose={() => setErrorMessage('')} />
)}
</Flex>
);
}
export async function getServerSideProps(content: any) {
return {
props: {
...(await serviceSideProps(content))
}
};
}