Files
xboct-flow/src/ui/resolveTaskRef.ts
T

53 lines
1.5 KiB
TypeScript

import * as vscode from "vscode";
import { listLocatedTasks } from "@/commands/listLocatedTasks";
import { resolveTaskLocations } from "@/commands/resolveTaskLocations";
import { TaskLocation } from "@/model/taskLocation";
import { IConfigProvider, ITaskRepository } from "@/ports";
import { TaskTreeItem } from "@/views/taskTreeItem";
import { TASK_SCOPE_LABELS, TASK_STATUS_LABELS } from "./taskLabels";
export type TaskRef = {
id: string;
location: TaskLocation;
};
/**
* Resolve task id + location from a TreeView item, or QuickPick when
* the command is invoked from the Command Palette.
*/
export async function resolveTaskRef(
repo: ITaskRepository,
config: IConfigProvider,
item?: unknown,
): Promise<TaskRef | undefined> {
if (item instanceof TaskTreeItem) {
return { id: item.task.id, location: item.location };
}
const locations = resolveTaskLocations(config);
if (locations.length === 0) {
return undefined;
}
const result = await listLocatedTasks(repo, locations);
if (result.isErr() || result.value.length === 0) {
void vscode.window.showInformationMessage("No tasks found");
return undefined;
}
const picked = await vscode.window.showQuickPick(
result.value.map((located) => ({
label: located.task.title,
description: `${TASK_SCOPE_LABELS[located.location.scope]} · ${TASK_STATUS_LABELS[located.task.status]}`,
detail: located.task.priority,
ref: {
id: located.task.id,
location: located.location,
} satisfies TaskRef,
})),
{ placeHolder: "Select a task" },
);
return picked?.ref;
}