import intl from 'react-intl-universal' import React, { PureComponent, Fragment, useState, useEffect, useRef, } from 'react'; import { Button, message, Modal, TreeSelect } from 'antd'; import config from '@/utils/config'; import { PageContainer } from '@ant-design/pro-layout'; import { request } from '@/utils/http'; import Editor from '@monaco-editor/react'; import CodeMirror from '@uiw/react-codemirror'; import { useOutletContext } from '@umijs/max'; import { SharedContext } from '@/layouts'; import { langs } from '@uiw/codemirror-extensions-langs'; const Config = () => { const { headerStyle, isPhone, theme } = useOutletContext(); const [value, setValue] = useState(''); const [loading, setLoading] = useState(true); const [title, setTitle] = useState('config.sh'); const [select, setSelect] = useState('config.sh'); const [data, setData] = useState([]); const editorRef = useRef(null); const [confirmLoading, setConfirmLoading] = useState(false); const getConfig = (name: string) => { request.get(`${config.apiPrefix}configs/${name}`).then(({ code, data }) => { if (code === 200) { setValue(data); } }); }; const getFiles = () => { setLoading(true); request .get(`${config.apiPrefix}configs/files`) .then(({ code, data }) => { if (code === 200) { setData(data); } }) .finally(() => setLoading(false)); }; const updateConfig = () => { setConfirmLoading(true); const content = editorRef.current ? editorRef.current.getValue().replace(/\r\n/g, '\n') : value; request .post(`${config.apiPrefix}configs/save`, { content, name: select }) .then(({ code, data }) => { if (code === 200) { message.success(intl.get('保存成功')); } setConfirmLoading(false); }); }; const onSelect = (value: any, node: any) => { setSelect(value); setTitle(node.value); getConfig(node.value); }; useEffect(() => { getFiles(); getConfig('config.sh'); }, []); return ( , , ]} header={{ style: headerStyle, }} > {isPhone ? ( { setValue(value); }} /> ) : ( { editorRef.current = editor; }} /> )} ); }; export default Config;