import React, { useState, useEffect } from 'react'; import { Row, Col, Button, Form, Switch, Input, Modal, Badge, Radio } from 'antd'; import { CaretRightOutlined, PauseOutlined } from '@ant-design/icons'; import styles from '../../ui.module.less'; import { TaskState } from '../../../server/core/enums'; import { getTerminalRefIns, setTerminalRefIns } from '../../util'; import { useInit } from '../../hooks'; import { TaskComponentProps } from '..'; import Analyze from '../Analyze'; const DevComponent: React.FC = ({ taskType, namespace, api, detail = {}, dispatch, dbPath, iife, Terminal, }) => { const { intl } = api; const isEnglish = api.getLocale() === 'en-US'; const [form] = Form.useForm(); const [modalVisible, setModalVisible] = useState(false); const [log, setLog] = useState(''); const [view, setView] = useState('log'); const [env, setEnv] = useState({ UMI_UI_SERVER: 'none', UMI_UI_PORT: window.location.port, BABEL_POLYFILL: true, HMR: true, BABEL_CACHE: true, MOCK: true, BROWSER: true, CLEAR_CONSOLE: true, PORT: null, FORK_TS_CHECKER: false, UMI_UI: null, }); const [init] = useInit(detail); useEffect( () => { if (!init) { return () => {}; } if (view === 'log') { dispatch({ type: `${namespace}/getTaskDetail`, payload: { taskType, log: true, dbPath, callback: ({ log }) => { setLog(log); }, }, }); } if (iife) { dev(); } // UnMount: reset form return () => { form.resetFields(); const terminal = getTerminalRefIns(taskType, api.currentProject.key); if (terminal) { terminal.clear(); } }; }, [init, view, iife], ); async function dev() { dispatch({ type: `${namespace}/exec`, payload: { taskType, args: { analyze: true, dbPath, }, env, }, }); } async function cancelDev() { dispatch({ type: `${namespace}/cancel`, payload: { taskType, }, }); } const openModal = () => { setModalVisible(true); }; const handleOk = () => { form .validateFields() .then(values => { setEnv({ ...env, ...(values as any), }); setModalVisible(false); }) .catch(_ => {}); }; const handleCancel = () => { setModalVisible(false); }; const toggleView = e => { const { value } = e.target; setView(value); }; const stopEventPop = e => { e && e.stopPropagation(); e && e.preventDefault(); }; const EnvLabel = props => (
{intl({ id: props.title })}
{intl({ id: props.desc })} {intl({ id: 'org.umi.ui.tasks.env.detail' })}
); const isTaskRunning = detail && [TaskState.ING, TaskState.SUCCESS].indexOf(detail.state) > -1; const outputRunningInfo = ({ state, localUrl, hasError }) => { if (!state || state === TaskState.INIT) { return null; } const map = { [TaskState.ING]: { status: 'processing', text: ( {intl({ id: hasError ? 'org.umi.ui.tasks.dev.state.starting.error' : 'org.umi.ui.tasks.dev.state.starting', })} ), }, [TaskState.SUCCESS]: { status: 'success', text: ( {localUrl ? ( <> {intl({ id: 'org.umi.ui.tasks.dev.state.success' })} {localUrl} ) : null} ), }, [TaskState.FAIL]: { status: 'error', text: {intl({ id: 'org.umi.ui.tasks.dev.state.fail' })}, }, }; return (
{map[state].text}
); }; const detailHost = `https://umijs.org/${isEnglish ? '' : 'zh'}`; return ( <>

{intl({ id: 'org.umi.ui.tasks.dev' })}

<> {outputRunningInfo(detail)}
} name="BABEL_POLYFILL" valuePropName="checked" > } name="HMR" valuePropName="checked" > } name="BABEL_CACHE" valuePropName="checked" > } name="MOCK" valuePropName="checked" > {window.g_bigfish ? null : ( } name="BROWSER" valuePropName="checked" > )} } name="CLEAR_CONSOLE" valuePropName="checked" > } name="FORK_TS_CHECKER" valuePropName="checked" > } name="UMI_UI" valuePropName="checked" >
{intl({ id: 'org.umi.ui.tasks.log' })} {intl({ id: 'org.umi.ui.tasks.analyze' })}
{view === 'log' ? ( { if (ins) { setTerminalRefIns(taskType, api.currentProject.key, ins); } }} defaultValue={log} /> ) : ( )}
); }; export default DevComponent;