81 lines
2.7 KiB
TypeScript
81 lines
2.7 KiB
TypeScript
import { defineStore } from "pinia";
|
|
import { ref } from "vue";
|
|
|
|
import type { ActivityDetailInput } from "../dashboard/activity-detail.ts";
|
|
import type { Setup } from "../dashboard/setup-preference.ts";
|
|
import { api } from "../lib/api.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;
|
|
}
|
|
|
|
export 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", () => {
|
|
const summary = ref<Summary | null>(null);
|
|
const setup = ref<Setup | null>(null);
|
|
// null means loading, [] means loaded.
|
|
const tasks = ref<DashboardTask[] | null>(null);
|
|
const error = ref<string | null>(null);
|
|
const taskIDsBySource = ref(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 map = new Map<string, number>();
|
|
for (const task of nextTasks) {
|
|
if (task.owner && task.repo && task.issue_number)
|
|
map.set(`${task.owner}/${task.repo}#${task.issue_number}`, Number(task.id));
|
|
}
|
|
taskIDsBySource.value = map;
|
|
summary.value = nextSummary;
|
|
setup.value = nextSetup;
|
|
tasks.value = nextTasks;
|
|
} catch (err) {
|
|
error.value = String((err as Error).message || err);
|
|
}
|
|
}
|
|
|
|
/** 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 };
|
|
});
|