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(sql-editor): open linked insights when monaco loads after the wait (#110879)

Chris Shine545f2bf20cd4
diff --git a/frontend/src/scenes/data-warehouse/editor/sqlEditorLogic.test.ts b/frontend/src/scenes/data-warehouse/editor/sqlEditorLogic.test.ts
index 2183951675e4..8bf1a087a4c7 100644
--- a/frontend/src/scenes/data-warehouse/editor/sqlEditorLogic.test.ts
+++ b/frontend/src/scenes/data-warehouse/editor/sqlEditorLogic.test.ts
@@ -1241,6 +1241,52 @@ describe('sqlEditorLogic', () => {
})
})
+ it('opens the insight when Monaco loads after the URL handler stops waiting for it', async () => {
+ jest.useFakeTimers()
+ try {
+ logic = sqlEditorLogic({ tabId: TAB_ID })
+ logic.mount()
+
+ router.actions.push(urls.sqlEditor(), { open_insight: MOCK_INSIGHT_SHORT_ID })
+ await jest.advanceTimersByTimeAsync(11_000)
+ await expectLogic(logic).toNotHaveDispatchedActions(['editInsight'])
+ } finally {
+ jest.useRealTimers()
+ }
+
+ sqlEditorLogic({ tabId: TAB_ID, monaco: createMockMonaco(), editor: createMockEditor() })
+
+ await expectLogic(logic)
+ .toDispatchActions(['editInsight', 'createTab', 'updateTab'])
+ .toMatchValues({
+ editingInsight: partial({
+ short_id: MOCK_INSIGHT_SHORT_ID,
+ }),
+ })
+ })
+
+ it('does not open an older URL target over a newer one when Monaco loads between their timeouts', async () => {
+ jest.useFakeTimers()
+ try {
+ logic = sqlEditorLogic({ tabId: TAB_ID })
+ logic.mount()
+
+ router.actions.push(urls.sqlEditor(), { open_insight: MOCK_INSIGHT_SHORT_ID })
+ await jest.advanceTimersByTimeAsync(5_000)
+ router.actions.push(urls.sqlEditor(), { open_query: 'SELECT 2' })
+ await jest.advanceTimersByTimeAsync(6_000)
+
+ sqlEditorLogic({ tabId: TAB_ID, monaco: createMockMonaco(), editor: createMockEditor() })
+ await jest.advanceTimersByTimeAsync(1_000)
+ } finally {
+ jest.useRealTimers()
+ }
+
+ await expectLogic(logic).toNotHaveDispatchedActions(['editInsight']).toMatchValues({
+ queryInput: 'SELECT 2',
+ })
+ })
+
it('preserves editingInsight when reopening after starting from a new SQL tab', async () => {
logic = sqlEditorLogic({
tabId: TAB_ID,
diff --git a/frontend/src/scenes/data-warehouse/editor/sqlEditorLogic.tsx b/frontend/src/scenes/data-warehouse/editor/sqlEditorLogic.tsx
index 41e11e80355b..dac49e618704 100644
--- a/frontend/src/scenes/data-warehouse/editor/sqlEditorLogic.tsx
+++ b/frontend/src/scenes/data-warehouse/editor/sqlEditorLogic.tsx
@@ -1497,6 +1497,18 @@ export const sqlEditorLogic = kea<sqlEditorLogicType>([
) {
actions.initialize()
+ if (cache.pendingUrlOpen) {
+ // propsChanged runs inside the SQLEditor render, so open the URL target after the render
+ cache.disposables.add(() => {
+ const timeoutId = window.setTimeout(() => {
+ const pendingUrlOpen = cache.pendingUrlOpen
+ cache.pendingUrlOpen = null
+ void pendingUrlOpen?.()
+ }, 0)
+ return () => window.clearTimeout(timeoutId)
+ }, 'pendingUrlOpen')
+ }
+
// Listen for cursor position changes to update the active query highlight.
// Debounced because each run can fire a HogQLMetadata request for the current
// subquery, which is too expensive to do on every arrow key.
@@ -3680,7 +3692,7 @@ export const sqlEditorLogic = kea<sqlEditorLogicType>([
return [urls.sqlEditor(), undefined, getTabHash(values), { replace: true }]
},
})),
- urlToAction(({ actions, values, props }) => ({
+ urlToAction(({ actions, values, props, cache }) => ({
[urls.sqlEditor()]: async (_, searchParams, hashParams, { initial }) => {
if (isEmbeddedSQLEditorMode(props.mode ?? SQLEditorMode.FullScene)) {
return
@@ -4099,6 +4111,10 @@ export const sqlEditorLogic = kea<sqlEditorLogicType>([
if (props.monaco) {
await createQueryTab()
} else {
+ // The newest URL wins. A run that a newer URL replaced drops its target when its wait times out.
+ const urlOpenGeneration = (cache.urlOpenGeneration ?? 0) + 1
+ cache.urlOpenGeneration = urlOpenGeneration
+ cache.pendingUrlOpen = null
const waitUntilMonaco = async (): Promise<void> => {
return await new Promise((resolve, reject) => {
let intervalCount = 0
@@ -4123,6 +4139,10 @@ export const sqlEditorLogic = kea<sqlEditorLogicType>([
// Monaco timed out - still try to create tab if monaco loaded late
if (props.monaco) {
await createQueryTab()
+ } else if (cache.urlOpenGeneration === urlOpenGeneration) {
+ // A hidden browser tab can delay Monaco past the wait, and nothing runs this handler
+ // again when Monaco loads. propsChanged opens the URL target when Monaco arrives.
+ cache.pendingUrlOpen = createQueryTab
}
}
}