Skip to content

Commit cf16658

Browse files
committed
Fix timer
1 parent 9941e44 commit cf16658

File tree

2 files changed

+13
-35
lines changed

2 files changed

+13
-35
lines changed

packages/quizms/src/web/components/timer.tsx

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,17 @@
11
import { useEffect, useState } from "react";
22

33
import clsx from "clsx";
4-
import { addMinutes, differenceInSeconds, isAfter } from "date-fns";
4+
import { differenceInSeconds, isAfter } from "date-fns";
55

6-
type TimerProps =
7-
| {
8-
startTime?: undefined;
9-
duration?: undefined;
10-
endTime: Date;
11-
noAnimation?: boolean;
12-
hideMinutes?: boolean;
13-
}
14-
| {
15-
startTime: Date;
16-
duration: number;
17-
endTime?: undefined;
18-
noAnimation?: boolean;
19-
hideMinutes?: boolean;
20-
};
6+
type TimerProps = {
7+
endTime: Date;
8+
noAnimation?: boolean;
9+
hideMinutes?: boolean;
10+
};
2111

22-
export function Timer(props: TimerProps) {
12+
export function Timer({ endTime, noAnimation, hideMinutes }: TimerProps) {
2313
const [currentTime, setCurrentTime] = useState(Date.now());
2414

25-
const endTime = props.endTime ?? addMinutes(props.startTime, props.duration);
26-
2715
useEffect(() => {
2816
const id = setInterval(() => {
2917
const now = Date.now();
@@ -35,26 +23,20 @@ export function Timer(props: TimerProps) {
3523
return () => clearInterval(id);
3624
}, [endTime]);
3725

38-
let timeLeft = Math.max(differenceInSeconds(endTime!, currentTime), 0);
39-
40-
if (props.duration) {
41-
timeLeft = Math.min(timeLeft, props.duration * 60);
42-
}
26+
const timeLeft = Math.max(differenceInSeconds(endTime!, currentTime), 0);
4327

4428
const hours = Math.floor(timeLeft / 3600);
4529
const minutes = Math.floor(timeLeft / 60) % 60;
4630
const seconds = timeLeft % 60;
4731

4832
return hours > 0 ? (
49-
<span
50-
className={clsx("countdown font-mono", props.noAnimation && "[&_*:before]:transition-none")}>
33+
<span className={clsx("countdown font-mono", noAnimation && "[&_*:before]:transition-none")}>
5134
<span style={{ "--value": hours } as any} />h
5235
<span style={{ "--value": minutes } as any} />m
5336
</span>
5437
) : (
55-
<span
56-
className={clsx("countdown font-mono", props.noAnimation && "[&_*:before]:transition-none")}>
57-
{!props.hideMinutes && (
38+
<span className={clsx("countdown font-mono", noAnimation && "[&_*:before]:transition-none")}>
39+
{!hideMinutes && (
5840
<>
5941
<span style={{ "--value": minutes } as any} />m
6042
</>

packages/quizms/src/web/student/layout.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,10 @@ export function StudentLayout({
122122
{progress}%
123123
</Progress>
124124
<div className="px-3">
125-
{terminated || !participation.startingTime || !contest.hasOnline ? (
125+
{terminated || !student.finishedAt || !contest.hasOnline ? (
126126
<span className="font-mono">00:00</span>
127127
) : (
128-
<Timer
129-
startTime={participation.startingTime}
130-
duration={contest.duration}
131-
noAnimation
132-
/>
128+
<Timer endTime={student.finishedAt} noAnimation />
133129
)}
134130
</div>
135131
{terminated && reset ? (

0 commit comments

Comments
 (0)