@@ -16,7 +16,6 @@ import { CodeFile, OutputResult } from "@/lib/types";
1616import { OutputReducerAction } from "@/lib/reducers" ;
1717import CertificateButton from "../CertificateButton/CertificateButton" ;
1818
19- // Custom hook for editor theme setup
2019const useEditorTheme = ( monaco : Monaco , colorMode : "dark" | "light" ) => {
2120 useEffect ( ( ) => {
2221 if ( monaco ) {
@@ -33,7 +32,6 @@ const useEditorTheme = (monaco: Monaco, colorMode: "dark" | "light") => {
3332 } , [ monaco , colorMode ] ) ;
3433} ;
3534
36- // Custom hook for keyboard shortcuts
3735const useValidationShortcut = (
3836 handleValidate : ( ) => void ,
3937 codeString : string ,
@@ -56,7 +54,6 @@ const useValidationShortcut = (
5654 } , [ handleValidate , codeString ] ) ;
5755} ;
5856
59- // Custom hook for code persistence
6057const useCodePersistence = (
6158 chapterIndex : number ,
6259 stepIndex : number ,
@@ -66,7 +63,6 @@ const useCodePersistence = (
6663) => {
6764 const userSolutionStore = useUserSolutionStore ( ) ;
6865
69- // Load saved code
7066 useEffect ( ( ) => {
7167 const savedCode = userSolutionStore . getSavedUserSolutionByLesson (
7268 chapterIndex ,
@@ -77,24 +73,16 @@ const useCodePersistence = (
7773 }
7874 } , [ chapterIndex , stepIndex ] ) ;
7975
80- // Save code changes
81- useEffect ( ( ) => {
82- userSolutionStore . saveUserSolutionForLesson (
83- chapterIndex ,
84- stepIndex ,
85- codeString ,
86- ) ;
87- } , [ codeString , chapterIndex , stepIndex ] ) ;
8876
89- // Initialize code if no saved solutions
9077 useEffect ( ( ) => {
9178 if ( Object . keys ( userSolutionStore . userSolutionsByLesson ) . length === 0 ) {
9279 setCodeString ( JSON . stringify ( codeFile . code , null , 2 ) ) ;
9380 }
9481 } , [ userSolutionStore ] ) ;
82+
83+ return userSolutionStore ;
9584} ;
9685
97- // EditorControls component for the buttons section
9886const EditorControls = ( {
9987 handleValidate,
10088 isValidating,
@@ -160,6 +148,9 @@ export default function CodeEditor({
160148 stepIndex,
161149 chapterIndex,
162150 outputResult,
151+ solutionRequested,
152+ resetSolution,
153+ hasValidated,
163154} : {
164155 codeString : string ;
165156 setCodeString : ( codeString : string ) => void ;
@@ -169,16 +160,28 @@ export default function CodeEditor({
169160 stepIndex : number ;
170161 chapterIndex : number ;
171162 outputResult : OutputResult ;
163+ solutionRequested : boolean ;
164+ resetSolution : ( ) => void ;
165+ hasValidated : boolean ;
172166} ) {
173167 const { colorMode } = useColorMode ( ) ;
174168 const [ monaco , setMonaco ] = useState < any > ( null ) ;
175169 const [ isValidating , setIsValidating ] = useState ( false ) ;
176170 const editorStore = useEditorStore ( ) ;
177171 const editorRef = useRef < any > ( null ) ;
178172
179- // Apply custom hooks
173+ const [ activeView , setActiveView ] = useState < 'code' | 'solution' > ( 'code' ) ;
174+
180175 useEditorTheme ( monaco , colorMode ) ;
181176
177+ const userSolutionStore = useCodePersistence (
178+ chapterIndex ,
179+ stepIndex ,
180+ codeString ,
181+ setCodeString ,
182+ codeFile ,
183+ ) ;
184+
182185 const handleValidate = ( ) => {
183186 setIsValidating ( true ) ;
184187 setTimeout ( ( ) => {
@@ -195,42 +198,84 @@ export default function CodeEditor({
195198 } ;
196199
197200 useValidationShortcut ( handleValidate , codeString ) ;
198- useCodePersistence (
199- chapterIndex ,
200- stepIndex ,
201- codeString ,
202- setCodeString ,
203- codeFile ,
204- ) ;
205201
206202 const resetCode = ( ) => {
207- setCodeString ( JSON . stringify ( codeFile . code , null , 2 ) ) ;
203+ const initialCode = JSON . stringify ( codeFile . code , null , 2 ) ;
204+ setCodeString ( initialCode ) ;
208205 dispatchOutput ( { type : "RESET" } ) ;
206+
207+ resetSolution ( ) ;
208+ setActiveView ( 'code' ) ;
209+
210+ userSolutionStore . saveUserSolutionForLesson ( chapterIndex , stepIndex , initialCode ) ;
209211 } ;
210212
211213 const handleEditorMount = ( editor : any , monaco : Monaco ) => {
212214 setMonaco ( monaco ) ;
213-
214215 editorRef . current = editor ;
215216 editorStore . setEditor ( editor ) ;
216217 editorStore . setMonaco ( monaco ) ;
217218 } ;
218219
220+ const handleCodeChange = ( newCode : string | undefined ) => {
221+ if ( activeView === 'code' && newCode !== undefined ) {
222+ setCodeString ( newCode ) ;
223+ userSolutionStore . saveUserSolutionForLesson (
224+ chapterIndex ,
225+ stepIndex ,
226+ newCode
227+ ) ;
228+ }
229+ } ;
230+
231+ useEffect ( ( ) => {
232+ if ( solutionRequested && activeView !== 'solution' ) {
233+ setActiveView ( 'solution' ) ;
234+ }
235+ if ( ! solutionRequested && activeView === 'solution' ) {
236+ setActiveView ( 'code' ) ;
237+ }
238+ } , [ solutionRequested ] ) ;
239+
240+ const isSolutionView = activeView === 'solution' ;
241+ const editorContent = isSolutionView
242+ ? JSON . stringify ( codeFile . solution , null , 2 )
243+ : codeString ;
244+
219245 return (
220246 < >
247+ < div className = { styles . tabBar } >
248+ < button
249+ className = { ctx ( styles . tabButton , activeView === 'code' && styles . activeTab ) }
250+ onClick = { ( ) => setActiveView ( 'code' ) }
251+ >
252+ My Code
253+ </ button >
254+
255+ { solutionRequested && (
256+ < button
257+ className = { ctx ( styles . tabButton , activeView === 'solution' && styles . activeTab ) }
258+ onClick = { ( ) => setActiveView ( 'solution' ) }
259+ >
260+ Solution
261+ </ button >
262+ ) }
263+ </ div >
264+
221265 < div className = { ctx ( styles . codeEditor , GeistMono . className ) } >
222266 < Editor
223267 language = "json"
224- defaultValue = { codeString }
268+ value = { editorContent }
269+ key = { activeView }
225270 theme = { colorMode === "light" ? "light" : "my-theme" }
226- value = { codeString }
227271 height = "100%"
228- onChange = { ( codeString ) => setCodeString ( codeString ?? "" ) }
272+ onChange = { handleCodeChange }
229273 options = { {
230274 minimap : { enabled : false } ,
231275 fontSize : 14 ,
232276 formatOnPaste : true ,
233277 formatOnType : true ,
278+ readOnly : isSolutionView ,
234279 } }
235280 onMount = { handleEditorMount }
236281 />
@@ -244,4 +289,4 @@ export default function CodeEditor({
244289 />
245290 </ >
246291 ) ;
247- }
292+ }
0 commit comments