11import { useEffect , useState } from "react" ;
22
33import 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 </ >
0 commit comments