// Pure move/reorder logic for the shared SortableKanbanBoard. Kept free of React // and dnd-kit so it can be unit-tested directly (the board only wires dnd-kit // events into `computeKanbanMove`). The reducer mirrors dnd-kit's arrayMove // insertion semantics: dropping a card "over" another card inserts it at that // card's slot (post-removal splice), and dropping over an (empty) column // appends to the end. /** Sentinel prefix for a column-level droppable id (see SortableKanbanBoard). * Column drop-zones use this so an empty column is still a valid drop target and * `over.id` can be told apart from a card id. */ export const COLUMN_DROPPABLE_PREFIX = "kanban-col:"; export function columnDroppableId(columnId: string): string { return `${COLUMN_DROPPABLE_PREFIX}${columnId}`; } export interface KanbanMoveColumn { readonly id: string; readonly cards: readonly TCard[]; } export interface KanbanCardLocation { readonly columnId: string; readonly index: number; } export interface KanbanMoveResult { /** New columns array with the card moved (extra column fields preserved). */ readonly columns: Col[]; readonly cardId: string; readonly from: KanbanCardLocation; readonly to: KanbanCardLocation; /** True when the card changed column (the only case that is persisted). */ readonly crossColumn: boolean; } function clamp(value: number, min: number, max: number): number { return Math.max(min, Math.min(max, value)); } /** Find which column a card currently lives in, and at which index. */ export function locateCard( columns: readonly KanbanMoveColumn[], cardId: string, ): KanbanCardLocation | null { for (const column of columns) { const index = column.cards.findIndex((card) => card.id === cardId); if (index >= 0) return { columnId: column.id, index }; } return null; } /** Resolve a dnd-kit `over.id` (either a card id or a `kanban-col:` sentinel) * to a concrete drop target column + insert index. */ function resolveDropTarget( columns: readonly KanbanMoveColumn[], overId: string, ): KanbanCardLocation | null { if (overId.startsWith(COLUMN_DROPPABLE_PREFIX)) { const columnId = overId.slice(COLUMN_DROPPABLE_PREFIX.length); const column = columns.find((candidate) => candidate.id === columnId); if (!column) return null; // Dropping onto the column background appends to the end. return { columnId, index: column.cards.length }; } return locateCard(columns, overId); } /** * Compute the next board state for a drag from `activeId` dropped over `overId`. * Returns `null` for no-ops (dropped on itself / unresolvable / same slot). * Column identity and any extra column fields (name/price/badge) are preserved. */ export function computeKanbanMove< TCard extends { readonly id: string }, Col extends KanbanMoveColumn, >(columns: readonly Col[], activeId: string, overId: string): KanbanMoveResult | null { if (activeId === overId) return null; const from = locateCard(columns, activeId); if (!from) return null; const target = resolveDropTarget(columns, overId); if (!target) return null; // Clone with mutable card arrays; keep every other column field by reference. const next = columns.map((column) => ({ ...column, cards: [...column.cards] as TCard[] })); const fromColumn = next.find((column) => column.id === from.columnId)!; const toColumn = from.columnId === target.columnId ? fromColumn : next.find((column) => column.id === target.columnId)!; const [moved] = fromColumn.cards.splice(from.index, 1); const insertAt = clamp(target.index, 0, toColumn.cards.length); toColumn.cards.splice(insertAt, 0, moved); const crossColumn = from.columnId !== target.columnId; if (!crossColumn && insertAt === from.index) return null; // same column, same slot return { columns: next as unknown as Col[], cardId: activeId, from, to: { columnId: toColumn.id, index: insertAt }, crossColumn, }; }