feat: preserve Herdr task counts
This commit is contained in:
+18
-9
@@ -3,7 +3,8 @@ import type { Task } from "./types.ts";
|
||||
import { orderedTreeRows } from "./ui.ts";
|
||||
|
||||
const SOURCE = "pi-todo-herdr";
|
||||
const TOKEN = "task";
|
||||
const TASK_TOKEN = "task";
|
||||
const COUNT_TOKEN = "task_count";
|
||||
let reportSeq = Date.now() * 1000;
|
||||
|
||||
function nextSeq(): number {
|
||||
@@ -24,7 +25,7 @@ function hasDescendantWithStatus(task: Task, status: Task["status"], byId: Map<n
|
||||
return false;
|
||||
}
|
||||
|
||||
export function currentTaskLabel(tasks: Task[]): string | null {
|
||||
export function currentTaskTokens(tasks: Task[]): Record<string, string | null> {
|
||||
const byId = new Map(tasks.map((task) => [task.id, task]));
|
||||
const ordered = orderedTreeRows(tasks).map(({ task }) => task);
|
||||
const inProgress = ordered.filter(
|
||||
@@ -34,9 +35,17 @@ export function currentTaskLabel(tasks: Task[]): string | null {
|
||||
inProgress.length > 0
|
||||
? inProgress
|
||||
: ordered.filter((task) => task.status === "blocked" && !hasDescendantWithStatus(task, "blocked", byId));
|
||||
if (candidates.length === 0) return null;
|
||||
const suffix = candidates.length > 1 ? ` +${candidates.length - 1}` : "";
|
||||
return `#${candidates[0]!.id} ${candidates[0]!.name}${suffix}`;
|
||||
return {
|
||||
[TASK_TOKEN]: candidates.length > 0 ? `#${candidates[0]!.id} ${candidates[0]!.name}` : null,
|
||||
[COUNT_TOKEN]: candidates.length > 1 ? `+${candidates.length - 1}` : null,
|
||||
};
|
||||
}
|
||||
|
||||
export function currentTaskLabel(tasks: Task[]): string | null {
|
||||
const tokens = currentTaskTokens(tasks);
|
||||
const task = tokens[TASK_TOKEN];
|
||||
const count = tokens[COUNT_TOKEN];
|
||||
return task ? `${task}${count ? ` ${count}` : ""}` : null;
|
||||
}
|
||||
|
||||
function sendAttempt(request: unknown, timeoutMs: number): Promise<boolean> {
|
||||
@@ -69,7 +78,7 @@ async function send(request: unknown): Promise<void> {
|
||||
await sendAttempt(request, 1500);
|
||||
}
|
||||
|
||||
async function reportToken(value: string | null): Promise<void> {
|
||||
async function reportTokens(tokens: Record<string, string | null>): Promise<void> {
|
||||
if (!isHerdrEnvironment()) return;
|
||||
await send({
|
||||
id: `${SOURCE}:${Date.now()}:${Math.random().toString(36).slice(2)}`,
|
||||
@@ -77,16 +86,16 @@ async function reportToken(value: string | null): Promise<void> {
|
||||
params: {
|
||||
pane_id: process.env.HERDR_PANE_ID,
|
||||
source: SOURCE,
|
||||
tokens: { [TOKEN]: value },
|
||||
tokens,
|
||||
seq: nextSeq(),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function reportCurrentTask(tasks: Task[]): Promise<void> {
|
||||
return reportToken(currentTaskLabel(tasks));
|
||||
return reportTokens(currentTaskTokens(tasks));
|
||||
}
|
||||
|
||||
export function clearCurrentTask(): Promise<void> {
|
||||
return reportToken(null);
|
||||
return reportTokens({ [TASK_TOKEN]: null, [COUNT_TOKEN]: null });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user