import intl from 'react-intl-universal'; import React, { PureComponent, useRef, useState, useEffect } from 'react'; import { Button, message, Select, Form, Row, Col } from 'antd'; import config from '@/utils/config'; import { PageContainer } from '@ant-design/pro-layout'; import { request } from '@/utils/http'; import './index.less'; import { DiffEditor } from '@monaco-editor/react'; import ReactDiffViewer from 'react-diff-viewer'; import { useOutletContext } from '@umijs/max'; import { SharedContext } from '@/layouts'; import { getEditorMode } from '@/utils'; const { Option } = Select; const Diff = () => { const { headerStyle, isPhone, theme } = useOutletContext(); const [origin, setOrigin] = useState('sample/config.sample.sh'); const [current, setCurrent] = useState('config.sh'); const [originValue, setOriginValue] = useState(''); const [currentValue, setCurrentValue] = useState(''); const [loading, setLoading] = useState(true); const [files, setFiles] = useState([]); const editorRef = useRef(null); const [language, setLanguage] = useState('shell'); const getConfig = () => { request .get(`${config.apiPrefix}configs/detail?path=${encodeURIComponent(current)}`) .then(({ code, data }) => { if (code === 200) { setCurrentValue(data); } }); }; const getSample = () => { request .get(`${config.apiPrefix}configs/detail?path=${encodeURIComponent(origin)}`) .then(({ code, data }) => { if (code === 200) { setOriginValue(data); } }); }; const updateConfig = () => { const content = editorRef.current ? editorRef.current.getModel().modified.getValue().replace(/\r\n/g, '\n') : currentValue; request .post(`${config.apiPrefix}configs/save`, { content, name: current, }) .then(({ code, data }) => { if (code === 200) { message.success(intl.get('保存成功')); } }); }; const getFiles = () => { setLoading(true); request .get(`${config.apiPrefix}configs/sample`) .then(({ code, data }) => { if (code === 200) { setFiles(data); } }) .finally(() => setLoading(false)); }; const originFileChange = (value: string, op) => { setCurrent(op.extra.target); setOrigin(value); const newMode = getEditorMode(value); setLanguage(newMode); }; useEffect(() => { getFiles(); }, []); useEffect(() => { getSample(); }, [origin]); useEffect(() => { getConfig(); }, [current]); return ( {intl.get('保存')} , ] } > {current} {isPhone ? ( ) : ( { editorRef.current = editor; }} /> )} ); }; export default Diff;