import type { VMState } from "../index.js"; /** * Task list view-model (AK-P4b ยง6). Pure function: the loader builds it once * server-side, the UI only renders. No React, no Prisma, no app imports * (DoD A.3). All static texts are i18n keys; resolved names (assignee, entity * label) are data passed in by the loader. * * This VM backs the REUSABLE task list (DoD 8): the standalone `/tasks` browser * and the embedded "Tasks tab" in every entity detail view (P12/P14/P16) render * the same component from this shape. `detailHref`/`createHref`/`sourceHref` are * always internal routes built by the loader, so the VM stays routing-agnostic. */ export type TaskStatus = "open" | "in_progress" | "blocked" | "done" | "cancelled"; export type TaskPriority = "low" | "normal" | "high" | "urgent"; export type TaskSource = "manual" | "chat" | "form_submission" | "automation" | "api"; export type TaskListPreset = "mine" | "created" | "all"; export interface TaskAssignee { readonly kind: "user" | "group"; /** Resolved display name (data), built by the loader. */ readonly label: string; } export interface TaskLinkedEntity { readonly entityType: string; readonly entityId: string; /** i18n key for the entity-type label (e.g. `tasks.entity.contact`). */ readonly labelKey: string; } export interface TaskListRow { readonly id: string; readonly title: string; readonly status: TaskStatus; /** i18n key for the status label (e.g. `tasks.status.open`). */ readonly statusKey: string; readonly priority: TaskPriority; /** i18n key for the priority label (e.g. `tasks.priority.high`). */ readonly priorityKey: string; readonly deadlineIso: string | null; /** Loader-computed: deadline is in the past and the task is not terminal. */ readonly isOverdue: boolean; readonly assignee: TaskAssignee | null; readonly linkedEntity: TaskLinkedEntity | null; readonly source: TaskSource; /** i18n key for the source marker (e.g. `tasks.source.chat`). */ readonly sourceKey: string; /** Internal link back to the task's origin (chat/form/automation) or null. */ readonly sourceHref: string | null; /** Internal detail route (e.g. `/acme/tasks/`). */ readonly detailHref: string; } export interface TaskListFilterState { readonly preset: TaskListPreset; readonly status: TaskStatus | null; readonly priority: TaskPriority | null; readonly assignedToUserId: string | null; readonly includeArchived: boolean; } /** Optional embedding context: when set, the list is the "Tasks tab" of an * entity and the create CTA pre-fills the link to that entity. */ export interface TaskListEmbedContext { readonly linkedEntityType: string; readonly linkedEntityId: string; } export interface TaskListData { readonly rows: readonly TaskListRow[]; readonly filter: TaskListFilterState; readonly canWrite: boolean; readonly canDelete: boolean; /** Internal route for the "new task" CTA (pre-filled when embedded). */ readonly createHref: string; readonly embed: TaskListEmbedContext | null; readonly nextCursor: string | null; readonly emptyHeadlineKey: string; readonly emptyBodyKey: string; } export type TaskListVM = VMState; /** * Normalized prop-model the UI implementation renders (M015 Subframe-adapter * contract). The ui-app adapter maps {@link TaskListData} -> this model once, * then the ui-subframe / ui-mock renderers stay dumb (hot-swap drill, M017). */ export interface TaskListModelRow { readonly id: string; readonly title: string; readonly statusKey: string; readonly priorityKey: string; readonly deadlineIso: string | null; readonly isOverdue: boolean; readonly assigneeLabel: string | null; readonly detailHref: string; } export interface TaskListModel { readonly titleKey: string; readonly emptyHeadlineKey: string; readonly emptyBodyKey: string; readonly canWrite: boolean; readonly createHref: string; readonly rows: readonly TaskListModelRow[]; readonly rowCount: number; } export interface BuildTaskListVMInput { readonly rows: readonly TaskListRow[]; readonly filter: TaskListFilterState; readonly canWrite: boolean; readonly canDelete: boolean; readonly createHref: string; readonly embed?: TaskListEmbedContext | null; readonly nextCursor?: string | null; } export function buildTaskListVM(input: BuildTaskListVMInput): TaskListVM { // Empty stays a `ready` state with a plain-text empty state (DoD 23) so the // filter bar + "new task" CTA remain visible. return { status: "ready", data: { rows: input.rows, filter: input.filter, canWrite: input.canWrite, canDelete: input.canDelete, createHref: input.createHref, embed: input.embed ?? null, nextCursor: input.rows.length === 0 ? null : (input.nextCursor ?? null), emptyHeadlineKey: "tasks.list.empty.headline", emptyBodyKey: "tasks.list.empty.body", }, }; } /** Map the domain data to the normalized hot-swap model (used by the ui-app adapter). */ export function toTaskListModel(data: TaskListData): TaskListModel { return { titleKey: "tasks.list.title", emptyHeadlineKey: data.emptyHeadlineKey, emptyBodyKey: data.emptyBodyKey, canWrite: data.canWrite, createHref: data.createHref, rows: data.rows.map((row) => ({ id: row.id, title: row.title, statusKey: row.statusKey, priorityKey: row.priorityKey, deadlineIso: row.deadlineIso, isOverdue: row.isOverdue, assigneeLabel: row.assignee?.label ?? null, detailHref: row.detailHref, })), rowCount: data.rows.length, }; } // --- Pure helpers reused by loaders to keep i18n keys consistent ---------- export function taskStatusKey(status: TaskStatus): string { return `tasks.status.${status}`; } export function taskPriorityKey(priority: TaskPriority): string { return `tasks.priority.${priority}`; } export function taskSourceKey(source: TaskSource): string { return `tasks.source.${source}`; } export function taskEntityLabelKey(entityType: string): string { return `tasks.entity.${entityType}`; }