import { COLUMN_STEP, ROW_STEP, type TreeNote, type TechTreeLayout } from "./types"; /** * Pure layout helpers for Tech Tree note nodes. * * Notes are annotations, not technologies: they never enter `computeLayout`, * never affect edges, and are not counted as prerequisites or columns. A note * only owns a canvas position: * - dragged notes use their manual `treePosition`; * - untouched notes stack in a dedicated notes column to the right of the * technology columns, in creation order. * * Note node ids are prefixed so a note id can never collide with a technology * id in the React Flow node list (both are independent auto-increment integers). */ export const NOTE_NODE_PREFIX = "note-"; export function noteNodeId(noteId: number): string { return `${NOTE_NODE_PREFIX}${noteId}`; } /** Returns the note id for a note node id, or null for any other node. */ export function parseNoteNodeId(nodeId: string): number | null { if (!nodeId.startsWith(NOTE_NODE_PREFIX)) return null; const id = Number(nodeId.slice(NOTE_NODE_PREFIX.length)); return Number.isInteger(id) && id > 0 ? id : null; } export type NoteLayoutNode = { noteId: number; x: number; y: number; }; /** * Positions for every note. `tree.columns` is the number of technology * columns in use, so the fallback notes column sits one step to its right * (column 0 when the tree is empty). */ export function computeNoteLayout( notes: TreeNote[], tree: Pick, ): NoteLayoutNode[] { const fallbackColumn = Math.max(0, tree.columns); let nextRow = 0; return notes.map((note) => { if (note.treePosition) { return { noteId: note.id, x: note.treePosition.x, y: note.treePosition.y }; } const row = nextRow; nextRow += 1; return { noteId: note.id, x: fallbackColumn * COLUMN_STEP, y: row * ROW_STEP }; }); }