@@ -7,8 +7,6 @@ import Editor from "@monaco-editor/react";
77import { Flex , useColorMode } from "@chakra-ui/react" ;
88import { useEffect , useState , useRef } from "react" ;
99import MyBtn from "../MyBtn" ;
10- import { CodeFile , OutputResult } from "@/lib/types" ;
11- import { OutputReducerAction } from "@/lib/reducers" ;
1210import { tryFormattingCode , validateCode } from "@/lib/client-functions" ;
1311import FiChevronRight from "@/app/styles/icons/HiChevronRightGreen" ;
1412import { useRouter } from "next/navigation" ;
@@ -24,22 +22,14 @@ export default function CodeEditor({
2422 stepIndex,
2523 chapterIndex,
2624 outputResult,
27- } : {
28- codeString : string ;
29- setCodeString : ( codeString : string ) => void ;
30- codeFile : CodeFile ;
31- dispatchOutput : React . Dispatch < OutputReducerAction > ;
32- nextStepPath : string | undefined ;
33- stepIndex : number ;
34- chapterIndex : number ;
35- outputResult : OutputResult ;
3625} ) {
3726 const { colorMode } = useColorMode ( ) ;
38- const [ monaco , setMonaco ] = useState < any > ( null ) ;
27+ const [ monaco , setMonaco ] = useState ( null ) ;
28+ const [ isValidating , setIsValidating ] = useState ( false ) ;
3929 const router = useRouter ( ) ;
4030 const editorStore = useEditorStore ( ) ;
4131 const userSolutionStore = useUserSolutionStore ( ) ;
42- const editorRef = useRef < any > ( null ) ;
32+ const editorRef = useRef ( null ) ;
4333
4434 useEffect ( ( ) => {
4535 if ( monaco ) {
@@ -54,26 +44,30 @@ export default function CodeEditor({
5444 monaco . editor . setTheme ( colorMode === "light" ? "light" : "my-theme" ) ;
5545 }
5646 } , [ monaco , colorMode ] ) ;
47+
5748 useEffect ( ( ) => {
58- const handleKeyDown = ( event : KeyboardEvent ) => {
49+ const handleKeyDown = ( event ) => {
5950 if ( event . key == "Enter" && event . shiftKey ) {
6051 sendGAEvent ( "event" , "buttonClicked" , {
6152 value : "Validate (through shortcut)" ,
6253 } ) ;
6354 event . preventDefault ( ) ;
64- tryFormattingCode ( editorRef , setCodeString ) ;
65- validateCode (
66- codeString ,
67- codeFile ,
68- dispatchOutput ,
69- stepIndex ,
70- chapterIndex ,
71- ) ;
55+ setIsValidating ( true ) ;
56+ setTimeout ( ( ) => {
57+ tryFormattingCode ( editorRef , setCodeString ) ;
58+ validateCode (
59+ codeString ,
60+ codeFile ,
61+ dispatchOutput ,
62+ stepIndex ,
63+ chapterIndex ,
64+ ) ;
65+ setIsValidating ( false ) ;
66+ } , 500 ) ;
7267 }
7368 } ;
7469
7570 document . addEventListener ( "keydown" , handleKeyDown ) ;
76-
7771 return ( ) => {
7872 document . removeEventListener ( "keydown" , handleKeyDown ) ;
7973 } ;
@@ -111,11 +105,10 @@ export default function CodeEditor({
111105 defaultValue = { codeString }
112106 theme = { colorMode === "light" ? "light" : "my-theme" }
113107 value = { codeString }
114- height = { "100%" }
108+ height = "100%"
115109 onChange = { ( codeString ) => setCodeString ( codeString ?? "" ) }
116110 options = { {
117111 minimap : { enabled : false } ,
118-
119112 fontSize : 14 ,
120113 formatOnPaste : true ,
121114 formatOnType : true ,
@@ -129,32 +122,37 @@ export default function CodeEditor({
129122 />
130123 </ div >
131124 < div className = { styles . buttonsWrapper } >
132- < Flex dir = "row" gap = { "8px" } alignItems = { "end" } >
125+ < Flex dir = "row" gap = "8px" alignItems = "end" >
133126 < MyBtn
134- onClick = { async ( ) => {
135- tryFormattingCode ( editorRef , setCodeString ) ;
136- validateCode (
137- codeString ,
138- codeFile ,
139- dispatchOutput ,
140- stepIndex ,
141- chapterIndex ,
142- ) ;
127+ onClick = { ( ) => {
128+ setIsValidating ( true ) ;
129+ setTimeout ( ( ) => {
130+ tryFormattingCode ( editorRef , setCodeString ) ;
131+ validateCode (
132+ codeString ,
133+ codeFile ,
134+ dispatchOutput ,
135+ stepIndex ,
136+ chapterIndex ,
137+ ) ;
138+ setIsValidating ( false ) ;
139+ } , 500 ) ;
143140 } }
141+ isDisabled = { isValidating }
144142 variant = {
145143 outputResult . validityStatus === "valid" ? "success" : "default"
146144 }
147145 tooltip = "Shift + Enter"
148146 >
149- Validate
147+ { isValidating ? "Validating..." : " Validate" }
150148 </ MyBtn >
151149
152150 < MyBtn
153151 onClick = { ( ) => {
154152 setCodeString ( JSON . stringify ( codeFile . code , null , 2 ) ) ;
155153 dispatchOutput ( { type : "RESET" } ) ;
156154 } }
157- variant = { "error" }
155+ variant = "error"
158156 >
159157 Reset
160158 </ MyBtn >
0 commit comments