import React, { useState, useEffect } from 'react' import { Link } from 'react-router-dom' import axios from 'axios' import Editor from '@monaco-editor/react' import Box from '@mui/material/Box' import Paper from '@mui/material/Paper' import Stack from '@mui/material/Stack' import Button from '@mui/material/Button' import Toolbar from '@mui/material/Toolbar' import CircularProgress from '@mui/material/CircularProgress' const Main = (props: any) => { const baseUrl = window.location.origin const [isLoading, setIsLoading] = useState(false) const [fileContentBeforeEdit, setFileContentBeforeEdit] = useState('') const [fileContent, setFileContent] = useState('') const [editMode, setEditMode] = useState(false) useEffect(() => { if (props.selectedFilePath) { setIsLoading(true) axios .get(`/SASjsApi/drive/file?filePath=${props.selectedFilePath}`) .then((res: any) => { setFileContent(res.data.fileContent) }) .catch((err) => { console.log(err) }) .finally(() => { setIsLoading(false) }) } }, [props.selectedFilePath]) const handleEditSaveBtnClick = () => { if (!editMode) { setFileContentBeforeEdit(fileContent) setEditMode(true) } else { setIsLoading(true) axios .patch(`/SASjsApi/drive/file`, { filePath: props.selectedFilePath, fileContent: fileContent }) .then((res) => { setEditMode(false) }) .catch((err) => { console.log(err) }) .finally(() => { setIsLoading(false) }) } } const handleCancelExecuteBtnClick = () => { if (editMode) { setFileContent(fileContentBeforeEdit) setEditMode(false) } else { window.open( `${baseUrl}/SASjsApi/stp/execute?_program=${props.selectedFilePath.replace( /.sas$/, '' )}` ) } } return ( {isLoading && ( )} {!isLoading && props?.selectedFilePath && !editMode && ( {fileContent} )} {!isLoading && props?.selectedFilePath && editMode && ( { if (val) setFileContent(val) }} /> )} {props?.selectedFilePath && ( )} ) } export default Main