Skip to content

Commit

Permalink
refactor: pass output into component
Browse files Browse the repository at this point in the history
  • Loading branch information
octokatherine authored Oct 29, 2021
1 parent b12217b commit 1682a0a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
12 changes: 11 additions & 1 deletion example-nextjs/components/CodeRunnerUI.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ export default function CodeRunnerUI(props: Props) {
onRunCode,
} = props

const [output, setOutput] = React.useState('')

async function runCode(code: string) {
const output = await onRunCode(code)
if (output) {
setOutput(output)
}
}

return (
<>
<Navbar current={languageLabel} />
Expand Down Expand Up @@ -51,11 +60,12 @@ export default function CodeRunnerUI(props: Props) {
</div>
<Editor
initialCode={initialCode}
output={output}
languageLabel={languageLabel}
hideOutputEditor={hideOutputEditor}
isLoading={isLoading}
defaultLanguage={defaultLanguage}
onRunCode={onRunCode}
onRunCode={runCode}
/>
</div>
</>
Expand Down
14 changes: 4 additions & 10 deletions example-nextjs/components/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { addKeyBinding, CustomKeyBinding } from '../utils'

interface Props {
initialCode: string
output: string
languageLabel: string
hideOutputEditor?: boolean
isLoading?: boolean
Expand All @@ -15,6 +16,7 @@ interface Props {
export default function Editor(props: Props) {
const {
initialCode,
output,
languageLabel,
hideOutputEditor,
isLoading = false,
Expand All @@ -27,14 +29,6 @@ export default function Editor(props: Props) {
const editorRef = React.useRef(null)

const [monaco, setMonaco] = React.useState<Monaco>(null)
const [output, setOutput] = React.useState('')

async function runCode(code: string) {
const output = await onRunCode(code)
if (output) {
setOutput(output)
}
}

function handleEditorDidMount(editor: any, monaco: Monaco) {
editorRef.current = editor
Expand All @@ -48,7 +42,7 @@ export default function Editor(props: Props) {
const runCodeBinding: CustomKeyBinding = {
label: 'run',
keybinding: monaco.KeyMod.CtrlCmd | monaco.KeyCode.Enter,
callback: () => runCode(inputCodeRef.current),
callback: () => onRunCode(inputCodeRef.current),
editor: editorRef.current,
}
return addKeyBinding(runCodeBinding)
Expand Down Expand Up @@ -87,7 +81,7 @@ export default function Editor(props: Props) {
<div className="relative group">
<button
className="relative flex items-center py-4 leading-none bg-black divide-x divide-gray-600 rounded-lg px-7 border-gray-300 disabled:bg-gray-700 disabled:cursor-not-allowed"
onClick={() => runCode(inputCodeRef.current)}
onClick={() => onRunCode(inputCodeRef.current)}
disabled={isLoading}
>
<span className="text-gray-100 transition duration-200 group-hover:text-gray-100">
Expand Down

0 comments on commit 1682a0a

Please sign in to comment.