Skip to content

: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.

fix(dashboards): stop tile group grids swapping layouts forever (#123833)

Sam Penningtonba175b0247f6
diff --git a/frontend/src/scenes/dashboard/DashboardItems.test.tsx b/frontend/src/scenes/dashboard/DashboardItems.test.tsx
index f627d759e2c3..1382f994dac8 100644
--- a/frontend/src/scenes/dashboard/DashboardItems.test.tsx
+++ b/frontend/src/scenes/dashboard/DashboardItems.test.tsx
@@ -145,6 +145,49 @@ jest.mock('products/dashboards/frontend/components/DashboardTextItem/DashboardTe
}))
jest.mock('react-grid-layout', () => {
+ const MockGrid = ({
+ className,
+ style,
+ rowHeight,
+ margin,
+ gridConfig,
+ resizeConfig,
+ dragConfig,
+ positionStrategy,
+ children,
+ }: {
+ className: string
+ style?: Record<string, number>
+ rowHeight?: number
+ margin?: [number, number]
+ gridConfig?: { rowHeight: number; margin: [number, number] }
+ resizeConfig: { enabled: boolean }
+ dragConfig: { enabled: boolean }
+ positionStrategy?: unknown
+ children: any
+ }): JSX.Element => {
+ jest.requireActual('react').useEffect(() => {
+ mockGridMounts++
+ }, [])
+ return (
+ <div
+ ref={(element) => {
+ if (element) {
+ mockPositionStrategies.set(element, positionStrategy)
+ }
+ }}
+ data-attr="react-grid-layout"
+ data-style-width={style?.width}
+ data-class-name={className}
+ data-row-height={String(rowHeight ?? gridConfig?.rowHeight)}
+ data-margin={(margin ?? gridConfig?.margin ?? []).join(',')}
+ data-resize-enabled={String(resizeConfig.enabled)}
+ data-drag-enabled={String(dragConfig.enabled)}
+ >
+ {children}
+ </div>
+ )
+ }
return {
cloneLayoutItem: (item: Record<string, unknown>) => ({ ...item }),
useContainerWidth: () => ({
@@ -157,47 +200,8 @@ jest.mock('react-grid-layout', () => {
allowOverlap: false,
compact: (layout: unknown[]) => layout,
},
- Responsive: ({
- className,
- style,
- rowHeight,
- margin,
- resizeConfig,
- dragConfig,
- positionStrategy,
- children,
- }: {
- className: string
- style?: Record<string, number>
- rowHeight: number
- margin: [number, number]
- resizeConfig: { enabled: boolean }
- dragConfig: { enabled: boolean }
- positionStrategy?: unknown
- children: any
- }) => {
- jest.requireActual('react').useEffect(() => {
- mockGridMounts++
- }, [])
- return (
- <div
- ref={(element) => {
- if (element) {
- mockPositionStrategies.set(element, positionStrategy)
- }
- }}
- data-attr="react-grid-layout"
- data-style-width={style?.width}
- data-class-name={className}
- data-row-height={String(rowHeight)}
- data-margin={margin.join(',')}
- data-resize-enabled={String(resizeConfig.enabled)}
- data-drag-enabled={String(dragConfig.enabled)}
- >
- {children}
- </div>
- )
- },
+ Responsive: MockGrid,
+ GridLayout: MockGrid,
}
})
diff --git a/frontend/src/scenes/dashboard/DashboardItems.tsx b/frontend/src/scenes/dashboard/DashboardItems.tsx
index 0c6c6d207d71..5f5ae9f0f805 100644
--- a/frontend/src/scenes/dashboard/DashboardItems.tsx
+++ b/frontend/src/scenes/dashboard/DashboardItems.tsx
@@ -4,7 +4,7 @@ import clsx from 'clsx'
import { useActions, useAsyncActions, useValues } from 'kea'
import { router } from 'kea-router'
import { RefObject, memo, useCallback, useEffect, useMemo, useRef, useState } from 'react'
-import { Responsive as ReactGridLayout } from 'react-grid-layout'
+import { GridLayout, Responsive as ReactGridLayout } from 'react-grid-layout'
import type { Layout, LayoutItem } from 'react-grid-layout'
import { type PositionStrategy, setTransform } from 'react-grid-layout/core'
@@ -485,6 +485,19 @@ export function DashboardItems({ showCreateAnomalyAlertButton }: DashboardItemsP
updateContainerWidth(gridWidth, BREAKPOINT_COLUMN_COUNTS[bandBreakpoint])
}
}, [showBands, gridWidth, bandBreakpoint, updateContainerWidth])
+ const bandGridConfig = useMemo(
+ () => ({
+ cols: BREAKPOINT_COLUMN_COUNTS[bandBreakpoint],
+ rowHeight,
+ margin,
+ containerPadding: CONTAINER_PADDING,
+ }),
+ [bandBreakpoint, rowHeight, margin]
+ )
+ const handleBandLayoutChange = useCallback(
+ (layout: Layout) => handleLayoutChange(layout, { [bandBreakpoint]: layout }),
+ [handleLayoutChange, bandBreakpoint]
+ )
const handleResizeStart = useCallback(
(
@@ -755,12 +768,6 @@ export function DashboardItems({ showCreateAnomalyAlertButton }: DashboardItemsP
dragConfig,
resizeConfig,
compactor: gridCompactor,
- rowHeight,
- margin,
- containerPadding: CONTAINER_PADDING,
- onLayoutChange: handleLayoutChange,
- breakpoints: BREAKPOINTS,
- cols: BREAKPOINT_COLUMN_COUNTS,
onResizeStart: handleResizeStart,
onResizeStop: handleResizeStop,
onDragStart: handleDragStart,
@@ -790,9 +797,10 @@ export function DashboardItems({ showCreateAnomalyAlertButton }: DashboardItemsP
height={sectionRows * (rowHeight + margin[1]) - margin[1]}
/>
)}
- <ReactGridLayout
+ {/* Not Responsive: its layout copy and the inner grid's can swap on every render while tiles stream in. */}
+ <GridLayout
{...gridProps}
- // The grid takes a new tile's position from `layouts`, so a stable key lets tiles stream
+ // The grid takes a new tile's position from `layout`, so a stable key lets tiles stream
// into a band without a remount that resets every chart already in it.
key={section.key}
width={gridWidth}
@@ -802,11 +810,12 @@ export function DashboardItems({ showCreateAnomalyAlertButton }: DashboardItemsP
layoutEditMode && (isLastSection ? 'pb-[40vh]' : 'pb-24'),
isDraggingOutOfSection && '[&_.react-grid-placeholder]:hidden'
)}
- breakpoint={bandBreakpoint}
- layouts={sectionLayouts}
+ gridConfig={bandGridConfig}
+ layout={sectionLayouts[bandBreakpoint] ?? []}
+ onLayoutChange={handleBandLayoutChange}
>
{section.tiles.map(renderTile)}
- </ReactGridLayout>
+ </GridLayout>
</div>
)
}
@@ -865,6 +874,12 @@ export function DashboardItems({ showCreateAnomalyAlertButton }: DashboardItemsP
<ReactGridLayout
{...gridProps}
+ rowHeight={rowHeight}
+ margin={margin}
+ containerPadding={CONTAINER_PADDING}
+ breakpoints={BREAKPOINTS}
+ cols={BREAKPOINT_COLUMN_COUNTS}
+ onLayoutChange={handleLayoutChange}
width={gridWidth}
style={{ width: gridWidth }}
className={className}