89 lines
3.3 KiB
TypeScript
89 lines
3.3 KiB
TypeScript
import { defineStore } from "pinia";
|
|
import { shallowRef } from "vue";
|
|
|
|
import type { ActivityDetailInput } from "../dashboard/activity-detail.ts";
|
|
import type { Setup } from "../dashboard/setup-preference.ts";
|
|
import { errors } from "../copy/errors/index.ts";
|
|
import { api, describeError } from "../lib/api.ts";
|
|
import { keepRows, keepValue } from "../lib/keep-unchanged.ts";
|
|
|
|
export interface Summary {
|
|
statuses?: Record<string, number>;
|
|
tokens_by_day?: Array<{ day: string; tokens?: number }>;
|
|
}
|
|
|
|
/** An event off the live stream: the payload the detail column reads, plus
|
|
* enough to point the row at the task it belongs to. */
|
|
export interface ActivityEvent extends ActivityDetailInput {
|
|
kind?: string;
|
|
type?: string;
|
|
task_id?: number | string;
|
|
repo?: string;
|
|
issue?: number | string;
|
|
at?: string | number;
|
|
}
|
|
|
|
/** A task, as far as the dashboard's links read it. */
|
|
export interface DashboardTask {
|
|
id: number | string;
|
|
status?: string;
|
|
owner?: string;
|
|
repo?: string;
|
|
issue_number?: number;
|
|
handoff_to_task_id?: number;
|
|
}
|
|
|
|
function taskIDForEvent(event: ActivityEvent, bySource: Map<string, number>): number {
|
|
if (Number(event.task_id) > 0) return Number(event.task_id);
|
|
if (!event.repo || !event.issue) return 0;
|
|
return Number(bySource.get(`${event.repo}#${event.issue}`)) || 0;
|
|
}
|
|
|
|
/** The board as the dashboard reads it: counts, setup progress and the
|
|
* recent tasks. The Tasks page pages its own list. */
|
|
export const useTasksStore = defineStore("tasks", () => {
|
|
// Shallow and replaced only on change: every task event reloads the board,
|
|
// and a reload that changed nothing must not re-render it.
|
|
const summary = shallowRef<Summary | null>(null);
|
|
const setup = shallowRef<Setup | null>(null);
|
|
// null means loading, [] means loaded.
|
|
const tasks = shallowRef<DashboardTask[] | null>(null);
|
|
const error = shallowRef<string | null>(null);
|
|
const taskIDsBySource = shallowRef(new Map<string, number>());
|
|
|
|
async function load(): Promise<void> {
|
|
error.value = null;
|
|
try {
|
|
const [nextSummary, nextSetup, nextTasks] = await Promise.all([
|
|
api.summary<Summary>(),
|
|
api.setup<Setup>().catch(() => null),
|
|
// Not defaulted to []: an empty list reads as "nothing needs you",
|
|
// which a failed fetch must never claim.
|
|
api.tasks<DashboardTask[]>(),
|
|
]);
|
|
const kept = keepRows(tasks.value, nextTasks);
|
|
summary.value = keepValue(summary.value, nextSummary);
|
|
setup.value = keepValue(setup.value, nextSetup);
|
|
if (kept === tasks.value) return;
|
|
const map = new Map<string, number>();
|
|
for (const task of kept) {
|
|
// A run that handed off yields its work item to its successor.
|
|
if (task.owner && task.repo && task.issue_number && !task.handoff_to_task_id)
|
|
map.set(`${task.owner}/${task.repo}#${task.issue_number}`, Number(task.id));
|
|
}
|
|
taskIDsBySource.value = map;
|
|
tasks.value = kept;
|
|
} catch (err) {
|
|
error.value = describeError(err, errors.common.unknown);
|
|
}
|
|
}
|
|
|
|
/** The task an activity row opens, or 0. An event carries the id, or a
|
|
* repo/issue pair the task list resolves. */
|
|
function taskIDFor(event: ActivityEvent): number {
|
|
return taskIDForEvent(event, taskIDsBySource.value);
|
|
}
|
|
|
|
return { summary, setup, tasks, error, load, taskIDFor };
|
|
});
|