-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
261 lines (238 loc) · 11.8 KB
/
index.html
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="./styles.css" />
</head>
<body>
<div id="buttons">
<button data-type="W" class="type-W" title="0:00">W</button>
<!--
<button data-type="LP" class="type-LP" title="0:00">LP</button>
<button data-type="DS" class="type-DS" title="0:00">DS</button>
-->
<button data-type="BR" class="type-BR" title="0:00">BR</button>
<button data-type="NEXT" class="type-NEXT" title="0:00">NEXT</button>
<button data-type="empty" class="type-empty" title="0:00">PAUSE</button>
</div>
<div id="days">
</div>
<script>
/*
localStorage format: {
oldestEntry: '2023-09-01',
'2023-09-01': {
datestamp: '2023-09-01',
startTs: 1693605355612,
blocks: [
{type: 'W', duration: 3600000},
{type: 'LP', duration: 3600000},
...
],
},
'2023-09-04': {
datestamp: '2023-09-04',
startTs: 1693864678754,
blocks: [
{type: 'W', duration: 3600000},
{type: 'LP', duration: 3600000},
...
],
},
}
*/
const daysOfWeek = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
const widthInMs = (18 - 8) * 60 * 60 * 1000;
(async () => {
let oldestEntry = localStorage.oldestEntry;
const days = [];
if (oldestEntry) {
let timestamp = Date.now();
let date;
do {
date = new Date(timestamp);
let datestamp = getDatestamp(date);
if (localStorage[datestamp]) {
days.unshift(JSON.parse(localStorage[datestamp]));
}
timestamp -= 24 * 60 * 60 * 1000;
} while (date > new Date(oldestEntry));
}
try {
const imports = await (await fetch('/imports.json')).json();
Object.entries(imports).forEach(([datestamp, data]) => {
if (!localStorage[datestamp]) {
localStorage[datestamp] = JSON.stringify(data);
if (
!localStorage.oldestEntry ||
new Date(localStorage.oldestEntry) > new Date(datestamp)
) {
localStorage.oldestEntry = datestamp;
}
}
});
} catch(e) {}
days.forEach(dayEntryData => createDayElement(dayEntryData));
createDayElement({
datestamp: '2222-12-22',
startTs: 7983043200000,
blocks: [['BR', 30], ['LP', 60], ['W', 120], ['BR', 30], ['DS', 30], ['W', 60], ['LP', 30], ['W', 60], ['BR', 30], ['DS', 30]]
.map(([type, minutes]) => ({ type, duration: minutes * 60 * 1000 })),
});
document.querySelectorAll('button[data-type]').forEach(button => {
button.addEventListener('click', () => switchToActivity(button.dataset.type));
});
let currentActivityBlock = {
type: 'empty',
start: Date.now(),
};
let initialTodayStamp = getDatestamp(new Date());
let todayData = days.find(dayData => dayData.datestamp === initialTodayStamp);
if (todayData) {
const lastBlock = todayData.blocks[todayData.blocks.length - 1];
const lastBlockStart = todayData.startTs + todayData.blocks.slice(0, -1).map(b => b.duration).reduce((a, b) => a + b, 0);
console.log({
lastBlock,
lastBlockStart,
lastBlockEnd: lastBlockStart + lastBlock.duration,
now: Date.now(),
diff: Date.now() - (lastBlockStart + lastBlock.duration),
});
if (Date.now() - (lastBlockStart + lastBlock.duration) < 60_000) {
currentActivityBlock = {
type: lastBlock.type,
start: lastBlockStart,
};
}
}
indicateActiveButton();
setInterval(logActivity, 30000);
logActivity();
function createDiv(idOrClass) {
const div = document.createElement('div');
if (idOrClass[0] === '#') {
div.id = idOrClass.substr(1);
} else {
div.classList.add(idOrClass.substr(1));
}
return div;
}
function leadZero(n) { return n < 10 ? `0${n}` : n; }
function getDatestamp(date) {
let year = date.getFullYear(), month = date.getMonth() + 1, day = date.getDate();
return `${year}-${leadZero(month)}-${leadZero(day)}`;
}
function indicateActiveButton() {
document.querySelectorAll('button').forEach(b => b.style.color = '');
document.querySelector('button.type-' + currentActivityBlock.type).style.color = 'gray';
}
function switchToActivity(type) {
currentActivityBlock = {
type,
start: Date.now(),
};
indicateActiveButton();
const todayStamp = getDatestamp(new Date());
if (!localStorage.oldestEntry) {
localStorage.oldestEntry = todayStamp;
}
if (localStorage[todayStamp]) {
logActivity();
} else {
localStorage[todayStamp] = JSON.stringify({
datestamp: todayStamp,
startTs: Date.now(),
blocks: [{ type, duration: 0 }],
});
}
}
function logActivity() {
const now = new Date();
if (now.getHours() < 8 || now.getHours() >= 18) return;
const todayStamp = getDatestamp(now);
if (!localStorage[todayStamp]) return;
const todayData = JSON.parse(localStorage[todayStamp]);
const lastBlock = todayData.blocks[todayData.blocks.length - 1];
if (lastBlock) {
if (lastBlock.type === currentActivityBlock.type) {
lastBlock.duration = Date.now() - currentActivityBlock.start;
} else {
todayData.blocks.push({
type: currentActivityBlock.type,
duration: Date.now() - currentActivityBlock.start,
});
}
localStorage[todayStamp] = JSON.stringify(todayData);
}
let todayEl = document.querySelector('#day-' + todayStamp);
if (!todayEl) {
todayEl = createDayElement(todayData);
}
if (todayEl) {
const markedAsToday = document.querySelector('.today');
if (todayEl !== markedAsToday) {
todayEl.classList.add('today');
markedAsToday?.classList.remove('today');
}
}
let lastBlockEl = document.querySelector('#day-' + todayStamp + ' .block:last-child');
if (!lastBlockEl.classList.contains('type-' + currentActivityBlock.type)) {
const newBlockEl = createDiv('.block');
newBlockEl.classList.add('type-' + currentActivityBlock.type);
lastBlockEl.parentElement.appendChild(newBlockEl);
lastBlockEl = newBlockEl;
}
lastBlockEl.style.width = ((Date.now() - currentActivityBlock.start) / widthInMs * 100) + '%';
const durationsByType = {};
todayData.blocks.forEach(block => {
durationsByType[block.type] = durationsByType[block.type] || 0;
durationsByType[block.type] += block.duration;
});
Object.entries(durationsByType).map(([type, totalDuration]) => {
const hours = totalDuration / 3_600_000 | 0;
const minutes = (totalDuration / 60_000 | 0) % 60;
document.querySelector('button.type-' + type).title = `${hours}:${leadZero(minutes)}`;
});
}
function createDayElement(dayEntryData) {
if (dayEntryData.blocks.length === 1 && dayEntryData.blocks[0].duration === 0) {
return null;
}
const dayEntryEl = createDiv('.day');
dayEntryEl.id = 'day-' + dayEntryData.datestamp;
const date = new Date(dayEntryData.datestamp);
dayEntryEl.textContent = dayEntryData.datestamp === '2222-12-22' ?
'Planned' :
daysOfWeek[date.getDay()] + ' ' + dayEntryData.datestamp;
const line = createDiv('.line');
dayEntryEl.appendChild(line);
const notches = createDiv('.notches');
Array(40).fill(0).forEach(() => notches.appendChild(createDiv('.notch')));
dayEntryEl.appendChild(notches);
const introBlock = createDiv('.block');
introBlock.classList.add('type-empty');
introBlock.style.width = ((dayEntryData.startTs - new Date(dayEntryData.datestamp + ' 08:00:00')) / widthInMs * 100) + '%';
line.appendChild(introBlock);
dayEntryData.blocks.forEach(blockData => {
const blockEl = createDiv('.block');
blockEl.classList.add('type-' + blockData.type);
blockEl.style.width = (blockData.duration / widthInMs * 100) + '%';
line.appendChild(blockEl);
});
const timelineEl = createDiv('.timeline');
Array(11).fill(0).forEach((_, i) => {
const hour = i + 8;
const hourEl = createDiv('.hour');
hourEl.textContent = hour;
timelineEl.appendChild(hourEl);
});
dayEntryEl.appendChild(timelineEl);
document.querySelector('#days').insertBefore(
dayEntryEl,
document.querySelector('#day-2222-12-22')
);
return dayEntryEl;
}
})();
</script>
</body>
</html>