Added a feature to clean compleeted tasks either Never, 24 hours, 7 days or 30 days
This commit is contained in:
43
main.ts
43
main.ts
@@ -32,6 +32,14 @@ export default class TaskWeaverPlugin extends Plugin {
|
||||
this.app.workspace.onLayoutReady(() => {
|
||||
this.activateView();
|
||||
});
|
||||
|
||||
// Clean up old completed tasks on startup
|
||||
await this.cleanupCompletedTasks();
|
||||
|
||||
// Clean up old completed tasks every hour
|
||||
this.registerInterval(
|
||||
window.setInterval(() => this.cleanupCompletedTasks(), 60 * 60 * 1000)
|
||||
);
|
||||
}
|
||||
|
||||
async onunload(): Promise<void> {
|
||||
@@ -180,4 +188,39 @@ export default class TaskWeaverPlugin extends Plugin {
|
||||
}
|
||||
return elapsed;
|
||||
}
|
||||
|
||||
async cleanupCompletedTasks(): Promise<void> {
|
||||
if (this.settings.completedTaskRetention === "never") {
|
||||
return;
|
||||
}
|
||||
|
||||
const now = Date.now();
|
||||
let retentionMs: number;
|
||||
|
||||
switch (this.settings.completedTaskRetention) {
|
||||
case "1day":
|
||||
retentionMs = 24 * 60 * 60 * 1000;
|
||||
break;
|
||||
case "7days":
|
||||
retentionMs = 7 * 24 * 60 * 60 * 1000;
|
||||
break;
|
||||
case "30days":
|
||||
retentionMs = 30 * 24 * 60 * 60 * 1000;
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
|
||||
const initialCount = this.settings.tasks.length;
|
||||
this.settings.tasks = this.settings.tasks.filter(task => {
|
||||
if (!task.completed || !task.completedAt) {
|
||||
return true;
|
||||
}
|
||||
return (now - task.completedAt) < retentionMs;
|
||||
});
|
||||
|
||||
if (this.settings.tasks.length !== initialCount) {
|
||||
await this.saveSettings();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user