|
| 1 | +(() => { |
| 2 | + // lib/plugin.js |
| 3 | + var plugin = { |
| 4 | + constants: {}, |
| 5 | + taskOption: { |
| 6 | + "Clear Overdue Tasks": { |
| 7 | + run: async function(app, noteUUID) { |
| 8 | + try { |
| 9 | + const oneHourAgo = Math.floor(Date.now() / 1e3) - 3600; |
| 10 | + const taskDomains = await app.getTaskDomains(); |
| 11 | + if (!taskDomains || taskDomains.length === 0) { |
| 12 | + await app.alert("No task domains found."); |
| 13 | + return; |
| 14 | + } |
| 15 | + const overdueTasks = []; |
| 16 | + const tasksByDomain = {}; |
| 17 | + for (const taskDomain of taskDomains) { |
| 18 | + const domainTasks = []; |
| 19 | + for await (const task of app.getTaskDomainTasks(taskDomain.uuid)) { |
| 20 | + const taskDate = task.startAt || task.endAt; |
| 21 | + if (taskDate && taskDate < oneHourAgo) { |
| 22 | + overdueTasks.push(task); |
| 23 | + domainTasks.push(task); |
| 24 | + } |
| 25 | + } |
| 26 | + if (domainTasks.length > 0) { |
| 27 | + tasksByDomain[taskDomain.name] = domainTasks; |
| 28 | + } |
| 29 | + } |
| 30 | + if (overdueTasks.length === 0) { |
| 31 | + await app.alert("No overdue tasks found."); |
| 32 | + return; |
| 33 | + } |
| 34 | + const domainBreakdown = Object.entries(tasksByDomain).map(([domainName, tasks]) => { |
| 35 | + const taskList = tasks.map((task) => { |
| 36 | + const content = task.content || "Untitled task"; |
| 37 | + const truncated = content.length > 60 ? content.substring(0, 57) + "..." : content; |
| 38 | + return ` - ${truncated}`; |
| 39 | + }).join("\n"); |
| 40 | + return ` ${domainName} (${tasks.length}): |
| 41 | +${taskList}`; |
| 42 | + }).join("\n\n"); |
| 43 | + const confirmMessage = `Found ${overdueTasks.length} overdue task${overdueTasks.length > 1 ? "s" : ""}: |
| 44 | +
|
| 45 | +${domainBreakdown} |
| 46 | +
|
| 47 | +Remove from calendar?`; |
| 48 | + const confirmed = await app.alert(confirmMessage, { |
| 49 | + actions: [ |
| 50 | + { label: "Cancel", icon: "cancel" }, |
| 51 | + { label: "Remove from calendar", icon: "check" } |
| 52 | + ] |
| 53 | + }); |
| 54 | + if (confirmed === 1) { |
| 55 | + let successCount = 0; |
| 56 | + let failCount = 0; |
| 57 | + const errors = []; |
| 58 | + for (const task of overdueTasks) { |
| 59 | + try { |
| 60 | + const updates = {}; |
| 61 | + console.log(`Processing task ${task.uuid}:`); |
| 62 | + console.log(` startAt: ${task.startAt}`); |
| 63 | + console.log(` endAt: ${task.endAt}`); |
| 64 | + console.log(` hideUntil: ${task.hideUntil}`); |
| 65 | + if (task.startAt !== null && task.startAt !== void 0) { |
| 66 | + updates.startAt = null; |
| 67 | + } |
| 68 | + if (task.endAt !== null && task.endAt !== void 0) { |
| 69 | + delete updates.endAt; |
| 70 | + } |
| 71 | + if (task.hideUntil !== null && task.hideUntil !== void 0) { |
| 72 | + delete updates.hideUntil; |
| 73 | + } |
| 74 | + console.log(` Updates to apply:`, updates); |
| 75 | + if (Object.keys(updates).length > 0) { |
| 76 | + await app.updateTask(task.uuid, updates); |
| 77 | + console.log(` \u2713 Successfully updated task ${task.uuid}`); |
| 78 | + successCount++; |
| 79 | + } else { |
| 80 | + console.log( |
| 81 | + ` \u26A0 Task ${task.uuid} has no calendar dates to remove` |
| 82 | + ); |
| 83 | + successCount++; |
| 84 | + } |
| 85 | + } catch (error) { |
| 86 | + const errorMsg = `Task "${task.content || task.uuid}": ${error.message}`; |
| 87 | + console.error(`Failed to reset task ${task.uuid}:`, error); |
| 88 | + console.error(`Task data:`, JSON.stringify(task)); |
| 89 | + errors.push(errorMsg); |
| 90 | + failCount++; |
| 91 | + } |
| 92 | + } |
| 93 | + let resultMessage = `Successfully reset ${successCount} task${successCount !== 1 ? "s" : ""}.`; |
| 94 | + if (failCount > 0) { |
| 95 | + resultMessage += ` |
| 96 | +
|
| 97 | +${failCount} task${failCount !== 1 ? "s" : ""} failed: |
| 98 | +`; |
| 99 | + resultMessage += errors.map((e) => `\u2022 ${e}`).join("\n"); |
| 100 | + } |
| 101 | + await app.alert(resultMessage); |
| 102 | + } |
| 103 | + } catch (error) { |
| 104 | + console.error("Error in Bulk Reset Overdue Tasks:", error); |
| 105 | + await app.alert(`Error: ${error.message}`); |
| 106 | + } |
| 107 | + } |
| 108 | + }, |
| 109 | + "Hide Lower Priority Tasks": { |
| 110 | + run: async function(app, noteUUID) { |
| 111 | + try { |
| 112 | + const taskDomains = await app.getTaskDomains(); |
| 113 | + if (!taskDomains || taskDomains.length === 0) { |
| 114 | + await app.alert("No task domains found."); |
| 115 | + return; |
| 116 | + } |
| 117 | + const tasksByDomain = {}; |
| 118 | + for (const taskDomain of taskDomains) { |
| 119 | + const domainTasks = app.getTaskDomainTasks(taskDomain.uuid); |
| 120 | + if (domainTasks.length > 0) { |
| 121 | + tasksByDomain[taskDomain.name] = domainTasks; |
| 122 | + } |
| 123 | + } |
| 124 | + await app.alert(JSON.stringify(tasksByDomain)); |
| 125 | + } catch (error) { |
| 126 | + console.error("Error in Bulk Reset Overdue Tasks:", error); |
| 127 | + await app.alert(`Error: ${error.message}`); |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + }, |
| 132 | + insertText: {}, |
| 133 | + replaceText: {} |
| 134 | + }; |
| 135 | + var plugin_default = plugin; |
| 136 | +})(); |
0 commit comments