forked from pullfrog/pullfrog
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtodoTracking.ts
More file actions
167 lines (150 loc) · 5.35 KB
/
Copy pathtodoTracking.ts
File metadata and controls
167 lines (150 loc) · 5.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import { log } from "./log.ts";
type TodoItem = {
id: string;
content: string;
status: "pending" | "in_progress" | "completed" | "cancelled";
};
function isValidTodoStatus(value: string): value is TodoItem["status"] {
return (
value === "pending" || value === "in_progress" || value === "completed" || value === "cancelled"
);
}
function parseTodowriteInput(input: unknown): { todos: unknown[]; merge: boolean } | undefined {
if (!input || typeof input !== "object" || !("todos" in input)) return undefined;
if (!Array.isArray(input.todos)) return undefined;
const merge = "merge" in input && input.merge === true;
return { todos: input.todos, merge };
}
function parseTodoItem(entry: unknown, index: number): TodoItem | undefined {
if (!entry || typeof entry !== "object") return undefined;
if (!("content" in entry) || typeof entry.content !== "string") return undefined;
const id = "id" in entry && typeof entry.id === "string" ? entry.id : String(index);
const status =
"status" in entry && typeof entry.status === "string" && isValidTodoStatus(entry.status)
? entry.status
: "pending";
return { id, content: entry.content, status };
}
function renderTodoMarkdown(todos: TodoItem[]): string {
return todos
.map((todo) => {
switch (todo.status) {
case "completed":
return `- [x] ${todo.content}`;
case "cancelled":
return `- ~~${todo.content}~~`;
case "in_progress":
return `- [ ] <img src="https://uploads.pullfrog.com/Progress%20Indicator.gif" width="11" style="visibility: visible; max-width: 100%;" /> ${todo.content}`;
case "pending":
return `- [ ] ${todo.content}`;
default:
todo.status satisfies never;
return `- [ ] ${todo.content}`;
}
})
.join("\n");
}
export type TodoTracker = {
update: (input: unknown) => void;
flush: () => Promise<void>;
cancel: () => void;
/** resolves when any in-flight onUpdate call completes */
settled: () => Promise<void>;
/** mark in-progress items as completed (for final snapshot before review/progress post) */
completeInProgress: () => void;
renderCollapsible: (options?: { completeInProgress?: boolean }) => string;
readonly enabled: boolean;
/** true after the tracker has successfully called onUpdate at least once */
readonly hasPublished: boolean;
};
const DEBOUNCE_MS = 2000;
export function createTodoTracker(onUpdate: (body: string) => Promise<void>): TodoTracker {
const state = new Map<string, TodoItem>();
let enabled = true;
let hasPublished = false;
let debounceTimer: ReturnType<typeof setTimeout> | null = null;
let inflightPromise: Promise<void> = Promise.resolve();
function scheduleUpdate() {
if (!enabled) return;
if (debounceTimer) clearTimeout(debounceTimer);
debounceTimer = setTimeout(() => {
debounceTimer = null;
if (!enabled || state.size === 0) return;
const markdown = renderTodoMarkdown(Array.from(state.values()));
inflightPromise = inflightPromise
.then(async () => {
if (!enabled) return;
await onUpdate(markdown);
hasPublished = true;
})
.catch((err) => {
log.debug(`todo progress update failed: ${err}`);
});
}, DEBOUNCE_MS);
}
return {
update(input: unknown) {
if (!enabled) return;
const parsed = parseTodowriteInput(input);
if (!parsed) return;
if (!parsed.merge) state.clear();
for (const [index, entry] of parsed.todos.entries()) {
const item = parseTodoItem(entry, index);
if (item) state.set(item.id, item);
}
log.debug(`» todowrite: ${state.size} items tracked`);
scheduleUpdate();
},
async flush() {
if (debounceTimer) {
clearTimeout(debounceTimer);
debounceTimer = null;
}
if (!enabled || state.size === 0) return;
const markdown = renderTodoMarkdown(Array.from(state.values()));
inflightPromise = inflightPromise
.then(async () => {
if (!enabled) return;
await onUpdate(markdown);
hasPublished = true;
})
.catch((err) => {
log.debug(`todo progress flush failed: ${err}`);
});
await inflightPromise;
},
cancel() {
enabled = false;
if (debounceTimer) {
clearTimeout(debounceTimer);
debounceTimer = null;
}
},
async settled() {
await inflightPromise;
},
completeInProgress() {
for (const item of state.values()) {
if (item.status === "in_progress") item.status = "completed";
}
},
renderCollapsible(options?: { completeInProgress?: boolean }): string {
if (state.size === 0) return "";
const shouldCompleteInProgress = options?.completeInProgress === true;
const todos = Array.from(state.values()).map((item) =>
shouldCompleteInProgress && item.status === "in_progress"
? { ...item, status: "completed" as const }
: item
);
const completed = todos.filter((t) => t.status === "completed").length;
const markdown = renderTodoMarkdown(todos);
return `<details>\n<summary>Task list (${completed}/${todos.length} completed)</summary>\n\n${markdown}\n\n</details>`;
},
get enabled() {
return enabled;
},
get hasPublished() {
return hasPublished;
},
};
}