14c880123c
Expo app with dashboard, time clock, invoices, and settings — native tabs, glass UI, theme-aware components, and iOS Live Activities. Co-authored-by: Cursor <cursoragent@cursor.com>
30 lines
676 B
TypeScript
30 lines
676 B
TypeScript
import { useEffect, useState } from "react";
|
|
|
|
/** Live elapsed seconds since `startedAt`, ticking every second. */
|
|
export function useRunningElapsed(startedAt?: string | Date | null) {
|
|
const [elapsed, setElapsed] = useState(0);
|
|
|
|
useEffect(() => {
|
|
if (!startedAt) {
|
|
setElapsed(0);
|
|
return;
|
|
}
|
|
|
|
const startMs = new Date(startedAt).getTime();
|
|
if (Number.isNaN(startMs)) {
|
|
setElapsed(0);
|
|
return;
|
|
}
|
|
|
|
const tick = () => {
|
|
setElapsed(Math.max(0, Math.floor((Date.now() - startMs) / 1000)));
|
|
};
|
|
|
|
tick();
|
|
const id = setInterval(tick, 1000);
|
|
return () => clearInterval(id);
|
|
}, [startedAt]);
|
|
|
|
return elapsed;
|
|
}
|