import { useEffect, useState, useRef, useMemo } from 'react'; import { Modal } from '@/components/modal'; import { Timer } from '@/components/timer'; import { dispatch } from '@/lib/event'; import { useSoundStore } from '@/store'; import { cn } from '@/helpers/styles'; import { FADE_OUT } from '@/constants/events'; import styles from './sleep-timer.module.css'; interface SleepTimerModalProps { onClose: () => void; show: boolean; } export function SleepTimerModal({ onClose, show }: SleepTimerModalProps) { const [running, setRunning] = useState(false); const [hours, setHours] = useState('0'); const [minutes, setMinutes] = useState('10'); const totalSeconds = useMemo( () => (hours === '' ? 0 : parseInt(hours)) * 3600 + (minutes === '' ? 0 : parseInt(minutes)) * 60, [hours, minutes], ); const [timeSpent, setTimeSpent] = useState(0); const timeLeft = useMemo( () => totalSeconds - timeSpent, [totalSeconds, timeSpent], ); const timerId = useRef>(); const isPlaying = useSoundStore(state => state.isPlaying); const play = useSoundStore(state => state.play); const pause = useSoundStore(state => state.pause); const handleStart = () => { if (timerId.current) clearInterval(timerId.current); if (!isPlaying) play(); if (totalSeconds > 0) { setRunning(true); const newTimerId = setInterval(() => { setTimeSpent(prev => prev + 1); }, 1000); timerId.current = newTimerId; } }; useEffect(() => { if (timeLeft === 0) { setRunning(false); // pause(); dispatch(FADE_OUT, { duration: 1000 }); setTimeSpent(0); if (timerId.current) clearInterval(timerId.current); } }, [timeLeft, pause]); const handleReset = () => { if (timerId.current) clearInterval(timerId.current); setTimeSpent(0); setHours('0'); setMinutes('10'); setRunning(false); }; const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); handleStart(); }; return (

Sleep Timer

Stop sounds after a certain amount of time.

{!running && ( )} {!running && ( )}
{running ? : null}
{running && ( )} {!running && ( )}
); } interface FieldProps { label: string; onChange: (value: string) => void; value: string; } function Field({ label, onChange, value }: FieldProps) { return (
onChange(e.target.value === '' ? '' : e.target.value)} />
); }