index.tsx 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. import { addApp, apiInterceptors, getAppList, getTeamMode, updateApp } from '@/client/api';
  2. import { CreateAppParams, TeamMode } from '@/types/app';
  3. import { useRequest } from 'ahooks';
  4. import { App, ConfigProvider, Divider, Form, Input, Modal, Spin } from 'antd';
  5. import classNames from 'classnames';
  6. import Image from 'next/image';
  7. import { useRouter } from 'next/router';
  8. import React, { useMemo, useState } from 'react';
  9. import { useTranslation } from 'react-i18next';
  10. import styles from './styles.module.css';
  11. interface WorkModeSelectProps {
  12. disable: boolean;
  13. options: TeamMode[];
  14. value?: TeamMode;
  15. onChange?: (value: TeamMode) => void;
  16. }
  17. // 自定义team_mode选择
  18. const WorkModeSelect: React.FC<WorkModeSelectProps> = ({ disable = false, options = [], value, onChange }) => {
  19. const [selected, setSelected] = useState<TeamMode>(value || ({} as TeamMode));
  20. const { i18n } = useTranslation();
  21. const returnOptionStyle = (item: TeamMode) => {
  22. if (disable) {
  23. return classNames(
  24. `flex items-center p-4 border rounded-lg border-[#d9d9d9] cursor-not-allowed relative transition-all duration-500 ease-in-out`,
  25. {
  26. 'bg-[rgba(0,0,0,0.04)] dark:bg-[#606264]': item.value === selected?.value,
  27. },
  28. );
  29. }
  30. return `flex items-center p-4 border dark:border-[rgba(217,217,217,0.85)] rounded-lg cursor-pointer hover:border-[#0c75fc] hover:bg-[#f5faff] dark:hover:border-[rgba(12,117,252,0.85)] dark:hover:bg-[#606264] relative transition-all duration-300 ease-in-out ${
  31. item.value === selected?.value
  32. ? 'border-[#0c75fc] bg-[#f5faff] dark:bg-[#606264] dark:border-[#0c75fc]'
  33. : 'border-[#d9d9d9]'
  34. } `;
  35. };
  36. const language = i18n.language === 'en';
  37. return (
  38. <div className='grid grid-cols-2 gap-4'>
  39. {options.map(item => (
  40. <div
  41. className={returnOptionStyle(item)}
  42. key={item.value}
  43. onClick={() => {
  44. if (disable) {
  45. return;
  46. }
  47. setSelected(item);
  48. onChange?.({ ...value, ...item });
  49. }}
  50. >
  51. <Image src={`/icons/app/${item.value}.png`} width={48} height={48} alt={item.value} />
  52. <div className='flex flex-col ml-3'>
  53. <span className='text-xs font-medium text-[rgba(0,0,0,0.85)] dark:text-[rgba(255,255,255,0.85)] first-line:leading-6'>
  54. {language ? item.name_en : item.name_cn}
  55. </span>
  56. <span className='text-xs text-[rgba(0,0,0,0.45)] dark:text-[rgba(255,255,255,0.85)]'>
  57. {language ? item.description_en : item.description}
  58. </span>
  59. </div>
  60. {item.value === selected?.value && (
  61. <div
  62. className='w-3 h-3 rounded-tr-md absolute top-[1px] right-[1px] transition-all duration-300 ease-in-out'
  63. style={{
  64. background: `linear-gradient(to right top, transparent 50%, transparent 50%, ${disable ? '#d0d0d0' : '#0c75fc'} 0)`,
  65. }}
  66. />
  67. )}
  68. </div>
  69. ))}
  70. </div>
  71. );
  72. };
  73. const CreateAppModal: React.FC<{
  74. open: boolean;
  75. onCancel: () => void;
  76. refresh?: any;
  77. type?: 'add' | 'edit';
  78. }> = ({ open, onCancel, type = 'add', refresh }) => {
  79. const { t, i18n } = useTranslation();
  80. const appInfo = JSON.parse(localStorage.getItem('new_app_info') || '{}');
  81. const { message } = App.useApp();
  82. const [form] = Form.useForm();
  83. const teamMode = Form.useWatch('team_mode', form);
  84. const router = useRouter();
  85. const language = i18n.language === 'en';
  86. // 获取工作模式列表
  87. const { data, loading } = useRequest(async () => {
  88. const [_, res] = await apiInterceptors(getTeamMode());
  89. return res ?? [];
  90. });
  91. // 创建应用
  92. const { run: createApp, loading: createLoading } = useRequest(
  93. async (params: CreateAppParams) => {
  94. if (type === 'edit') {
  95. return await apiInterceptors(
  96. updateApp({
  97. app_code: appInfo?.app_code,
  98. language: 'zh',
  99. ...params,
  100. }),
  101. );
  102. } else {
  103. return await apiInterceptors(
  104. addApp({
  105. language: 'zh',
  106. ...params,
  107. }),
  108. );
  109. }
  110. },
  111. {
  112. manual: true,
  113. onSuccess: async res => {
  114. const [error, data] = res;
  115. if (!error) {
  116. if (type === 'edit') {
  117. const [, res] = await apiInterceptors(getAppList({}));
  118. const curApp = res?.app_list?.find(item => item.app_code === appInfo?.app_code);
  119. localStorage.setItem('new_app_info', JSON.stringify({ ...curApp, isEdit: true }));
  120. message.success(t('Update_successfully'));
  121. } else {
  122. message.success(t('Create_successfully'));
  123. localStorage.setItem('new_app_info', JSON.stringify(data));
  124. router.push(`/construct/app/extra`);
  125. }
  126. } else {
  127. message.error(type === 'edit' ? t('Update_failure') : t('Create_failure'));
  128. }
  129. await refresh?.();
  130. onCancel();
  131. },
  132. },
  133. );
  134. const mode = useMemo(() => {
  135. return data?.filter(item => item.value === appInfo?.team_mode)?.[0];
  136. }, [appInfo, data]);
  137. if (loading) {
  138. return null;
  139. }
  140. return (
  141. <ConfigProvider
  142. theme={{
  143. components: {
  144. Button: {
  145. defaultBorderColor: '#d9d9d9',
  146. },
  147. },
  148. }}
  149. >
  150. <Modal
  151. className={styles['create-app-modal-container']}
  152. title={t('create_app')}
  153. width={900}
  154. open={open}
  155. onOk={async () => {
  156. form.validateFields().then(async (values: any) => {
  157. await createApp({
  158. app_name: values?.app_name,
  159. app_describe: values?.app_describe,
  160. team_mode: values?.team_mode?.value,
  161. });
  162. });
  163. }}
  164. onCancel={onCancel}
  165. centered={true}
  166. >
  167. <Spin spinning={createLoading}>
  168. <div className='flex flex-1'>
  169. <Form
  170. layout='vertical'
  171. className='w-3/5'
  172. form={form}
  173. initialValues={{
  174. team_mode: mode || data?.[0],
  175. app_name: appInfo?.app_name,
  176. app_describe: appInfo?.app_describe,
  177. }}
  178. >
  179. <Form.Item
  180. label={t('team_modal')}
  181. name='team_mode'
  182. required
  183. rules={[{ required: true, message: t('Please_input_the_work_modal') }]}
  184. >
  185. <WorkModeSelect disable={type === 'edit'} options={data || []} />
  186. </Form.Item>
  187. <Form.Item
  188. label={`${t('app_name')}:`}
  189. name='app_name'
  190. required
  191. rules={[{ required: true, message: t('input_app_name') }]}
  192. >
  193. <Input placeholder={t('input_app_name')} autoComplete='off' className='h-8' />
  194. </Form.Item>
  195. <Form.Item
  196. label={`${t('Description')}:`}
  197. name='app_describe'
  198. required
  199. rules={[
  200. {
  201. required: true,
  202. message: t('Please_input_the_description'),
  203. },
  204. ]}
  205. >
  206. <Input.TextArea
  207. autoComplete='off'
  208. placeholder={t('Please_input_the_description')}
  209. autoSize={{ minRows: 2.5 }}
  210. />
  211. </Form.Item>
  212. {/* <Form.Item label="应用图标:" name="app_icon" valuePropName="fileList">
  213. <Upload listType="picture-card">
  214. <button style={{ border: 0, background: 'none' }} type="button">
  215. <PlusOutlined />
  216. <div style={{ marginTop: 8 }}>上传图标</div>
  217. </button>
  218. </Upload>
  219. </Form.Item> */}
  220. </Form>
  221. <Divider type='vertical' className='h-auto mx-6 bg-[rgba(0,0,0,0.06)] dark:bg-[rgba(255,255,255,0.5)] ' />
  222. <div className='flex flex-col w-2/5 pl-6 pt-8 '>
  223. <span className='text-base text-[rgba(0,0,0,0.85)] font-medium mb-6 dark:text-[rgba(255,255,255,0.85)]'>
  224. {language ? teamMode?.name_en : teamMode?.name_cn}
  225. </span>
  226. <div className='flex items-start'>
  227. <span className='flex flex-shrink-0 w-1 h-1 rounded-full bg-[rgba(0,0,0,0.45)] mt-2 mr-1 dark:bg-[rgba(255,255,255,0.65)]' />
  228. <span className='text-xs leading-5 text-[rgba(0,0,0,0.45)] dark:text-[rgba(255,255,255,0.65)]'>
  229. {language ? teamMode?.remark_en : teamMode?.remark}
  230. </span>
  231. </div>
  232. </div>
  233. </div>
  234. </Spin>
  235. </Modal>
  236. </ConfigProvider>
  237. );
  238. };
  239. export default CreateAppModal;