1
1
import axios , { isAxiosError } from "axios" ;
2
2
import { GetStaticPaths , GetStaticProps , GetStaticPropsContext } from "next" ;
3
3
import { useRouter } from "next/router" ;
4
- import { RefObject , useEffect , useReducer , useRef , useState } from "react" ;
4
+ import { useEffect , useReducer , useRef , useState } from "react" ;
5
5
6
6
import { quizAPI } from "../../apis/quiz" ;
7
7
import { Editor } from "../../components/editor" ;
@@ -10,6 +10,7 @@ import { Graph } from "../../components/graph";
10
10
import { SolvedModal , useSolvedModal } from "../../components/quiz" ;
11
11
import { QuizGuide } from "../../components/quiz/QuizGuide" ;
12
12
import { Terminal } from "../../components/terminal" ;
13
+ import { CommandInputForwardRefType } from "../../components/terminal/CommandInput" ;
13
14
import { BROWSWER_PATH } from "../../constants/path" ;
14
15
import {
15
16
UserQuizStatusActionType ,
@@ -25,7 +26,6 @@ import {
25
26
} from "../../reducers/terminalReducer" ;
26
27
import { Categories , Quiz , QuizGitGraphCommit } from "../../types/quiz" ;
27
28
import { TerminalContentType } from "../../types/terminalType" ;
28
- import { focusRef , scrollIntoViewRef } from "../../utils/refObject" ;
29
29
import { isString } from "../../utils/typeGuard" ;
30
30
31
31
import * as styles from "./quiz.css" ;
@@ -44,7 +44,7 @@ export default function QuizPage({ quiz }: { quiz: Quiz }) {
44
44
const [ { terminalMode, editorFile, contentArray } , terminalDispatch ] =
45
45
useReducer ( terminalReducer , initialTerminalState ) ;
46
46
47
- const terminalInputRef = useRef < HTMLSpanElement > ( null ) ;
47
+ const terminalInputRef = useRef < CommandInputForwardRefType > ( null ) ;
48
48
49
49
const fetchGitGraphDataRef = useRef ( async ( curId : number ) => {
50
50
try {
@@ -131,8 +131,8 @@ export default function QuizPage({ quiz }: { quiz: Quiz }) {
131
131
await quizAPI . resetQuizById ( numId ) ;
132
132
fetchGitGraphDataRef ?. current ( numId ) ;
133
133
terminalDispatch ( { type : TerminalActionTypes . reset } ) ;
134
- clearTextContent ( terminalInputRef ) ;
135
- focusRef ( terminalInputRef ) ;
134
+ terminalInputRef . current ?. clear ( ) ;
135
+ terminalInputRef . current ?. focus ( ) ;
136
136
userQuizStatusDispatcher ( {
137
137
type : UserQuizStatusActionType . ResetQuizById ,
138
138
id : numId ,
@@ -150,14 +150,17 @@ export default function QuizPage({ quiz }: { quiz: Quiz }) {
150
150
fetchGitGraphDataRef ?. current ( + id ) ;
151
151
}
152
152
terminalDispatch ( { type : TerminalActionTypes . reset } ) ;
153
- clearTextContent ( terminalInputRef ) ;
154
- focusRef ( terminalInputRef ) ;
153
+ terminalInputRef . current ?. clear ( ) ;
154
+ terminalInputRef . current ?. focus ( ) ;
155
155
} , [ id ] ) ;
156
156
157
157
useEffect ( ( ) => {
158
- scrollIntoViewRef ( terminalInputRef ) ;
159
- clearTextContent ( terminalInputRef ) ;
160
- focusRef ( terminalInputRef ) ;
158
+ const $terminalInput = terminalInputRef . current ;
159
+ if ( ! $terminalInput ) return ;
160
+
161
+ $terminalInput . scrollIntoView ( ) ;
162
+ $terminalInput . clear ( ) ;
163
+ $terminalInput . focus ( ) ;
161
164
} , [ contentArray ] ) ;
162
165
163
166
const { barRef, topRef, handleBarHover } = useResizableSplitView ( ) ;
@@ -239,15 +242,6 @@ export const getStaticPaths = (async () => {
239
242
return { paths, fallback : "blocking" } ;
240
243
} ) satisfies GetStaticPaths ;
241
244
242
- function clearTextContent < T extends Element > ( ref : RefObject < T > ) {
243
- const $element = ref . current ;
244
- if ( ! $element ) {
245
- return ;
246
- }
247
-
248
- $element . textContent = "" ;
249
- }
250
-
251
245
function isEditorMode ( terminalMode : "editor" | "command" ) {
252
246
return terminalMode === "editor" ;
253
247
}
0 commit comments