11"use client" ;
22
3+ import { START_TIME } from "@/libs/data" ;
34import { cn } from "@/libs/utils" ;
45import NumberFlow , { NumberFlowGroup } from "@number-flow/react" ;
56import { useEffect , useState } from "react" ;
@@ -19,24 +20,38 @@ export default function CountdownTimer({
1920} : CountdownTimerProps ) {
2021 const [ timeRemaining , setTimeRemaining ] = useState < number > ( 0 ) ;
2122 const [ isCritical , setIsCritical ] = useState < boolean > ( false ) ;
22- const [ isActive , setIsActive ] = useState ( true ) ;
23+ const [ isActive , setIsActive ] = useState < boolean > ( true ) ;
24+ const [ statusMessage , setStatusMessage ] = useState < string > ( "" ) ;
2325
2426 useEffect ( ( ) => {
2527 const calculateTimeRemaining = ( ) => {
26- const now = new Date ( ) . getTime ( ) ;
28+ const current = new Date ( ) . getTime ( ) ;
29+ const start = START_TIME . getTime ( ) ;
2730 const end = typeof endTime === "number" ? endTime : endTime . getTime ( ) ;
28- const remaining = Math . max ( 0 , end - now ) ;
2931
32+ if ( current < start ) {
33+ // If current time is before the start time, set remaining to 0
34+ setTimeRemaining ( 0 ) ;
35+ setIsActive ( false ) ;
36+ setStatusMessage ( "Countdown initiates soon." ) ;
37+ return ;
38+ }
39+
40+ // Calculate remaining time
41+ const remaining = Math . max ( 0 , end - current ) ;
3042 setTimeRemaining ( remaining ) ;
3143
3244 // Check if we're in the critical period
3345 const criticalTimeInMs = criticalHours * 60 * 60 * 1000 ;
3446 setIsCritical ( remaining > 0 && remaining <= criticalTimeInMs ) ;
3547
36- // If countdown is finished, clear the interval
48+ // If countdown is over
3749 if ( remaining <= 0 ) {
3850 setIsActive ( false ) ;
39- // setTimeRemaining(0); // Reset to 0 when countdown is finished
51+ setStatusMessage ( "Time’s up — the countdown has reached zero." ) ;
52+ } else {
53+ setIsActive ( true ) ;
54+ setStatusMessage ( "" ) ;
4055 }
4156 } ;
4257
@@ -58,7 +73,7 @@ export default function CountdownTimer({
5873 // Format time values to always have two digits
5974 const formatTime = ( value : number ) => value . toString ( ) . padStart ( 2 , "0" ) ;
6075
61- return isActive ? (
76+ return isActive === true ? (
6277 < NumberFlowGroup >
6378 < div
6479 className = { cn (
@@ -93,8 +108,8 @@ export default function CountdownTimer({
93108 </ NumberFlowGroup >
94109 ) : (
95110 < h1 >
96- < span className = "text-primary-100 text-xl font-bold lg:text-6xl " >
97- Countdown Finished
111+ < span className = "text-primary-100 text-xl font-bold lg:text-3xl xl:text-5xl " >
112+ { statusMessage || " Countdown has ended" }
98113 </ span >
99114 </ h1 >
100115 ) ;
0 commit comments