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; 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): 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(null); const setup = ref(null); // null means loading, [] means loaded. const tasks = ref(null); const error = ref(null); const taskIDsBySource = ref(new Map()); async function load(): Promise { error.value = null; try { const [nextSummary, nextSetup, nextTasks] = await Promise.all([ api.summary(), api.setup().catch(() => null), // Not defaulted to []: an empty list reads as "nothing needs you", // which a failed fetch must never claim. api.tasks(), ]); const map = new Map(); 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 }; });