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: refresh dashboards after insight additions (#85753)

Vasco de Krijger5897a1c01084
diff --git a/frontend/src/scenes/dashboard/DashboardHeader.tsx b/frontend/src/scenes/dashboard/DashboardHeader.tsx
index 9e7d026d9ad6..7f8e35d5fb20 100644
--- a/frontend/src/scenes/dashboard/DashboardHeader.tsx
+++ b/frontend/src/scenes/dashboard/DashboardHeader.tsx
@@ -89,7 +89,11 @@ export function DashboardHeader({ loading = false }: { loading?: boolean }): JSX
text: dashboard.name,
icon: iconForType('dashboard'),
},
- callback: () => loadDashboard({ action: DashboardLoadAction.Update }),
+ callback: (toolOutput: { dashboard_id?: string | number }) => {
+ if (Number(toolOutput?.dashboard_id) === dashboard.id) {
+ loadDashboard({ action: DashboardLoadAction.Update })
+ }
+ },
}
: undefined
}
diff --git a/frontend/src/scenes/saved-insights/addSavedInsightsModalLogic.test.ts b/frontend/src/scenes/saved-insights/addSavedInsightsModalLogic.test.ts
index 523ec9eb70d6..a9809db8d780 100644
--- a/frontend/src/scenes/saved-insights/addSavedInsightsModalLogic.test.ts
+++ b/frontend/src/scenes/saved-insights/addSavedInsightsModalLogic.test.ts
@@ -4,6 +4,9 @@ import { expectLogic, partial } from 'kea-test-utils'
import { FEATURE_FLAGS } from 'lib/constants'
import { featureFlagLogic } from 'lib/logic/featureFlagLogic'
+import { DashboardLoadAction, dashboardLogic } from 'scenes/dashboard/dashboardLogic'
+import { dashboardResult } from 'scenes/dashboard/dashboardLogic.testHelpers'
+import { insightsApi } from 'scenes/insights/utils/api'
import { useMocks } from '~/mocks/jest'
import { initKeaTests } from '~/test/init'
@@ -263,4 +266,22 @@ describe('addSavedInsightsModalLogic', () => {
expect(apiCallCount).toBe(1)
})
})
+
+ it('refreshes the mounted dashboard after adding an insight', async () => {
+ initKeaTests()
+ const dashboard = dashboardLogic({ id: 1, dashboard: dashboardResult(1, []) })
+ dashboard.mount()
+ const loadDashboard = jest.spyOn(dashboard.actions, 'loadDashboard').mockImplementation()
+ jest.spyOn(insightsApi, 'update').mockResolvedValue(createInsight(1))
+
+ const logic = addSavedInsightsModalLogic()
+ logic.mount()
+
+ await expectLogic(logic, () => logic.actions.addInsightToDashboard(createInsight(1), 1)).toFinishAllListeners()
+
+ expect(loadDashboard).toHaveBeenCalledWith({ action: DashboardLoadAction.Update })
+
+ logic.unmount()
+ dashboard.unmount()
+ })
})
diff --git a/frontend/src/scenes/saved-insights/addSavedInsightsModalLogic.ts b/frontend/src/scenes/saved-insights/addSavedInsightsModalLogic.ts
index e6af5a5add41..e5e166698aef 100644
--- a/frontend/src/scenes/saved-insights/addSavedInsightsModalLogic.ts
+++ b/frontend/src/scenes/saved-insights/addSavedInsightsModalLogic.ts
@@ -371,10 +371,9 @@ export const addSavedInsightsModalLogic = kea<addSavedInsightsModalLogicType>([
})
if (response) {
actions.updateInsight(response)
- const logic = dashboardLogic({ id: dashboardId })
- logic.mount()
- logic.actions.loadDashboard({ action: DashboardLoadAction.Update })
- logic.unmount()
+ dashboardLogic.findMounted({ id: dashboardId })?.actions.loadDashboard({
+ action: DashboardLoadAction.Update,
+ })
lemonToast.success('Insight added to dashboard')
}
} catch (e) {
@@ -402,10 +401,9 @@ export const addSavedInsightsModalLogic = kea<addSavedInsightsModalLogicType>([
})
if (response) {
actions.updateInsight(response)
- const logic = dashboardLogic({ id: dashboardId })
- logic.mount()
- logic.actions.loadDashboard({ action: DashboardLoadAction.Update })
- logic.unmount()
+ dashboardLogic.findMounted({ id: dashboardId })?.actions.loadDashboard({
+ action: DashboardLoadAction.Update,
+ })
lemonToast.success('Insight removed from dashboard')
}
} catch (e) {