-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog.js
More file actions
172 lines (141 loc) · 5.67 KB
/
log.js
File metadata and controls
172 lines (141 loc) · 5.67 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
const LOGS_TO_DISPLAY = 5; // Number of logs to display in the recent log section
const logItems = [];
let logForm;
let activityInput;
let timeInput;
let ticketInput;
let durationInput;
let previousLogsContainer;
document.addEventListener("DOMContentLoaded", () => {
logForm = document.getElementById("logForm");
activityInput = document.getElementById("form-activity");
timeInput = document.getElementById("form-time");
ticketInput = document.getElementById("form-ticket");
durationInput = document.getElementById("form-duration");
previousLogsContainer = document.getElementById("previous");
// Set default time to the current time, down to the minute
setTimeNow();
// Fetch the 10 most recent logs
chrome.storage.local.get({ logs: [] }, (data) => {
const logs = data.logs;
if (logs.length > 0) {
// Get the last 10 logs (or fewer if not enough)
const recentLogs = logs.slice(LOGS_TO_DISPLAY * -1).reverse(); // Reverse to get most recent first
spawnLogs(recentLogs);
prefillDuration(logs); // Pre-fill the duration field based on the most recent log
}
});
// When form is submitted
logForm.addEventListener("submit", (e) => {
e.preventDefault();
const activity = activityInput.value.trim();
const ticket = ticketInput.value.trim() || "other";
// Convert local time to UTC
const localTime = new Date(timeInput.value);
const utcTime = new Date(localTime.getTime() - localTime.getTimezoneOffset() * 60000).toISOString();
const duration = durationInput.value.trim();
// Updated regex to match multiple unit-value pairs
const durationRegex = /(\d+)([hHmMwy])/g;
let match;
let durationInMinutes = 0;
// Parse each unit-value pair and calculate the total duration in minutes
while ((match = durationRegex.exec(duration)) !== null) {
const value = parseInt(match[1], 10);
const unit = match[2].toLowerCase();
durationInMinutes += stringToMinutes(value, unit);
}
if (durationInMinutes === 0 || isNaN(durationInMinutes)) {
alert("Invalid duration format. Use '1h', '30m', or combinations like '1h20m'.");
return;
}
chrome.storage.local.get({ logs: [], tickets: [] }, (data) => {
const logs = data.logs;
const log = { activity, time: utcTime, ticket, duration: durationInMinutes };
const tickets = new Set(data.tickets || []);
tickets.add(ticket);
chrome.storage.local.set({
logs: [...logs, log],
tickets: [...tickets]
}, () => {
appendLog(log, true);
logForm.reset();
setTimeNow(); // Reset time to now after submission
});
});
});
});
// Function to spawn logs
function spawnLogs(logs) {
console.log("Spawning logs: ", logs);
const template = document.getElementById("logTemplate");
template.style.display = "none";
logs.forEach((log) => {
appendLog(log);
});
}
function appendLog(log, isNew = false) {
const index = logItems.length;
const template = document.getElementById("logTemplate");
const logClone = template.cloneNode(true);
logClone.style.display = "block";
logClone.id = `log_${index}`;
const fields = logClone.childNodes;
fields[1].value = log.ticket;
fields[3].value = log.activity;
let timeFields = fields[5].childNodes;
timeFields[1].value = minutesToString(log.duration);
// Convert UTC time to local time for display
const localTime = new Date(log.time).toLocaleString("sv-SE", {
timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone,
hour12: false,
year: "numeric",
month: "2-digit",
day: "2-digit",
hour: "2-digit",
minute: "2-digit"
});
timeFields[3].value = localTime;
timeFields[5].addEventListener("click", () => {
useLog(index);
});
logItems.push(log);
if (isNew) {
previousLogsContainer.insertBefore(logClone, previousLogsContainer.firstChild);
} else {
previousLogsContainer.appendChild(logClone);
}
}
// Fill the log into the logForm
function useLog(index) {
console.log("Clicked ", index, logItems[index]);
const log = logItems[index];
activityInput.value = log.activity;
ticketInput.value = log.ticket;
durationInput.value = minutesToString(log.duration);
}
// Function to pre-fill the duration field
function prefillDuration(logs) {
const today = new Date().toISOString().slice(0, 10); // Get today's date in UTC
const todayLogs = logs.filter(log => log.time.startsWith(today)); // Filter logs for today
if (todayLogs.length > 0) {
// Get the most recent log for today
const lastLog = todayLogs.reduce((latest, log) => {
return new Date(log.time) > new Date(latest.time) ? log : latest;
});
// Calculate the time difference in minutes
const now = new Date();
const lastLogTime = new Date(lastLog.time);
const diffInMinutes = Math.floor((now - lastLogTime) / 60000); // Convert milliseconds to minutes
// Pre-fill the duration field
if (diffInMinutes > 0) {
durationInput.value = minutesToString(diffInMinutes); // Set the duration in minutes
}
}
}
function setTimeNow() {
const now = new Date();
const localTime = new Date(now.getTime() - now.getTimezoneOffset() * 60000) // Adjust for timezone offset
.toISOString()
.slice(0, 16); // Format as YYYY-MM-DDTHH:mm
timeInput.value = localTime;
}