2026-10-15 07:23:17 -07:00
|
|
|
import { get, post } from "./api.js";
|
|
|
|
|
import { errorText, h, replace, showAlert } from "./dom.js";
|
|
|
|
|
import { markdown } from "./markdown.js";
|
|
|
|
|
import { models } from "./task.js";
|
2026-10-15 09:59:13 -07:00
|
|
|
import { currentSession, focusedProject, focusTask, on, onEach, state } from "./state.js";
|
2026-10-15 07:23:17 -07:00
|
|
|
const refreshKinds = new Set(["task.merged", "project.created"]);
|
|
|
|
|
export function mountTodo(root) {
|
|
|
|
|
const todos = new Map();
|
|
|
|
|
const starting = new Set();
|
|
|
|
|
let disposed = false;
|
|
|
|
|
const render = () => {
|
|
|
|
|
const scrollTop = root.querySelector(".todo-view")?.scrollTop ?? 0;
|
2026-10-15 09:59:13 -07:00
|
|
|
const p = focusedProject();
|
|
|
|
|
replace(root, h("div", { class: "todo-view" }, p ? projectBlock(p, todos.get(p.id), starting, start) : h("div", { class: "empty" }, "No project selected.")));
|
2026-10-15 07:23:17 -07:00
|
|
|
root.querySelector(".todo-view").scrollTop = scrollTop;
|
|
|
|
|
};
|
|
|
|
|
const load = async (projectId) => {
|
|
|
|
|
try {
|
|
|
|
|
const todo = await get("/api/projects/{projectId}/todo", { path: { projectId } });
|
|
|
|
|
todos.set(projectId, todo);
|
|
|
|
|
}
|
|
|
|
|
catch (err) {
|
|
|
|
|
todos.set(projectId, `Couldn't read TODO.md: ${errorText(err)}`);
|
|
|
|
|
}
|
|
|
|
|
if (!disposed) {
|
|
|
|
|
render();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
const refresh = () => {
|
2026-10-15 09:59:13 -07:00
|
|
|
const p = focusedProject();
|
2026-10-15 09:40:30 -07:00
|
|
|
if (p) {
|
2026-10-15 07:23:17 -07:00
|
|
|
void load(p.id);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
const start = async (project, item) => {
|
|
|
|
|
const key = `${project.id}\n${item.title}`;
|
|
|
|
|
starting.add(key);
|
|
|
|
|
render();
|
|
|
|
|
try {
|
|
|
|
|
const task = await post("/api/projects/{projectId}/todo/tasks", { path: { projectId: project.id }, body: { title: item.title, model: models[0] } });
|
2026-10-15 09:40:30 -07:00
|
|
|
focusTask(task.id);
|
2026-10-15 07:23:17 -07:00
|
|
|
}
|
|
|
|
|
catch (err) {
|
|
|
|
|
void showAlert(errorText(err));
|
|
|
|
|
}
|
|
|
|
|
starting.delete(key);
|
|
|
|
|
if (!disposed) {
|
|
|
|
|
render();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
const offs = [
|
|
|
|
|
on("event", (detail) => {
|
|
|
|
|
const ev = detail;
|
|
|
|
|
if (!refreshKinds.has(ev.kind)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (ev.project_id) {
|
|
|
|
|
void load(ev.project_id);
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
on("connection", () => {
|
|
|
|
|
if (state.connected) {
|
|
|
|
|
refresh();
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
on("projects", refresh),
|
2026-10-15 09:59:13 -07:00
|
|
|
onEach(["focus", "selection"], () => {
|
2026-10-15 09:40:30 -07:00
|
|
|
render();
|
2026-10-15 09:59:13 -07:00
|
|
|
const p = focusedProject();
|
2026-10-15 09:40:30 -07:00
|
|
|
if (p && !todos.has(p.id)) {
|
|
|
|
|
void load(p.id);
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
onEach(["tasks", "sessions"], render),
|
2026-10-15 07:23:17 -07:00
|
|
|
];
|
|
|
|
|
render();
|
|
|
|
|
refresh();
|
|
|
|
|
return () => {
|
|
|
|
|
disposed = true;
|
|
|
|
|
for (const off of offs) {
|
|
|
|
|
off();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
function projectBlock(project, todo, starting, start) {
|
|
|
|
|
const head = h("h2", {}, project.name, " ", h("span", { class: "repo" }, `TODO.md on ${project.default_branch}`));
|
|
|
|
|
if (todo === undefined) {
|
|
|
|
|
return h("section", { class: "project" }, head, h("p", { class: "empty-row" }, "Loading…"));
|
|
|
|
|
}
|
|
|
|
|
if (typeof todo === "string") {
|
|
|
|
|
return h("section", { class: "project" }, head, h("p", { class: "empty-row error-text" }, todo));
|
|
|
|
|
}
|
|
|
|
|
if (!todo.exists) {
|
|
|
|
|
return h("section", { class: "project" }, head, h("p", { class: "empty-row" }, `No TODO.md on ${project.default_branch}.`));
|
|
|
|
|
}
|
|
|
|
|
if (todo.items.length === 0) {
|
|
|
|
|
return h("section", { class: "project" }, head, h("p", { class: "empty-row" }, "TODO.md has no items."));
|
|
|
|
|
}
|
|
|
|
|
const active = (state.tasks.get(project.id) ?? []).filter((t) => t.status === "active" && t.todo_item);
|
|
|
|
|
const children = [];
|
|
|
|
|
let section = "";
|
|
|
|
|
let list;
|
|
|
|
|
for (const item of todo.items) {
|
|
|
|
|
if (!list || item.section !== section) {
|
|
|
|
|
section = item.section;
|
|
|
|
|
if (section) {
|
|
|
|
|
children.push(h("h3", { class: "todo-section" }, section));
|
|
|
|
|
}
|
|
|
|
|
list = h("ul", { class: "todo-list" });
|
|
|
|
|
children.push(list);
|
|
|
|
|
}
|
|
|
|
|
const task = active.find((t) => t.todo_item === item.title);
|
|
|
|
|
let action;
|
|
|
|
|
if (task) {
|
|
|
|
|
const sess = currentSession(task.id);
|
2026-10-15 09:40:30 -07:00
|
|
|
action = h("a", {
|
|
|
|
|
class: "todo-task",
|
|
|
|
|
href: `#/task/${task.id}`,
|
|
|
|
|
onclick: (e) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
focusTask(task.id);
|
|
|
|
|
},
|
|
|
|
|
}, h("span", { class: `dot ${sess?.state ?? "none"}`, title: sess?.state ?? "no session" }), sess?.state ?? "no agent");
|
2026-10-15 07:23:17 -07:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
const busy = starting.has(`${project.id}\n${item.title}`);
|
|
|
|
|
const button = h("button", { disabled: busy }, busy ? "Starting…" : "Start");
|
|
|
|
|
button.onclick = () => void start(project, item);
|
|
|
|
|
action = button;
|
|
|
|
|
}
|
|
|
|
|
list.append(h("li", { class: task && task.id === state.selectedTaskId ? "selected" : "" }, h("div", { class: "todo-head" }, h("span", { class: "todo-title" }, item.title), action), item.body && markdown(item.body, "md todo-body")));
|
|
|
|
|
}
|
|
|
|
|
return h("section", { class: "project" }, head, ...children);
|
|
|
|
|
}
|
|
|
|
|
//# sourceMappingURL=todo.js.map
|