:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(dashboards): preserve sections during free-form tile drags (#107676)
ella-c9300f77d78b3
diff --git a/frontend/src/scenes/dashboard/dashboardResizeCompactor.test.ts b/frontend/src/scenes/dashboard/dashboardResizeCompactor.test.tsindex e6ba093856f3..895e07d45abb 100644--- a/frontend/src/scenes/dashboard/dashboardResizeCompactor.test.ts+++ b/frontend/src/scenes/dashboard/dashboardResizeCompactor.test.ts@@ -20,6 +20,18 @@ describe('dashboard resize compactor', () => {])})+ it('restores displaced tiles even where they overlap a free-form drag', () => {+ const layout: Layout = [+ { i: 'active', x: 6, y: 0, w: 6, h: 4, minW: 2 },+ { i: 'neighbor', x: 6, y: 4, w: 6, h: 4, minW: 2 },+ ]++ expect(restoreUnmovedItemPositions(layout, baseline, 'active', undefined, true)).toEqual([+ { i: 'active', x: 6, y: 0, w: 6, h: 4, minW: 2 },+ { i: 'neighbor', x: 6, y: 0, w: 6, h: 4, minW: 2 },+ ])+ })+test.each([{name: 'shrinks a neighbor that still meets its minimum width',diff --git a/frontend/src/scenes/dashboard/dashboardResizeCompactor.ts b/frontend/src/scenes/dashboard/dashboardResizeCompactor.tsindex 454e80d5f605..2451c2bf7d69 100644--- a/frontend/src/scenes/dashboard/dashboardResizeCompactor.ts+++ b/frontend/src/scenes/dashboard/dashboardResizeCompactor.ts@@ -9,7 +9,8 @@ export function restoreUnmovedItemPositions(layout: Layout,baseline: Layout,activeItemId: string,- baselineById: Map<string, LayoutItem> = new Map(baseline.map((item) => [item.i, item]))+ baselineById: Map<string, LayoutItem> = new Map(baseline.map((item) => [item.i, item])),+ restoreCollidingItems = false): Layout {const activeItem = layout.find((item) => item.i === activeItemId)@@ -20,7 +21,7 @@ export function restoreUnmovedItemPositions(}const restoredItem = { ...item, x: baselineItem.x, y: baselineItem.y }- if (activeItem && itemsOverlap(restoredItem, activeItem)) {+ if (!restoreCollidingItems && activeItem && itemsOverlap(restoredItem, activeItem)) {return item}return restoredItemdiff --git a/frontend/src/scenes/dashboard/useDashboardLayoutInteraction.ts b/frontend/src/scenes/dashboard/useDashboardLayoutInteraction.tsindex fe5a39d40f50..84c3c82cd42a 100644--- a/frontend/src/scenes/dashboard/useDashboardLayoutInteraction.ts+++ b/frontend/src/scenes/dashboard/useDashboardLayoutInteraction.ts@@ -55,12 +55,24 @@ export function useDashboardLayoutInteraction({return compactor.compact(layout, cols)}- const restoredLayout = restoreUnmovedItemPositions(layout, baseline, activeTileId, baselineById.current)+ const restoredLayout = restoreUnmovedItemPositions(+ layout,+ baseline,+ activeTileId,+ baselineById.current,+ layoutCompaction === DashboardGridCompaction.Stable && interactionKind.current === 'drag'+ )const resizedLayout =interactionKind.current === 'resize'? resizeNeighborToFitRow(restoredLayout, baseline, activeTileId, resizeNeighbors.current): restoredLayout- return compactor.compactInteraction(cols, activeTileId, restoredLayout, resizedLayout)+ return compactor.compactInteraction(+ cols,+ activeTileId,+ restoredLayout,+ resizedLayout,+ interactionKind.current === 'drag'+ )},}}, [layoutCompaction])diff --git a/products/dashboards/frontend/dashboardCustomization.test.ts b/products/dashboards/frontend/dashboardCustomization.test.tsindex 9224e0d1cc2d..2bba0f6e0e83 100644--- a/products/dashboards/frontend/dashboardCustomization.test.ts+++ b/products/dashboards/frontend/dashboardCustomization.test.ts@@ -1,24 +1,217 @@-import type { Layout } from 'react-grid-layout'+import { moveElement } from 'react-grid-layout'+import type { Layout, LayoutItem } from 'react-grid-layout'-import { resolveFreePlacementCollisions } from './dashboardCustomization'+import {+ DashboardGridCompaction,+ getDashboardGridCompactor,+ resolveFreePlacementCollisions,+} from './dashboardCustomization'const geometry = (layout: Layout): Layout => layout.map(({ i, x, y, w, h }) => ({ i, x, y, w, h }))describe('dashboard grid compactors', () => {+ it.each([+ { dropColumn: 4, dropRow: 0, rightStartRow: 0, imageColumn: 6, leftRow: 0, rightRow: 4 },+ { dropColumn: 3, dropRow: 0, rightStartRow: 0, imageColumn: 2, leftRow: 4, rightRow: 0 },+ { dropColumn: 4, imageColumn: 6, leftRow: 0, rightRow: 6, rightStartRow: 1, dropRow: 2 },+ ])(+ 'inserts an image between adjacent charts near column $dropColumn at row $dropRow',+ ({ dropColumn, imageColumn, leftRow, rightRow, rightStartRow, dropRow }) => {+ const image: LayoutItem = { i: 'image', x: 0, y: 8, w: 4, h: 4 }+ const layout: Layout = [+ { i: 'left-chart', x: 0, y: 0, w: 6, h: 4 },+ { i: 'right-chart', x: 6, y: rightStartRow, w: 6, h: 4 },+ image,+ ]+ const compactor = getDashboardGridCompactor(DashboardGridCompaction.Stable)+ const moved = moveElement(+ layout,+ image,+ dropColumn,+ dropRow,+ true,+ false,+ compactor.type,+ 12,+ compactor.allowOverlap+ )++ expect(geometry(compactor.compactInteraction(12, image.i, moved, moved))).toEqual([+ { i: 'left-chart', x: 0, y: leftRow, w: 6, h: 4 },+ { i: 'right-chart', x: 6, y: rightRow, w: 6, h: 4 },+ { i: 'image', x: imageColumn, y: dropRow, w: 4, h: 4 },+ ])+ }+ )++ it.each([+ { dropColumn: 4, imageColumn: 2, leftRow: 6, topRow: 0, bottomRow: 4 },+ { dropColumn: 5, imageColumn: 6, leftRow: 0, topRow: 6, bottomRow: 10 },+ ])(+ 'snaps a seam drop beside stacked charts at column $dropColumn',+ ({ dropColumn, imageColumn, leftRow, topRow, bottomRow }) => {+ const layout: Layout = [+ { i: 'left', x: 0, y: 0, w: 6, h: 8 },+ { i: 'top', x: 6, y: 0, w: 6, h: 4 },+ { i: 'bottom', x: 6, y: 4, w: 6, h: 4 },+ { i: 'image', x: dropColumn, y: 2, w: 4, h: 4 },+ ]++ expect(geometry(resolveFreePlacementCollisions(layout, 12, 'image'))).toEqual([+ { i: 'left', x: 0, y: leftRow, w: 6, h: 8 },+ { i: 'top', x: 6, y: topRow, w: 6, h: 4 },+ { i: 'bottom', x: 6, y: bottomRow, w: 6, h: 4 },+ { i: 'image', x: imageColumn, y: 2, w: 4, h: 4 },+ ])+ }+ )++ it('keeps a resized tile in place when it grows across two charts', () => {+ const layout: Layout = [+ { i: 'left-chart', x: 0, y: 2, w: 6, h: 4 },+ { i: 'right-chart', x: 6, y: 2, w: 6, h: 4 },+ { i: 'image', x: 4, y: 0, w: 4, h: 4 },+ ]+ const compactor = getDashboardGridCompactor(DashboardGridCompaction.Stable)++ expect(geometry(compactor.compactInteraction(12, 'image', layout, layout, false))).toEqual([+ { i: 'left-chart', x: 0, y: 4, w: 6, h: 4 },+ { i: 'right-chart', x: 6, y: 4, w: 6, h: 4 },+ { i: 'image', x: 4, y: 0, w: 4, h: 4 },+ ])+ })++ it('keeps a dragged image at the intended row instead of moving it below a chart', () => {+ const image: LayoutItem = { i: 'image', x: 0, y: 12, w: 6, h: 4 }+ const layout: Layout = [+ { i: 'chart', x: 0, y: 4, w: 6, h: 4 },+ { i: 'side-chart', x: 6, y: 4, w: 6, h: 4 },+ { i: 'next-chart', x: 0, y: 8, w: 6, h: 4 },+ image,+ ]+ const compactor = getDashboardGridCompactor(DashboardGridCompaction.Stable)+ const moved = moveElement(layout, image, 0, 2, true, false, compactor.type, 12, compactor.allowOverlap)++ expect(geometry(compactor.compactInteraction(12, image.i, moved, moved))).toEqual([+ { i: 'chart', x: 0, y: 6, w: 6, h: 4 },+ { i: 'side-chart', x: 6, y: 4, w: 6, h: 4 },+ { i: 'next-chart', x: 0, y: 10, w: 6, h: 4 },+ { i: 'image', x: 0, y: 2, w: 6, h: 4 },+ ])+ })++ it('leaves the image beside a displaced insight in place', () => {+ const layout: Layout = [+ { i: 'top-left', x: 0, y: 0, w: 6, h: 3 },+ { i: 'top-right', x: 6, y: 0, w: 6, h: 3 },+ { i: 'image', x: 0, y: 3, w: 6, h: 3 },+ { i: 'retention', x: 6, y: 3, w: 6, h: 3 },+ { i: 'active', x: 6, y: 3, w: 6, h: 3 },+ ]++ expect(geometry(resolveFreePlacementCollisions(layout, 12, 'active'))).toEqual([+ { i: 'top-left', x: 0, y: 0, w: 6, h: 3 },+ { i: 'top-right', x: 6, y: 0, w: 6, h: 3 },+ { i: 'image', x: 0, y: 3, w: 6, h: 3 },+ { i: 'retention', x: 6, y: 6, w: 6, h: 3 },+ { i: 'active', x: 6, y: 3, w: 6, h: 3 },+ ])+ })+it('moves a collision chain below the active tile', () => {const layout = [{ i: 'active', x: 0, y: 2, w: 6, h: 4 },+ { i: 'side-tile', x: 6, y: 3, w: 6, h: 1 },{ i: 'neighbor', x: 0, y: 4, w: 6, h: 4 },{ i: 'next-neighbor', x: 0, y: 8, w: 6, h: 4 },+ { i: 'later-tile', x: 0, y: 20, w: 6, h: 4 },] as Layoutexpect(geometry(resolveFreePlacementCollisions(layout, 12, 'active'))).toEqual([{ i: 'active', x: 0, y: 2, w: 6, h: 4 },+ { i: 'side-tile', x: 6, y: 3, w: 6, h: 1 },{ i: 'neighbor', x: 0, y: 6, w: 6, h: 4 },{ i: 'next-neighbor', x: 0, y: 10, w: 6, h: 4 },+ { i: 'later-tile', x: 0, y: 20, w: 6, h: 4 },])})+ it.each([1, 2, 3])('keeps three sections in order when a tile is dropped at row %i', (dropRow) => {+ const shift = dropRow + 2+ const layout = [+ { i: 'heading-one', x: 0, y: 0, w: 12, h: 1 },+ { i: 'heading-two', x: 0, y: 4, w: 12, h: 1 },+ { i: 'one-left', x: 0, y: 1, w: 6, h: 3 },+ { i: 'one-right', x: 6, y: 1, w: 6, h: 3 },+ { i: 'active', x: 0, y: dropRow, w: 6, h: 3 },+ { i: 'two-left', x: 0, y: 5, w: 6, h: 3 },+ { i: 'two-right', x: 6, y: 5, w: 6, h: 3 },+ { i: 'heading-three', x: 0, y: 8, w: 12, h: 1 },+ { i: 'three-left', x: 0, y: 9, w: 6, h: 3 },+ ] as Layout++ expect(geometry(resolveFreePlacementCollisions(layout, 12, 'active'))).toEqual([+ { i: 'heading-one', x: 0, y: 0, w: 12, h: 1 },+ { i: 'heading-two', x: 0, y: 4 + shift, w: 12, h: 1 },+ { i: 'one-left', x: 0, y: 1 + shift, w: 6, h: 3 },+ { i: 'one-right', x: 6, y: 1, w: 6, h: 3 },+ { i: 'active', x: 0, y: dropRow, w: 6, h: 3 },+ { i: 'two-left', x: 0, y: 5 + shift, w: 6, h: 3 },+ { i: 'two-right', x: 6, y: 5 + shift, w: 6, h: 3 },+ { i: 'heading-three', x: 0, y: 8 + shift, w: 12, h: 1 },+ { i: 'three-left', x: 0, y: 9 + shift, w: 6, h: 3 },+ ])+ })++ it('keeps tiles below a displaced heading when the section has a gap', () => {+ const layout: Layout = [+ { i: 'upper', x: 0, y: 1, w: 6, h: 4 },+ { i: 'next-heading', x: 0, y: 5, w: 12, h: 1 },+ { i: 'section-right', x: 6, y: 7, w: 6, h: 2 },+ { i: 'distant', x: 6, y: 25, w: 6, h: 2 },+ { i: 'active', x: 0, y: 2, w: 6, h: 4 },+ ]++ expect(geometry(resolveFreePlacementCollisions(layout, 12, 'active'))).toEqual([+ { i: 'upper', x: 0, y: 6, w: 6, h: 4 },+ { i: 'next-heading', x: 0, y: 10, w: 12, h: 1 },+ { i: 'section-right', x: 6, y: 12, w: 6, h: 2 },+ { i: 'distant', x: 6, y: 25, w: 6, h: 2 },+ { i: 'active', x: 0, y: 2, w: 6, h: 4 },+ ])+ })++ it('pushes a tall tile below the drop without moving its side neighbor', () => {+ const layout: Layout = [+ { i: 'heading', x: 0, y: 0, w: 12, h: 1 },+ { i: 'tall', x: 0, y: 1, w: 6, h: 6 },+ { i: 'side-tile', x: 6, y: 1, w: 6, h: 3 },+ { i: 'active', x: 0, y: 4, w: 6, h: 3 },+ { i: 'next-heading', x: 0, y: 7, w: 12, h: 1 },+ { i: 'next-tile', x: 0, y: 8, w: 6, h: 3 },+ ]++ expect(geometry(resolveFreePlacementCollisions(layout, 12, 'active'))).toEqual([+ { i: 'heading', x: 0, y: 0, w: 12, h: 1 },+ { i: 'tall', x: 0, y: 7, w: 6, h: 6 },+ { i: 'side-tile', x: 6, y: 1, w: 6, h: 3 },+ { i: 'active', x: 0, y: 4, w: 6, h: 3 },+ { i: 'next-heading', x: 0, y: 13, w: 12, h: 1 },+ { i: 'next-tile', x: 0, y: 14, w: 6, h: 3 },+ ])+ })++ it('leaves other tiles in place when the drop is in empty space', () => {+ const layout = [+ { i: 'heading', x: 0, y: 0, w: 12, h: 1 },+ { i: 'neighbor', x: 0, y: 1, w: 6, h: 3 },+ { i: 'active', x: 6, y: 1, w: 6, h: 3 },+ { i: 'next-heading', x: 0, y: 5, w: 12, h: 1 },+ ] as Layout++ expect(geometry(resolveFreePlacementCollisions(layout, 12, 'active'))).toEqual(geometry(layout))+ })+it('bounds malformed tile heights before resolving collisions', () => {const layout = [{ i: 'active', x: 0, y: 0, w: 6, h: 1_000_000 },diff --git a/products/dashboards/frontend/dashboardCustomization.ts b/products/dashboards/frontend/dashboardCustomization.tsindex 45dea1ccb369..a268ee9f3932 100644--- a/products/dashboards/frontend/dashboardCustomization.ts+++ b/products/dashboards/frontend/dashboardCustomization.ts@@ -69,20 +69,143 @@ function occupy(occupancy: GridOccupancy, item: LayoutItem, cols: number): void}}-export const freePlacementCompactor: Compactor = noCompactor+function snapBetweenAdjacentTiles(+ items: Layout,+ activeTile: LayoutItem,+ overlappingItems: LayoutItem[],+ cols: number+): void {+ if (overlappingItems.length < 2) {+ return+ }++ const [left, ...others] = [...overlappingItems].sort((first, second) => first.x - second.x)+ const right = others.find((item) => item.x > left.x)+ if (+ !right ||+ left.x + left.w > right.x ||+ overlappingItems.some(+ (item) => (item.x !== left.x || item.w !== left.w) && (item.x !== right.x || item.w !== right.w)+ )+ ) {+ return+ }++ for (let distance = 1; distance <= activeTile.w; distance++) {+ let bestCandidate: { x: number; collisions: number } | undefined+ for (const candidateX of [activeTile.x + distance, activeTile.x - distance]) {+ if (candidateX < 0 || candidateX + activeTile.w > cols) {+ continue+ }++ const collisions = items.filter(+ (item) =>+ item.i !== activeTile.i &&+ item.x < candidateX + activeTile.w &&+ item.x + item.w > candidateX &&+ item.y < activeTile.y + activeTile.h &&+ item.y + item.h > activeTile.y+ )+ if (+ collisions.length > 0 &&+ collisions.every((collision) => overlappingItems.includes(collision)) &&+ (collisions.every((collision) => collision.x === left.x) ||+ collisions.every((collision) => collision.x === right.x)) &&+ (!bestCandidate || collisions.length < bestCandidate.collisions)+ ) {+ bestCandidate = { x: candidateX, collisions: collisions.length }+ }+ }+ if (bestCandidate) {+ activeTile.x = bestCandidate.x+ return+ }+ }+}++export const freePlacementCompactor: Compactor = { ...noCompactor, allowOverlap: true }export const makeRoomInRowCompactor: Compactor = horizontalCompactorexport interface DashboardGridCompactor extends Compactor {- compactInteraction: (cols: number, activeTileId: string, restoredLayout: Layout, resizedLayout: Layout) => Layout+ compactInteraction: (+ cols: number,+ activeTileId: string,+ restoredLayout: Layout,+ resizedLayout: Layout,+ isDragging?: boolean+ ) => Layout}-export function resolveFreePlacementCollisions(layout: Layout, cols: number, activeTileId?: string | null): Layout {+export function resolveFreePlacementCollisions(+ layout: Layout,+ cols: number,+ activeTileId?: string | null,+ isDragging = true+): Layout {const items = layout.map((item) => ({...cloneLayoutItem(item),h: Math.min(item.h, MAX_FREE_FORM_TILE_HEIGHT_ROWS),}))const activeTile = activeTileId ? items.find((item) => item.i === activeTileId) : undefined+ if (activeTile && !activeTile.static && !items.some((item) => item.static)) {+ const overlappingItems = items.filter(+ (item) =>+ item.i !== activeTileId &&+ item.x < activeTile.x + activeTile.w &&+ item.x + item.w > activeTile.x &&+ item.y < activeTile.y + activeTile.h &&+ item.y + item.h > activeTile.y+ )++ if (overlappingItems.length > 0) {+ if (isDragging) {+ snapBetweenAdjacentTiles(items, activeTile, overlappingItems, cols)+ }++ const occupancy: GridOccupancy = new Map()+ const shiftByColumn = Array.from({ length: cols }, () => 0)+ const lastOriginalBottomByColumn = Array.from({ length: cols }, () => 0)+ const lastPlacedBottomByColumn = Array.from({ length: cols }, () => 0)+ occupy(occupancy, activeTile, cols)++ for (const item of items+ .filter((item) => item.i !== activeTileId)+ .sort((first, second) => first.y - second.y || first.x - second.x)) {+ const originalY = item.y+ const firstColumn = Math.max(0, item.x)+ const lastColumn = Math.min(cols, item.x + item.w)++ for (let column = firstColumn; column < lastColumn; column++) {+ if (+ originalY > lastOriginalBottomByColumn[column] &&+ originalY >= lastPlacedBottomByColumn[column]+ ) {+ shiftByColumn[column] = 0+ }+ item.y = Math.max(item.y, originalY + shiftByColumn[column])+ }++ let collisions = getOccupants(occupancy, item, cols)+ while (collisions.length > 0) {+ item.y = Math.max(...collisions.map((collision) => collision.y + collision.h))+ collisions = getOccupants(occupancy, item, cols)+ }++ for (let column = firstColumn; column < lastColumn; column++) {+ shiftByColumn[column] = Math.max(shiftByColumn[column], item.y - originalY)+ lastOriginalBottomByColumn[column] = Math.max(+ lastOriginalBottomByColumn[column],+ originalY + item.h+ )+ lastPlacedBottomByColumn[column] = Math.max(lastPlacedBottomByColumn[column], item.y + item.h)+ }+ occupy(occupancy, item, cols)+ }+ return items+ }+ }+const occupancy: GridOccupancy = new Map()for (const item of items) {if (item.static) {@@ -124,10 +247,10 @@ export function getDashboardGridCompactor(layoutCompaction?: DashboardGridCompacreturn {...compactor,- compactInteraction: (cols, activeTileId, restoredLayout, resizedLayout): Layout => {+ compactInteraction: (cols, activeTileId, restoredLayout, resizedLayout, isDragging = true): Layout => {switch (selectedCompaction) {case DashboardGridCompaction.Stable:- return resolveFreePlacementCollisions(restoredLayout, cols, activeTileId)+ return resolveFreePlacementCollisions(restoredLayout, cols, activeTileId, isDragging)case DashboardGridCompaction.Vertical:return compactor.compact(resizedLayout, cols)default: