Skip to content

Commit 54e463c

Browse files
committed
enhance time handle
1 parent a00e9de commit 54e463c

File tree

1 file changed

+35
-8
lines changed

1 file changed

+35
-8
lines changed

app/page.tsx

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export default function Page() {
2727
const [userAnswer, setUserAnswer] = useState<UserAnswer>({ content: "" })
2828
const [timeRemaining, setTimeRemaining] = useState(600) // 10 minutes
2929
const [timerWarning, setTimerWarning] = useState(false)
30+
const [isTimerRunning, setIsTimerRunning] = useState(false)
3031
const [isSubmitted, setIsSubmitted] = useState(false)
3132
const [showResultsModal, setShowResultsModal] = useState(false)
3233
const [showAnswerModal, setShowAnswerModal] = useState(false)
@@ -96,31 +97,43 @@ export default function Page() {
9697
});
9798
} finally {
9899
setIsLoadingQuestion(false);
100+
// Reset the timer and start it when question loading is complete
101+
setTimeRemaining(600);
102+
setIsTimerRunning(true);
99103
}
100104
};
101105
fetchQuestion();
102106
}, []);
103107

104108
useEffect(() => {
109+
// Only run the timer if isTimerRunning is true and we're not loading a question
110+
if (!isTimerRunning || isLoadingQuestion) {
111+
return;
112+
}
113+
114+
// Don't auto-submit when timer reaches zero, just show a warning
105115
if (timeRemaining <= 0) {
106-
onSubmit()
107-
return
116+
setTimerWarning(true);
117+
return;
108118
}
109119

110120
if (timeRemaining <= 60) {
111-
setTimerWarning(true)
121+
setTimerWarning(true);
112122
} else {
113-
setTimerWarning(false)
123+
setTimerWarning(false);
114124
}
115125

116126
const intervalId = setInterval(() => {
117-
setTimeRemaining((prevTime) => prevTime - 1)
118-
}, 1000)
127+
setTimeRemaining((prevTime) => prevTime - 1);
128+
}, 1000);
119129

120-
return () => clearInterval(intervalId)
121-
}, [timeRemaining]) // Added onSubmit to dependency array
130+
return () => clearInterval(intervalId);
131+
}, [timeRemaining, isTimerRunning, isLoadingQuestion]);
122132

123133
const onSubmit = async () => {
134+
// Stop the timer when submitting
135+
setIsTimerRunning(false);
136+
124137
setShowResultsModal(true)
125138
setIsSubmitted(true)
126139
setResults(null)
@@ -163,6 +176,9 @@ export default function Page() {
163176
setIsSubmitted(false);
164177
setTimeRemaining(600);
165178
setIsLoadingQuestion(true);
179+
// Reset timer state
180+
setIsTimerRunning(false);
181+
166182
try {
167183
const question = await generateRandomQuestion();
168184
setCurrentQuestion(question);
@@ -197,6 +213,8 @@ export default function Page() {
197213
});
198214
} finally {
199215
setIsLoadingQuestion(false);
216+
// Start the timer after loading is complete
217+
setIsTimerRunning(true);
200218
}
201219
}, [toast, t, language]);
202220

@@ -223,6 +241,9 @@ export default function Page() {
223241
return
224242
}
225243

244+
// Stop the timer when viewing answer
245+
setIsTimerRunning(false);
246+
226247
// Set streaming state and prepare to display inline answer
227248
setIsStreaming(true)
228249
setShowInlineAnswer(true)
@@ -338,6 +359,8 @@ export default function Page() {
338359
setUserAnswer({ content: "" });
339360
setIsSubmitted(false);
340361
setTimeRemaining(600);
362+
// Reset and start the timer when loading a question from history
363+
setIsTimerRunning(true);
341364
setShowHistoryModal(false);
342365
};
343366

@@ -359,6 +382,10 @@ export default function Page() {
359382
setCurrentQuestion(updatedQuestion);
360383
setForcedQuestionType("Coding");
361384

385+
// Reset the timer when switching question type
386+
setTimeRemaining(600);
387+
setIsTimerRunning(true);
388+
362389
toast({
363390
title: t("toast.switchedToCode.title") || "Switched to Code Editor",
364391
description: t("toast.switchedToCode.description") || "The question type has been changed to coding",

0 commit comments

Comments
 (0)