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

View File

@@ -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")

View File

@@ -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"
};