Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 | 38x 38x 38x 38x 11x 38x 11x 11x 11x 6x 38x 13x 13x 13x 9x 9x 13x 38x 8x 8x 5x 8x 39x 39x 59x 39x 35x 35x 2x 2x 2x 2x 33x 8x 8x 8x 10x 38x 2x 2x 2x 2x 2x 2x 2x 38x 1x 1x 1x 1x 1x 1x 1x 38x 1x 1x 1x 1x 2x 38x 2x 2x 2x 1x 1x 38x 28x 28x 28x 28x 84x 28x 28x 28x 28x 27x 27x 28x 21x 21x 21x 21x 4x 17x | import { useState, useEffect, useRef, useCallback } from 'react'
export interface Timer {
id: string
label: string
duration: number // seconds
remaining: number // seconds
isRunning: boolean
}
export interface UseTimersReturn {
timers: Timer[]
addTimer: (label: string, duration: number, autoStart?: boolean) => string
startTimer: (id: string) => void
pauseTimer: (id: string) => void
resetTimer: (id: string) => void
deleteTimer: (id: string) => void
toggleTimer: (id: string) => void
}
export function useTimers(onTimerComplete?: (timer: Timer) => void): UseTimersReturn {
const [timers, setTimers] = useState<Timer[]>([])
const intervalsRef = useRef<Map<string, number>>(new Map())
const onCompleteRef = useRef(onTimerComplete)
// Keep callback ref up to date
useEffect(() => {
onCompleteRef.current = onTimerComplete
}, [onTimerComplete])
// Cleanup intervals on unmount
useEffect(() => {
const intervals = intervalsRef.current
return () => {
intervals.forEach((intervalId) => {
clearInterval(intervalId)
})
}
}, [])
const addTimer = useCallback((label: string, duration: number, autoStart: boolean = true): string => {
const id = crypto.randomUUID()
// Add timer to state FIRST to ensure it exists before interval ticks
setTimers((prev) => [
...prev,
{
id,
label,
duration,
remaining: duration,
isRunning: autoStart,
},
])
// Set up interval AFTER state update is queued (timer will exist before first tick)
if (autoStart) {
const intervalId = window.setInterval(() => {
setTimers((prev) =>
prev.map((timer) => {
if (timer.id !== id) return timer
if (!timer.isRunning) return timer
const newRemaining = timer.remaining - 1
if (newRemaining <= 0) {
// Timer completed
clearInterval(intervalsRef.current.get(id))
intervalsRef.current.delete(id)
onCompleteRef.current?.({ ...timer, remaining: 0, isRunning: false })
return { ...timer, remaining: 0, isRunning: false }
}
return { ...timer, remaining: newRemaining }
})
)
}, 1000)
intervalsRef.current.set(id, intervalId)
}
return id
}, [])
const startTimer = useCallback((id: string) => {
// Clear any existing interval for this timer
const existingInterval = intervalsRef.current.get(id)
if (existingInterval) {
clearInterval(existingInterval)
}
// Start new interval
const intervalId = window.setInterval(() => {
setTimers((prev) =>
prev.map((timer) => {
if (timer.id !== id) return timer
if (!timer.isRunning) return timer
const newRemaining = timer.remaining - 1
if (newRemaining <= 0) {
// Timer completed
clearInterval(intervalsRef.current.get(id))
intervalsRef.current.delete(id)
onCompleteRef.current?.({ ...timer, remaining: 0, isRunning: false })
return { ...timer, remaining: 0, isRunning: false }
}
return { ...timer, remaining: newRemaining }
})
)
}, 1000)
intervalsRef.current.set(id, intervalId)
setTimers((prev) =>
prev.map((timer) =>
timer.id === id ? { ...timer, isRunning: true } : timer
)
)
}, [])
const pauseTimer = useCallback((id: string) => {
// Clear interval
const intervalId = intervalsRef.current.get(id)
Eif (intervalId) {
clearInterval(intervalId)
intervalsRef.current.delete(id)
}
setTimers((prev) =>
prev.map((timer) =>
timer.id === id ? { ...timer, isRunning: false } : timer
)
)
}, [])
const resetTimer = useCallback((id: string) => {
// Clear interval
const intervalId = intervalsRef.current.get(id)
Eif (intervalId) {
clearInterval(intervalId)
intervalsRef.current.delete(id)
}
setTimers((prev) =>
prev.map((timer) =>
timer.id === id
? { ...timer, remaining: timer.duration, isRunning: false }
: timer
)
)
}, [])
const deleteTimer = useCallback((id: string) => {
// Clear interval
const intervalId = intervalsRef.current.get(id)
Eif (intervalId) {
clearInterval(intervalId)
intervalsRef.current.delete(id)
}
setTimers((prev) => prev.filter((timer) => timer.id !== id))
}, [])
const toggleTimer = useCallback(
(id: string) => {
const timer = timers.find((t) => t.id === id)
Iif (!timer) return
if (timer.isRunning) {
pauseTimer(id)
} else {
startTimer(id)
}
},
[timers, pauseTimer, startTimer]
)
return {
timers,
addTimer,
startTimer,
pauseTimer,
resetTimer,
deleteTimer,
toggleTimer,
}
}
/**
* Detects time mentions in text and returns durations in seconds.
*
* Supports patterns like:
* - "15 minutes", "15 min", "15m"
* - "2 hours", "2 hr", "2h"
* - "30 seconds", "30 sec", "30s"
*/
export function detectTimes(text: string): number[] {
const times: number[] = []
const seen = new Set<string>()
// Patterns for different time units
const patterns = [
{ regex: /(\d+)\s*(?:hours?|hrs?|h)\b/gi, multiplier: 3600 },
{ regex: /(\d+)\s*(?:minutes?|mins?|m)\b/gi, multiplier: 60 },
{ regex: /(\d+)\s*(?:seconds?|secs?|s)\b/gi, multiplier: 1 },
]
for (const { regex, multiplier } of patterns) {
let match
while ((match = regex.exec(text)) !== null) {
const value = parseInt(match[1], 10)
const seconds = value * multiplier
const key = `${match.index}-${value}-${multiplier}`
if (!seen.has(key) && seconds > 0) {
seen.add(key)
times.push(seconds)
}
}
}
// Sort by appearance in text (earlier matches first)
return times
}
/**
* Formats seconds as a human-readable time string.
* e.g., 90 -> "1:30", 3661 -> "1:01:01"
*/
export function formatTimerDisplay(seconds: number): string {
const hrs = Math.floor(seconds / 3600)
const mins = Math.floor((seconds % 3600) / 60)
const secs = seconds % 60
if (hrs > 0) {
return `${hrs}:${mins.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`
}
return `${mins}:${secs.toString().padStart(2, '0')}`
}
|