import type { LayoutItem } from 'react-grid-layout' export type GridItem = { id: string w: number h: number x?: number y?: number } export type PositionedGridItem = GridItem & { x: number y: number } export type GridPosition = { id: string x: number y: number } type StoredGridItem = { i: string x: number y: number } export function findFreePosition( existing: LayoutItem[], w: number, h: number, cols: number ): { x: number; y: number } { const maxY = existing.reduce((m, l) => Math.max(m, l.y + l.h), 0) for (let y = 0; y <= maxY + h; y++) { for (let x = 0; x <= cols - w; x++) { const overlaps = existing.some( l => !(x + w <= l.x || l.x + l.w <= x || y + h <= l.y || l.y + l.h <= y) ) if (!overlaps) return { x, y } } } return { x: 0, y: maxY } } export function packItems(items: GridItem[], existing: LayoutItem[] = [], cols = 4): LayoutItem[] { const placed: LayoutItem[] = [] for (const item of items) { const { x, y } = item.x !== undefined && item.y !== undefined ? { x: item.x, y: item.y } : findFreePosition([...existing, ...placed], item.w, item.h, cols) placed.push({ i: item.id, x, y, w: item.w, h: item.h }) } return placed } export function findWidgetPosition( existing: LayoutItem[], w: number, h: number ): { x: number; y: number } { return findFreePosition(existing, w, h, 4) } export function updateStoredGridPositions( items: readonly T[], positions: readonly GridPosition[] ): T[] { const positionById = new Map(positions.map(position => [position.id, position])) return items.map(item => { const position = positionById.get(item.i) return position ? { ...item, x: position.x, y: position.y } : item }) }