diff options
| author | Jan Tuomi <jans.tuomi@gmail.com> | 2022-02-05 16:48:16 +0200 |
|---|---|---|
| committer | Jan Tuomi <jans.tuomi@gmail.com> | 2022-02-05 16:48:16 +0200 |
| commit | a96d162f649704df5d9b567158366015cbe3172c (patch) | |
| tree | 656f946eeb6748fe01447004399e08a45ff0b5f5 /src/Editor.tsx | |
| parent | 7d78bf4d2111af87636069ee2d35d6323ffc96be (diff) | |
Get basic stuff working
Diffstat (limited to 'src/Editor.tsx')
| -rw-r--r-- | src/Editor.tsx | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/src/Editor.tsx b/src/Editor.tsx new file mode 100644 index 0000000..d179b83 --- /dev/null +++ b/src/Editor.tsx @@ -0,0 +1,68 @@ +import AceEditor from "react-ace" +import "ace-builds/src-noconflict/mode-java" +import "ace-builds/src-noconflict/theme-github" +import { useState } from "react" +import styles from "./Editor.module.css" +import { APIResult, submitSource } from "./api" + +const Editor = () => { + const [sourceText, setSourceText] = useState("") + const [result, setResult] = useState<APIResult<string, Error>>({ type: "not_yet_requested" }) + const onChange = setSourceText + + const executeCode = async (text: string) => { + const result = await submitSource(text) + setResult(result) + } + + const renderResults = () => { + switch (result.type) { + case "not_yet_requested": + case "loading": + return null + case "failed": + return ( + <div className={styles.error}> + Failed to run code on the server. See console for details. + </div> + ) + case "success": + return ( + <AceEditor + mode="" + theme="github" + onChange={() => undefined} + value={result.data} + name="results" + editorProps={{ $blockScrolling: true }} + width="100%" + fontSize={16} + readOnly + /> + ) + } + } + + return ( + <> + <AceEditor + mode="" + theme="github" + onChange={onChange} + value={sourceText} + name="editor" + editorProps={{ $blockScrolling: true }} + width="100%" + fontSize={16} + /> + <button className={styles.runButton} onClick={() => executeCode(sourceText)}> + ▶️ Run code on server + </button> + <div className={styles.results}> + {renderResults()} + </div> + </> + ) +} + +export default Editor |
