-
Notifications
You must be signed in to change notification settings - Fork 2k
/
index.tsx
317 lines (302 loc) · 10.1 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import { postDeployApp } from '@/api/app';
import { getPlatformEnv } from '@/api/platform';
import MyIcon from '@/components/Icon';
import { editModeMap } from '@/constants/editApp';
import { useLoading } from '@/hooks/useLoading';
import { useToast } from '@/hooks/useToast';
import { YamlItemType } from '@/types';
import { TemplateSourceType, TemplateType } from '@/types/app';
import { EnvResponse } from '@/types/index';
import { serviceSideProps } from '@/utils/i18n';
import {
developGenerateYamlList,
getTemplateDataSource,
handleTemplateToInstanceYaml,
parseTemplateString
} from '@/utils/json-yaml';
import { getTemplateDefaultValues } from '@/utils/template';
import { downLoadBold } from '@/utils/tools';
import { Button, Center, Flex, Spinner, Text } from '@chakra-ui/react';
import { useQuery } from '@tanstack/react-query';
import dayjs from 'dayjs';
import JsYaml from 'js-yaml';
import { debounce, has, isObject, mapValues } from 'lodash';
import { useTranslation } from 'next-i18next';
import { useCallback, useEffect, useRef, 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 { type EditorState } from '@codemirror/state';
import dynamic from 'next/dynamic';
const Editor = dynamic(() => import('./components/Editor'), {
loading() {
return (
<Center w="full" h="full">
<Spinner size={'lg'} />
</Center>
);
},
ssr: false
});
export default function Develop() {
const { t } = useTranslation();
const { toast } = useToast();
const [yamlSource, setYamlSource] = useState<TemplateSourceType>();
const [yamlList, setYamlList] = useState<YamlItemType[]>([]);
const { Loading, setIsLoading } = useLoading();
const [errorMessage, setErrorMessage] = useState('');
const { applySuccess, applyError } = editModeMap(false);
const SuccessfulDryRun = useRef(false);
const { data: platformEnvs } = useQuery(['getPlatformEnvs'], getPlatformEnv) as {
data: EnvResponse;
};
const onYamlChange = debounce((state: EditorState) => {
const value = state.doc.toString();
parseTemplate(value);
}, 1000);
const getYamlSource = (str: string): TemplateSourceType => {
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, platformEnvs);
const _instanceName = dataSource?.defaults?.app_name?.value || '';
const instanceYaml = handleTemplateToInstanceYaml(templateYaml, _instanceName);
yamlList.unshift(instanceYaml);
const result: TemplateSourceType = {
source: {
...dataSource,
...platformEnvs
},
yamlList: yamlList,
templateYaml: templateYaml
};
return result;
};
const generateCorrectYamlList = (
yamlSource: TemplateSourceType,
inputsForm = {}
): YamlItemType[] => {
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
});
const _instanceName = yamlSource?.source?.defaults?.app_name?.value || '';
return developGenerateYamlList(generateStr, _instanceName);
};
const parseTemplate = (str: string) => {
if (!str || !str.trim()) {
setYamlSource(void 0);
setYamlList([]);
return;
}
try {
const result = getYamlSource(str);
const defaultInputes = getTemplateDefaultValues(result);
setYamlSource(result);
const correctYamlList = generateCorrectYamlList(result, defaultInputes);
setYamlList(correctYamlList);
} 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
useEffect(
() =>
formHook.watch((data: any) => {
data && formOnchangeDebounce(data);
}).unsubscribe,
[formHook.watch]
);
const formOnchangeDebounce = debounce((data: any) => {
try {
if (yamlSource) {
const correctYamlList = generateCorrectYamlList(yamlSource, data);
setYamlList(correctYamlList);
}
} catch (error) {
console.log(error);
}
}, 1000);
const submitSuccess = async () => {
setIsLoading(true);
try {
const result: string[] = await postDeployApp(
yamlList.map((item) => item.value),
'dryrun'
);
if (result.length > 0) {
SuccessfulDryRun.current = true;
}
toast({
title: t(applySuccess),
status: 'success',
description: result?.toString()
});
} catch (error) {
console.log(error, '===apply err===');
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
});
};
const handleExportYaml = useCallback(async () => {
const exportYamlString = yamlList.map((i) => i.value).join('---\n');
if (!exportYamlString) return;
downLoadBold(
exportYamlString,
'application/yaml',
`yaml${dayjs().format('YYYYMMDDHHmmss')}.yaml`
);
}, [yamlList]);
const formalApply = async () => {
try {
if (yamlList.length !== 0 && SuccessfulDryRun.current) {
const result: string[] = await postDeployApp(yamlList.map((item) => item.value));
toast({
title: t('Deployment successful, please go to My Application to view') || ' ',
status: 'success'
});
}
} catch (error) {
console.log(error, 'FormalApply');
}
};
return (
<Flex flexDirection={'column'} p={'0px 34px 20px 34px '} h="100vh" maxW={'1440px'} mx="auto">
<BreadCrumbHeader
applyCb={() => formHook.handleSubmit(submitSuccess, submitError)()}
isShowBtn={SuccessfulDryRun.current}
applyFormalCb={formalApply}
/>
<Flex
border={'1px solid #DEE0E2'}
borderRadius={'8px'}
overflowY={'hidden'}
overflowX={'scroll'}
flex={1}>
{/* left */}
<Flex flexDirection={'column'} w="50%" 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>
<Editor
h="100%"
w="100%"
position={'relative'}
overflow={'auto'}
onDocChange={(s) => onYamlChange(s)}
/>
</Flex>
{/* right */}
<Flex w="50%" flexDirection={'column'} position={'relative'}>
<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'} flex={1} overflow={'scroll'}>
<Flex
pl="42px"
pt="26px"
pr={{ sm: '20px', md: '60px' }}
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'}>
<Flex alignItems={'center'} justifyContent={'space-between'}>
<Text fontWeight={'500'} fontSize={'18px'} color={'#24282C'} cursor={'default'}>
{t('develop.YAML File')}
</Text>
<Button
ml="auto"
minW={'100px'}
h={'34px'}
variant={'link'}
onClick={handleExportYaml}>
{t('Export')} Yaml
</Button>
</Flex>
<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))
}
};
}