Skip to content

Commit 183c9cc

Browse files
committed
feat: improve countdown
1 parent f142ce1 commit 183c9cc

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

src/Schedule.tsx

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import {
66
formatUserTime,
77
toUserTime,
88
} from './time'
9+
import { formatDistance } from 'date-fns'
10+
11+
const diff = (startTime: Date) => formatDistance(startTime, new Date())
912

1013
const startsInMinutes = (startTime: Date) => {
1114
return Math.floor((startTime.getTime() - Date.now()) / 1000 / 60)
@@ -18,23 +21,29 @@ const Countdown = ({
1821
startTime: Date
1922
warnTime?: number
2023
}) => {
21-
const [timeToStart, setTimeToStart] = React.useState(
22-
startsInMinutes(startTime),
23-
)
24+
const [timeToStart, setTimeToStart] = React.useState({
25+
text: diff(startTime),
26+
minutes: startsInMinutes(startTime),
27+
})
2428
React.useEffect(() => {
2529
const interval = setInterval(() => {
26-
setTimeToStart(startsInMinutes(startTime))
30+
setTimeToStart({
31+
text: diff(startTime),
32+
minutes: startsInMinutes(startTime),
33+
})
2734
}, 1000 * 60)
2835

2936
return () => clearInterval(interval)
3037
}, [startTime])
3138
return (
3239
<td
3340
className={
34-
timeToStart > 0 && timeToStart <= (warnTime || 5) ? 'time hot' : 'time'
41+
timeToStart.minutes > 0 && timeToStart.minutes <= (warnTime || 5)
42+
? 'time hot'
43+
: 'time'
3544
}
3645
>
37-
{timeToStart > 0 ? `${timeToStart}m` : '-'}
46+
{timeToStart.minutes > 0 ? timeToStart.text : '-'}
3847
</td>
3948
)
4049
}

0 commit comments

Comments
 (0)