-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
210 lines (171 loc) · 5.34 KB
/
app.js
File metadata and controls
210 lines (171 loc) · 5.34 KB
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
const STORAGE_KEY = 'timerState';
const MODES = {
IDLE: 'idle',
WORKING: 'working',
BREAK: 'break',
};
const DEFAULT_STATE = {
mode: MODES.IDLE,
workStartedAt: null,
breakStartedAt: null,
breakDurationMs: null,
};
let intervalId = null;
let state = loadState();
const timerDisplay = document.getElementById('timer');
const startStopBtn = document.getElementById('startStopBtn');
const statusDisplay = document.getElementById('status');
function formatTime(ms) {
const totalSeconds = Math.max(0, Math.floor(ms / 1000));
const hours = Math.floor(totalSeconds / 3600);
const minutes = Math.floor((totalSeconds % 3600) / 60);
const seconds = totalSeconds % 60;
if (hours > 0) {
return `${String(hours).padStart(2, '0')}:${String(minutes).padStart(2, '0')}:${String(seconds).padStart(2, '0')}`;
}
return `${String(minutes).padStart(2, '0')}:${String(seconds).padStart(2, '0')}`;
}
function calculateBreakTime(workTimeMs) {
const workMinutes = workTimeMs / 60000;
if (workMinutes <= 25) {
return 5 * 60000;
}
if (workMinutes <= 50) {
return 8 * 60000;
}
if (workMinutes <= 90) {
return 10 * 60000;
}
return 15 * 60000;
}
function getBreakRemainingMs() {
if (state.mode !== MODES.BREAK || !state.breakStartedAt || !state.breakDurationMs) {
return 0;
}
return state.breakDurationMs - (Date.now() - state.breakStartedAt);
}
function loadState() {
try {
const rawState = localStorage.getItem(STORAGE_KEY);
if (!rawState) {
return { ...DEFAULT_STATE };
}
const parsedState = JSON.parse(rawState);
if (!parsedState || typeof parsedState !== 'object') {
return { ...DEFAULT_STATE };
}
const mode = Object.values(MODES).includes(parsedState.mode) ? parsedState.mode : MODES.IDLE;
return {
mode,
workStartedAt: Number.isFinite(parsedState.workStartedAt) ? parsedState.workStartedAt : null,
breakStartedAt: Number.isFinite(parsedState.breakStartedAt) ? parsedState.breakStartedAt : null,
breakDurationMs: Number.isFinite(parsedState.breakDurationMs) ? parsedState.breakDurationMs : null,
};
} catch (error) {
return { ...DEFAULT_STATE };
}
}
function saveState() {
try {
localStorage.setItem(STORAGE_KEY, JSON.stringify(state));
} catch (error) {
// Ignore storage failures so the timer still works without persistence.
}
}
function clearTicker() {
if (intervalId !== null) {
clearInterval(intervalId);
intervalId = null;
}
}
function startTicker() {
clearTicker();
intervalId = window.setInterval(tick, 1000);
}
function resetToIdle(statusMessage = 'Ready to start a new session?') {
clearTicker();
state = { ...DEFAULT_STATE };
saveState();
render(statusMessage);
}
function startWorkSession() {
state = {
mode: MODES.WORKING,
workStartedAt: Date.now(),
breakStartedAt: null,
breakDurationMs: null,
};
saveState();
render();
startTicker();
}
function startBreakSession() {
const workTimeMs = Date.now() - state.workStartedAt;
state = {
mode: MODES.BREAK,
workStartedAt: null,
breakStartedAt: Date.now(),
breakDurationMs: calculateBreakTime(workTimeMs),
};
saveState();
render();
startTicker();
}
function tick() {
if (state.mode === MODES.BREAK && getBreakRemainingMs() <= 0) {
resetToIdle('Break over! Start a new session?');
return;
}
render();
saveState();
}
function render(customStatus) {
if (state.mode === MODES.WORKING && state.workStartedAt) {
timerDisplay.textContent = formatTime(Date.now() - state.workStartedAt);
startStopBtn.textContent = 'Stop Work';
statusDisplay.textContent = customStatus || 'Working...';
return;
}
if (state.mode === MODES.BREAK && state.breakDurationMs) {
const remainingMs = getBreakRemainingMs();
timerDisplay.textContent = formatTime(remainingMs);
startStopBtn.textContent = 'Stop Break';
statusDisplay.textContent = customStatus || `Break time: ${state.breakDurationMs / 60000} minutes`;
return;
}
timerDisplay.textContent = '00:00';
startStopBtn.textContent = 'Start Work';
statusDisplay.textContent = customStatus || 'Ready to start a new session?';
}
function resumeIfNeeded() {
if (state.mode === MODES.WORKING && state.workStartedAt) {
render();
startTicker();
return;
}
if (state.mode === MODES.BREAK && state.breakStartedAt && state.breakDurationMs) {
if (getBreakRemainingMs() <= 0) {
resetToIdle('Break over! Start a new session?');
return;
}
render();
startTicker();
return;
}
resetToIdle();
}
startStopBtn.addEventListener('click', () => {
if (state.mode === MODES.BREAK) {
if (!window.confirm('Are you sure you want to stop your break early? It is important to take sufficient breaks!')) {
return;
}
resetToIdle('Break stopped. Ready to start a new session?');
return;
}
if (state.mode === MODES.WORKING) {
startBreakSession();
return;
}
startWorkSession();
});
resumeIfNeeded();