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): show loading state for saved insights (#111013)

Marius Andra82446c1e0892
diff --git a/frontend/snapshots.yml b/frontend/snapshots.yml
index aa167a2e9b54..e36f355a47ec 100644
--- a/frontend/snapshots.yml
+++ b/frontend/snapshots.yml
@@ -9510,6 +9510,10 @@ snapshots:
hash: v1.k794b7964.8c150639468430cd106d26a5e3dde526b0d08bde8d07f37527113386e022d97b.xTBWyA6WWMQjVdb8wJvusy7_4oieQVnkHe2FCJxpQHg
scenes-app-data-warehouse-sql-editor--lazy-schema--light:
hash: v1.k794b7964.3b4c2157445c95071aca5b730f192c94fe1c7379bbec6f9e662fb0f52c74a1cb.P6dqBWgIQEb6E9d0pr2oYfd_klhDKPyksBErJxXKTY4
+ scenes-app-data-warehouse-sql-editor--loading-insight--dark:
+ hash: v1.k794b7964.c5cca89da338912886f2d329a909da87ca3ce5e6f12e0c90ee23c7c8241f768e.XfxPjuo4QbCabh0VgS40-cL0nvbKa4LIqmMHGreN0zI
+ scenes-app-data-warehouse-sql-editor--loading-insight--light:
+ hash: v1.k794b7964.2d303aecb03d48aa8dacc6e8d6cf8ff68abb8229a4338689b74b6f79d8589ed3.JQsHYIybl3SsiKa9u_elKJhuk1Td__oUl5pjqfEO-OE
scenes-app-data-warehouse-sql-editor--managed-warehouse-connection--dark:
hash: v1.k794b7964.3c4bbe63ff30dee2cdef4a41a06d5a83bedcbaf8fd06bd14e8f7632f5c3d097c.qzBmH7GQm_oM_jCNWuSApX-cI65YcyEnwosDth6tlnE
scenes-app-data-warehouse-sql-editor--managed-warehouse-connection--light:
diff --git a/frontend/src/scenes/data-warehouse/editor/QueryPane.tsx b/frontend/src/scenes/data-warehouse/editor/QueryPane.tsx
index 13d2d3b39b55..e9eea36fd0d6 100644
--- a/frontend/src/scenes/data-warehouse/editor/QueryPane.tsx
+++ b/frontend/src/scenes/data-warehouse/editor/QueryPane.tsx
@@ -2,6 +2,7 @@ import { useActions, useValues } from 'kea'
import { useMemo } from 'react'
import { IconCheck, IconX } from '@posthog/icons'
+import { Spinner } from '@posthog/lemon-ui'
import { AutoSizer } from 'lib/components/AutoSizer'
import { Resizer } from 'lib/components/Resizer/Resizer'
@@ -28,7 +29,8 @@ interface QueryPaneProps {
export function QueryPane(props: QueryPaneProps): JSX.Element {
const { queryPaneHeight, queryPaneDesiredSize, queryPaneResizerProps } = useValues(editorSizingLogic)
const { onAcceptSuggestedQueryInput, onRejectSuggestedQueryInput } = useActions(sqlEditorLogic)
- const { acceptText, rejectText, diffShowRunButton } = useValues(sqlEditorLogic)
+ const { acceptText, rejectText, diffShowRunButton, insightLoading } = useValues(sqlEditorLogic)
+ const queryLoading = insightLoading && !props.queryInput
const editorOptions = useMemo<CodeEditorProps['options']>(
() => ({
minimap: {
@@ -43,8 +45,9 @@ export function QueryPane(props: QueryPaneProps): JSX.Element {
showInlineDetails: true,
},
quickSuggestionsDelay: 300,
+ readOnly: queryLoading,
}),
- [props.originalValue]
+ [props.originalValue, queryLoading]
)
// Without an output pane beneath it the editor owns its column, so it takes whatever height the
// database tree gives the row rather than leaving dead space next to the schema list.
@@ -87,14 +90,23 @@ export function QueryPane(props: QueryPaneProps): JSX.Element {
width={width}
originalValue={props.originalValue}
enableVimMode={props.editorVimModeEnabled}
- autoFocus={true}
{...props.codeEditorProps}
+ autoFocus={!queryLoading && (props.codeEditorProps.autoFocus ?? true)}
options={editorOptions}
/>
) : null
}
/>
</div>
+ {queryLoading && (
+ <div
+ className="absolute inset-0 z-10 flex items-center justify-center gap-2 bg-primary"
+ role="status"
+ >
+ <Spinner />
+ <span>Loading query…</span>
+ </div>
+ )}
{props.originalValue && (
<div
className="absolute flex gap-1 bg-bg-light rounded border py-1 px-1.5 z-10 left-1/2 -translate-x-1/2 bottom-4 whitespace-nowrap"
diff --git a/frontend/src/scenes/data-warehouse/editor/SQLEditorScene.stories.tsx b/frontend/src/scenes/data-warehouse/editor/SQLEditorScene.stories.tsx
index 74aab7e61706..46041708a098 100644
--- a/frontend/src/scenes/data-warehouse/editor/SQLEditorScene.stories.tsx
+++ b/frontend/src/scenes/data-warehouse/editor/SQLEditorScene.stories.tsx
@@ -1,5 +1,6 @@
import { Decorator, Meta, StoryObj } from '@storybook/react'
import { BindLogic } from 'kea'
+import { delay } from 'msw'
import { useEffect, useRef } from 'react'
import { FEATURE_FLAGS } from 'lib/constants'
@@ -183,6 +184,27 @@ export default meta
type Story = StoryObj<{}>
export const TopToolsPerServer: Story = {}
+export const LoadingInsight: Story = {
+ parameters: {
+ pageUrl: `${urls.sqlEditor()}?open_insight=loading1`,
+ testOptions: {
+ waitForLoadersToDisappear: false,
+ waitForSelector: '[data-attr="hogql-query-editor"] ~ [role="status"]',
+ },
+ msw: {
+ mocks: {
+ get: {
+ '/api/environments/:team_id/insights/': async () => {
+ await delay('infinite')
+ return [200, { results: [] }]
+ },
+ '/api/projects/:team_id/warehouse_expressions/': { results: [] },
+ },
+ },
+ },
+ },
+}
+
// Selecting the managed warehouse puts its long name in the sidebar's connection selector, where it
// has to ellipsize on one line rather than wrap or overflow into the Run button's toolbar.
export const ManagedWarehouseConnection: Story = {
diff --git a/frontend/src/scenes/data-warehouse/editor/sqlEditorLogic.test.ts b/frontend/src/scenes/data-warehouse/editor/sqlEditorLogic.test.ts
index e867432d8c85..0670b66a77a6 100644
--- a/frontend/src/scenes/data-warehouse/editor/sqlEditorLogic.test.ts
+++ b/frontend/src/scenes/data-warehouse/editor/sqlEditorLogic.test.ts
@@ -1234,19 +1234,40 @@ describe('sqlEditorLogic', () => {
}
)
- it('sets insightLoading to false after insight finishes loading', async () => {
- logic = sqlEditorLogic({
- tabId: TAB_ID,
- monaco: createMockMonaco(),
- editor: createMockEditor(),
- })
- logic.mount()
+ it.each([
+ ['found', MOCK_INSIGHT],
+ ['not found', null],
+ ] as const)('shows initial loading until the insight resolves: %s', async (_, insight) => {
+ jest.useFakeTimers()
+ let resolveInsight!: (insight: InsightModel | null) => void
+ const getInsight = jest.spyOn(insightsApi, 'getByShortId').mockImplementationOnce(
+ () =>
+ new Promise((resolve) => {
+ resolveInsight = resolve
+ })
+ )
+ try {
+ logic = sqlEditorLogic({ tabId: TAB_ID })
+ logic.mount()
+ router.actions.push(urls.sqlEditor(), { open_insight: MOCK_INSIGHT_SHORT_ID })
- router.actions.push(urls.sqlEditor(), { open_insight: MOCK_INSIGHT_SHORT_ID })
+ expect(logic.values.insightLoading).toBe(true)
+ expect(logic.values.queryInput).toBe(null)
+ expect(getInsight).not.toHaveBeenCalled()
- await expectLogic(logic).toDispatchActions(['editInsight', 'createTab', 'updateTab']).toMatchValues({
- insightLoading: false,
- })
+ sqlEditorLogic({ tabId: TAB_ID, monaco: createMockMonaco(), editor: createMockEditor() })
+ await jest.advanceTimersByTimeAsync(300)
+ expect(getInsight).toHaveBeenCalled()
+ expect(logic.values.insightLoading).toBe(true)
+
+ resolveInsight(insight)
+ await jest.advanceTimersByTimeAsync(0)
+ expect(logic.values.insightLoading).toBe(false)
+ expect(logic.values.queryInput).toBe(insight ? MOCK_INSIGHT_QUERY.source.query : null)
+ } finally {
+ getInsight.mockRestore()
+ jest.useRealTimers()
+ }
})
it('opens the insight when Monaco loads after the URL handler stops waiting for it', async () => {
@@ -1258,6 +1279,7 @@ describe('sqlEditorLogic', () => {
router.actions.push(urls.sqlEditor(), { open_insight: MOCK_INSIGHT_SHORT_ID })
await jest.advanceTimersByTimeAsync(11_000)
await expectLogic(logic).toNotHaveDispatchedActions(['editInsight'])
+ expect(logic.values.insightLoading).toBe(true)
} finally {
jest.useRealTimers()
}
@@ -1267,6 +1289,7 @@ describe('sqlEditorLogic', () => {
await expectLogic(logic)
.toDispatchActions(['editInsight', 'createTab', 'updateTab'])
.toMatchValues({
+ insightLoading: false,
editingInsight: partial({
short_id: MOCK_INSIGHT_SHORT_ID,
}),
diff --git a/frontend/src/scenes/data-warehouse/editor/sqlEditorLogic.tsx b/frontend/src/scenes/data-warehouse/editor/sqlEditorLogic.tsx
index 5c38b18d91c6..298c31cb6a0a 100644
--- a/frontend/src/scenes/data-warehouse/editor/sqlEditorLogic.tsx
+++ b/frontend/src/scenes/data-warehouse/editor/sqlEditorLogic.tsx
@@ -4128,6 +4128,12 @@ export const sqlEditorLogic = kea<sqlEditorLogicType>([
}
}
+ if (values.queryInput === null) {
+ actions.setInsightLoading(
+ !!(!draftIdFromUrl && !viewIdFromUrl && insightShortIdFromUrl && insightShortIdFromUrl !== 'new')
+ )
+ }
+
if (props.monaco) {
await createQueryTab()
} else {