-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
807 lines (717 loc) · 25.5 KB
/
Copy pathmain.ts
File metadata and controls
807 lines (717 loc) · 25.5 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
import {
Notice,
Plugin,
TFile,
WorkspaceLeaf,
requestUrl,
debounce,
} from "obsidian";
import { TimeTrackerSettingTab } from "./src/settings";
import {
TimeTrackerView,
VIEW_TYPE_TIME_TRACKER,
} from "./src/views/TimeTrackerView";
import { AnalyticsView, VIEW_TYPE_ANALYTICS } from "./src/views/AnalyticsView";
import { CSVHandler } from "./src/handlers/csvHandler";
import { BackupHandler } from "./src/handlers/backupHandler";
import { TimeblocksHandler } from "./src/handlers/timeblocksHandler";
import type {
PluginSettings,
TimesheetData,
TimeblocksData,
TimeRecord,
Project,
Timeblock,
IcsCalendarEvent,
} from "./src/types";
import { ImportModal } from "./src/modals/ImportSTTModal";
import { BackupViewerModal } from "./src/modals/BackupViewerModal";
import { CategoryHandler } from "./src/handlers/categoryHandler";
import { CreateRecordModal } from "./src/modals/CreateRecordModal";
import { hslToHex } from "./src/utils/colorUtils";
const DEFAULT_SETTINGS: PluginSettings = {
timesheetPath: "timesheet.csv",
timeblocksPath: "timeblocks.csv",
settingsPath: "time-tracker-settings.json",
retroactiveTrackingEnabled: false,
enableTimeblocking: true,
showSeconds: true,
showArchivedProjects: false,
gridColumns: 5,
defaultTimeRange: "day",
customStartDate: Date.now(),
customEndDate: Date.now(),
sortMode: "color",
categoryFilter: [],
icsCalendars: [],
scheduleZoom: 60,
};
export default class TimeTrackerPlugin extends Plugin {
settings: PluginSettings;
timesheetData: TimesheetData = {
records: [],
projects: [],
categories: [],
};
timeblocksData: TimeblocksData = {
timeblocks: [],
};
csvHandler: CSVHandler;
backupHandler: BackupHandler;
timeblocksHandler: TimeblocksHandler;
categoryHandler: CategoryHandler;
private timesheetFile: TFile | null = null;
private timeblocksFile: TFile | null = null;
error: string | null = null;
isLoading: boolean = true;
// cache ics events, dont fetch every time schedule refreshed
icsCache: {
events: IcsCalendarEvent[];
fetched: boolean;
loading: boolean;
} = {
events: [],
fetched: false,
loading: false,
};
// schedule view state (fix scroll position)
scheduleState: { scrollTop: number } = { scrollTop: 0 };
// ----- OBSIDIAN FUNCTIONS -----
async onload() {
await this.loadSettings();
this.csvHandler = new CSVHandler(this.app.vault);
this.backupHandler = new BackupHandler(this.app);
this.timeblocksHandler = new TimeblocksHandler(this.app.vault);
this.categoryHandler = new CategoryHandler(this);
this.registerView(
VIEW_TYPE_TIME_TRACKER,
(leaf) => new TimeTrackerView(leaf, this),
);
this.registerView(
VIEW_TYPE_ANALYTICS,
(leaf) => new AnalyticsView(leaf, this),
);
this.addRibbonIcon("clock", "Time tracker", async () => {
await this.activateView();
});
this.addCommand({
id: "open-time-tracking-view",
name: "Open time tracking view",
callback: async () => await this.activateView(),
});
this.addCommand({
id: "open-analytics-view",
name: "Open analytics view",
callback: () => this.activateAnalyticsView(),
});
this.addCommand({
id: "track-time",
name: "Track time",
callback: () =>
new CreateRecordModal(this.app, this, () => {}, false).open(),
});
this.addCommand({
id: "import-data",
name: "Import data from other apps",
callback: () => new ImportModal(this.app, this).open(),
});
this.addCommand({
id: "view-backups",
name: "View backups",
callback: () => new BackupViewerModal(this.app, this).open(),
});
this.addSettingTab(new TimeTrackerSettingTab(this.app, this));
this.app.workspace.onLayoutReady(async () => {
await this.loadPluginData();
await this.backupHandler.createBackup(this.settings.timesheetPath);
await this.activateView();
});
// backup every 3 hours
this.registerInterval(
window.setInterval(
() =>
void this.backupHandler.createBackup(
this.settings.timesheetPath,
),
3 * 60 * 60 * 1000,
),
);
// reload timesheet when app comes back to foreground
this.registerDomEvent(document, "visibilitychange", () => {
if (document.visibilityState === "visible") {
void this.loadPluginData();
}
});
// reload on external file changes instead of polling
const debouncedReload = debounce(
() => void this.loadPluginData(),
1000,
true,
);
this.registerEvent(
this.app.vault.on("modify", (file) => {
if (
file instanceof TFile &&
(file.path === this.settings.timesheetPath ||
file.path === this.settings.timeblocksPath)
) {
debouncedReload();
}
}),
);
}
async loadPluginData(retries = 5, delay = 500) {
for (let i = 0; i < retries; i++) {
try {
await this.loadTimesheet();
await this.loadTimeblocks();
return;
} catch (err) {
if (i < retries - 1) {
await new Promise((r) => setTimeout(r, delay));
} else {
new Notice("Error loading plugin data: " + err);
}
}
}
}
async savePluginData() {
await this.saveTimesheet();
await this.saveTimeblocks();
}
onunload() {
void this.savePluginData();
}
// ----- LOADING DATA -----
async loadSettings() {
this.settings = Object.assign(
{},
DEFAULT_SETTINGS,
(await this.loadData()) || {},
) as PluginSettings;
}
async saveSettings() {
await this.saveData(this.settings);
}
async loadTimesheet() {
const hasExistingData =
this.timesheetData.records.length > 0 ||
this.timesheetData.projects.length > 0;
// only show loading if no data exists yet
if (!hasExistingData) {
this.isLoading = true;
}
try {
const file = this.app.vault.getAbstractFileByPath(
this.settings.timesheetPath,
);
let newData: TimesheetData;
if (file instanceof TFile) {
this.timesheetFile = file;
newData = await this.csvHandler.parseTimesheet(file);
} else {
this.timesheetFile = await this.csvHandler.createTimesheet(
this.settings.timesheetPath,
);
newData = await this.csvHandler.parseTimesheet(
this.timesheetFile,
);
}
// compare new data with current data, skip refresh if unchanged
if (this.isTimesheetDataEqual(this.timesheetData, newData)) {
this.isLoading = false;
return;
}
// register commands for each new non-archived project
for (const project of newData.projects) {
if (project.archived) continue;
if (
this.timesheetData.projects.find((p) => p.id === project.id)
)
continue;
const title = this.settings.retroactiveTrackingEnabled
? "Retroactively track"
: "Start time tracker for";
this.addCommand({
id: `track-${project.id}`,
name: `${title} ${project.name}`,
callback: () => this.startTimer(project.id),
});
}
this.timesheetData = newData;
this.isLoading = false;
this.refreshViews();
} catch (err) {
this.error = "Error loading Timesheet: " + err;
this.isLoading = false;
this.refreshViews();
}
}
private isTimesheetDataEqual(a: TimesheetData, b: TimesheetData): boolean {
if (
a.records.length !== b.records.length ||
a.projects.length !== b.projects.length ||
a.categories.length !== b.categories.length
) {
return false;
}
// compare records by stringifying (handles Date objects)
const aRecords = JSON.stringify(a.records);
const bRecords = JSON.stringify(b.records);
if (aRecords !== bRecords) return false;
const aProjects = JSON.stringify(a.projects);
const bProjects = JSON.stringify(b.projects);
if (aProjects !== bProjects) return false;
const aCategories = JSON.stringify(a.categories);
const bCategories = JSON.stringify(b.categories);
if (aCategories !== bCategories) return false;
return true;
}
async saveTimesheet() {
if (!this.timesheetFile) return;
try {
await this.csvHandler.writeTimesheet(
this.timesheetFile,
this.timesheetData,
);
} catch (err) {
this.error = "Error saving Timesheet: " + err;
}
}
// ----- TIMEBLOCKS -----
async loadTimeblocks() {
try {
const file = this.app.vault.getAbstractFileByPath(
this.settings.timeblocksPath,
);
let newData: TimeblocksData;
if (file instanceof TFile) {
this.timeblocksFile = file;
newData = await this.timeblocksHandler.parseTimeblocks(file);
} else {
this.timeblocksFile =
await this.timeblocksHandler.createTimeblocks(
this.settings.timeblocksPath,
);
newData = { timeblocks: [] };
}
// skip refresh if unchanged
if (
JSON.stringify(this.timeblocksData) === JSON.stringify(newData)
) {
return;
}
this.timeblocksData = newData;
} catch (err) {
new Notice("Error loading Timeblocks: " + err);
}
}
async saveTimeblocks() {
if (!this.timeblocksFile) return;
try {
await this.timeblocksHandler.writeTimeblocks(
this.timeblocksFile,
this.timeblocksData,
);
} catch (err) {
new Notice("Error saving Timeblocks: " + err);
}
}
async createTimeblock(
timeblock: Omit<Timeblock, "id">,
): Promise<Timeblock> {
const startTime = new Date(timeblock.startTime);
startTime.setMilliseconds(0);
const endTime = new Date(timeblock.endTime);
endTime.setMilliseconds(0);
timeblock.color = hslToHex(timeblock.color);
const newTimeblock: Timeblock = {
id: TimeblocksHandler.getNextId(this.timeblocksData.timeblocks),
...timeblock,
startTime,
endTime,
notes: (timeblock.notes ?? "").toString(),
};
this.timeblocksData.timeblocks = [
...this.timeblocksData.timeblocks,
newTimeblock,
];
await this.saveTimeblocks();
return newTimeblock;
}
async updateTimeblock(
id: number,
changes: Partial<Timeblock>,
): Promise<void> {
const index = this.timeblocksData.timeblocks.findIndex(
(t) => t.id === id,
);
if (index === -1) return;
if (changes.startTime) {
changes.startTime = new Date(changes.startTime);
changes.startTime.setMilliseconds(0);
}
if (changes.endTime) {
changes.endTime = new Date(changes.endTime);
changes.endTime.setMilliseconds(0);
}
this.timeblocksData.timeblocks = this.timeblocksData.timeblocks.map(
(t, i) => (i === index ? { ...t, ...changes } : t),
);
await this.saveTimeblocks();
}
async deleteTimeblock(id: number): Promise<void> {
this.timeblocksData.timeblocks = this.timeblocksData.timeblocks.filter(
(t) => t.id !== id,
);
await this.saveTimeblocks();
}
getTimeblockById(id: number): Timeblock | undefined {
return this.timeblocksData.timeblocks.find((t) => t.id === id);
}
get runningTimers(): TimeRecord[] {
if (!this.timesheetData || !this.timesheetData.records) {
return [];
}
return this.timesheetData.records.filter(
(record) => record.endTime === null,
);
}
// ----- RECORDS -----
/*
* start tracking time without a project
* can only be used when retroactive mode is off
* @param title - The title of the task being tracked
*/
async startTimerWithoutProject(title: string = "") {
if (this.settings.retroactiveTrackingEnabled) {
throw new Error(
"Cannot start timer without project in retroactive mode",
);
}
const now = new Date();
await this.stopAllTimers();
// Create a new record with null endTime (running timer) and empty projectName
const newRecord: TimeRecord = {
id: CSVHandler.getNextId(this.timesheetData.records),
projectId: -1,
startTime: now,
endTime: null,
title: title,
};
this.timesheetData.records.push(newRecord);
await this.saveTimesheet();
this.refreshViews();
}
/** start tracking time for a project */
async startTimer(
projectId: number = -1,
title: string = "",
startTime?: Date,
endTime?: Date,
) {
let project = this.getProjectById(projectId);
if (projectId === -1) {
project = undefined;
}
const now = new Date();
// for retroactive
if (this.settings.retroactiveTrackingEnabled) {
const lastRecord = this.getLastStoppedRecord();
if (lastRecord && lastRecord.endTime) {
const gap = now.getTime() - lastRecord.endTime.getTime();
if (gap > 0) {
// extend record if the project is the same and start time is the same as last project end time(within 10 minutes)
const startTimeSame = startTime
? Math.abs(
lastRecord.endTime.getTime() -
startTime.getTime(),
) <
10 * 60 * 1000
: true;
if (
project &&
lastRecord.projectId === project.id &&
startTimeSame
) {
const index = this.timesheetData.records.findIndex(
(r) => r.id === lastRecord.id,
);
if (index !== -1) {
this.timesheetData.records[index].endTime =
endTime ?? now;
}
}
// otherwise we create a new record
else {
const newRecord: TimeRecord = {
id: CSVHandler.getNextId(
this.timesheetData.records,
),
projectId: project ? project.id : -1,
startTime: startTime ?? lastRecord.endTime,
endTime: endTime ?? now,
title: title,
};
this.timesheetData.records.push(newRecord);
}
}
}
} else {
await this.stopAllTimers();
// check if this project is already running
const existingTimer = this.runningTimers.find(
(t) => t.projectId === projectId,
);
if (existingTimer) {
await this.stopTimer(existingTimer.id);
} else {
// create a new record with null endTime (running timer)
const newRecord: TimeRecord = {
id: CSVHandler.getNextId(this.timesheetData.records),
projectId: project ? project.id : -1,
startTime: startTime ?? now,
endTime: null,
title: title,
};
this.timesheetData.records.push(newRecord);
}
}
await this.saveTimesheet();
this.refreshViews();
}
/** find record by ID and extend its time */
async repeatRecord(recordId: number, endTime: Date) {
const record = this.timesheetData.records.find(
(r) => r.id === recordId,
);
if (!record) return;
if (this.settings.retroactiveTrackingEnabled) {
const lastRecord = this.getLastStoppedRecord();
if (
lastRecord &&
lastRecord.projectId === record.projectId &&
lastRecord.title === record.title
) {
const index = this.timesheetData.records.findIndex(
(r) => r.id === lastRecord.id,
);
if (index !== -1) {
this.timesheetData.records[index].endTime = new Date();
await this.saveTimesheet();
this.refreshViews();
}
} else {
const now = new Date();
const startTime = lastRecord?.endTime || now;
const newRecord: TimeRecord = {
id: CSVHandler.getNextId(this.timesheetData.records),
projectId: record.projectId,
startTime: startTime,
endTime: now,
title: record.title,
};
this.timesheetData.records.push(newRecord);
await this.saveTimesheet();
this.refreshViews();
}
} else {
if (record.projectId) {
await this.startTimer(record.projectId);
setTimeout(() => {
void (async () => {
const runningRecord = this.timesheetData.records.find(
(r) =>
r.projectId === record.projectId &&
r.endTime === null,
);
if (runningRecord) {
runningRecord.title = record.title;
await this.saveTimesheet();
}
})();
}, 100);
} else {
await this.startTimerWithoutProject(record.title);
}
}
}
/** stop a specific timer by record ID */
async stopTimer(recordId: number) {
const recordIndex = this.timesheetData.records.findIndex(
(r) => r.id === recordId && r.endTime === null,
);
if (recordIndex === -1) return;
this.timesheetData.records[recordIndex].endTime = new Date();
await this.saveTimesheet();
this.refreshViews();
}
/*
* edit a specific record
* returns true if the record was successfully edited
*/
async editRecord(id: number, changes: TimeRecord): Promise<boolean> {
const recordIndex = this.timesheetData.records.findIndex(
(r) => r.id === id,
);
if (recordIndex === -1) return false;
this.timesheetData.records[recordIndex] = {
...this.timesheetData.records[recordIndex],
...changes,
};
await this.saveTimesheet();
// this.refreshViews();
return true;
}
/** stop all running timers */
async stopAllTimers() {
const now = new Date();
for (const record of this.timesheetData.records) {
if (record.endTime === null) {
record.endTime = now;
}
}
await this.saveTimesheet();
// this.refreshViews();
}
/** set the last stopped record (completed, with endTime) */
getLastStoppedRecord(): TimeRecord | null {
const completedRecords = this.timesheetData.records.filter(
(r) => r.endTime !== null,
);
if (completedRecords.length === 0) return null;
return completedRecords.reduce((latest, record) => {
if (!latest.endTime) return record;
if (!record.endTime) return latest;
return record.endTime.getTime() > latest.endTime.getTime()
? record
: latest;
});
}
/** toggle the last used timer */
async toggleLastTimer() {
const lastRecord = this.getLastStoppedRecord();
if (!lastRecord) return;
const project = this.getProjectById(lastRecord.projectId);
if (!project) return;
const isRunning = this.runningTimers.some(
(t) => t.projectId === project.id,
);
if (isRunning) {
const runningRecord = this.runningTimers.find(
(r) => r.projectId === project.id,
);
if (runningRecord) {
await this.stopTimer(runningRecord.id);
}
} else {
await this.startTimer(project.id);
}
}
/** check if a project is currently running */
isProjectRunning(projectId: number): boolean {
return this.runningTimers.some((t) => t.projectId === projectId);
}
/** update a record's title */
async updateRecordTitle(recordId: number, title: string): Promise<void> {
const index = this.timesheetData.records.findIndex(
(r) => r.id === recordId,
);
if (index !== -1) {
this.timesheetData.records[index].title = title;
await this.saveTimesheet();
}
}
/** get the current running record (first one if multiple) */
getCurrentRunningRecord(): TimeRecord | null {
const timer = this.runningTimers[0];
if (!timer) return null;
return (
this.timesheetData.records.find((r) => r.id === timer.id) || null
);
}
// ----- PROJECT -----
getProjectByName(name: string): Project | undefined {
if (!this.timesheetData || !this.timesheetData.projects) {
return undefined;
}
return this.timesheetData.projects.find((p) => p.name === name);
}
getProjectById(id: number): Project | undefined {
if (!this.timesheetData || !this.timesheetData.projects) {
return undefined;
}
return this.timesheetData.projects.find((p) => p.id === id);
}
async fetchUrl(url: string): Promise<string> {
const response = await requestUrl({ url });
return response.text;
}
async editProject(project: Project): Promise<void> {
const index = this.timesheetData.projects.findIndex(
(p) => p.id === project.id,
);
if (index !== -1) {
this.timesheetData.projects[index] = project;
}
await this.saveTimesheet();
// this.refreshViews();
}
// ----- VIEWS -----
async activateView() {
const { workspace } = this.app;
let leaf: WorkspaceLeaf | null = null;
const leaves = workspace.getLeavesOfType(VIEW_TYPE_TIME_TRACKER);
if (leaves.length > 0) {
leaf = leaves[0];
} else {
const rightLeaf = workspace.getRightLeaf(false);
if (rightLeaf) {
leaf = rightLeaf;
await leaf.setViewState({
type: VIEW_TYPE_TIME_TRACKER,
active: true,
});
}
}
if (leaf) {
void workspace.revealLeaf(leaf);
}
}
async activateAnalyticsView() {
const { workspace } = this.app;
let leaf: WorkspaceLeaf | null = null;
const leaves = workspace.getLeavesOfType(VIEW_TYPE_ANALYTICS);
if (leaves.length > 0) {
leaf = leaves[0];
} else {
leaf = workspace.getLeaf(true);
if (leaf) {
await leaf.setViewState({
type: VIEW_TYPE_ANALYTICS,
active: true,
});
}
}
if (leaf) {
void workspace.revealLeaf(leaf);
}
}
/** Refresh all open views */
refreshViews() {
this.app.workspace
.getLeavesOfType(VIEW_TYPE_TIME_TRACKER)
.forEach((leaf) => {
if (leaf.view instanceof TimeTrackerView) {
leaf.view.refresh();
}
});
this.app.workspace
.getLeavesOfType(VIEW_TYPE_ANALYTICS)
.forEach((leaf) => {
if (leaf.view instanceof AnalyticsView) {
leaf.view.refresh();
}
});
}
}