From 730d99fc80c39bac6604ecfdd06b6bf70c21a565 Mon Sep 17 00:00:00 2001 From: crib Date: Wed, 17 Dec 2025 21:43:11 +0100 Subject: [PATCH] Added a feature to clean compleeted tasks either Never, 24 hours, 7 days or 30 days --- main.ts | 43 +++++++++++++++++++++++++++++++++++++++++++ settings.ts | 14 ++++++++++++++ types.ts | 6 +++++- 3 files changed, 62 insertions(+), 1 deletion(-) diff --git a/main.ts b/main.ts index 0488b0a..5299ab5 100644 --- a/main.ts +++ b/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 { @@ -180,4 +188,39 @@ export default class TaskWeaverPlugin extends Plugin { } return elapsed; } + + async cleanupCompletedTasks(): Promise { + 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(); + } + } } diff --git a/settings.ts b/settings.ts index d03841d..04f5bb1 100644 --- a/settings.ts +++ b/settings.ts @@ -13,6 +13,20 @@ export class TaskWeaverSettingTab extends PluginSettingTab { const { containerEl } = this; containerEl.empty(); + new Setting(containerEl) + .setName("Clear completed tasks") + .setDesc("Automatically remove completed tasks after a specified period") + .addDropdown(dropdown => dropdown + .addOption("never", "Never") + .addOption("1day", "After 24 hours") + .addOption("7days", "After 7 days") + .addOption("30days", "After 30 days") + .setValue(this.plugin.settings.completedTaskRetention) + .onChange(async (value) => { + this.plugin.settings.completedTaskRetention = value as any; + await this.plugin.saveSettings(); + })); + new Setting(containerEl) .setName("Enable daily note logging") .setDesc("Log completed tasks to your daily note") diff --git a/types.ts b/types.ts index ce40abc..867640b 100644 --- a/types.ts +++ b/types.ts @@ -15,16 +15,20 @@ export interface Task { completedAt?: number; } +export type CompletedTaskRetention = "never" | "1day" | "7days" | "30days"; + export interface TaskWeaverSettings { categories: Category[]; tasks: Task[]; enableDailyNoteLogging: boolean; dailyNoteFormat: string; + completedTaskRetention: CompletedTaskRetention; } export const DEFAULT_SETTINGS: TaskWeaverSettings = { categories: [], tasks: [], enableDailyNoteLogging: false, - dailyNoteFormat: "- [x] {{title}} ({{duration}}) {{emoji}}" + dailyNoteFormat: "- [x] {{title}} ({{duration}}) {{emoji}}", + completedTaskRetention: "7days" };