import intl from 'react-intl-universal'; import React, { useState, useEffect, useRef } from 'react'; import { Button, InputNumber, Form, message, Input, Alert } from 'antd'; import config from '@/utils/config'; import { request } from '@/utils/http'; import './index.less'; import Ansi from 'ansi-to-react'; import pick from 'lodash/pick'; import WebSocketManager from '@/utils/websocket'; const dataMap = { 'dependence-proxy': 'dependenceProxy', 'node-mirror': 'nodeMirror', 'python-mirror': 'pythonMirror', 'linux-mirror': 'linuxMirror', }; const Dependence = () => { const [systemConfig, setSystemConfig] = useState<{ dependenceProxy?: string; nodeMirror?: string; pythonMirror?: string; linuxMirror?: string; }>(); const [form] = Form.useForm(); const [log, setLog] = useState(''); const [loading, setLoading] = useState(false); const getSystemConfig = () => { request .get(`${config.apiPrefix}system/config`) .then(({ code, data }) => { if (code === 200 && data.info) { setSystemConfig(data.info); } }) .catch((error: any) => { console.log(error); }); }; const updateSystemConfigStream = (path: keyof typeof dataMap) => { setLoading(true); setLog('in progress...\n'); request .put( `${config.apiPrefix}system/config/${path}`, pick(systemConfig, dataMap[path]), ) .then((res) => {}) .catch((error: any) => { console.log(error); }); }; const updateSystemConfig = (path: keyof typeof dataMap) => { setLoading(true); setLog(''); request .put( `${config.apiPrefix}system/config/${path}`, pick(systemConfig, dataMap[path]), ) .then(({ code, data }) => { if (code === 200) { message.success(intl.get('更新成功')); } }) .catch((error: any) => { console.log(error); }) .finally(() => setLoading(false)); }; const handleMessage = (payload: any) => { const { message } = payload; setLog((p) => `${p}${message}`); if ( message.includes('update node mirror end') || message.includes('update linux mirror end') ) { setLoading(false); } }; useEffect(() => { const ws = WebSocketManager.getInstance(); ws.subscribe('updateNodeMirror', handleMessage); ws.subscribe('updateLinuxMirror', handleMessage); return () => { ws.subscribe('updateNodeMirror', handleMessage); ws.unsubscribe('updateLinuxMirror', handleMessage); }; }, []); useEffect(() => { getSystemConfig(); }, []); return (
{ setSystemConfig({ ...systemConfig, dependenceProxy: e.target.value, }); }} /> { setSystemConfig({ ...systemConfig, nodeMirror: e.target.value, }); }} /> { setSystemConfig({ ...systemConfig, pythonMirror: e.target.value, }); }} /> { setSystemConfig({ ...systemConfig, linuxMirror: e.target.value, }); }} />
        {log}
      
); }; export default Dependence;