@@ -7,8 +7,6 @@ import Editor from "@monaco-editor/react";
7
7
import { Flex , useColorMode } from "@chakra-ui/react" ;
8
8
import { useEffect , useState , useRef } from "react" ;
9
9
import MyBtn from "../MyBtn" ;
10
- import { CodeFile , OutputResult } from "@/lib/types" ;
11
- import { OutputReducerAction } from "@/lib/reducers" ;
12
10
import { tryFormattingCode , validateCode } from "@/lib/client-functions" ;
13
11
import FiChevronRight from "@/app/styles/icons/HiChevronRightGreen" ;
14
12
import { useRouter } from "next/navigation" ;
@@ -24,22 +22,14 @@ export default function CodeEditor({
24
22
stepIndex,
25
23
chapterIndex,
26
24
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 ;
36
25
} ) {
37
26
const { colorMode } = useColorMode ( ) ;
38
- const [ monaco , setMonaco ] = useState < any > ( null ) ;
27
+ const [ monaco , setMonaco ] = useState ( null ) ;
28
+ const [ isValidating , setIsValidating ] = useState ( false ) ;
39
29
const router = useRouter ( ) ;
40
30
const editorStore = useEditorStore ( ) ;
41
31
const userSolutionStore = useUserSolutionStore ( ) ;
42
- const editorRef = useRef < any > ( null ) ;
32
+ const editorRef = useRef ( null ) ;
43
33
44
34
useEffect ( ( ) => {
45
35
if ( monaco ) {
@@ -54,26 +44,30 @@ export default function CodeEditor({
54
44
monaco . editor . setTheme ( colorMode === "light" ? "light" : "my-theme" ) ;
55
45
}
56
46
} , [ monaco , colorMode ] ) ;
47
+
57
48
useEffect ( ( ) => {
58
- const handleKeyDown = ( event : KeyboardEvent ) => {
49
+ const handleKeyDown = ( event ) => {
59
50
if ( event . key == "Enter" && event . shiftKey ) {
60
51
sendGAEvent ( "event" , "buttonClicked" , {
61
52
value : "Validate (through shortcut)" ,
62
53
} ) ;
63
54
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 ) ;
72
67
}
73
68
} ;
74
69
75
70
document . addEventListener ( "keydown" , handleKeyDown ) ;
76
-
77
71
return ( ) => {
78
72
document . removeEventListener ( "keydown" , handleKeyDown ) ;
79
73
} ;
@@ -111,11 +105,10 @@ export default function CodeEditor({
111
105
defaultValue = { codeString }
112
106
theme = { colorMode === "light" ? "light" : "my-theme" }
113
107
value = { codeString }
114
- height = { "100%" }
108
+ height = "100%"
115
109
onChange = { ( codeString ) => setCodeString ( codeString ?? "" ) }
116
110
options = { {
117
111
minimap : { enabled : false } ,
118
-
119
112
fontSize : 14 ,
120
113
formatOnPaste : true ,
121
114
formatOnType : true ,
@@ -129,32 +122,37 @@ export default function CodeEditor({
129
122
/>
130
123
</ div >
131
124
< div className = { styles . buttonsWrapper } >
132
- < Flex dir = "row" gap = { "8px" } alignItems = { "end" } >
125
+ < Flex dir = "row" gap = "8px" alignItems = "end" >
133
126
< 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 ) ;
143
140
} }
141
+ isDisabled = { isValidating }
144
142
variant = {
145
143
outputResult . validityStatus === "valid" ? "success" : "default"
146
144
}
147
145
tooltip = "Shift + Enter"
148
146
>
149
- Validate
147
+ { isValidating ? "Validating..." : " Validate" }
150
148
</ MyBtn >
151
149
152
150
< MyBtn
153
151
onClick = { ( ) => {
154
152
setCodeString ( JSON . stringify ( codeFile . code , null , 2 ) ) ;
155
153
dispatchOutput ( { type : "RESET" } ) ;
156
154
} }
157
- variant = { "error" }
155
+ variant = "error"
158
156
>
159
157
Reset
160
158
</ MyBtn >
0 commit comments