Skip to content

Commit

Permalink
feat: Add loading state to RunButton in CodeBlock component (#237)
Browse files Browse the repository at this point in the history
* feat: Add loading state to RunButton in CodeBlock component

* audited
  • Loading branch information
kartik-gupta-ij authored Oct 4, 2024
1 parent 4827e5d commit 776b662
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 19 deletions.
24 changes: 12 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 14 additions & 6 deletions src/components/Common/CodeBlock.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useEffect, useState } from 'react';
import PropTypes from 'prop-types';
import { Highlight, Prism, themes } from 'prism-react-renderer';
import Editor from 'react-simple-code-editor';
import { alpha, Box, Button } from '@mui/material';
import { alpha, Box, Button, CircularProgress } from '@mui/material';
import { styled, useTheme } from '@mui/material/styles';
import { PlayArrowOutlined } from '@mui/icons-material';
import { CopyButton } from './CopyButton';
Expand All @@ -25,25 +25,32 @@ const StyledEditor = styled((props) => <Editor padding={0} textareaClassName={'c
* @return {JSX.Element}
* @constructor
*/
export const RunButton = ({ code, onRun }) => {
export const RunButton = ({ code, onRun, loading }) => {
return (
<Button variant="outlined" endIcon={<PlayArrowOutlined />} onClick={() => onRun(code)} data-testid="code-block-run">
Run
<Button
variant="outlined"
endIcon={loading ? <CircularProgress size={24} color="inherit" /> : <PlayArrowOutlined />}
onClick={() => onRun(code)}
disabled={loading}
data-testid="code-block-run"
>
{loading ? 'Running...' : 'Run'}
</Button>
);
};

RunButton.propTypes = {
code: PropTypes.string.isRequired,
onRun: PropTypes.func.isRequired,
loading: PropTypes.bool,
};

/**
* Code block with syntax highlighting
* @return {JSX.Element}
* @constructor
*/
export const CodeBlock = ({ codeStr, language, withRunButton, onRun, title, editable = true }) => {
export const CodeBlock = ({ codeStr, language, withRunButton, onRun, title, editable = true, loading }) => {
const [code, setCode] = useState(codeStr);
const theme = useTheme();
const prismTheme = theme.palette.mode === 'light' ? themes.nightOwlLight : themes.vsDark;
Expand Down Expand Up @@ -102,7 +109,7 @@ export const CodeBlock = ({ codeStr, language, withRunButton, onRun, title, edit
>
{withRunButton && onRun && (
<Box sx={{ flexGrow: '1' }}>
<RunButton code={code} onRun={onRun} />
<RunButton code={code} onRun={onRun} loading={loading} />
</Box>
)}
{title && <Box>{title}</Box>}
Expand Down Expand Up @@ -131,4 +138,5 @@ CodeBlock.propTypes = {
onRun: PropTypes.func,
title: PropTypes.string,
editable: PropTypes.bool, // by default code block is editable
loading: PropTypes.bool,
};
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,25 @@ export const MdxCodeBlock = ({ children }) => {
const language = className.replace(/language-/, '');
const withRunButton = children.props.withRunButton && bigIntJSON.parse(children.props.withRunButton);
const { setResult } = useTutorial();
const [loading, setLoading] = React.useState(false);

const handleRun = (code) => {
setLoading(true);
setResult('{}');
requestFromCode(code, false)
.then((res) => {
setResult(() => bigIntJSON.stringify(res));
setLoading(false);
})
.catch((err) => {
setResult(() => bigIntJSON.stringify(err));
setLoading(false);
});
};

return <CodeBlock codeStr={code} language={language} withRunButton={withRunButton} onRun={handleRun} />;
return (
<CodeBlock codeStr={code} language={language} withRunButton={withRunButton} onRun={handleRun} loading={loading} />
);
};

MdxCodeBlock.propTypes = {
Expand Down

0 comments on commit 776b662

Please sign in to comment.