-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
983 lines (830 loc) · 35.3 KB
/
app.js
File metadata and controls
983 lines (830 loc) · 35.3 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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
// Daily Log Application with Flask Backend Integration
class DailyLogApp {
constructor() {
// Configuration
this.API_BASE_URL = 'http://localhost:3001/api';
// Initialize data structures
this.logs = [];
this.diaryEntries = [];
this.tasks = [];
this.currentTab = 'logs';
this.nextLogId = 1;
this.nextTaskId = 1;
this.nextDiaryId = 1;
this.apiConnected = false;
}
async init() {
// Check API connection
await this.checkApiConnection();
this.setupEventListeners();
this.updateCharCounter();
await this.loadData();
this.renderLogs();
this.updateLogsStats();
this.updateLastSync();
this.showEmptyStates();
}
async checkApiConnection() {
try {
const response = await fetch(`${this.API_BASE_URL}/status`);
if (response.ok) {
this.apiConnected = true;
this.updateApiStatus('connected', '✅ API Connected');
} else {
throw new Error('API not responding');
}
} catch (error) {
this.apiConnected = false;
this.updateApiStatus('error', '❌ Backend Not Available');
console.error('API connection failed:', error);
}
}
// Data Persistence using Flask Backend
async loadData() {
try {
// Load logs
const logsResponse = await fetch(`${this.API_BASE_URL}/logs`);
if (logsResponse.ok) {
const logsData = await logsResponse.json();
this.logs = logsData.logs || [];
this.nextLogId = logsData.nextId || 1;
}
// Load diary entries
const diaryResponse = await fetch(`${this.API_BASE_URL}/diary`);
if (diaryResponse.ok) {
const diaryData = await diaryResponse.json();
this.diaryEntries = diaryData.entries || [];
this.nextDiaryId = diaryData.nextId || 1;
}
// Load tasks
const tasksResponse = await fetch(`${this.API_BASE_URL}/tasks`);
if (tasksResponse.ok) {
const tasksData = await tasksResponse.json();
this.tasks = tasksData.tasks || [];
this.nextTaskId = tasksData.nextId || 1;
}
} catch (error) {
console.error('Error loading data:', error);
this.showToast('Failed to load data from server', 'error');
}
}
saveToStorage() {
// Data is automatically saved on the server for each operation
this.updateLastSync();
}
loadFromStorage() {
// No longer needed - data is loaded from server
}
updateLastSync() {
const lastSyncElement = document.getElementById('last-sync');
if (lastSyncElement) {
lastSyncElement.textContent = `Last updated: ${new Date().toLocaleTimeString()}`;
}
}
updateApiStatus(type, message) {
const statusIndicator = document.getElementById('api-status-indicator');
if (statusIndicator) {
statusIndicator.className = `status-indicator ${type}`;
statusIndicator.textContent = message;
}
}
setupEventListeners() {
// Tab navigation
document.querySelectorAll('.tab-btn').forEach(btn => {
btn.addEventListener('click', (e) => {
e.preventDefault();
const tabName = e.currentTarget.getAttribute('data-tab');
this.switchTab(tabName);
});
});
// Log input character counter
const logInput = document.getElementById('log-input');
if (logInput) {
logInput.addEventListener('input', () => this.updateCharCounter());
}
// Add log button
const addLogBtn = document.getElementById('add-log-btn');
if (addLogBtn) {
addLogBtn.addEventListener('click', async (e) => {
e.preventDefault();
await this.addLog();
});
}
// Diary generation
const generateDiaryBtn = document.getElementById('generate-diary-btn');
if (generateDiaryBtn) {
generateDiaryBtn.addEventListener('click', async (e) => {
e.preventDefault();
await this.generateDiary();
});
}
const regenerateDiaryBtn = document.getElementById('regenerate-diary-btn');
if (regenerateDiaryBtn) {
regenerateDiaryBtn.addEventListener('click', async (e) => {
e.preventDefault();
await this.generateDiary(true);
});
}
const updateDiaryBtn = document.getElementById('update-diary-btn');
if (updateDiaryBtn) {
updateDiaryBtn.addEventListener('click', async (e) => {
e.preventDefault();
await this.generateDiary(false, true);
});
}
const exportDiaryBtn = document.getElementById('export-diary-btn');
if (exportDiaryBtn) {
exportDiaryBtn.addEventListener('click', (e) => {
e.preventDefault();
this.exportDiary();
});
}
// Diary history controls
const diaryHistoryBtn = document.getElementById('diary-history-btn');
if (diaryHistoryBtn) {
diaryHistoryBtn.addEventListener('click', (e) => {
e.preventDefault();
this.showDiaryHistory();
});
}
const closeHistoryBtn = document.getElementById('close-history-btn');
if (closeHistoryBtn) {
closeHistoryBtn.addEventListener('click', (e) => {
e.preventDefault();
this.hideDiaryHistory();
});
}
// Task extraction
const extractTasksBtn = document.getElementById('extract-tasks-btn');
if (extractTasksBtn) {
extractTasksBtn.addEventListener('click', async (e) => {
e.preventDefault();
await this.extractTasks();
});
}
// Task filtering
const taskFilter = document.getElementById('task-filter');
if (taskFilter) {
taskFilter.addEventListener('change', (e) => {
this.filterTasks(e.target.value);
});
}
// Manual task addition
const addManualTaskBtn = document.getElementById('add-manual-task-btn');
if (addManualTaskBtn) {
addManualTaskBtn.addEventListener('click', (e) => {
e.preventDefault();
this.addManualTask();
});
}
// Enter key handling for inputs
if (logInput) {
logInput.addEventListener('keydown', async (e) => {
if (e.key === 'Enter' && e.ctrlKey) {
e.preventDefault();
await this.addLog();
}
});
}
const manualTaskInput = document.getElementById('manual-task-input');
if (manualTaskInput) {
manualTaskInput.addEventListener('keydown', (e) => {
if (e.key === 'Enter') {
e.preventDefault();
this.addManualTask();
}
});
}
}
switchTab(tabName) {
if (!tabName) return;
console.log('Switching to tab:', tabName); // Debug log
// Update tab buttons
document.querySelectorAll('.tab-btn').forEach(btn => {
btn.classList.remove('active');
});
const activeBtn = document.querySelector(`[data-tab="${tabName}"]`);
if (activeBtn) {
activeBtn.classList.add('active');
}
// Update tab content - Fixed implementation
document.querySelectorAll('.tab-content').forEach(content => {
content.classList.remove('active');
});
const activeContent = document.getElementById(`${tabName}-section`);
if (activeContent) {
activeContent.classList.add('active');
}
this.currentTab = tabName;
// Update content based on current tab
if (tabName === 'logs') {
this.renderLogs();
this.updateLogsStats();
} else if (tabName === 'diary') {
this.updateDiarySection();
} else if (tabName === 'tasks') {
this.updateTasksSection();
}
}
generateTimestamp() {
const now = new Date();
const hours = String(now.getHours()).padStart(2, '0');
const minutes = String(now.getMinutes()).padStart(2, '0');
const seconds = String(now.getSeconds()).padStart(2, '0');
const day = String(now.getDate()).padStart(2, '0');
const month = String(now.getMonth() + 1).padStart(2, '0');
const year = String(now.getFullYear());
return `${hours}${minutes}${seconds}${day}${month}${year}`;
}
formatTimestamp(timestamp) {
if (!timestamp || timestamp.length !== 12) return 'Invalid timestamp';
const hours = timestamp.substring(0, 2);
const minutes = timestamp.substring(2, 4);
const seconds = timestamp.substring(4, 6);
const day = timestamp.substring(6, 8);
const month = timestamp.substring(8, 10);
const year = timestamp.substring(10, 12);
return `${day}/${month}/20${year} at ${hours}:${minutes}:${seconds}`;
}
updateCharCounter() {
const logInput = document.getElementById('log-input');
const counter = document.getElementById('char-counter');
if (logInput && counter) {
const count = logInput.value.length;
counter.textContent = `${count} characters`;
}
}
async addLog() {
const logInput = document.getElementById('log-input');
const addLogBtn = document.getElementById('add-log-btn');
if (!logInput) return;
const content = logInput.value.trim();
if (!content) {
this.showToast('Please enter some content for your log', 'error');
return;
}
// Show loading state
if (addLogBtn) {
addLogBtn.classList.add('loading');
addLogBtn.disabled = true;
}
try {
const response = await fetch(`${this.API_BASE_URL}/logs`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ content: content })
});
if (!response.ok) {
throw new Error('Failed to save log');
}
const result = await response.json();
// Add the new log to local state
this.logs.push(result.log);
this.nextLogId = result.log.id + 1;
logInput.value = '';
this.updateCharCounter();
this.renderLogs();
this.updateLogsStats();
this.showToast('Log entry added successfully!', 'success');
// Check if automatic generation is enabled
const autoDiary = document.getElementById('auto-diary');
const autoTasks = document.getElementById('auto-tasks');
if (autoDiary && autoDiary.checked) {
await this.generateDiary(false, true); // Incremental update
}
if (autoTasks && autoTasks.checked) {
await this.extractTasks(true); // Extract only new tasks
}
} catch (error) {
console.error('Error adding log:', error);
this.showToast('Failed to add log entry', 'error');
} finally {
if (addLogBtn) {
addLogBtn.classList.remove('loading');
addLogBtn.disabled = false;
}
}
}
async editLog(logId) {
const log = this.logs.find(l => l.id === logId);
if (!log) return;
const newContent = prompt('Edit your log:', log.content);
if (newContent !== null && newContent.trim() !== '') {
log.content = newContent.trim();
this.renderLogs();
this.saveToStorage();
this.showToast('Log updated successfully!', 'success');
// Update diary if it exists
await this.handleLogChange();
}
}
async deleteLog(logId) {
if (confirm('Are you sure you want to delete this log?')) {
try {
if (this.apiConnected) {
// Call backend API to delete
const response = await fetch(`${this.API_BASE_URL}/logs/${logId}`, {
method: 'DELETE'
});
if (!response.ok) {
throw new Error('Failed to delete log from server');
}
}
// Update local data
this.logs = this.logs.filter(l => l.id !== logId);
this.renderLogs();
this.updateLogsStats();
this.saveToStorage();
this.showToast('Log deleted successfully!', 'success');
// Update diary if it exists
await this.handleLogChange();
} catch (error) {
console.error('Error deleting log:', error);
this.showToast(`Failed to delete log: ${error.message}`, 'error');
}
}
}
async handleLogChange() {
// If diary exists for today, offer to update it
const today = new Date().toISOString().split('T')[0];
const existingDiary = this.diaryEntries.find(entry => entry.date === today);
if (existingDiary) {
const updateBtn = document.getElementById('update-diary-btn');
if (updateBtn) {
updateBtn.classList.remove('hidden');
}
}
}
renderLogs() {
const logsList = document.getElementById('logs-list');
if (!logsList) return;
if (this.logs.length === 0) {
logsList.innerHTML = `
<div class="empty-state">
<div class="empty-state-icon">📝</div>
<h3>No logs yet</h3>
<p>Start by adding your first log entry above</p>
</div>
`;
return;
}
// Sort logs by timestamp (newest first)
const sortedLogs = [...this.logs].sort((a, b) => b.timestamp.localeCompare(a.timestamp));
logsList.innerHTML = sortedLogs.map(log => `
<div class="log-entry">
<div class="log-entry-header">
<span class="log-timestamp">${this.formatTimestamp(log.timestamp)}</span>
<div class="log-actions">
<button class="log-action-btn" onclick="app.editLog(${log.id})" title="Edit log">
✏️
</button>
<button class="log-action-btn" onclick="app.deleteLog(${log.id})" title="Delete log">
🗑️
</button>
</div>
</div>
<div class="log-content">${this.escapeHtml(log.content)}</div>
</div>
`).join('');
}
updateLogsStats() {
const logsCount = document.getElementById('logs-count');
if (logsCount) {
const count = this.logs.length;
logsCount.textContent = `${count} log${count !== 1 ? 's' : ''} today`;
}
}
async generateDiary(isRegenerate = false, isIncremental = false) {
if (this.logs.length === 0) {
this.showToast('Add some logs first to generate a diary entry', 'error');
return;
}
if (!this.apiConnected) {
this.showToast('Backend server not available', 'error');
return;
}
// Show loading state
const loadingElement = document.getElementById('diary-loading');
const loadingText = document.getElementById('diary-loading-text');
const contentElement = document.getElementById('diary-content');
const emptyStateElement = document.getElementById('diary-empty-state');
if (loadingElement) loadingElement.classList.remove('hidden');
if (contentElement) contentElement.classList.add('hidden');
if (emptyStateElement) emptyStateElement.classList.add('hidden');
const generateBtn = document.getElementById('generate-diary-btn');
const regenerateBtn = document.getElementById('regenerate-diary-btn');
const updateBtn = document.getElementById('update-diary-btn');
if (generateBtn) generateBtn.disabled = true;
if (regenerateBtn) regenerateBtn.disabled = true;
if (updateBtn) updateBtn.disabled = true;
if (loadingText) {
loadingText.textContent = isIncremental ?
'Updating diary with new logs...' :
'Analyzing your logs and generating diary entry...';
}
try {
const today = new Date().toISOString().split('T')[0];
const response = await fetch(`${this.API_BASE_URL}/generate-diary`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ date: today })
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.message || 'Failed to generate diary');
}
const result = await response.json();
// Reload diary data from server
await this.loadData();
// Find and display the diary entry for today
const diaryEntry = this.diaryEntries.find(entry => entry.date === today);
if (diaryEntry) {
this.displayDiaryEntry(diaryEntry);
}
const actionText = isRegenerate ? 'regenerated' : isIncremental ? 'updated' : 'generated';
this.showToast(`Diary entry ${actionText} successfully!`, 'success');
} catch (error) {
console.error('Error generating diary:', error);
this.showToast(`Failed to generate diary entry: ${error.message}`, 'error');
if (emptyStateElement) emptyStateElement.classList.remove('hidden');
} finally {
if (loadingElement) loadingElement.classList.add('hidden');
if (generateBtn) generateBtn.disabled = false;
if (regenerateBtn) regenerateBtn.disabled = false;
if (updateBtn) updateBtn.disabled = false;
}
}
displayDiaryEntry(diaryEntry) {
const contentElement = document.getElementById('diary-content');
const emptyStateElement = document.getElementById('diary-empty-state');
const dateElement = document.getElementById('diary-date');
const sourceCountElement = document.getElementById('diary-source-count');
const lastUpdatedElement = document.getElementById('diary-last-updated');
const textElement = document.getElementById('diary-text');
const regenerateBtn = document.getElementById('regenerate-diary-btn');
const updateBtn = document.getElementById('update-diary-btn');
if (contentElement) contentElement.classList.remove('hidden');
if (emptyStateElement) emptyStateElement.classList.add('hidden');
if (dateElement) dateElement.textContent = "Today's Entry";
if (sourceCountElement) sourceCountElement.textContent = `Based on ${diaryEntry.logIds.length} logs`;
if (lastUpdatedElement) {
const updatedDate = new Date(diaryEntry.lastUpdated);
lastUpdatedElement.textContent = `Updated: ${updatedDate.toLocaleTimeString()}`;
}
if (textElement) textElement.textContent = diaryEntry.content;
if (regenerateBtn) regenerateBtn.classList.remove('hidden');
if (updateBtn) updateBtn.classList.add('hidden'); // Hide update button after showing content
}
exportDiary() {
const diaryText = document.getElementById('diary-text');
if (!diaryText || !diaryText.textContent) {
this.showToast('No diary entry to export', 'error');
return;
}
const date = new Date().toLocaleDateString();
const content = `Daily Diary Entry - ${date}\n\n${diaryText.textContent}`;
const blob = new Blob([content], { type: 'text/plain' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `diary-${new Date().toISOString().split('T')[0]}.txt`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
this.showToast('Diary exported successfully!', 'success');
}
showDiaryHistory() {
const historyElement = document.getElementById('diary-history');
const contentElement = document.getElementById('diary-content');
const emptyStateElement = document.getElementById('diary-empty-state');
if (historyElement) historyElement.classList.remove('hidden');
if (contentElement) contentElement.classList.add('hidden');
if (emptyStateElement) emptyStateElement.classList.add('hidden');
this.renderDiaryHistory();
}
hideDiaryHistory() {
const historyElement = document.getElementById('diary-history');
const contentElement = document.getElementById('diary-content');
const emptyStateElement = document.getElementById('diary-empty-state');
if (historyElement) historyElement.classList.add('hidden');
// Show today's diary if exists, otherwise show empty state
const today = new Date().toISOString().split('T')[0];
const todaysDiary = this.diaryEntries.find(entry => entry.date === today);
if (todaysDiary) {
this.displayDiaryEntry(todaysDiary);
} else {
if (contentElement) contentElement.classList.add('hidden');
if (emptyStateElement) emptyStateElement.classList.remove('hidden');
}
}
renderDiaryHistory() {
const historyListElement = document.getElementById('diary-history-list');
if (!historyListElement) return;
if (this.diaryEntries.length === 0) {
historyListElement.innerHTML = `
<div class="empty-state">
<div class="empty-state-icon">📔</div>
<h3>No diary entries found</h3>
<p>Generate your first diary entry to start building your history</p>
</div>
`;
return;
}
// Sort entries by date (most recent first)
const sortedEntries = [...this.diaryEntries].sort((a, b) =>
new Date(b.date) - new Date(a.date)
);
historyListElement.innerHTML = sortedEntries.map(entry => {
const date = new Date(entry.date);
const formattedDate = date.toLocaleDateString('en-US', {
weekday: 'long',
year: 'numeric',
month: 'long',
day: 'numeric'
});
const preview = entry.content.substring(0, 150) + (entry.content.length > 150 ? '...' : '');
return `
<div class="diary-history-item" onclick="app.viewHistoryEntry('${entry.date}')">
<div class="diary-history-header">
<h4 class="diary-history-date">${formattedDate}</h4>
<span class="diary-history-time">${new Date(entry.lastUpdated).toLocaleTimeString()}</span>
</div>
<p class="diary-history-preview">${preview}</p>
<div class="diary-history-meta">
<span>${entry.logIds.length} logs processed</span>
</div>
</div>
`;
}).join('');
}
viewHistoryEntry(date) {
const entry = this.diaryEntries.find(e => e.date === date);
if (entry) {
this.hideDiaryHistory();
this.displayDiaryEntry(entry);
}
}
async extractTasks(onlyNew = false) {
if (this.logs.length === 0) {
this.showToast('Add some logs first to extract tasks', 'error');
return;
}
if (!this.apiConnected) {
this.showToast('Backend server not available', 'error');
return;
}
// Show loading state
const loadingElement = document.getElementById('tasks-loading');
const loadingText = document.getElementById('tasks-loading-text');
const emptyStateElement = document.getElementById('tasks-empty-state');
if (loadingElement) loadingElement.classList.remove('hidden');
if (emptyStateElement) emptyStateElement.classList.add('hidden');
const extractBtn = document.getElementById('extract-tasks-btn');
if (extractBtn) extractBtn.disabled = true;
if (loadingText) {
loadingText.textContent = onlyNew ?
'Extracting tasks from new logs...' :
'Analyzing logs for tasks and action items...';
}
try {
const today = new Date().toISOString().split('T')[0];
const response = await fetch(`${this.API_BASE_URL}/generate-tasks`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ date: today })
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.message || 'Failed to generate tasks');
}
const result = await response.json();
// Reload task data from server
await this.loadData();
this.renderTasks();
this.updateTasksStats();
this.showToast(result.message || 'Tasks extracted successfully!', 'success');
} catch (error) {
console.error('Error extracting tasks:', error);
this.showToast(`Failed to extract tasks: ${error.message}`, 'error');
} finally {
if (loadingElement) loadingElement.classList.add('hidden');
if (extractBtn) extractBtn.disabled = false;
}
}
async toggleTaskComplete(taskId) {
try {
const response = await fetch(`${this.API_BASE_URL}/tasks/${taskId}/toggle`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
}
});
if (!response.ok) {
throw new Error('Failed to update task');
}
// Update local state
const task = this.tasks.find(t => t.id === taskId);
if (task) {
task.completed = !task.completed;
this.renderTasks();
this.updateTasksStats();
const status = task.completed ? 'completed' : 'reopened';
this.showToast(`Task ${status}!`, 'success');
}
} catch (error) {
console.error('Error toggling task:', error);
this.showToast('Failed to update task', 'error');
}
}
async deleteTask(taskId) {
if (confirm('Are you sure you want to delete this task?')) {
try {
if (this.apiConnected) {
// Call backend API to delete
const response = await fetch(`${this.API_BASE_URL}/tasks/${taskId}`, {
method: 'DELETE'
});
if (!response.ok) {
throw new Error('Failed to delete task from server');
}
}
// Update local data
this.tasks = this.tasks.filter(t => t.id !== taskId);
this.saveToStorage();
this.renderTasks();
this.updateTasksStats();
this.showToast('Task deleted successfully!', 'success');
} catch (error) {
console.error('Error deleting task:', error);
this.showToast(`Failed to delete task: ${error.message}`, 'error');
}
}
}
addManualTask() {
const input = document.getElementById('manual-task-input');
const prioritySelect = document.getElementById('manual-task-priority');
if (!input || !prioritySelect) return;
const description = input.value.trim();
const priority = prioritySelect.value;
if (!description) {
this.showToast('Please enter a task description', 'error');
return;
}
const task = {
id: this.nextTaskId++,
description: description,
completed: false,
priority: priority,
sourceLogId: null,
extractedAt: this.generateTimestamp()
};
this.tasks.push(task);
input.value = '';
this.saveToStorage();
this.renderTasks();
this.updateTasksStats();
this.showToast('Manual task added successfully!', 'success');
}
filterTasks(filter = 'all') {
this.renderTasks(filter);
}
renderTasks(filter = 'all') {
const tasksList = document.getElementById('tasks-list');
if (!tasksList) return;
let filteredTasks = this.tasks;
if (filter === 'completed') {
filteredTasks = this.tasks.filter(task => task.completed);
} else if (filter === 'pending') {
filteredTasks = this.tasks.filter(task => !task.completed);
}
if (filteredTasks.length === 0) {
tasksList.innerHTML = `
<div class="empty-state">
<div class="empty-state-icon">📋</div>
<h3>No tasks found</h3>
<p>${filter === 'all' ? 'Extract tasks from your logs or add manual tasks' : `No ${filter} tasks`}</p>
</div>
`;
return;
}
// Sort tasks by priority and completion status
const priorityOrder = { 'High': 3, 'Medium': 2, 'Low': 1 };
filteredTasks.sort((a, b) => {
if (a.completed !== b.completed) {
return a.completed - b.completed; // Completed tasks last
}
return priorityOrder[b.priority] - priorityOrder[a.priority]; // High priority first
});
tasksList.innerHTML = filteredTasks.map(task => `
<div class="task-item ${task.completed ? 'completed' : ''}">
<input
type="checkbox"
class="task-checkbox"
${task.completed ? 'checked' : ''}
onchange="app.toggleTaskComplete(${task.id})"
>
<div class="task-content">
<div class="task-description">${this.escapeHtml(task.description)}</div>
<div class="task-meta">
<span class="priority-badge priority-${task.priority.toLowerCase()}">${task.priority} Priority</span>
${task.sourceLogId ? `<span>From log #${task.sourceLogId}</span>` : '<span>Manual task</span>'}
</div>
</div>
<div class="task-actions">
<button class="task-action-btn" onclick="app.deleteTask(${task.id})" title="Delete task">
🗑️
</button>
</div>
</div>
`).join('');
}
updateTasksStats() {
const totalTasks = this.tasks.length;
const completedTasks = this.tasks.filter(task => task.completed).length;
const pendingTasks = totalTasks - completedTasks;
const totalElement = document.getElementById('total-tasks');
const completedElement = document.getElementById('completed-tasks');
const pendingElement = document.getElementById('pending-tasks');
const statsElement = document.getElementById('tasks-stats');
if (totalElement) totalElement.textContent = totalTasks;
if (completedElement) completedElement.textContent = completedTasks;
if (pendingElement) pendingElement.textContent = pendingTasks;
if (statsElement) {
if (totalTasks > 0) {
statsElement.classList.remove('hidden');
} else {
statsElement.classList.add('hidden');
}
}
}
updateDiarySection() {
const today = new Date().toISOString().split('T')[0];
const hasDiary = this.diaryEntries.find(entry => entry.date === today);
const hasLogs = this.logs.length > 0;
const emptyStateElement = document.getElementById('diary-empty-state');
const contentElement = document.getElementById('diary-content');
if (!hasLogs) {
if (emptyStateElement) emptyStateElement.classList.remove('hidden');
if (contentElement) contentElement.classList.add('hidden');
} else if (hasDiary) {
this.displayDiaryEntry(hasDiary);
} else {
if (emptyStateElement) emptyStateElement.classList.add('hidden');
if (contentElement) contentElement.classList.add('hidden');
}
}
updateTasksSection() {
this.renderTasks();
this.updateTasksStats();
const emptyStateElement = document.getElementById('tasks-empty-state');
if (emptyStateElement) {
if (this.tasks.length === 0 && this.logs.length === 0) {
emptyStateElement.classList.remove('hidden');
} else {
emptyStateElement.classList.add('hidden');
}
}
}
showEmptyStates() {
// Show appropriate empty states based on current data
if (this.currentTab === 'diary') {
this.updateDiarySection();
} else if (this.currentTab === 'tasks') {
this.updateTasksSection();
}
}
showToast(message, type = 'success') {
const toastContainer = document.getElementById('toast-container');
if (!toastContainer) return;
const toast = document.createElement('div');
toast.className = `toast ${type}`;
toast.textContent = message;
toastContainer.appendChild(toast);
// Remove toast after 4 seconds
setTimeout(() => {
toast.style.animation = 'slideOut 0.3s ease-in';
setTimeout(() => {
if (toastContainer.contains(toast)) {
toastContainer.removeChild(toast);
}
}, 300);
}, 4000);
}
escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
}
// Initialize the application when DOM is loaded
document.addEventListener('DOMContentLoaded', () => {
window.app = new DailyLogApp();
window.app.init(); // Explicitly call init
});