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'; const { Option } = Select; const Diff = () => { const { headerStyle, isPhone, theme } = useOutletContext(); const [origin, setOrigin] = useState('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 getConfig = () => { request .get(`${config.apiPrefix}configs/${current}`) .then(({ code, data }) => { if (code === 200) { setCurrentValue(data); } }); }; const getSample = () => { request .get(`${config.apiPrefix}configs/${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`, { data: { content, name: current }, }) .then(({ code, data }) => { if (code === 200) { message.success('保存成功'); } }); }; const getFiles = () => { setLoading(true); request .get(`${config.apiPrefix}configs/files`) .then(({ code, data }) => { if (code === 200) { setFiles(data); } }) .finally(() => setLoading(false)); }; const originFileChange = (value: string) => { setOrigin(value); }; const currentFileChange = (value: string) => { setCurrent(value); }; useEffect(() => { getFiles(); }, []); useEffect(() => { getSample(); }, [origin]); useEffect(() => { getConfig(); }, [current]); return ( 保存 , ] } > {isPhone ? ( ) : ( { editorRef.current = editor; }} /> )} ); }; export default Diff;