Files
beenvoice-app/lib/use-running-elapsed.ts
T
soconnor 14c880123c Add beenvoice mobile companion app with full dark mode support.
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>
2026-06-17 22:36:37 -04:00

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;
}