import { useState, useEffect, useRef } from 'react' const PHASES = [ { label: 'Inhale', duration: 4, scale: 1.55 }, { label: 'Hold', duration: 4, scale: 1.55 }, { label: 'Exhale', duration: 6, scale: 1.0 }, { label: 'Hold', duration: 2, scale: 1.0 }, ] export default function DeepBreathWidget() { const [running, setRunning] = useState(false) const [phaseIdx, setPhaseIdx] = useState(0) const [tick, setTick] = useState(PHASES[0].duration) const [breaths, setBreaths] = useState(0) const intervalRef = useRef(null) function stop() { clearInterval(intervalRef.current) setRunning(false) setPhaseIdx(0) setTick(PHASES[0].duration) } function start() { setRunning(true) setPhaseIdx(0) setTick(PHASES[0].duration) } useEffect(() => { if (!running) return intervalRef.current = setInterval(() => { setTick(t => { if (t > 1) return t - 1 setPhaseIdx(pi => { const next = (pi + 1) % PHASES.length if (next === 0) setBreaths(b => b + 1) setTick(PHASES[next].duration) return next }) return PHASES[0].duration }) }, 1000) return () => clearInterval(intervalRef.current) }, [running]) const phase = PHASES[phaseIdx] const pct = running ? (tick / phase.duration) : 1 return (
{running && ( )}
{running ? phase.label : 'Box Breathing'}
{running ? tick : '—'}
{breaths > 0 || running ? `${breaths} breath${breaths !== 1 ? 's' : ''} completed` : 'Tap start to begin'}
{!running ? : } {!running && breaths > 0 && ( )}
) }