"use client"; import * as React from "react"; import { cn } from "./lib/utils"; export interface StopwatchV2Props extends React.HTMLAttributes { label?: string; } function pad(n: number, len = 2): string { return String(n).padStart(len, "0"); } export function StopwatchV2({ label = "CHRONO", className, ...props }: StopwatchV2Props) { const [elapsed, setElapsed] = React.useState(0); const [running, setRunning] = React.useState(false); const [laps, setLaps] = React.useState([]); const startRef = React.useRef(0); const baseRef = React.useRef(0); React.useEffect(() => { if (!running) return; startRef.current = performance.now(); let raf = 0; const tick = (t: number) => { setElapsed(baseRef.current + (t - startRef.current)); raf = requestAnimationFrame(tick); }; raf = requestAnimationFrame(tick); return () => cancelAnimationFrame(raf); }, [running ]); const toggle = () => { if (running) { baseRef.current = elapsed; setRunning(false); } else { setRunning(true); } }; const reset = () => { setRunning(false); baseRef.current = 0; setElapsed(0); setLaps([]); }; const ms = Math.floor(elapsed % 1000); const s = Math.floor(elapsed / 1000) % 60; const m = Math.floor(elapsed / 60000) % 60; const h = Math.floor(elapsed / 3600000); return (

{label}

{h > 0 ? `${pad(h)}:` : ""} {pad(m)}:{pad(s)} .{pad(Math.floor(ms / 10))}

{laps.length > 0 ? (
    {laps.map((lap, i) => (
  1. LAP {i + 1} {pad(Math.floor(lap / 60000))}:{pad(Math.floor(lap / 1000) % 60)}.{pad(Math.floor((lap % 1000) / 10))}
  2. ))}
) : null}
); }