import React, { useState, useEffect } from "react"; import { SevenSegmentDigit } from "../SevenSegmentDigit/SevenSegmentDigit"; import "../../styles.css"; export interface DigitalClockProps { jsTime?: Date; } export const DigitalClockDisplay: React.FC = ({ jsTime, }) => { const [internalTime, setInternalTime] = useState(new Date()); const isLiveClock = jsTime === undefined; const displayTime = isLiveClock ? internalTime : jsTime; useEffect(() => { let timerId: any; if (isLiveClock) { timerId = setInterval(() => { setInternalTime(new Date()); }, 500); } return () => { if (timerId) { clearInterval(timerId); } }; }, [isLiveClock]); const hours = displayTime.getHours(); const minutes = displayTime.getMinutes(); const h1 = Math.floor(hours / 10); const h2 = hours % 10; const m1 = Math.floor(minutes / 10); const m2 = minutes % 10; const colonClass = `seven-segment-colon ${isLiveClock ? "colon-blink" : ""}`; return (
); };