Added a feature to clean compleeted tasks either Never, 24 hours, 7 days or 30 days

This commit is contained in:
2025-12-17 21:43:11 +01:00
parent e7557af55a
commit 730d99fc80
3 changed files with 62 additions and 1 deletions

43
main.ts
View File

@@ -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();
}
}
}