forked from CCExtractor/ccsync
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtasks-utils.ts
More file actions
341 lines (304 loc) · 8.21 KB
/
Copy pathtasks-utils.ts
File metadata and controls
341 lines (304 loc) · 8.21 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
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import { Task } from '@/components/utils/types';
import { url } from '@/components/utils/URLs';
import { format, parseISO } from 'date-fns';
import { toast } from 'react-toastify';
export type Props = {
email: string;
encryptionSecret: string;
origin: string;
UUID: string;
};
export const sortTasks = (tasks: Task[], order: 'asc' | 'desc') => {
return tasks.sort((a, b) => {
if (a.status < b.status) return order === 'asc' ? -1 : 1;
if (a.status > b.status) return order === 'asc' ? 1 : -1;
return 0;
});
};
export const markTaskAsCompleted = async (
email: string,
encryptionSecret: string,
UUID: string,
taskuuid: string
) => {
try {
const backendURL = url.backendURL + `complete-task`;
const response = await fetch(backendURL, {
method: 'POST',
body: JSON.stringify({
email: email,
encryptionSecret: encryptionSecret,
UUID: UUID,
taskuuid: taskuuid,
}),
});
if (!response) {
console.error('Failed to mark task as completed');
}
} catch (error) {
console.error('Error marking task as completed:', error);
}
};
export const bulkMarkTasksAsCompleted = async (
email: string,
encryptionSecret: string,
UUID: string,
taskUUIDs: string[]
) => {
try {
const backendURL = url.backendURL + `complete-tasks`;
const response = await fetch(backendURL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
email,
encryptionSecret,
UUID,
taskuuids: taskUUIDs,
}),
});
if (response.ok) {
toast.success(
`${taskUUIDs.length} ${
taskUUIDs.length === 1 ? 'task' : 'tasks'
} marked as completed.`
);
return true;
} else {
toast.error('Bulk completion failed!');
console.error('Failed bulk completion');
return false;
}
} catch (error) {
console.error('Error in bulk complete:', error);
toast.error('Bulk complete failed');
return false;
}
};
export const bulkMarkTasksAsDeleted = async (
email: string,
encryptionSecret: string,
UUID: string,
taskUUIDs: string[]
) => {
try {
const backendURL = url.backendURL + `delete-tasks`;
const response = await fetch(backendURL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
email,
encryptionSecret,
UUID,
taskuuids: taskUUIDs,
}),
});
if (response.ok) {
toast.success(
`${taskUUIDs.length} ${
taskUUIDs.length === 1 ? 'task' : 'tasks'
} deleted.`
);
return true;
} else {
toast.error('Bulk deletion failed!');
console.error('Failed bulk deletion');
return false;
}
} catch (error) {
console.error('Error in bulk delete:', error);
toast.error('Bulk delete failed');
return false;
}
};
export const markTaskAsDeleted = async (
email: string,
encryptionSecret: string,
UUID: string,
taskuuid: string
) => {
try {
const backendURL = url.backendURL + `delete-task`;
const response = await fetch(backendURL, {
method: 'POST',
body: JSON.stringify({
email: email,
encryptionSecret: encryptionSecret,
UUID: UUID,
taskuuid: taskuuid,
}),
});
if (!response) {
console.error('Failed to mark task as deleted');
}
} catch (error) {
console.error('Error marking task as deleted:', error);
}
};
export const getDisplayedPages = (totalPages: number, currentPage: number) => {
const pages: number[] = [];
if (totalPages <= 3) {
for (let i = 1; i <= totalPages; i++) {
pages.push(i);
}
} else {
if (currentPage === 1) {
pages.push(currentPage, currentPage + 1, currentPage + 2);
} else if (currentPage === totalPages) {
pages.push(currentPage - 2, currentPage - 1, currentPage);
} else {
pages.push(currentPage - 1, currentPage, currentPage + 1);
}
}
return pages;
};
export const formattedDate = (dateString: string) => {
try {
return format(parseISO(dateString), 'PPpp');
} catch (error) {
return dateString;
}
};
export const parseTaskwarriorDate = (dateString: string) => {
if (!dateString) return null;
const year = dateString.substring(0, 4);
const month = dateString.substring(4, 6);
const day = dateString.substring(6, 8);
const hour = dateString.substring(9, 11);
const min = dateString.substring(11, 13);
const sec = dateString.substring(13, 15);
const parsed = `${year}-${month}-${day}T${hour}:${min}:${sec}Z`;
const date = new Date(parsed);
return isNaN(date.getTime()) ? null : date;
};
export const isOverdue = (due?: string) => {
if (!due) return false;
const dueDate = parseTaskwarriorDate(due);
if (!dueDate) return false;
dueDate.setHours(0, 0, 0, 0);
const today = new Date();
today.setHours(0, 0, 0, 0);
return dueDate < today;
};
export const sortTasksById = (tasks: Task[], order: 'asc' | 'desc') => {
return tasks.sort((a, b) => {
if (order === 'asc') {
return a.id < b.id ? -1 : 1;
} else {
return b.id < a.id ? -1 : 1;
}
});
};
export const handleCopy = (text: string) => {
toast.success(`${text} copied to clipboard!`, {
position: 'bottom-left',
autoClose: 3000,
hideProgressBar: false,
closeOnClick: true,
pauseOnHover: true,
draggable: true,
progress: undefined,
});
};
export const handleDate = (v: string) => {
const date = new Date(v);
const isValid =
!isNaN(date.getTime()) && v === date.toISOString().split('T')[0];
if (!isValid) {
toast.error('Invalid Date Format. Please use the YYYY-MM-DD format.', {
position: 'bottom-left',
autoClose: 3000,
hideProgressBar: false,
closeOnClick: true,
pauseOnHover: true,
draggable: true,
progress: undefined,
});
return false;
}
return true;
};
export const getTimeSinceLastSync = (
lastSyncTimestamp: number | null
): string => {
if (!lastSyncTimestamp) {
return 'Never synced';
}
const now = Date.now();
const diffMs = now - lastSyncTimestamp;
const diffSeconds = Math.floor(diffMs / 1000);
const diffMinutes = Math.floor(diffSeconds / 60);
const diffHours = Math.floor(diffMinutes / 60);
const diffDays = Math.floor(diffHours / 24);
if (diffSeconds < 60) {
return `Last updated ${diffSeconds} second${
diffSeconds !== 1 ? 's' : ''
} ago`;
} else if (diffMinutes < 60) {
return `Last updated ${diffMinutes} minute${
diffMinutes !== 1 ? 's' : ''
} ago`;
} else if (diffHours < 24) {
return `Last updated ${diffHours} hour${diffHours !== 1 ? 's' : ''} ago`;
} else {
return `Last updated ${diffDays} day${diffDays !== 1 ? 's' : ''} ago`;
}
};
export const hashKey = (key: string, email: string): string => {
const str = key + email;
let hash = 0;
for (let i = 0; i < str.length; i++) {
const char = str.charCodeAt(i);
hash = (hash << 5) - hash + char;
hash = hash & hash;
}
return Math.abs(hash).toString(36);
};
/**
* Get the set of pinned task UUIDs from localStorage
*/
export const getPinnedTasks = (email: string): Set<string> => {
const hashedKey = hashKey('pinnedTasks', email);
const stored = localStorage.getItem(hashedKey);
if (!stored) return new Set();
try {
return new Set(JSON.parse(stored));
} catch {
return new Set();
}
};
/**
* Save the set of pinned task UUIDs to localStorage
*/
export const savePinnedTasks = (
email: string,
pinnedUuids: Set<string>
): void => {
const hashedKey = hashKey('pinnedTasks', email);
localStorage.setItem(hashedKey, JSON.stringify([...pinnedUuids]));
};
/**
* Toggle the pinned status of a task
* Returns the new pinned state
*/
export const togglePinnedTask = (email: string, taskUuid: string): boolean => {
const pinnedTasks = getPinnedTasks(email);
const isPinned = pinnedTasks.has(taskUuid);
if (isPinned) {
pinnedTasks.delete(taskUuid);
} else {
pinnedTasks.add(taskUuid);
}
savePinnedTasks(email, pinnedTasks);
return !isPinned;
};
/**
* Check if a task is pinned
*/
export const isTaskPinned = (email: string, taskUuid: string): boolean => {
return getPinnedTasks(email).has(taskUuid);
};