206 lines
7.1 KiB
TypeScript
206 lines
7.1 KiB
TypeScript
import type { StoreApi } from "zustand";
|
|
import type { AppState } from "@/lib/state/store";
|
|
import type { WsHandlers } from "@/lib/ws/handlers/types";
|
|
import type { KanbanState } from "@/lib/state/slices/kanban/types";
|
|
import { cleanupTaskStorage } from "@/lib/local-storage";
|
|
import { useContextFilesStore } from "@/lib/state/context-files-store";
|
|
|
|
type KanbanTask = KanbanState["tasks"][number];
|
|
|
|
/** Falls back to existing value when incoming is null, undefined, or empty string. */
|
|
function withFallback<T>(value: T | null | undefined, fallback: T | undefined): T | undefined {
|
|
return value || fallback;
|
|
}
|
|
|
|
/** Falls back only on null/undefined -- preserves 0 and other falsy non-string values. */
|
|
function withNullishFallback<T>(
|
|
value: T | null | undefined,
|
|
fallback: T | undefined,
|
|
): T | undefined {
|
|
return value ?? fallback;
|
|
}
|
|
|
|
function buildNullableFields(
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
payload: any,
|
|
existing?: KanbanTask,
|
|
): Pick<
|
|
KanbanTask,
|
|
| "repositoryId"
|
|
| "primarySessionId"
|
|
| "primarySessionState"
|
|
| "sessionCount"
|
|
| "reviewStatus"
|
|
| "primaryExecutorId"
|
|
| "primaryExecutorType"
|
|
| "primaryExecutorName"
|
|
| "parentTaskId"
|
|
| "updatedAt"
|
|
| "createdAt"
|
|
> {
|
|
return {
|
|
repositoryId: withFallback(payload.repository_id, existing?.repositoryId),
|
|
primarySessionId: withFallback(payload.primary_session_id, existing?.primarySessionId),
|
|
primarySessionState: withFallback(payload.primary_session_state, existing?.primarySessionState),
|
|
sessionCount: withNullishFallback(payload.session_count, existing?.sessionCount),
|
|
reviewStatus: withFallback(payload.review_status, existing?.reviewStatus),
|
|
primaryExecutorId: withFallback(payload.primary_executor_id, existing?.primaryExecutorId),
|
|
primaryExecutorType: withFallback(payload.primary_executor_type, existing?.primaryExecutorType),
|
|
primaryExecutorName: withFallback(payload.primary_executor_name, existing?.primaryExecutorName),
|
|
parentTaskId: withFallback(payload.parent_id, existing?.parentTaskId),
|
|
updatedAt: withFallback(payload.updated_at, existing?.updatedAt),
|
|
createdAt: withFallback(payload.created_at, existing?.createdAt),
|
|
};
|
|
}
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
function buildTaskFromPayload(payload: any, existing?: KanbanTask): KanbanTask {
|
|
return {
|
|
id: payload.task_id,
|
|
workflowStepId: payload.workflow_step_id,
|
|
title: payload.title,
|
|
description: payload.description,
|
|
position: payload.position ?? 0,
|
|
state: payload.state,
|
|
isRemoteExecutor: payload.is_remote_executor ?? existing?.isRemoteExecutor ?? false,
|
|
...buildNullableFields(payload, existing),
|
|
};
|
|
}
|
|
|
|
function upsertTask(tasks: KanbanTask[], nextTask: KanbanTask): KanbanTask[] {
|
|
const exists = tasks.some((task) => task.id === nextTask.id);
|
|
return exists
|
|
? tasks.map((task) => (task.id === nextTask.id ? nextTask : task))
|
|
: [...tasks, nextTask];
|
|
}
|
|
|
|
function upsertMultiTask(state: AppState, workflowId: string, task: KanbanTask): AppState {
|
|
const snapshot = state.kanbanMulti.snapshots[workflowId];
|
|
if (!snapshot) return state;
|
|
return {
|
|
...state,
|
|
kanbanMulti: {
|
|
...state.kanbanMulti,
|
|
snapshots: {
|
|
...state.kanbanMulti.snapshots,
|
|
[workflowId]: {
|
|
...snapshot,
|
|
tasks: upsertTask(snapshot.tasks, task),
|
|
},
|
|
},
|
|
},
|
|
};
|
|
}
|
|
|
|
/** Upsert a task in both single-kanban and multi-kanban snapshots. */
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
function upsertTaskInBothKanbans(state: AppState, wfId: string, payload: any): AppState {
|
|
// Skip ephemeral tasks - they should never be added to kanban
|
|
if (payload.is_ephemeral) {
|
|
return state;
|
|
}
|
|
|
|
let next = state;
|
|
|
|
if (state.kanban.workflowId === wfId) {
|
|
const existing = state.kanban.tasks.find((t) => t.id === payload.task_id);
|
|
const nextTask = buildTaskFromPayload(payload, existing);
|
|
next = { ...next, kanban: { ...next.kanban, tasks: upsertTask(next.kanban.tasks, nextTask) } };
|
|
}
|
|
|
|
const snapshot = state.kanbanMulti.snapshots[wfId];
|
|
if (snapshot) {
|
|
const existing = snapshot.tasks.find((t) => t.id === payload.task_id);
|
|
const nextTask = buildTaskFromPayload(payload, existing);
|
|
next = upsertMultiTask(next, wfId, nextTask);
|
|
}
|
|
|
|
return next;
|
|
}
|
|
|
|
/** Remove a task from both single-kanban and multi-kanban snapshots. */
|
|
function removeTaskFromBothKanbans(state: AppState, wfId: string, taskId: string): AppState {
|
|
let next = state;
|
|
if (state.kanban.workflowId === wfId) {
|
|
next = {
|
|
...next,
|
|
kanban: { ...next.kanban, tasks: next.kanban.tasks.filter((t) => t.id !== taskId) },
|
|
};
|
|
}
|
|
const snapshot = state.kanbanMulti.snapshots[wfId];
|
|
if (snapshot) {
|
|
next = {
|
|
...next,
|
|
kanbanMulti: {
|
|
...next.kanbanMulti,
|
|
snapshots: {
|
|
...next.kanbanMulti.snapshots,
|
|
[wfId]: { ...snapshot, tasks: snapshot.tasks.filter((t) => t.id !== taskId) },
|
|
},
|
|
},
|
|
};
|
|
}
|
|
return next;
|
|
}
|
|
|
|
export function registerTasksHandlers(store: StoreApi<AppState>): WsHandlers {
|
|
return {
|
|
"task.created": (message) => {
|
|
// Skip ephemeral tasks (e.g., quick chat) - they shouldn't appear on the Kanban board
|
|
if (message.payload.is_ephemeral) return;
|
|
store.setState((state) =>
|
|
upsertTaskInBothKanbans(state, message.payload.workflow_id, message.payload),
|
|
);
|
|
},
|
|
"task.updated": (message) => {
|
|
// Skip ephemeral tasks (e.g., quick chat) - they shouldn't appear on the Kanban board
|
|
if (message.payload.is_ephemeral) return;
|
|
|
|
store.setState((state) => {
|
|
const wfId = message.payload.workflow_id;
|
|
const taskId = message.payload.task_id;
|
|
|
|
if (message.payload.archived_at) {
|
|
return removeTaskFromBothKanbans(state, wfId, taskId);
|
|
}
|
|
|
|
return upsertTaskInBothKanbans(state, wfId, message.payload);
|
|
});
|
|
},
|
|
"task.deleted": (message) => {
|
|
const deletedId = message.payload.task_id;
|
|
const wfId = message.payload.workflow_id;
|
|
|
|
const currentState = store.getState();
|
|
const sessionIds = (currentState.taskSessionsByTask.itemsByTaskId[deletedId] ?? []).map(
|
|
(s) => s.id,
|
|
);
|
|
const task = currentState.kanban.tasks.find((t) => t.id === deletedId);
|
|
if (task?.primarySessionId && !sessionIds.includes(task.primarySessionId)) {
|
|
sessionIds.push(task.primarySessionId);
|
|
}
|
|
cleanupTaskStorage(deletedId, sessionIds);
|
|
for (const sid of sessionIds) {
|
|
useContextFilesStore.getState().clearSession(sid);
|
|
}
|
|
|
|
store.setState((state) => {
|
|
const isActive = state.tasks.activeTaskId === deletedId;
|
|
let next = removeTaskFromBothKanbans(state, wfId, deletedId);
|
|
if (isActive) {
|
|
next = { ...next, tasks: { ...next.tasks, activeTaskId: null, activeSessionId: null } };
|
|
}
|
|
return next;
|
|
});
|
|
},
|
|
"task.state_changed": (message) => {
|
|
// Skip ephemeral tasks (e.g., quick chat) - they shouldn't appear on the Kanban board
|
|
if (message.payload.is_ephemeral) return;
|
|
|
|
store.setState((state) =>
|
|
upsertTaskInBothKanbans(state, message.payload.workflow_id, message.payload),
|
|
);
|
|
},
|
|
};
|
|
}
|