forked from labring/sealos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.tsx
316 lines (281 loc) · 10 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
import { getKindTemplate, getTemplate, postDeployApp } from '@/api/app';
import MyIcon from '@/components/Icon';
import { editModeMap } from '@/constants/editApp';
import { useConfirm } from '@/hooks/useConfirm';
import { useLoading } from '@/hooks/useLoading';
import { useToast } from '@/hooks/useToast';
import { GET } from '@/services/request';
import { useCachedStore } from '@/store/cached';
import { useGlobalStore } from '@/store/global';
import type { QueryType, YamlItemType } from '@/types';
import { TemplateSource, TemplateType } from '@/types/app';
import { serviceSideProps } from '@/utils/i18n';
import { generateYamlList, parseTemplateString } from '@/utils/json-yaml';
import { processEnvValue } from '@/utils/tools';
import { Box, Breadcrumb, BreadcrumbItem, BreadcrumbLink, Flex } from '@chakra-ui/react';
import { useQuery } from '@tanstack/react-query';
import JSYAML from 'js-yaml';
import { has, isObject, mapValues, reduce } from 'lodash';
import debounce from 'lodash/debounce';
import { useTranslation } from 'next-i18next';
import dynamic from 'next/dynamic';
import { useRouter } from 'next/router';
import { useEffect, useMemo, useState } from 'react';
import { useForm } from 'react-hook-form';
import { sealosApp } from 'sealos-desktop-sdk/app';
import Form from './components/Form';
import ReadMe from './components/ReadMe';
import Yaml from './components/Yaml';
const ErrorModal = dynamic(() => import('./components/ErrorModal'));
const Header = dynamic(() => import('./components/Header'), { ssr: false });
const EditApp = ({ appName, tabType }: { appName?: string; tabType: string }) => {
const { t, i18n } = useTranslation();
const { toast } = useToast();
const router = useRouter();
const { templateName } = router.query as QueryType;
const { Loading, setIsLoading } = useLoading();
const [forceUpdate, setForceUpdate] = useState(false);
const { title, applyBtnText, applyMessage, applySuccess, applyError } = editModeMap(!!appName);
const [templateSource, setTemplateSource] = useState<TemplateSource>();
const [yamlList, setYamlList] = useState<YamlItemType[]>([]);
const [correctYaml, setCorrectYaml] = useState('');
const [errorMessage, setErrorMessage] = useState('');
const { screenWidth } = useGlobalStore();
const { setCached, cached, insideCloud, deleteCached, setInsideCloud } = useCachedStore();
const detailName = useMemo(
() => templateSource?.source?.defaults?.app_name?.value || '',
[templateSource]
);
const { data: platformEnvs } = useQuery(['getPlatformEnvs'], () => GET('/api/platform/getEnv'));
const { data: templateDetail } = useQuery(['getKindTemplate', templateName], () =>
getKindTemplate(templateName)
);
const { openConfirm, ConfirmChild } = useConfirm({
content: insideCloud ? 'Confirm Deploy Application?' : 'Heading to sealos soon'
});
const { openConfirm: openConfirm2, ConfirmChild: ConfirmChild2 } = useConfirm({
content: 'Do you want to jump to the app details page'
});
const pxVal = useMemo(() => {
const val = Math.floor((screenWidth - 1050) / 2);
if (val < 20) {
return 20;
}
return val;
}, [screenWidth]);
const getFormDefaultValues = (templateSource: TemplateSource | undefined) => {
const inputs = templateSource?.source?.inputs;
return reduce(
inputs,
(acc, item) => {
// @ts-ignore
acc[item.key] = item.default;
return acc;
},
{}
);
};
const formOnchangeDebounce = debounce((data: any) => {
try {
if (!templateSource) return;
const yamlString = templateSource.yamlList?.map((item) => JSYAML.dump(item)).join('---\n');
const output = mapValues(templateSource?.source.defaults, (value) => value.value);
const generateStr = parseTemplateString(yamlString, /\$\{\{\s*(.*?)\s*\}\}/g, {
...templateSource?.source,
inputs: data,
defaults: output
});
setCorrectYaml(generateStr);
setYamlList(generateYamlList(generateStr, detailName));
} catch (error) {
console.log(error);
}
}, 200);
const getCachedValue = () => {
if (!cached) return null;
const cachedValue = JSON.parse(cached);
return cachedValue?.cachedKey === templateName ? cachedValue : null;
};
// form
const formHook = useForm({
defaultValues: getFormDefaultValues(templateSource),
values: getCachedValue()
});
// watch form change, compute new yaml
formHook.watch((data: any) => {
data && formOnchangeDebounce(data);
setForceUpdate(!forceUpdate);
});
const submitSuccess = async () => {
setIsLoading(true);
try {
if (!insideCloud) {
setIsLoading(false);
setCached(JSON.stringify({ ...formHook.getValues(), cachedKey: templateName }));
const _name = encodeURIComponent(`?templateName=${templateName}&sealos_inside=true`);
const _domain = platformEnvs.SEALOS_CLOUD_DOMAIN;
const href = `https://${_domain}/?openapp=system-fastdeploy${_name}`;
return window.open(href, '_self');
}
const detailName = templateSource?.source?.defaults?.app_name?.value;
const yamls = JSYAML.loadAll(correctYaml).map((item: any) => {
let _item = processEnvValue(item, detailName!);
return JSYAML.dump(_item);
});
const result = await postDeployApp(yamls, 'create');
toast({
title: t(applySuccess),
status: 'success'
});
deleteCached();
openConfirm2(() => {
sealosApp.runEvents('openDesktopApp', {
appKey: 'system-applaunchpad',
pathname: '/app/detail',
query: { name: detailName },
messageData: {}
});
})();
} catch (error) {
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 getTemplateData = async () => {
if (!templateName) {
toast({
title: t('TemplateNameError'),
status: 'error',
position: 'top',
duration: 3000,
isClosable: true
});
return null;
}
const res: TemplateSource = await getTemplate(templateName);
console.log(res);
setTemplateSource(res);
try {
const yamlString = res.yamlList?.map((item) => JSYAML.dump(item)).join('---\n');
const output = mapValues(res?.source.defaults, (value) => value.value);
const generateStr = parseTemplateString(yamlString, /\$\{\{\s*(.*?)\s*\}\}/g, {
...res?.source,
defaults: output,
inputs: getCachedValue() ? JSON.parse(cached) : getFormDefaultValues(res)
});
setCorrectYaml(generateStr);
setYamlList(generateYamlList(generateStr, detailName));
} catch (err) {
console.log(err, 'getTemplateData');
}
};
useEffect(() => {
setInsideCloud(!(window.top === window));
(async () => {
try {
await getTemplateData();
} catch (error) {}
})();
}, []);
return (
<>
<Flex flexDirection={'column'} alignItems={'center'} h={'100%'} minWidth={'1024px'}>
<Flex
zIndex={99}
position={'fixed'}
top={0}
left={0}
w={'100%'}
h={'50px'}
borderBottom={'1px solid #DEE0E2'}
justifyContent={'start'}
alignItems={'center'}
backgroundColor={'rgba(255, 255, 255)'}
backdropBlur={'100px'}>
<Box cursor={'pointer'} onClick={() => router.push('/')}>
<MyIcon ml={'46px'} name="arrowLeft" color={'#24282C'} w={'16px'} h={'16px'}></MyIcon>
</Box>
<Breadcrumb
ml={'14px'}
fontWeight={500}
fontSize={16}
textDecoration={'none'}
color={'#7B838B'}>
<BreadcrumbItem textDecoration={'none'}>
<BreadcrumbLink _hover={{ color: '#219BF4', textDecoration: 'none' }} href="/">
{t('Template List')}
</BreadcrumbLink>
</BreadcrumbItem>
<BreadcrumbItem color={'#262A32'} isCurrentPage={router.pathname === 'deploy'}>
<BreadcrumbLink _hover={{ color: '#219BF4', textDecoration: 'none' }} href="#">
{templateDetail?.metadata?.name}
</BreadcrumbLink>
</BreadcrumbItem>
</Breadcrumb>
</Flex>
<Flex
mt={'50px'}
flexDirection={'column'}
width={'100%'}
flexGrow={1}
backgroundColor={'rgba(255, 255, 255, 0.90)'}>
<Header
templateDetail={templateDetail}
appName={''}
title={title}
yamlList={yamlList}
applyBtnText={insideCloud ? applyBtnText : 'Deploy on sealos'}
applyCb={() => formHook.handleSubmit(openConfirm(submitSuccess), submitError)()}
/>
<Flex w={{ md: '1000px', base: '800px' }} m={'32px auto'} flexDirection="column">
<Form formHook={formHook} pxVal={pxVal} formSource={templateSource?.source} />
{/* <Yaml yamlList={yamlList} pxVal={pxVal}></Yaml> */}
<ReadMe templateDetail={templateDetail} />
</Flex>
</Flex>
</Flex>
<ConfirmChild />
<ConfirmChild2 />
<Loading />
{!!errorMessage && (
<ErrorModal title={applyError} content={errorMessage} onClose={() => setErrorMessage('')} />
)}
</>
);
};
export async function getServerSideProps(content: any) {
const appName = content?.query?.name || '';
const tabType = content?.query?.type || 'form';
return {
props: {
appName,
tabType,
...(await serviceSideProps(content))
}
};
}
export default EditApp;