import { useMemo } from 'react'; import { useTaskStore, type LocalTask } from './task-store'; import { visibleWorkspaceTasks } from './sync/workspace-policy'; const MAX_BOARD_ITEMS = 64; const DEFAULT_LIMIT = 48; /** * The board, shaped exactly like the old `api.tasks.getBoard` return so the screens * and the widget bridge consume it unchanged: open/ready/done buckets plus the * action-session tally. */ export type Board = { open: LocalTask[]; ready: LocalTask[]; done: LocalTask[]; doneThisSession: number; }; /** `open`: both resisted-and-kept-open and newly-demoted-from-ready tasks sink toward the * back of the review queue by how recently they were touched. `createdAt` tiebreaks a * same-instant batch (e.g. a whole demote pass shares one `updatedAt`). */ const byOpenReviewOrder = (a: LocalTask, b: LocalTask) => a.updatedAt - b.updatedAt || a.createdAt - b.createdAt; /** `ready`: the manual "Next Up" order. A missing `order` (a legacy row untouched since * before this field existed) sinks to the very end rather than crashing the comparator. */ const byManualOrder = (a: LocalTask, b: LocalTask) => (a.order ?? Number.POSITIVE_INFINITY) - (b.order ?? Number.POSITIVE_INFINITY) || a.createdAt - b.createdAt; /** Most-recently-completed first. */ const byCompletedDesc = (a: LocalTask, b: LocalTask) => (b.completedAt ?? 0) - (a.completedAt ?? 0); function bucket( tasks: LocalTask[], status: LocalTask['status'], compare: (a: LocalTask, b: LocalTask) => number, limit: number ): LocalTask[] { return tasks .filter((task) => task.status === status) .sort(compare) .slice(0, limit); } function countDoneThisSession(live: LocalTask[], planSessionAt: number, localFallback: number): number { if (planSessionAt <= 0) return localFallback; return live.filter( (task) => task.status === 'done' && task.completedAt !== undefined && task.completedAt >= planSessionAt ).length; } /** * Reactive read of the local board. Subscribes to the task store, so any mutation * (a swipe, a completion, a capture) re-renders consumers — the same live-update * feel the Convex subscription gave, but sourced entirely on-device. * * Tombstoned (`deletedAt`) rows are excluded. Open is ordered by recency of touch * (`updatedAt` ascending, `createdAt` tiebreak) — resisting or demoting a task sinks it * toward the back of the next review pass. Ready is ordered by the manual `order` field * ("Next Up" drag order). Done is ordered by `completedAt` descending (most-recent first). * The result is memoised, so an unrelated store change won't churn the board reference. */ export function useBoard(limit: number = DEFAULT_LIMIT): Board { const tasks = useTaskStore((state) => state.tasks); const session = useTaskStore((state) => state.session); const workspaceOwnerId = useTaskStore((state) => state.workspace.ownerId); return useMemo(() => { const capped = Math.min(Math.max(limit, 1), MAX_BOARD_ITEMS); const live = visibleWorkspaceTasks(tasks, workspaceOwnerId); return { open: bucket(live, 'open', byOpenReviewOrder, capped), ready: bucket(live, 'ready', byManualOrder, capped), done: bucket(live, 'done', byCompletedDesc, capped), doneThisSession: countDoneThisSession(live, session.planSessionAt, session.doneThisSession), }; }, [tasks, session, workspaceOwnerId, limit]); }