|
| 1 | +import React, { useEffect, useState } from "react"; |
| 2 | + |
| 3 | +import Timer from "./timer"; |
| 4 | + |
| 5 | +interface TimeDisplayProps { |
| 6 | + statusSignal: (data: string) => void; |
| 7 | + time: string; |
| 8 | + current: boolean; |
| 9 | +} |
| 10 | + |
| 11 | +export default function TimeDisplay({ |
| 12 | + statusSignal, |
| 13 | + time, |
| 14 | + current, |
| 15 | +}: TimeDisplayProps) { |
| 16 | + const [timeRemaining, setTimeRemaining] = useState(0); |
| 17 | + |
| 18 | + useEffect(() => { |
| 19 | + const getTimeRemaining = setInterval(() => { |
| 20 | + const [Hours, Mins, Sec] = time.split(":").map(Number); |
| 21 | + |
| 22 | + const d = new Date(); |
| 23 | + const time_serial = Hours * 60 * 60 + Mins * 60 + Sec; |
| 24 | + const cur_time = |
| 25 | + d.getHours() * 60 * 60 + d.getMinutes() * 60 + d.getSeconds(); |
| 26 | + |
| 27 | + let remaining_time = 0; |
| 28 | + |
| 29 | + remaining_time = time_serial - cur_time; |
| 30 | + if (remaining_time <= 1) { |
| 31 | + clearInterval(getTimeRemaining); |
| 32 | + } |
| 33 | + |
| 34 | + setTimeRemaining(remaining_time); |
| 35 | + }, 1000); |
| 36 | + |
| 37 | + return () => clearInterval(getTimeRemaining); |
| 38 | + }, [time, timeRemaining]); |
| 39 | + |
| 40 | + useEffect(() => { |
| 41 | + if (timeRemaining <= 1) { |
| 42 | + statusSignal("finished"); |
| 43 | + } |
| 44 | + }, [timeRemaining, statusSignal]); |
| 45 | + |
| 46 | + return ( |
| 47 | + <div className="m-3 flex flex-col items-center justify-center rounded-3xl bg-slate-900 p-10 shadow-xl shadow-black/40"> |
| 48 | + {current ? ( |
| 49 | + <> |
| 50 | + <h1 className="mb-2 font-inter text-4xl font-semibold">Timer</h1> |
| 51 | + </> |
| 52 | + ) : ( |
| 53 | + <> |
| 54 | + <h1 className="mb-2 font-inter text-4xl font-semibold">Next Task</h1> |
| 55 | + </> |
| 56 | + )} |
| 57 | + <div className="time_left">{Timer(timeRemaining)}</div> |
| 58 | + <div></div> |
| 59 | + </div> |
| 60 | + ); |
| 61 | +} |
0 commit comments