import React, { useState, useEffect } from 'react' 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/files?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 .post(`/SASjsApi/files`, { 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}/SASjsExecutor/do?_program=${props.selectedFilePath}` ) } } return ( {isLoading && ( )} {!isLoading && props?.selectedFilePath !== '' && !editMode && ( {fileContent} )} {!isLoading && props?.selectedFilePath !== '' && editMode && ( { if (val) setFileContent(val) }} /> )} ) } export default Main