Refactor tasks module to use Preact components
This commit is contained in:
1 parent
fd32459dc0
commit
4e5f6f78f5
2 files changed
+519
-456
No files matched your search
+37
-23
@@ -1,35 +1,49 @@
|
||||
import { el, empty } from "../base/dom.jsx";
|
||||
import { logRow } from "../base/log-row.jsx";
|
||||
import { h } from "preact";
|
||||
import { useRef, useEffect } from "preact/hooks";
|
||||
import { logRow as renderLogRow } from "../base/log-row.jsx";
|
||||
|
||||
/**
|
||||
* Renders one task's persisted attempt log (GET /api/tasks/:id/logs) --
|
||||
* stage timings, iteration counts, model, token/prompt/completion/cached
|
||||
* breakdowns -- the detail the daemon-wide log page never carries because it
|
||||
* only sees the daemon's own component-scoped lines.
|
||||
*
|
||||
* `state` is one of the values loadTaskLogs (in tasks.js) puts in its cache:
|
||||
* undefined while loading, null on fetch failure, or the API response body
|
||||
* once it lands. Row rendering itself is shared with the daemon-wide log via
|
||||
* ui/src/base/log-row.js -- this module only owns the task-attempt framing
|
||||
* and the "no persisted log for this attempt" / "log unavailable" states.
|
||||
*/
|
||||
export function taskLogPanel(state, opts = {}) {
|
||||
function LogRowNode({ entry }) {
|
||||
const ref = useRef(null);
|
||||
useEffect(() => {
|
||||
if (ref.current) ref.current.replaceChildren(renderLogRow(entry));
|
||||
}, [entry]);
|
||||
return <div ref={ref} style={{ display: 'contents' }} />;
|
||||
}
|
||||
|
||||
export function TaskLogPanel({ state, onRetry }) {
|
||||
if (state === undefined) {
|
||||
return el("div.task-log-loading", "Loading attempt log…");
|
||||
return <div className="task-log-loading">Loading attempt log…</div>;
|
||||
}
|
||||
if (state === null) {
|
||||
return el(
|
||||
"div.task-log-error",
|
||||
el("div.task-timeline-detail", "Could not load this attempt's log."),
|
||||
el("button.btn", { onclick: opts.onRetry }, "Retry"),
|
||||
return (
|
||||
<div className="task-log-error">
|
||||
<div className="task-timeline-detail">Could not load this attempt's log.</div>
|
||||
<button className="btn" onClick={onRetry}>Retry</button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
if (state.disabled) {
|
||||
return empty("No persisted log for this attempt", "Task logging is optional and was not enabled for this run.");
|
||||
return (
|
||||
<div className="empty">
|
||||
<div className="empty-title">No persisted log for this attempt</div>
|
||||
<div>Task logging is optional and was not enabled for this run.</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
const entries = state.entries || [];
|
||||
if (!entries.length) {
|
||||
return empty("Nothing recorded", "This attempt's log file has no entries matching the current filter.");
|
||||
return (
|
||||
<div className="empty">
|
||||
<div className="empty-title">Nothing recorded</div>
|
||||
<div>This attempt's log file has no entries matching the current filter.</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
return el("div.log-list.task-log-list", ...entries.map(logRow));
|
||||
return (
|
||||
<div className="log-list task-log-list">
|
||||
{entries.map((entry, i) => (
|
||||
<LogRowNode key={i} entry={entry} />
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
+482
-433
@@ -1,117 +1,162 @@
|
||||
import { h } from "preact";
|
||||
import { useState, useEffect, useRef } from "preact/hooks";
|
||||
import "./tasks.css";
|
||||
import { api } from "../base/api.jsx";
|
||||
import { ago, el, empty, mount, pill } from "../base/dom.jsx";
|
||||
import { statTile } from "../base/statTile.jsx";
|
||||
import { ago } from "../base/dom.jsx";
|
||||
import { statTile as renderStatTile } from "../base/statTile.jsx";
|
||||
import { taskRowA11y } from "./task-row.jsx";
|
||||
import { initialTaskFilter, taskMatchesStatus } from "./task-filters.jsx";
|
||||
import { describeTimelineEvent } from "./timeline-event.jsx";
|
||||
import { taskLogPanel } from "./task-logs.jsx";
|
||||
import { TaskLogPanel } from "./task-logs.jsx";
|
||||
import { actionFor, statusIds, statusKind, statusLabel } from "../base/task-meta.jsx";
|
||||
|
||||
/**
|
||||
* The work board: every task archied knows about, filterable by status and
|
||||
* text, with a per-task event timeline available on click. Task rows carry
|
||||
* carries created_at/updated_at, so age is shown in the list and once
|
||||
* a row's timeline is loaded, sourced from its most recent event.
|
||||
*/
|
||||
export function tasksPage(params = new URLSearchParams()) {
|
||||
const root = el("div");
|
||||
const summaryRow = el("div.grid.grid-4.task-summary");
|
||||
const body = el("tbody");
|
||||
let tasks = [];
|
||||
let filterStatus = initialTaskFilter(params);
|
||||
let search = "";
|
||||
const requestedTask = Number(params.get("task"));
|
||||
let expandedId = Number.isFinite(requestedTask) && requestedTask > 0 ? requestedTask : null;
|
||||
let focusRequestedTask = expandedId !== null;
|
||||
const eventCache = new Map();
|
||||
const logCache = new Map();
|
||||
const logsExpanded = new Set();
|
||||
// Keyed by task id. A single shared string leaked one row's failure into
|
||||
// every other row's action cell.
|
||||
const actionErrors = new Map();
|
||||
// Tasks with an action in flight, so the buttons can be disabled rather
|
||||
// than letting an impatient second click fire the same action twice.
|
||||
const actionsInFlight = new Set();
|
||||
const searchInput = el("input.task-search", {
|
||||
type: "search",
|
||||
placeholder: "Search by title or repo…",
|
||||
oninput: (e) => {
|
||||
search = e.target.value.trim().toLowerCase();
|
||||
renderRows();
|
||||
},
|
||||
});
|
||||
const statusSelect = el(
|
||||
"select.task-filter",
|
||||
{
|
||||
onchange: (e) => {
|
||||
filterStatus = e.target.value;
|
||||
renderRows();
|
||||
},
|
||||
},
|
||||
el("option", { value: "" }, "All statuses"),
|
||||
el("option", { value: "needs_you", selected: filterStatus === "needs_you" }, "Needs you"),
|
||||
...statusIds().map((value) =>
|
||||
el("option", { value, selected: filterStatus === value }, statusLabel(value))),
|
||||
function StatTileNode(props) {
|
||||
const ref = useRef(null);
|
||||
useEffect(() => {
|
||||
if (ref.current) ref.current.replaceChildren(renderStatTile(props));
|
||||
}, [props]);
|
||||
return <div ref={ref} style={{ display: 'contents' }} />;
|
||||
}
|
||||
|
||||
function Pill({ text, kind = "idle" }) {
|
||||
return <span className={`pill pill-${kind}`}>{text}</span>;
|
||||
}
|
||||
|
||||
function Empty({ title, detail }) {
|
||||
return (
|
||||
<div className="empty">
|
||||
<div className="empty-title">{title}</div>
|
||||
{detail && <div>{detail}</div>}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
render();
|
||||
load();
|
||||
function TasksApp({ query }) {
|
||||
const [tasks, setTasks] = useState(null);
|
||||
const [error, setError] = useState(null);
|
||||
const [filterStatus, setFilterStatus] = useState(() => initialTaskFilter(query));
|
||||
const [search, setSearch] = useState("");
|
||||
|
||||
const requestedTask = Number(query.get("task"));
|
||||
const initialExpandedId = Number.isFinite(requestedTask) && requestedTask > 0 ? requestedTask : null;
|
||||
const [expandedId, setExpandedId] = useState(initialExpandedId);
|
||||
const [focusRequestedTask, setFocusRequestedTask] = useState(initialExpandedId !== null);
|
||||
|
||||
const [eventCache, setEventCache] = useState(new Map());
|
||||
const [logCache, setLogCache] = useState(new Map());
|
||||
const [logsExpanded, setLogsExpanded] = useState(new Set());
|
||||
const [actionErrors, setActionErrors] = useState(new Map());
|
||||
const [actionsInFlight, setActionsInFlight] = useState(new Set());
|
||||
|
||||
function render() {
|
||||
mount(
|
||||
root,
|
||||
el(
|
||||
"div.page-head",
|
||||
el(
|
||||
"div",
|
||||
el("h1.page-title", "Tasks"),
|
||||
el("p.page-sub", "Every issue archied has picked up, and where it stands."),
|
||||
),
|
||||
el("div.page-actions", el("button.btn", { onclick: load }, "Refresh")),
|
||||
),
|
||||
summaryRow,
|
||||
el(
|
||||
"div.card",
|
||||
el(
|
||||
"div.card-head",
|
||||
el("div", el("h2.card-title", "All tasks"), el("p.card-sub", "Click a row for its timeline")),
|
||||
el("div.task-filters", searchInput, statusSelect),
|
||||
),
|
||||
el(
|
||||
"div.table-scroll",
|
||||
el(
|
||||
"table.table",
|
||||
el(
|
||||
"thead",
|
||||
el(
|
||||
"tr",
|
||||
...["Repo", "Issue", "Title", "Status", "Workflow", "Stage", "Last activity", "Action"].map((h) => el("th", h)),
|
||||
),
|
||||
),
|
||||
body,
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
async function load() {
|
||||
const load = async () => {
|
||||
try {
|
||||
tasks = await api.tasks();
|
||||
renderSummary();
|
||||
renderRows();
|
||||
if (expandedId && tasks.some((task) => String(task.id) === String(expandedId))) {
|
||||
loadTimeline(expandedId);
|
||||
setError(null);
|
||||
const res = await api.tasks();
|
||||
setTasks(res);
|
||||
if (expandedId && res.some(t => String(t.id) === String(expandedId))) {
|
||||
if (!eventCache.has(expandedId)) {
|
||||
loadTimeline(expandedId);
|
||||
}
|
||||
}
|
||||
focusDirectTask();
|
||||
} catch (err) {
|
||||
mount(summaryRow, el("div.card", empty("Cannot reach archied", String(err.message || err))));
|
||||
mount(body, el("tr", el("td", { colspan: 8 }, empty("Cannot reach archied", String(err.message || err)))));
|
||||
setError(String(err.message || err));
|
||||
setTasks(null);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
function renderSummary() {
|
||||
useEffect(() => {
|
||||
load();
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, []);
|
||||
|
||||
const loadTimeline = async (id) => {
|
||||
try {
|
||||
const events = await api.task(id);
|
||||
setEventCache(prev => new Map(prev).set(id, events || []));
|
||||
} catch {
|
||||
setEventCache(prev => new Map(prev).set(id, null));
|
||||
}
|
||||
};
|
||||
|
||||
const loadTaskLogs = async (id) => {
|
||||
setLogCache(prev => {
|
||||
const next = new Map(prev);
|
||||
next.delete(id);
|
||||
return next;
|
||||
});
|
||||
try {
|
||||
const res = await api.taskLogs(id, { limit: 500 });
|
||||
setLogCache(prev => new Map(prev).set(id, res));
|
||||
} catch {
|
||||
setLogCache(prev => new Map(prev).set(id, null));
|
||||
}
|
||||
};
|
||||
|
||||
const performAction = async (id, action) => {
|
||||
setActionErrors(prev => {
|
||||
const next = new Map(prev);
|
||||
next.delete(id);
|
||||
return next;
|
||||
});
|
||||
setActionsInFlight(prev => new Set(prev).add(id));
|
||||
|
||||
try {
|
||||
await api.taskAction(id, action);
|
||||
setActionsInFlight(prev => {
|
||||
const next = new Set(prev);
|
||||
next.delete(id);
|
||||
return next;
|
||||
});
|
||||
if (action === "archive") setExpandedId(null);
|
||||
await load();
|
||||
} catch (err) {
|
||||
setActionsInFlight(prev => {
|
||||
const next = new Set(prev);
|
||||
next.delete(id);
|
||||
return next;
|
||||
});
|
||||
setActionErrors(prev => new Map(prev).set(id, err.message || "Action failed"));
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (focusRequestedTask && expandedId && tasks) {
|
||||
setFocusRequestedTask(false);
|
||||
// requestAnimationFrame to ensure it has rendered
|
||||
requestAnimationFrame(() => {
|
||||
const row = document.getElementById(`task-row-${expandedId}`);
|
||||
if (row) {
|
||||
row.focus({ preventScroll: true });
|
||||
row.scrollIntoView({ block: "center", behavior: "smooth" });
|
||||
}
|
||||
});
|
||||
}
|
||||
}, [focusRequestedTask, expandedId, tasks]);
|
||||
|
||||
const toggleTask = (id, event) => {
|
||||
if (event) event.stopPropagation();
|
||||
const isExpanding = expandedId !== id;
|
||||
setExpandedId(isExpanding ? id : null);
|
||||
if (isExpanding) {
|
||||
loadTimeline(id);
|
||||
}
|
||||
};
|
||||
|
||||
const toggleLogs = (id) => {
|
||||
setLogsExpanded(prev => {
|
||||
const next = new Set(prev);
|
||||
if (next.has(id)) {
|
||||
next.delete(id);
|
||||
} else {
|
||||
next.add(id);
|
||||
if (!logCache.has(id)) loadTaskLogs(id);
|
||||
}
|
||||
return next;
|
||||
});
|
||||
};
|
||||
|
||||
const renderSummary = () => {
|
||||
if (!tasks) return null;
|
||||
const total = tasks.length;
|
||||
const counts = {};
|
||||
for (const t of tasks) counts[t.status] = (counts[t.status] || 0) + 1;
|
||||
@@ -119,352 +164,356 @@ export function tasksPage(params = new URLSearchParams()) {
|
||||
const needsYou = (counts.waiting_human || 0) + (counts.parked || 0);
|
||||
const delivered = (counts.merged || 0) + (counts.pr_open || 0);
|
||||
|
||||
mount(
|
||||
summaryRow,
|
||||
statTile({
|
||||
label: "Total tasks",
|
||||
value: total,
|
||||
compare: total ? "Everything archied has ever picked up" : "No tasks yet",
|
||||
}),
|
||||
statTile({
|
||||
label: "Working now",
|
||||
value: working,
|
||||
compare: total ? `${working} of ${total} in progress` : "Nothing running",
|
||||
}),
|
||||
statTile({
|
||||
label: "Needs you",
|
||||
value: needsYou,
|
||||
compare: needsYou ? "Parked or awaiting a reply" : "Nothing is blocked",
|
||||
goodDirection: "down",
|
||||
}),
|
||||
statTile({
|
||||
label: "Delivered",
|
||||
value: delivered,
|
||||
compare: total ? `${Math.round((delivered / total) * 100)}% of all tasks` : "No tasks yet",
|
||||
}),
|
||||
return (
|
||||
<div className="grid grid-4 task-summary">
|
||||
<StatTileNode
|
||||
label="Total tasks"
|
||||
value={total}
|
||||
compare={total ? "Everything archied has ever picked up" : "No tasks yet"}
|
||||
/>
|
||||
<StatTileNode
|
||||
label="Working now"
|
||||
value={working}
|
||||
compare={total ? `${working} of ${total} in progress` : "Nothing running"}
|
||||
/>
|
||||
<StatTileNode
|
||||
label="Needs you"
|
||||
value={needsYou}
|
||||
compare={needsYou ? "Parked or awaiting a reply" : "Nothing is blocked"}
|
||||
goodDirection="down"
|
||||
/>
|
||||
<StatTileNode
|
||||
label="Delivered"
|
||||
value={delivered}
|
||||
compare={total ? `${Math.round((delivered / total) * 100)}% of all tasks` : "No tasks yet"}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
function filtered() {
|
||||
return tasks.filter((t) => {
|
||||
const renderTaskActions = (t) => {
|
||||
const message = actionErrors.get(t.id);
|
||||
const errorMsg = message ? <span className="task-action-error">{message}</span> : null;
|
||||
const busy = actionsInFlight.has(t.id);
|
||||
|
||||
const variantToken = (kind) =>
|
||||
kind === "quiet" ? "btn-quiet" : kind === "danger" ? "btn-danger" : "";
|
||||
|
||||
const controls = (t.actions || []).map(action => {
|
||||
const meta = actionFor(action);
|
||||
if (!meta) return null;
|
||||
const title = t.title || "this task";
|
||||
const confirmText = meta.confirm ? meta.confirm.replaceAll("{title}", title) : undefined;
|
||||
const cls = ["button", "btn", "btn-small", variantToken(meta.kind)].filter(Boolean).join(" ");
|
||||
|
||||
if (meta.kind === "link") {
|
||||
if (action === "open_pr") return taskLink(t, "pull_request", t.pr_number, meta.label);
|
||||
if (action === "open_issue") return taskLink(t, "issue", t.issue_number, meta.label);
|
||||
}
|
||||
|
||||
return (
|
||||
<button
|
||||
key={action}
|
||||
className={cls}
|
||||
type="button"
|
||||
disabled={busy}
|
||||
onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
if (confirmText && !window.confirm(confirmText)) return;
|
||||
performAction(t.id, action);
|
||||
}}
|
||||
>
|
||||
{meta.label}
|
||||
</button>
|
||||
);
|
||||
}).filter(Boolean);
|
||||
|
||||
if (!controls.length && !errorMsg) return "—";
|
||||
return <div className="task-actions">{errorMsg}{controls}</div>;
|
||||
};
|
||||
|
||||
const taskLink = (t, kind, number, label) => {
|
||||
const href = kind === "issue" ? t.issue_url : t.pr_url;
|
||||
if (!href || !number || (kind === "issue" && t.source === "chat")) {
|
||||
return (
|
||||
<button className="btn btn-small" type="button" disabled title={`${label} is unavailable`}>
|
||||
{label}
|
||||
</button>
|
||||
);
|
||||
}
|
||||
return (
|
||||
<a className="btn btn-small" href={href} target="_blank" rel="noreferrer" onClick={e => e.stopPropagation()}>
|
||||
{label}
|
||||
</a>
|
||||
);
|
||||
};
|
||||
|
||||
const repoLink = (t) => {
|
||||
const base = t.repo_url;
|
||||
if (!base) return null;
|
||||
return (
|
||||
<a
|
||||
className="task-source-link"
|
||||
href={base}
|
||||
target="_blank"
|
||||
rel="noreferrer"
|
||||
title={`Open ${t.owner}/${t.repo} on the forge`}
|
||||
onClick={e => e.stopPropagation()}
|
||||
>
|
||||
{t.owner}/{t.repo}
|
||||
</a>
|
||||
);
|
||||
};
|
||||
|
||||
const issueLink = (t) => {
|
||||
const href = t.issue_url;
|
||||
if (t.source === "chat" || !href || !t.issue_number) return null;
|
||||
return (
|
||||
<a
|
||||
className="task-source-link"
|
||||
href={href}
|
||||
target="_blank"
|
||||
rel="noreferrer"
|
||||
title={`Open issue #${t.issue_number} on the forge`}
|
||||
onClick={e => e.stopPropagation()}
|
||||
>
|
||||
#{t.issue_number}
|
||||
</a>
|
||||
);
|
||||
};
|
||||
|
||||
const renderTimelineRow = (t) => {
|
||||
const cached = eventCache.get(t.id);
|
||||
const logsExpandedForTask = logsExpanded.has(t.id);
|
||||
|
||||
return (
|
||||
<tr className="task-timeline-row" id={`task-timeline-${t.id}`}>
|
||||
<td colSpan={8}>
|
||||
<div className="task-detail-actions">
|
||||
<div className="task-decision-title">Available actions</div>
|
||||
{renderTaskActions(t)}
|
||||
</div>
|
||||
{t.status === "waiting_human" && t.plan && (
|
||||
<div className="task-decision">
|
||||
<div className="task-decision-title">Decision required</div>
|
||||
<div className="task-decision-plan">{t.plan}</div>
|
||||
</div>
|
||||
)}
|
||||
{t.status === "parked" && t.park_reason && (
|
||||
<div className="task-decision">
|
||||
<div className="task-decision-title">Park reason</div>
|
||||
<div className="task-decision-plan">{t.park_reason}</div>
|
||||
</div>
|
||||
)}
|
||||
{cached === undefined ? (
|
||||
<div className="task-timeline-loading">Loading timeline…</div>
|
||||
) : cached === null ? (
|
||||
<div className="task-timeline-error">
|
||||
<div className="task-timeline-title">Could not load timeline</div>
|
||||
<div className="task-timeline-detail">archied did not answer for this task's events. It may be restarting — retry, or check the daemon.</div>
|
||||
<button className="btn" onClick={() => loadTimeline(t.id)}>Retry</button>
|
||||
</div>
|
||||
) : cached.length ? (
|
||||
<ul className="task-timeline">
|
||||
{cached.map((ev, i) => {
|
||||
const desc = describeTimelineEvent(ev);
|
||||
return (
|
||||
<li className="task-timeline-entry" key={i}>
|
||||
<span className="task-timeline-dot"></span>
|
||||
<div>
|
||||
<div className="task-timeline-kind">{desc.title}</div>
|
||||
{desc.detail && <div className="task-timeline-detail">{desc.detail}</div>}
|
||||
<div className="task-timeline-when">{ago(ev.at)}</div>
|
||||
</div>
|
||||
</li>
|
||||
);
|
||||
})}
|
||||
</ul>
|
||||
) : (
|
||||
<Empty title="No events yet" detail="This task has not recorded any transitions." />
|
||||
)}
|
||||
<div className="task-logs-section">
|
||||
<div className="task-decision-title">
|
||||
Attempt log
|
||||
<button className="btn btn-small" onClick={() => toggleLogs(t.id)} style={{ marginLeft: "10px" }}>
|
||||
{logsExpandedForTask ? "Hide" : "Show"}
|
||||
</button>
|
||||
</div>
|
||||
{logsExpandedForTask && (
|
||||
<TaskLogPanel state={logCache.get(t.id)} onRetry={() => loadTaskLogs(t.id)} />
|
||||
)}
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
);
|
||||
};
|
||||
|
||||
const renderBody = () => {
|
||||
if (error) {
|
||||
return (
|
||||
<tr>
|
||||
<td colSpan={8}>
|
||||
<Empty title="Cannot reach archied" detail={error} />
|
||||
</td>
|
||||
</tr>
|
||||
);
|
||||
}
|
||||
if (!tasks) {
|
||||
return (
|
||||
<tr>
|
||||
<td colSpan={8}>
|
||||
<div className="task-timeline-loading">Loading tasks...</div>
|
||||
</td>
|
||||
</tr>
|
||||
);
|
||||
}
|
||||
if (!tasks.length) {
|
||||
return (
|
||||
<tr>
|
||||
<td colSpan={8}>
|
||||
<Empty title="No tasks yet" detail="Tasks appear here once archied picks up an issue to work." />
|
||||
</td>
|
||||
</tr>
|
||||
);
|
||||
}
|
||||
|
||||
const filtered = tasks.filter(t => {
|
||||
if (!taskMatchesStatus(t, filterStatus)) return false;
|
||||
if (!search) return true;
|
||||
const hay = `${t.title} ${t.repo}`.toLowerCase();
|
||||
return hay.includes(search);
|
||||
});
|
||||
}
|
||||
|
||||
function renderRows() {
|
||||
const rows = filtered();
|
||||
if (!tasks.length) {
|
||||
mount(
|
||||
body,
|
||||
el(
|
||||
"tr",
|
||||
el(
|
||||
"td",
|
||||
{ colspan: 8 },
|
||||
empty("No tasks yet", "Tasks appear here once archied picks up an issue to work."),
|
||||
),
|
||||
),
|
||||
if (!filtered.length) {
|
||||
return (
|
||||
<tr>
|
||||
<td colSpan={8}>
|
||||
<Empty title="No matching tasks" detail="Try a different search or status filter." />
|
||||
</td>
|
||||
</tr>
|
||||
);
|
||||
return;
|
||||
}
|
||||
if (!rows.length) {
|
||||
mount(
|
||||
body,
|
||||
el("tr", el("td", { colspan: 8 }, empty("No matching tasks", "Try a different search or status filter."))),
|
||||
);
|
||||
return;
|
||||
}
|
||||
body.replaceChildren();
|
||||
for (const t of rows) {
|
||||
body.append(taskRow(t));
|
||||
if (expandedId === t.id) body.append(timelineRow(t));
|
||||
}
|
||||
focusDirectTask();
|
||||
}
|
||||
|
||||
function focusDirectTask() {
|
||||
if (!focusRequestedTask || !expandedId) return;
|
||||
focusRequestedTask = false;
|
||||
requestAnimationFrame(() => {
|
||||
const row = document.getElementById(`task-row-${expandedId}`);
|
||||
if (!row) return;
|
||||
row.focus({ preventScroll: true });
|
||||
row.scrollIntoView({ block: "center", behavior: "smooth" });
|
||||
return filtered.map(t => {
|
||||
const expanded = expandedId === t.id;
|
||||
const timelineID = `task-timeline-${t.id}`;
|
||||
return (
|
||||
<h.Fragment key={t.id}>
|
||||
<tr
|
||||
className="task-row"
|
||||
id={`task-row-${t.id}`}
|
||||
tabIndex={0}
|
||||
onKeyDown={(e) => {
|
||||
if (e.key === "Enter" || e.key === " ") {
|
||||
e.preventDefault();
|
||||
toggleTask(t.id, e);
|
||||
}
|
||||
}}
|
||||
onClick={(e) => toggleTask(t.id, e)}
|
||||
{...taskRowA11y(t, expanded)}
|
||||
>
|
||||
<td className="mono" data-label="Repository">
|
||||
<button
|
||||
className="task-expand"
|
||||
type="button"
|
||||
aria-expanded={String(expanded)}
|
||||
aria-controls={timelineID}
|
||||
aria-label={`${expanded ? "Collapse" : "Expand"} timeline for ${t.title || "untitled task"}`}
|
||||
onClick={(e) => toggleTask(t.id, e)}
|
||||
>
|
||||
{expanded ? "−" : "+"}
|
||||
</button>
|
||||
{repoLink(t) ?? `${t.owner}/${t.repo}`}
|
||||
</td>
|
||||
<td className="mono" data-label="Issue">
|
||||
{issueLink(t) ?? `#${t.issue_number}`}
|
||||
</td>
|
||||
<td className="strong" data-label="Title">
|
||||
{t.title || "(untitled)"}
|
||||
</td>
|
||||
<td data-label="Status">
|
||||
<Pill text={statusLabel(t.status)} kind={statusKind(t.status)} />
|
||||
</td>
|
||||
<td data-label="Workflow">{t.workflow || "—"}</td>
|
||||
<td data-label="Stage">{t.stage || "—"}</td>
|
||||
<td
|
||||
data-label="Last activity"
|
||||
title={t.created_at ? `Created ${ago(t.created_at)}` : ""}
|
||||
>
|
||||
{ago(t.updated_at)}
|
||||
</td>
|
||||
<td data-label="Actions">{renderTaskActions(t)}</td>
|
||||
</tr>
|
||||
{expanded && renderTimelineRow(t)}
|
||||
</h.Fragment>
|
||||
);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
function taskRow(t) {
|
||||
const expanded = expandedId === t.id;
|
||||
const timelineID = `task-timeline-${t.id}`;
|
||||
const toggle = (event) => {
|
||||
event?.stopPropagation();
|
||||
expandedId = expandedId === t.id ? null : t.id;
|
||||
renderRows();
|
||||
if (expandedId === t.id) loadTimeline(t.id);
|
||||
};
|
||||
return el(
|
||||
"tr.task-row",
|
||||
{
|
||||
...taskRowA11y(t, expanded),
|
||||
id: `task-row-${t.id}`,
|
||||
tabindex: "0",
|
||||
onkeydown: (event) => {
|
||||
if (event.key !== "Enter" && event.key !== " ") return;
|
||||
event.preventDefault();
|
||||
toggle(event);
|
||||
},
|
||||
onclick: toggle,
|
||||
},
|
||||
el(
|
||||
"td.mono",
|
||||
{ "data-label": "Repository" },
|
||||
el(
|
||||
"button.task-expand",
|
||||
{
|
||||
type: "button",
|
||||
"aria-expanded": String(expanded),
|
||||
"aria-controls": timelineID,
|
||||
"aria-label": `${expanded ? "Collapse" : "Expand"} timeline for ${t.title || "untitled task"}`,
|
||||
onclick: toggle,
|
||||
},
|
||||
expanded ? "−" : "+",
|
||||
),
|
||||
repoLink(t) ?? `${t.owner}/${t.repo}`,
|
||||
),
|
||||
el("td.mono", { "data-label": "Issue" }, issueLink(t) ?? `#${t.issue_number}`),
|
||||
el("td.strong", { "data-label": "Title" }, t.title || "(untitled)"),
|
||||
el("td", { "data-label": "Status" }, pill(statusLabel(t.status), statusKind(t.status))),
|
||||
el("td", { "data-label": "Workflow" }, t.workflow || "—"),
|
||||
el("td", { "data-label": "Stage" }, t.stage || "—"),
|
||||
// updated_at moves on every transition, so this reads as "how long has
|
||||
// it been sitting like this" rather than "how old is the task".
|
||||
el(
|
||||
"td",
|
||||
{ "data-label": "Last activity", title: t.created_at ? `Created ${ago(t.created_at)}` : "" },
|
||||
ago(t.updated_at),
|
||||
),
|
||||
el("td", { "data-label": "Actions" }, taskActions(t)),
|
||||
);
|
||||
}
|
||||
return (
|
||||
<div>
|
||||
<div className="page-head">
|
||||
<div>
|
||||
<h1 className="page-title">Tasks</h1>
|
||||
<p className="page-sub">Every issue archied has picked up, and where it stands.</p>
|
||||
</div>
|
||||
<div className="page-actions">
|
||||
<button className="btn" onClick={load}>Refresh</button>
|
||||
</div>
|
||||
</div>
|
||||
{error ? (
|
||||
<div className="grid grid-4 task-summary">
|
||||
<div className="card">
|
||||
<Empty title="Cannot reach archied" detail={error} />
|
||||
</div>
|
||||
</div>
|
||||
) : (
|
||||
renderSummary()
|
||||
)}
|
||||
<div className="card">
|
||||
<div className="card-head">
|
||||
<div>
|
||||
<h2 className="card-title">All tasks</h2>
|
||||
<p className="card-sub">Click a row for its timeline</p>
|
||||
</div>
|
||||
<div className="task-filters">
|
||||
<input
|
||||
className="task-search"
|
||||
type="search"
|
||||
placeholder="Search by title or repo…"
|
||||
value={search}
|
||||
onInput={e => setSearch(e.target.value.trim().toLowerCase())}
|
||||
/>
|
||||
<select
|
||||
className="task-filter"
|
||||
value={filterStatus}
|
||||
onChange={e => setFilterStatus(e.target.value)}
|
||||
>
|
||||
<option value="">All statuses</option>
|
||||
<option value="needs_you">Needs you</option>
|
||||
{statusIds().map(value => (
|
||||
<option key={value} value={value}>{statusLabel(value)}</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
<div className="table-scroll">
|
||||
<table className="table">
|
||||
<thead>
|
||||
<tr>
|
||||
{["Repo", "Issue", "Title", "Status", "Workflow", "Stage", "Last activity", "Action"].map(hText => (
|
||||
<th key={hText}>{hText}</th>
|
||||
))}
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>{renderBody()}</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function taskActions(t) {
|
||||
const message = actionErrors.get(t.id);
|
||||
const error = message ? el("span.task-action-error", message) : null;
|
||||
const busy = actionsInFlight.has(t.id);
|
||||
|
||||
// The row itself toggles the timeline on click, so every button has to
|
||||
// stop the event or acting on a task also expands it.
|
||||
const button = (cls, action, label, confirmText) =>
|
||||
el(
|
||||
cls,
|
||||
{
|
||||
type: "button",
|
||||
disabled: busy,
|
||||
onclick: (e) => {
|
||||
e.stopPropagation();
|
||||
if (confirmText && !window.confirm(confirmText)) return;
|
||||
performAction(t.id, action);
|
||||
},
|
||||
},
|
||||
label,
|
||||
);
|
||||
|
||||
// Everything is derived from the server catalog: an action added on the
|
||||
// backend renders here the moment task-meta ships it, no frontend redeploy
|
||||
// needed. label, variant and confirm text all come from actionFor(action);
|
||||
// a new variant kind just needs a token below, not a new case.
|
||||
const variantToken = (kind) =>
|
||||
kind === "quiet" ? "btn-quiet" : kind === "danger" ? "btn-danger" : "";
|
||||
|
||||
const controls = (t.actions || [])
|
||||
.map((action) => {
|
||||
const meta = actionFor(action);
|
||||
if (!meta) return null; // unknown id -> render nothing for this entry
|
||||
const title = t.title || "this task";
|
||||
const confirmText = meta.confirm ? meta.confirm.replaceAll("{title}", title) : undefined;
|
||||
const cls = ["button", "btn", "btn-small", variantToken(meta.kind)].filter(Boolean).join(".");
|
||||
if (meta.kind === "link") {
|
||||
// Only the forge links have a destination to navigate to; the label
|
||||
// comes from the catalog rather than a hardcoded "Open PR".
|
||||
if (action === "open_pr") return taskLink(t, "pull_request", t.pr_number, meta.label);
|
||||
if (action === "open_issue") return taskLink(t, "issue", t.issue_number, meta.label);
|
||||
}
|
||||
return button(cls, action, meta.label, confirmText);
|
||||
})
|
||||
.filter(Boolean);
|
||||
if (!controls.length && !error) return "—";
|
||||
return el("div.task-actions", error, controls);
|
||||
}
|
||||
|
||||
async function performAction(id, action) {
|
||||
actionErrors.delete(id);
|
||||
actionsInFlight.add(id);
|
||||
renderRows();
|
||||
try {
|
||||
await api.taskAction(id, action);
|
||||
actionsInFlight.delete(id);
|
||||
if (action === "archive") expandedId = null;
|
||||
await load();
|
||||
} catch (err) {
|
||||
actionsInFlight.delete(id);
|
||||
actionErrors.set(id, err.message || "Action failed");
|
||||
renderRows();
|
||||
}
|
||||
}
|
||||
|
||||
function taskLink(t, kind, number, label) {
|
||||
const href = kind === "issue" ? t.issue_url : t.pr_url;
|
||||
if (!href || !number || (kind === "issue" && t.source === "chat")) {
|
||||
return el(
|
||||
"button.btn.btn-small",
|
||||
{ type: "button", disabled: true, title: `${label} is unavailable` },
|
||||
label,
|
||||
);
|
||||
}
|
||||
return el(
|
||||
"a.btn.btn-small",
|
||||
{
|
||||
href,
|
||||
target: "_blank",
|
||||
rel: "noreferrer",
|
||||
onclick: (event) => event.stopPropagation(),
|
||||
},
|
||||
label,
|
||||
);
|
||||
}
|
||||
|
||||
function repoLink(t) {
|
||||
const base = t.repo_url;
|
||||
if (!base) return null;
|
||||
return el(
|
||||
"a.task-source-link",
|
||||
{
|
||||
href: base,
|
||||
target: "_blank",
|
||||
rel: "noreferrer",
|
||||
title: `Open ${t.owner}/${t.repo} on the forge`,
|
||||
onclick: (e) => e.stopPropagation(),
|
||||
},
|
||||
`${t.owner}/${t.repo}`,
|
||||
);
|
||||
}
|
||||
|
||||
function issueLink(t) {
|
||||
const href = t.issue_url;
|
||||
// A chat-sourced task has no issue on the forge, but its repo may still
|
||||
// exist -- the repo link stays, the issue link does not.
|
||||
if (t.source === "chat" || !href || !t.issue_number) return null;
|
||||
return el(
|
||||
"a.task-source-link",
|
||||
{
|
||||
href,
|
||||
target: "_blank",
|
||||
rel: "noreferrer",
|
||||
title: `Open issue #${t.issue_number} on the forge`,
|
||||
onclick: (e) => e.stopPropagation(),
|
||||
},
|
||||
`#${t.issue_number}`,
|
||||
);
|
||||
}
|
||||
|
||||
function timelineRow(t) {
|
||||
const cached = eventCache.get(t.id);
|
||||
return el(
|
||||
"tr.task-timeline-row",
|
||||
{ id: `task-timeline-${t.id}` },
|
||||
el(
|
||||
"td",
|
||||
{ colspan: 8 },
|
||||
el(
|
||||
"div.task-detail-actions",
|
||||
el("div.task-decision-title", "Available actions"),
|
||||
taskActions(t),
|
||||
),
|
||||
t.status === "waiting_human" && t.plan
|
||||
? el("div.task-decision", el("div.task-decision-title", "Decision required"), el("div.task-decision-plan", t.plan))
|
||||
: t.status === "parked" && t.park_reason
|
||||
? el("div.task-decision", el("div.task-decision-title", "Park reason"), el("div.task-decision-plan", t.park_reason))
|
||||
: null,
|
||||
cached === undefined
|
||||
? el("div.task-timeline-loading", "Loading timeline…")
|
||||
: cached === null
|
||||
? el(
|
||||
"div.task-timeline-error",
|
||||
el("div.task-timeline-title", "Could not load timeline"),
|
||||
el(
|
||||
"div.task-timeline-detail",
|
||||
"archied did not answer for this task's events. It may be restarting — retry, or check the daemon.",
|
||||
),
|
||||
el("button.btn", { onclick: () => loadTimeline(t.id) }, "Retry"),
|
||||
)
|
||||
: cached.length
|
||||
? el("ul.task-timeline", ...cached.map(timelineEntry))
|
||||
: empty("No events yet", "This task has not recorded any transitions."),
|
||||
logsSection(t),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
function logsSection(t) {
|
||||
const expanded = logsExpanded.has(t.id);
|
||||
const toggle = () => {
|
||||
if (expanded) {
|
||||
logsExpanded.delete(t.id);
|
||||
} else {
|
||||
logsExpanded.add(t.id);
|
||||
if (!logCache.has(t.id)) loadTaskLogs(t.id);
|
||||
}
|
||||
renderRows();
|
||||
};
|
||||
return el(
|
||||
"div.task-logs-section",
|
||||
el(
|
||||
"div.task-decision-title",
|
||||
"Attempt log",
|
||||
el("button.btn.btn-small", { onclick: toggle }, expanded ? "Hide" : "Show"),
|
||||
),
|
||||
expanded ? taskLogPanel(logCache.get(t.id), { onRetry: () => loadTaskLogs(t.id) }) : null,
|
||||
);
|
||||
}
|
||||
|
||||
async function loadTaskLogs(id) {
|
||||
logCache.delete(id);
|
||||
renderRows();
|
||||
try {
|
||||
const res = await api.taskLogs(id, { limit: 500 });
|
||||
logCache.set(id, res);
|
||||
} catch {
|
||||
logCache.set(id, null);
|
||||
}
|
||||
renderRows();
|
||||
}
|
||||
|
||||
function timelineEntry(ev) {
|
||||
const description = describeTimelineEvent(ev);
|
||||
return el(
|
||||
"li.task-timeline-entry",
|
||||
el("span.task-timeline-dot"),
|
||||
el(
|
||||
"div",
|
||||
el("div.task-timeline-kind", description.title),
|
||||
description.detail && el("div.task-timeline-detail", description.detail),
|
||||
el("div.task-timeline-when", ago(ev.at)),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
async function loadTimeline(id) {
|
||||
try {
|
||||
const events = await api.task(id);
|
||||
eventCache.set(id, events || []);
|
||||
} catch {
|
||||
eventCache.set(id, null);
|
||||
}
|
||||
renderRows();
|
||||
}
|
||||
|
||||
return root;
|
||||
export function tasksPage(query) {
|
||||
return <TasksApp query={query} />;
|
||||
}
|
||||
Reference in new issue
Block a user