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: Reload insight when returning from SQL editor (#61692)

Luke Belton6fcbeb63fc8d
diff --git a/frontend/src/scenes/insights/InsightAsScene.tsx b/frontend/src/scenes/insights/InsightAsScene.tsx
index 4a6356e6f1fa..c4c35437e973 100644
--- a/frontend/src/scenes/insights/InsightAsScene.tsx
+++ b/frontend/src/scenes/insights/InsightAsScene.tsx
@@ -3,6 +3,7 @@ import { BindLogic, BuiltLogic, Logic, LogicWrapper, useActions, useValues } fro
import { AccessDenied } from 'lib/components/AccessDenied'
import { useFileSystemLogView } from 'lib/hooks/useFileSystemLogView'
+import { LemonSkeleton } from 'lib/lemon-ui/LemonSkeleton'
import { useAttachedLogic } from 'lib/logic/scenes/useAttachedLogic'
import { InsightModals } from 'scenes/insights/InsightModals'
import { insightSceneLogic } from 'scenes/insights/insightSceneLogic'
@@ -10,7 +11,7 @@ import { insightSceneLogic } from 'scenes/insights/insightSceneLogic'
import { SceneContent } from '~/layout/scenes/components/SceneContent'
import { Query } from '~/queries/Query/Query'
import { Node } from '~/queries/schema/schema-general'
-import { containsHogQLQuery, isInsightVizNode } from '~/queries/utils'
+import { containsHogQLQuery, isDataVisualizationNode, isInsightVizNode } from '~/queries/utils'
import { InsightShortId, ItemMode } from '~/types'
import { teamLogic } from '../teamLogic'
@@ -40,7 +41,7 @@ export function InsightAsScene({ insightId, attachTo, tabId }: InsightAsScenePro
filtersOverride,
variablesOverride,
})
- const { insightProps, accessDeniedToInsight } = useValues(logic)
+ const { insightProps, accessDeniedToInsight, insightLoading } = useValues(logic)
// insightDataLogic
const { query, showQueryEditor } = useValues(insightDataLogic(insightProps))
@@ -87,21 +88,26 @@ export function InsightAsScene({ insightId, attachTo, tabId }: InsightAsScenePro
<InsightSceneHeader insightLogicProps={insightProps} />
)}
- <Query
- attachTo={attachTo}
- query={isInsightVizNode(query) ? { ...query, full: true } : query}
- setQuery={setQuery}
- readOnly={insightMode !== ItemMode.Edit}
- editMode={insightMode === ItemMode.Edit}
- context={{
- showOpenEditorButton: false,
- showQueryEditor: actuallyShowQueryEditor,
- showQueryHelp: insightMode === ItemMode.Edit && !containsHogQLQuery(query),
- insightProps,
- }}
- filtersOverride={filtersOverride}
- variablesOverride={variablesOverride}
- />
+ {isDataVisualizationNode(query) && insightLoading ? (
+ // Avoid painting the stale chart type during a reload (the query re-syncs in insightDataLogic).
+ <LemonSkeleton className="h-100 w-full" />
+ ) : (
+ <Query
+ attachTo={attachTo}
+ query={isInsightVizNode(query) ? { ...query, full: true } : query}
+ setQuery={setQuery}
+ readOnly={insightMode !== ItemMode.Edit}
+ editMode={insightMode === ItemMode.Edit}
+ context={{
+ showOpenEditorButton: false,
+ showQueryEditor: actuallyShowQueryEditor,
+ showQueryHelp: insightMode === ItemMode.Edit && !containsHogQLQuery(query),
+ insightProps,
+ }}
+ filtersOverride={filtersOverride}
+ variablesOverride={variablesOverride}
+ />
+ )}
</SceneContent>
</BindLogic>
)
diff --git a/frontend/src/scenes/insights/insightDataLogic.tsx b/frontend/src/scenes/insights/insightDataLogic.tsx
index 9ab3ce56ed02..668307fe6b45 100644
--- a/frontend/src/scenes/insights/insightDataLogic.tsx
+++ b/frontend/src/scenes/insights/insightDataLogic.tsx
@@ -73,7 +73,7 @@ export const insightDataLogic = kea<insightDataLogicType>([
],
actions: [
insightLogic,
- ['setInsight', 'setInsightMetadata'],
+ ['setInsight', 'setInsightMetadata', 'loadInsightSuccess'],
dataNodeLogic({ key: insightVizDataNodeKey(props) } as DataNodeLogicProps),
['loadData', 'loadDataSuccess', 'loadDataFailure', 'setResponse as setInsightData'],
],
@@ -290,6 +290,14 @@ export const insightDataLogic = kea<insightDataLogicType>([
actions.setInsightData({ ...values.insightData, result })
}
},
+ loadInsightSuccess: ({ insight }) => {
+ // `internalQuery` wins over `insight.query` in the `query` selector, and the SQL editor
+ // updates a different logic instance — so a reload alone leaves this scene on the stale
+ // query until a hard refresh. Re-sync the override to the freshly loaded query.
+ if (insight.query && !objectsEqual(insight.query, values.query)) {
+ actions.syncQueryFromProps(insight.query)
+ }
+ },
cancelChanges: () => {
const savedQuery = values.savedInsight.query
const savedResult = values.savedInsight.result