: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): warn before adding an insight discards layout edits (#73093)
Xander9922ccab4496
diff --git a/frontend/src/scenes/dashboard/DashboardHeaderActions.tsx b/frontend/src/scenes/dashboard/DashboardHeaderActions.tsxindex f9aeedf062fa..9f8de2b7a704 100644--- a/frontend/src/scenes/dashboard/DashboardHeaderActions.tsx+++ b/frontend/src/scenes/dashboard/DashboardHeaderActions.tsx@@ -18,21 +18,20 @@ import { urls } from 'scenes/urls'import { iconForType } from '~/layout/panel-layout/ProjectTree/defaultTree'import { AccessControlLevel, AccessControlResourceType, DashboardMode } from '~/types'-import { addInsightToDashboardLogic } from './addInsightToDashboardModalLogic'import { DashboardLoadAction, dashboardLogic } from './dashboardLogic'import { DashboardSubscribeButton } from './DashboardSubscribeButton'export function getAddTileMenuItems({dashboardId,dashboardWidgetsEnabled,- showAddInsightToDashboardModal,+ onAddInsight,push,setAddWidgetModalOpen,onBeforeSelect,}: {dashboardId: numberdashboardWidgetsEnabled: boolean- showAddInsightToDashboardModal: () => void+ onAddInsight: () => voidpush: (url: string) => voidsetAddWidgetModalOpen: (open: boolean) => voidonBeforeSelect?: () => void@@ -47,7 +46,7 @@ export function getAddTileMenuItems({return [{label: 'Insight',- onClick: withBeforeSelect(showAddInsightToDashboardModal),+ onClick: withBeforeSelect(onAddInsight),'data-attr': 'dashboard-add-insight',},{@@ -79,8 +78,8 @@ export function getAddTileMenuItems({export function DashboardAddTileButton(): JSX.Element | null {const { dashboard, dashboardWidgetsEnabled } = useValues(dashboardLogic)- const { loadDashboard, setAddWidgetModalOpen, setPendingInsertion } = useActions(dashboardLogic)- const { showAddInsightToDashboardModal } = useActions(addInsightToDashboardLogic)+ const { loadDashboard, setAddWidgetModalOpen, setPendingInsertion, openAddInsightModal } =+ useActions(dashboardLogic)const { push } = useActions(router)if (!dashboard) {@@ -116,7 +115,7 @@ export function DashboardAddTileButton(): JSX.Element | null {items={getAddTileMenuItems({dashboardId: dashboard.id,dashboardWidgetsEnabled,- showAddInsightToDashboardModal,+ onAddInsight: openAddInsightModal,push,setAddWidgetModalOpen,// Adding from the header appends at the bottom; drop any stale inline-insertion target.diff --git a/frontend/src/scenes/dashboard/DashboardItems.tsx b/frontend/src/scenes/dashboard/DashboardItems.tsxindex 12e1ec232a72..84e2b485103b 100644--- a/frontend/src/scenes/dashboard/DashboardItems.tsx+++ b/frontend/src/scenes/dashboard/DashboardItems.tsx@@ -245,7 +245,7 @@ export function DashboardItems({ showCreateAnomalyAlertButton }: DashboardItemsP? getAddTileMenuItems({dashboardId: dashboard.id,dashboardWidgetsEnabled,- showAddInsightToDashboardModal,+ onAddInsight: showAddInsightToDashboardModal,push,setAddWidgetModalOpen,onBeforeSelect: () => setPendingInsertion({ x: targetX, y: targetY, w: targetW ?? null }),diff --git a/frontend/src/scenes/dashboard/dashboardLogic.test.ts b/frontend/src/scenes/dashboard/dashboardLogic.test.tsindex b93cb0026fea..d72033288f54 100644--- a/frontend/src/scenes/dashboard/dashboardLogic.test.ts+++ b/frontend/src/scenes/dashboard/dashboardLogic.test.ts@@ -13,6 +13,7 @@ import { FEATURE_FLAGS } from 'lib/constants'import { dayjs, now } from 'lib/dayjs'import { featureFlagLogic } from 'lib/logic/featureFlagLogic'import { DashboardEventSource, eventUsageLogic } from 'lib/utils/eventUsageLogic'+import { addInsightToDashboardLogic } from 'scenes/dashboard/addInsightToDashboardModalLogic'import { DashboardLoadAction, dashboardLogic } from 'scenes/dashboard/dashboardLogic'import * as dashboardUtils from 'scenes/dashboard/dashboardUtils'import * as widgetFetchUtils from 'scenes/dashboard/widgetFetchUtils'@@ -826,6 +827,69 @@ describe('dashboardLogic', () => {])})})++ describe('openAddInsightModal action', () => {+ let confirmSpy: jest.SpyInstance++ beforeEach(() => {+ confirmSpy = jest.spyOn(window, 'confirm').mockReturnValue(true)+ })++ afterEach(() => {+ confirmSpy.mockRestore()+ })++ const moveFirstTile = (): void => {+ const firstTile = logic.values.dashboard!.tiles[0]+ const currentLayouts = logic.values.layouts+ const modifiedLayouts: any = {+ ...currentLayouts,+ sm: currentLayouts.sm?.map((layout: any) =>+ layout.i === String(firstTile.id) ? { ...layout, x: (layout.x ?? 0) + 1 } : layout+ ),+ }+ logic.actions.updateLayouts(modifiedLayouts)+ }++ it('opens the modal without confirming when there are no unsaved layout changes', async () => {+ await expectLogic(logic).toFinishAllListeners()+ await expectLogic(logic, () => {+ logic.actions.setDashboardMode(DashboardMode.Edit, DashboardEventSource.SceneCommonButtons)+ }).toFinishAllListeners()++ await expectLogic(logic, () => {+ logic.actions.openAddInsightModal()+ }).toFinishAllListeners()++ expect(confirmSpy).not.toHaveBeenCalled()+ expect(addInsightToDashboardLogic.values.addInsightToDashboardModalVisible).toBe(true)+ })++ it.each([+ { accepted: true, expectedVisible: true, expectedUnsaved: false },+ { accepted: false, expectedVisible: false, expectedUnsaved: true },+ ])(+ 'gates the add-insight modal on the browser confirm (accepted=$accepted)',+ async ({ accepted, expectedVisible, expectedUnsaved }) => {+ confirmSpy.mockReturnValue(accepted)+ await expectLogic(logic).toFinishAllListeners()+ await expectLogic(logic, () => {+ logic.actions.setDashboardMode(DashboardMode.Edit, DashboardEventSource.SceneCommonButtons)+ }).toFinishAllListeners()+ await expectLogic(logic, moveFirstTile)+ .toFinishAllListeners()+ .toMatchValues({ hasUnsavedLayoutChanges: true })++ await expectLogic(logic, () => {+ logic.actions.openAddInsightModal()+ }).toFinishAllListeners()++ expect(confirmSpy).toHaveBeenCalledTimes(1)+ expect(addInsightToDashboardLogic.values.addInsightToDashboardModalVisible).toBe(expectedVisible)+ expect(logic.values.hasUnsavedLayoutChanges).toBe(expectedUnsaved)+ }+ )+ })})describe('moving between dashboards', () => {diff --git a/frontend/src/scenes/dashboard/dashboardLogic.tsx b/frontend/src/scenes/dashboard/dashboardLogic.tsxindex 0fdbbcaa9869..08a6798b9197 100644--- a/frontend/src/scenes/dashboard/dashboardLogic.tsx+++ b/frontend/src/scenes/dashboard/dashboardLogic.tsx@@ -50,6 +50,7 @@ import { DashboardEventSource, eventUsageLogic } from 'lib/utils/eventUsageLogicimport { objectsEqual } from 'lib/utils/objects'import { shouldCancelQuery } from 'lib/utils/requests'import { toParams } from 'lib/utils/url'+import { addInsightToDashboardLogic } from 'scenes/dashboard/addInsightToDashboardModalLogic'import { BREAKPOINTS, dashboardToSaveableTemplate, getDashboardTileDisplayName } from 'scenes/dashboard/dashboardUtils'import {calculateDuplicateLayout,@@ -558,6 +559,9 @@ export interface dashboardLogicActions {toDashboardName: string}}+ openAddInsightModal: () => {+ value: true+ }overrideVariableValue: (variableId: string,value: any,@@ -1105,7 +1109,7 @@ export const dashboardLogic = kea<dashboardLogicType>([dataThemeLogic,['getTheme'],],- logic: [dashboardsModel, insightsModel, eventUsageLogic],+ logic: [dashboardsModel, insightsModel, eventUsageLogic, addInsightToDashboardLogic],})),props({} as DashboardLogicProps),@@ -1292,6 +1296,7 @@ export const dashboardLogic = kea<dashboardLogicType>([}),setTextTileId: (textTileId: number | 'new' | null) => ({ textTileId }),setButtonTileId: (buttonTileId: number | 'new' | null) => ({ buttonTileId }),+ openAddInsightModal: true,setTileOverride: (tile: DashboardTile<QueryBasedInsightModel>) => ({ tile }),/**@@ -3738,6 +3743,19 @@ export const dashboardLogic = kea<dashboardLogicType>([})}},+ openAddInsightModal: () => {+ if (values.dashboardMode === DashboardMode.Edit && values.hasUnsavedLayoutChanges) {+ if (+ !window.confirm(+ 'Discard unsaved layout changes?\nAdding an insight reloads the dashboard and discards them.'+ )+ ) {+ return+ }+ actions.setDashboardMode(null, DashboardEventSource.DashboardHeaderDiscardChanges)+ }+ addInsightToDashboardLogic.actions.showAddInsightToDashboardModal()+ },cancelEditMode: () => {const discard = (): void =>actions.setDashboardMode(null, DashboardEventSource.DashboardHeaderDiscardChanges)