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 movement mode snapping back (#124343)

Matt Puacc8c8139c95f
diff --git a/frontend/src/scenes/dashboard/DashboardCustomizeSubMenu.tsx b/frontend/src/scenes/dashboard/DashboardCustomizeSubMenu.tsx
index b0449339ce91..9cbb3a3b0cfe 100644
--- a/frontend/src/scenes/dashboard/DashboardCustomizeSubMenu.tsx
+++ b/frontend/src/scenes/dashboard/DashboardCustomizeSubMenu.tsx
@@ -24,8 +24,14 @@ import {
import { dashboardLogic } from './dashboardLogic'
export function DashboardCustomizeSubMenu(): JSX.Element | null {
- const { dashboard, currentLayoutSize, layoutEditMode, tileGroupSections, tileGroupDisplay } =
- useValues(dashboardLogic)
+ const {
+ dashboard,
+ currentLayoutSize,
+ layoutEditMode,
+ tileGroupSections,
+ tileGroupDisplay,
+ dashboardGridCompactionSaving,
+ } = useValues(dashboardLogic)
const { setDashboardEditing, changeDashboardTileSpacing, changeDashboardGridCompaction, changeTileGroupDisplay } =
useActions(dashboardLogic)
@@ -82,6 +88,7 @@ export function DashboardCustomizeSubMenu(): JSX.Element | null {
<SceneMenuBarRadioItem
key={mode}
value={mode}
+ disabled={dashboardGridCompactionSaving}
data-attr={`dashboard-menubar-tile-movement-${mode}`}
>
{DASHBOARD_GRID_COMPACTION_LABELS[mode]}
diff --git a/frontend/src/scenes/dashboard/dashboardLogic.test.ts b/frontend/src/scenes/dashboard/dashboardLogic.test.ts
index d61540d9f175..55b9cd3ae083 100644
--- a/frontend/src/scenes/dashboard/dashboardLogic.test.ts
+++ b/frontend/src/scenes/dashboard/dashboardLogic.test.ts
@@ -24,7 +24,12 @@ import { pluralize } from 'lib/utils/strings'
import { addInsightToDashboardLogic } from 'scenes/dashboard/addInsightToDashboardModalLogic'
import { parseDashboardId } from 'scenes/dashboard/Dashboard'
import { dashboardInsightColorsModalLogic } from 'scenes/dashboard/dashboardInsightColorsModalLogic'
-import { DashboardLoadAction, dashboardLogic, RefreshDashboardItemsAction } from 'scenes/dashboard/dashboardLogic'
+import {
+ DashboardLoadAction,
+ dashboardLogic,
+ RefreshDashboardItemsAction,
+ type TilePlacement,
+} from 'scenes/dashboard/dashboardLogic'
import * as dashboardUtils from 'scenes/dashboard/dashboardUtils'
import { liveDashboardIds } from 'scenes/dashboard/liveDashboardIds'
import * as widgetFetchUtils from 'scenes/dashboard/widgetFetchUtils'
@@ -1648,66 +1653,28 @@ describe('dashboardLogic', () => {
}
})
- it('previews layout compaction immediately and persists only the final choice', async () => {
+ it('ignores movement mode changes while a mode save is in flight', async () => {
await expectLogic(logic).toFinishAllListeners()
- ;(api.update as jest.Mock).mockClear()
- jest.useFakeTimers()
-
- try {
- logic.actions.setDashboardGridCompaction(DashboardGridCompaction.Vertical)
- logic.actions.saveDashboardGridCompaction(DashboardGridCompaction.Vertical)
- logic.actions.setDashboardGridCompaction(DashboardGridCompaction.Horizontal)
- logic.actions.saveDashboardGridCompaction(DashboardGridCompaction.Horizontal)
-
- expect(logic.values.dashboard?.customization?.layout_compaction).toBe(
- DashboardGridCompaction.Horizontal
- )
- expect(api.update).not.toHaveBeenCalled()
+ let resolveSave: (dashboard: DashboardType) => void = () => {}
+ ;(api.update as jest.Mock)
+ .mockClear()
+ .mockImplementationOnce(() => new Promise((resolve) => (resolveSave = resolve)))
- await jest.advanceTimersByTimeAsync(750)
- await expectLogic(logic).toFinishAllListeners()
+ logic.actions.changeDashboardGridCompaction(DashboardGridCompaction.Horizontal)
+ logic.actions.changeDashboardGridCompaction(DashboardGridCompaction.Stable)
- expect(api.update).toHaveBeenCalledTimes(1)
- expect(api.update).toHaveBeenCalledWith(`api/projects/${MOCK_TEAM_ID}/dashboards/5`, {
- layout_compaction: DashboardGridCompaction.Horizontal,
- grid_spacing: 'standard',
- })
- } finally {
- jest.useRealTimers()
- }
- })
+ expect(logic.values.dashboard?.customization?.layout_compaction).toBe(DashboardGridCompaction.Horizontal)
+ expect(api.update).toHaveBeenCalledTimes(1)
- it('persists the latest movement mode after a save is already in flight', async () => {
+ resolveSave(logic.values.dashboard!)
+ await expectLogic(logic).toFinishAllListeners()
+ logic.actions.changeDashboardGridCompaction(DashboardGridCompaction.Stable)
await expectLogic(logic).toFinishAllListeners()
- let resolveFirstSave: (dashboard: DashboardType) => void = () => {
- throw new Error('First save resolver is unavailable')
- }
- const firstSave = new Promise<DashboardType>((resolve) => {
- resolveFirstSave = resolve
- })
- ;(api.update as jest.Mock).mockImplementationOnce(() => firstSave)
- jest.useFakeTimers()
-
- try {
- logic.actions.setDashboardGridCompaction(DashboardGridCompaction.Horizontal)
- logic.actions.saveDashboardGridCompaction(DashboardGridCompaction.Horizontal)
- await jest.advanceTimersByTimeAsync(750)
-
- logic.actions.setDashboardGridCompaction(DashboardGridCompaction.Stable)
- logic.actions.saveDashboardGridCompaction(DashboardGridCompaction.Stable)
- await jest.advanceTimersByTimeAsync(750)
-
- resolveFirstSave(logic.values.dashboard!)
- await jest.advanceTimersByTimeAsync(750)
- await expectLogic(logic).toFinishAllListeners()
- expect(api.update).toHaveBeenLastCalledWith(`api/projects/${MOCK_TEAM_ID}/dashboards/5`, {
- layout_compaction: DashboardGridCompaction.Stable,
- grid_spacing: 'standard',
- })
- } finally {
- jest.useRealTimers()
- }
+ expect(api.update).toHaveBeenLastCalledWith(`api/projects/${MOCK_TEAM_ID}/dashboards/5`, {
+ layout_compaction: DashboardGridCompaction.Stable,
+ grid_spacing: 'standard',
+ })
})
it('saving without changes does not call api', async () => {
@@ -3415,6 +3382,31 @@ describe('dashboardLogic', () => {
expect(dialogOpenSpy).not.toHaveBeenCalled()
})
+ it('does not count grid-added fields on the same tile cells as unsaved layout changes', async () => {
+ await expectLogic(logic).toFinishAllListeners()
+ const currentLayouts = logic.values.layouts
+ logic.actions.tilePlacementsSaved(
+ Object.fromEntries(
+ (currentLayouts.sm ?? []).map(({ i, x, y, w, h }): [number, TilePlacement] => [
+ Number(i),
+ { layouts: { sm: { x, y, w, h } } as TilePlacement['layouts'], group_key: null },
+ ])
+ )
+ )
+ logic.actions.stageLayouts({
+ ...currentLayouts,
+ sm: currentLayouts.sm?.map((layout) => ({
+ ...layout,
+ minW: 2,
+ minH: 2,
+ moved: false,
+ static: false,
+ })),
+ })
+
+ expect(logic.values.hasUnsavedLayoutChanges).toBe(false)
+ })
+
it('prompts before changing the movement mode when the layout is unsaved', async () => {
await expectLogic(logic).toFinishAllListeners()
await expectLogic(logic, moveFirstTile).toFinishAllListeners()
diff --git a/frontend/src/scenes/dashboard/dashboardLogic.tsx b/frontend/src/scenes/dashboard/dashboardLogic.tsx
index 6b4c6e194426..60dd090194b2 100644
--- a/frontend/src/scenes/dashboard/dashboardLogic.tsx
+++ b/frontend/src/scenes/dashboard/dashboardLogic.tsx
@@ -708,6 +708,7 @@ export interface dashboardLogicValues {
dashboardEditing: DashboardEditing | null
dashboardFailedToLoad: boolean
dashboardFiltersSaving: boolean
+ dashboardGridCompactionSaving: boolean
dashboardLoadData: {
action: DashboardLoadAction | undefined
dashboardQueryId: string
@@ -1320,6 +1321,9 @@ export interface dashboardLogicActions {
setDashboardGridCompaction: (layoutCompaction: DashboardGridCompaction) => {
layoutCompaction: DashboardGridCompaction
}
+ setDashboardGridCompactionSaving: (saving: boolean) => {
+ saving: boolean
+ }
setDashboardMode: (
mode: DashboardMode | null,
source: DashboardEventSource
@@ -2233,6 +2237,7 @@ export const dashboardLogic = kea<dashboardLogicType>([
setDataColorThemeId: (dataColorThemeId: number | null) => ({ dataColorThemeId }),
setDashboardTileSpacing: (tileSpacing: DashboardTileSpacing) => ({ tileSpacing }),
setDashboardGridCompaction: (layoutCompaction: DashboardGridCompaction) => ({ layoutCompaction }),
+ setDashboardGridCompactionSaving: (saving: boolean) => ({ saving }),
setDashboardTileSpacingSaving: (saving: boolean) => ({ saving }),
restoreTemporaryColorState: (colors: BreakdownColorConfig[], themeId: { themeId: number | null } | null) => ({
colors,
@@ -2710,6 +2715,12 @@ export const dashboardLogic = kea<dashboardLogicType>([
tileStreamingFailure: () => false,
},
],
+ dashboardGridCompactionSaving: [
+ false,
+ {
+ setDashboardGridCompactionSaving: (_, { saving }) => saving,
+ },
+ ],
dashboardTileSpacingSaving: [
false,
{
@@ -3674,8 +3685,14 @@ export const dashboardLogic = kea<dashboardLogicType>([
const draftByTile = draftLayouts ? layoutsByTile(draftLayouts) : null
return (dashboard.tiles || []).some((tile: DashboardTile) => {
const saved = savedTilePlacements[tile.id]
+ const savedSm = saved?.layouts?.sm
const currentSm = draftByTile?.[tile.id]?.sm ?? tile.layouts?.sm
- return !equal(saved?.layouts?.sm || {}, currentSm || {}) || tileGroupChanged(tile, saved)
+ // The grid adds its own fields (moved, static, min sizes) when it re-lays out, so compare the cell only.
+ const cellChanged =
+ savedSm && currentSm
+ ? !sameGridCell(savedSm, currentSm)
+ : Boolean(savedSm) !== Boolean(currentSm)
+ return cellChanged || tileGroupChanged(tile, saved)
})
},
],
@@ -5195,20 +5212,13 @@ export const dashboardLogic = kea<dashboardLogicType>([
}
}
},
- saveDashboardGridCompaction: async ({ layoutCompaction }, breakpoint) => {
- await breakpoint(750)
-
- if (cache.dashboardGridCompactionSaveInFlight) {
- cache.pendingDashboardGridCompaction = layoutCompaction
- return
- }
-
+ saveDashboardGridCompaction: async ({ layoutCompaction }) => {
const persistedDashboard = dashboardsModel.values.rawDashboards[props.id]
const persistedLayoutCompaction =
persistedDashboard && 'customization' in persistedDashboard
? (persistedDashboard.customization?.layout_compaction ?? DashboardGridCompaction.Vertical)
: DashboardGridCompaction.Vertical
- cache.dashboardGridCompactionSaveInFlight = true
+ actions.setDashboardGridCompactionSaving(true)
try {
// nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. Use dashboardsPartialUpdate() from 'products/dashboards/frontend/generated/api' instead.
const dashboard = await api.update<DashboardType>(
@@ -5223,21 +5233,11 @@ export const dashboardLogic = kea<dashboardLogicType>([
})
} catch (error) {
posthog.captureException(error)
- if (!cache.pendingDashboardGridCompaction) {
- actions.setDashboardGridCompaction(persistedLayoutCompaction)
- actions.loadDashboard({ action: DashboardLoadAction.Update })
- }
+ actions.setDashboardGridCompaction(persistedLayoutCompaction)
+ actions.loadDashboard({ action: DashboardLoadAction.Update })
lemonToast.error("Couldn't update tile movement. Try again.")
} finally {
- cache.dashboardGridCompactionSaveInFlight = false
- const pendingLayoutCompaction = cache.pendingDashboardGridCompaction as
- | DashboardGridCompaction
- | undefined
- cache.pendingDashboardGridCompaction = undefined
- if (pendingLayoutCompaction) {
- actions.setDashboardGridCompaction(pendingLayoutCompaction)
- actions.saveDashboardGridCompaction(pendingLayoutCompaction)
- }
+ actions.setDashboardGridCompactionSaving(false)
}
},
forceRefreshIfStale: () => {
@@ -6003,9 +6003,11 @@ export const dashboardLogic = kea<dashboardLogicType>([
actions.saveDashboardTileSpacing(tileSpacing)
},
changeDashboardGridCompaction: ({ layoutCompaction }) => {
+ // The options are disabled while a save is in flight, so a save response never carries an older choice.
if (
+ values.dashboardGridCompactionSaving ||
layoutCompaction ===
- (values.dashboard?.customization?.layout_compaction ?? DashboardGridCompaction.Vertical)
+ (values.dashboard?.customization?.layout_compaction ?? DashboardGridCompaction.Vertical)
) {
return
}
diff --git a/products/dashboards/frontend/components/DashboardCustomizeMenu/DashboardCustomizeMenu.tsx b/products/dashboards/frontend/components/DashboardCustomizeMenu/DashboardCustomizeMenu.tsx
index 5a62129785a5..9d057c95a418 100644
--- a/products/dashboards/frontend/components/DashboardCustomizeMenu/DashboardCustomizeMenu.tsx
+++ b/products/dashboards/frontend/components/DashboardCustomizeMenu/DashboardCustomizeMenu.tsx
@@ -52,8 +52,14 @@ const GRID_COMPACTION_OPTIONS: {
]
export function DashboardCustomizeMenu(): JSX.Element | null {
- const { dashboard, canEditDashboard, currentLayoutSize, tileGroupSections, tileGroupDisplay } =
- useValues(dashboardLogic)
+ const {
+ dashboard,
+ canEditDashboard,
+ currentLayoutSize,
+ tileGroupSections,
+ tileGroupDisplay,
+ dashboardGridCompactionSaving,
+ } = useValues(dashboardLogic)
const layoutAvailable = currentLayoutSize !== 'xs'
const { changeDashboardGridCompaction, changeDashboardTileSpacing, changeTileGroupDisplay } =
useActions(dashboardLogic)
@@ -85,7 +91,10 @@ export function DashboardCustomizeMenu(): JSX.Element | null {
<LemonRadio<DashboardGridCompactionType>
value={layoutCompaction}
onChange={changeDashboardGridCompaction}
- options={GRID_COMPACTION_OPTIONS}
+ options={GRID_COMPACTION_OPTIONS.map((option) => ({
+ ...option,
+ disabledReason: dashboardGridCompactionSaving ? 'Saving your last change' : undefined,
+ }))}
radioPosition="top"
className="flex-1"
aria-label="How moving a tile rearranges other tiles"