@@ -5,46 +5,32 @@ import { useEffect, useState, useRef } from "react";
55import clsx from "clsx" ;
66import TypeScriptSandboxEditor from "./TypeScriptSandboxEditor" ;
77import styles from "./styles.module.css" ;
8-
9- function useSavedCode ( key : string , defaultValue : string ) {
10- const [ code , setCode ] = useLocalStorage ( key , defaultValue ) ;
11-
12- return {
13- code,
14- setCode,
15- } ;
16- }
17-
18- function useShouldSaveCode ( ) {
19- const [ shouldAutosaveCode , setShouldAutosaveCode ] = useLocalStorage (
20- "shouldAutosaveCode" ,
21- true
22- ) ;
23-
24- return [ shouldAutosaveCode , setShouldAutosaveCode ] as const ;
25- }
8+ import BrowserOnly from "@docusaurus/BrowserOnly" ;
9+ import CopyToClipboardButton from "./CopyToClipboardButton" ;
2610
2711type CodeEditorProps = {
2812 codeKey : string ;
2913 defaultValue : string ;
3014 solution : string ;
3115} ;
3216
33- export default function CodeEditor ( {
17+ function BrowserCodeEditor ( {
3418 codeKey,
3519 defaultValue,
3620 solution,
3721} : CodeEditorProps ) {
3822 const monaco = useMonaco ( ) ;
23+ const [ shouldAutosaveCode , setShouldAutosaveCode ] = useLocalStorage (
24+ "shouldAutosaveCode" ,
25+ true
26+ ) ;
27+ const [ code , setCode ] = useLocalStorage ( codeKey , defaultValue ) ;
28+
3929 const [ shouldShowDiff , setShouldShowDiff ] = useState ( false ) ;
40- const { code, setCode } = useSavedCode ( codeKey , defaultValue ) ;
4130 const [ shouldShowSolution , setShouldShowSolution ] = useState ( false ) ;
4231 const [ value , setValue ] = useState ( code ) ;
43- const [ shouldAutosaveCode , setShouldAutosaveCode ] = useShouldSaveCode ( ) ;
44- const { colorMode } = useColorMode ( ) ;
45- const [ clipboardTick , setClipboardTick ] = useState ( false ) ;
46- const clipboardTimeoutRef = useRef < NodeJS . Timeout | null > ( null ) ;
4732
33+ const { colorMode } = useColorMode ( ) ;
4834 const theme = colorMode === "dark" ? "vs-dark" : "light" ;
4935
5036 const handleCodeChange = ( newValue : string | undefined ) => {
@@ -54,26 +40,12 @@ export default function CodeEditor({
5440 setValue ( newValue || "" ) ;
5541 } ;
5642
57- const handleCopyToClipboard = ( ) => {
58- navigator . clipboard . writeText ( value ) ;
59- setClipboardTick ( true ) ;
60-
61- if ( clipboardTimeoutRef . current ) {
62- clearTimeout ( clipboardTimeoutRef . current ) ;
63- }
64-
65- clipboardTimeoutRef . current = setTimeout ( ( ) => {
66- setClipboardTick ( false ) ;
67- } , 1000 ) ;
68- } ;
69-
7043 useEffect ( ( ) => {
7144 if ( shouldShowSolution ) {
7245 return ;
7346 }
7447
7548 if ( shouldAutosaveCode ) {
76- console . log ( "Autosaving code..." , value ) ;
7749 setCode ( value ) ;
7850 }
7951 } , [ value , shouldAutosaveCode , setCode , shouldShowSolution ] ) ;
@@ -83,9 +55,6 @@ export default function CodeEditor({
8355 return ;
8456 }
8557
86- // Show inlay hints
87- // monaco.languages.registerInlayHintsProvider("typescript", )
88-
8958 monaco . languages . typescript . typescriptDefaults . setDiagnosticsOptions ( {
9059 noSemanticValidation : false ,
9160 noSyntaxValidation : false ,
@@ -123,12 +92,7 @@ export default function CodeEditor({
12392 >
12493 Reset
12594 </ button >
126- < button
127- className = "button button--secondary button--outline"
128- onClick = { handleCopyToClipboard }
129- >
130- { clipboardTick ? "✔ Copied" : "Copy" }
131- </ button >
95+ < CopyToClipboardButton value = { value } />
13296 </ div >
13397 < div style = { { display : "flex" , gap : "1rem" } } >
13498 { shouldShowSolution && (
@@ -205,3 +169,7 @@ export default function CodeEditor({
205169 </ div >
206170 ) ;
207171}
172+
173+ export default function CodeEditor ( props : CodeEditorProps ) {
174+ return < BrowserOnly > { ( ) => < BrowserCodeEditor { ...props } /> } </ BrowserOnly > ;
175+ }
0 commit comments