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.app.workspace.onLayoutReady(() => {
|
||||||
this.activateView();
|
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> {
|
async onunload(): Promise<void> {
|
||||||
@@ -180,4 +188,39 @@ export default class TaskWeaverPlugin extends Plugin {
|
|||||||
}
|
}
|
||||||
return elapsed;
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
14
settings.ts
14
settings.ts
@@ -13,6 +13,20 @@ export class TaskWeaverSettingTab extends PluginSettingTab {
|
|||||||
const { containerEl } = this;
|
const { containerEl } = this;
|
||||||
containerEl.empty();
|
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)
|
new Setting(containerEl)
|
||||||
.setName("Enable daily note logging")
|
.setName("Enable daily note logging")
|
||||||
.setDesc("Log completed tasks to your daily note")
|
.setDesc("Log completed tasks to your daily note")
|
||||||
|
|||||||
6
types.ts
6
types.ts
@@ -15,16 +15,20 @@ export interface Task {
|
|||||||
completedAt?: number;
|
completedAt?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type CompletedTaskRetention = "never" | "1day" | "7days" | "30days";
|
||||||
|
|
||||||
export interface TaskWeaverSettings {
|
export interface TaskWeaverSettings {
|
||||||
categories: Category[];
|
categories: Category[];
|
||||||
tasks: Task[];
|
tasks: Task[];
|
||||||
enableDailyNoteLogging: boolean;
|
enableDailyNoteLogging: boolean;
|
||||||
dailyNoteFormat: string;
|
dailyNoteFormat: string;
|
||||||
|
completedTaskRetention: CompletedTaskRetention;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const DEFAULT_SETTINGS: TaskWeaverSettings = {
|
export const DEFAULT_SETTINGS: TaskWeaverSettings = {
|
||||||
categories: [],
|
categories: [],
|
||||||
tasks: [],
|
tasks: [],
|
||||||
enableDailyNoteLogging: false,
|
enableDailyNoteLogging: false,
|
||||||
dailyNoteFormat: "- [x] {{title}} ({{duration}}) {{emoji}}"
|
dailyNoteFormat: "- [x] {{title}} ({{duration}}) {{emoji}}",
|
||||||
|
completedTaskRetention: "7days"
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user