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 | 2x 93x 6x 1x 27x 2x 20x 20x 20x 20x 2x 74x 37x 37x 37x 37x 37x 37x 2x 2x 2x 2x 2x 1x 1x 5x 2x 7x 5x 2x 4x 4x 4x 4x 4x 2x 2x | import { getNormalizedNumericSetting } from '@shared/settingsValidation'
import type { AppSettings, TimerPhase, TimerSession, TimerSnapshot, TimerState } from '@shared/types'
export interface StartTimerInput {
now: number
phase: TimerPhase
taskId?: string | null
sessionId?: string | null
settings: AppSettings
}
export interface RestoreTimerInput {
session: TimerSession
now: number
}
export interface RestoreTimerResult {
action: 'resume' | 'needs-confirmation'
state: TimerState
}
export const phaseDurationMs = (phase: TimerPhase, settings: AppSettings): number => {
if (phase === 'focus') return getNormalizedNumericSetting(settings, 'focusMinutes') * 60_000
if (phase === 'shortBreak') return getNormalizedNumericSetting(settings, 'shortBreakMinutes') * 60_000
return getNormalizedNumericSetting(settings, 'longBreakMinutes') * 60_000
}
export const createIdleTimer = (settings: AppSettings, now: number): TimerState => ({
status: 'idle',
phase: 'focus',
taskId: null,
startedAt: null,
targetEndAt: null,
durationMs: phaseDurationMs('focus', settings),
remainingMs: phaseDurationMs('focus', settings),
focusCount: 0,
unboundFocusCount: 0,
lastFocusTaskId: null,
sessionId: null,
updatedAt: now
})
export const startTimer = (state: TimerState, input: StartTimerInput): TimerState => {
const durationMs = phaseDurationMs(input.phase, input.settings)
const nextTaskId = input.taskId ?? null
const isTaskBoundFocus = input.phase === 'focus' && nextTaskId !== null
return {
...state,
status: 'running',
phase: input.phase,
taskId: nextTaskId,
sessionId: input.sessionId ?? null,
startedAt: input.now,
targetEndAt: input.now + durationMs,
durationMs,
remainingMs: durationMs,
unboundFocusCount: isTaskBoundFocus ? 0 : state.unboundFocusCount,
updatedAt: input.now
}
}
export const deriveTimerSnapshot = (state: TimerState, now: number): TimerSnapshot => {
if (state.status === 'running' && state.startedAt !== null && state.targetEndAt !== null) {
const remainingMs = Math.max(0, state.targetEndAt - now)
const elapsedMs = Math.min(state.durationMs, Math.max(0, now - state.startedAt))
const progress = state.durationMs === 0 ? 1 : elapsedMs / state.durationMs
return {
...state,
status: remainingMs === 0 ? 'completed' : state.status,
remainingMs,
elapsedMs,
progress
}
}
const elapsedMs = Math.max(0, state.durationMs - state.remainingMs)
return {
...state,
elapsedMs,
progress: state.durationMs === 0 ? 0 : elapsedMs / state.durationMs
}
}
export const pauseTimer = (state: TimerState, now: number): TimerState => {
Iif (state.status !== 'running') return state
const snapshot = deriveTimerSnapshot(state, now)
return {
...state,
status: 'paused',
remainingMs: snapshot.remainingMs,
startedAt: null,
targetEndAt: null,
updatedAt: now
}
}
export const resumeTimer = (state: TimerState, now: number): TimerState => {
Iif (state.status !== 'paused') return state
return {
...state,
status: 'running',
startedAt: now,
targetEndAt: now + state.remainingMs,
updatedAt: now
}
}
export const completeTimer = (state: TimerState, now: number): TimerState => ({
...state,
status: 'completed',
remainingMs: 0,
targetEndAt: now,
focusCount: state.phase === 'focus' ? state.focusCount + 1 : state.focusCount,
unboundFocusCount:
state.phase === 'focus' && state.taskId === null ? state.unboundFocusCount + 1 : state.unboundFocusCount,
lastFocusTaskId: state.phase === 'focus' ? state.taskId : state.lastFocusTaskId,
updatedAt: now
})
export const getNextPhase = (phase: TimerPhase, completedFocusCount: number, settings: AppSettings): TimerPhase => {
if (phase !== 'focus') return 'focus'
return completedFocusCount % getNormalizedNumericSetting(settings, 'longBreakInterval') === 0 ? 'longBreak' : 'shortBreak'
}
export const restoreTimerFromSession = (input: RestoreTimerInput): RestoreTimerResult => {
const startedAt = new Date(input.session.startedAt).getTime()
const targetEndAt = startedAt + input.session.durationMs
const remainingMs = Math.max(0, targetEndAt - input.now)
const baseState: TimerState = {
status: remainingMs === 0 ? 'completed' : 'running',
phase: input.session.phase,
taskId: input.session.taskId,
sessionId: input.session.id,
startedAt,
targetEndAt,
durationMs: input.session.durationMs,
remainingMs,
focusCount: 0,
unboundFocusCount: 0,
lastFocusTaskId: null,
updatedAt: input.now
}
if (remainingMs === 0) {
return {
action: 'needs-confirmation',
state: baseState
}
}
return {
action: 'resume',
state: baseState
}
}
|