Live video on the AT Protocol
at next 50 lines 1.4 kB view raw
1import { Text, zero } from "@streamplace/components"; 2import { useEffect, useState } from "react"; 3import { View } from "react-native"; 4 5export default function Timer({ start }: { start: string | Date }) { 6 const [elapsedTime, setElapsedTime] = useState(0); 7 8 useEffect(() => { 9 const startDate = typeof start === "string" ? new Date(start) : start; 10 11 const interval = setInterval(() => { 12 const now = new Date(); 13 const elapsed = Math.floor((now.getTime() - startDate.getTime()) / 1000); 14 setElapsedTime(elapsed); 15 }, 250); 16 17 return () => clearInterval(interval); 18 }, [start]); 19 20 const formatTime = (seconds: number) => { 21 const hours = Math.floor(seconds / 3600); 22 const minutes = Math.floor((seconds % 3600) / 60); 23 const secs = seconds % 60; 24 25 return `${hours.toString().padStart(2, "0")}:${minutes.toString().padStart(2, "0")}:${secs.toString().padStart(2, "0")}`; 26 }; 27 28 return ( 29 <View 30 style={[ 31 { justifyContent: "center", alignItems: "center" }, 32 { flexDirection: "row" }, 33 zero.px[2], 34 zero.py[1], 35 ]} 36 > 37 <Text 38 style={[ 39 { fontFamily: "monospace" }, 40 { 41 textShadowOffset: { width: -1, height: 1 }, 42 textShadowRadius: 3, 43 }, 44 ]} 45 > 46 {formatTime(elapsedTime)} 47 </Text> 48 </View> 49 ); 50}