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(notebooks): keep SQL editor in sync with Tiptap node attributes (#59731)

Alex Lidera6bd3a3098b1
diff --git a/frontend/src/scenes/notebooks/Nodes/components/NotebookSQLEditor.tsx b/frontend/src/scenes/notebooks/Nodes/components/NotebookSQLEditor.tsx
index 05ccbde555d8..55bb7ebf89b1 100644
--- a/frontend/src/scenes/notebooks/Nodes/components/NotebookSQLEditor.tsx
+++ b/frontend/src/scenes/notebooks/Nodes/components/NotebookSQLEditor.tsx
@@ -1,6 +1,6 @@
import equal from 'fast-deep-equal'
import { useActions, useValues } from 'kea'
-import { useEffect, useMemo } from 'react'
+import { useEffect, useMemo, useRef } from 'react'
import { SQLEditor, SQLEditorPanel } from 'scenes/data-warehouse/editor/SQLEditor'
import { sqlEditorLogic } from 'scenes/data-warehouse/editor/sqlEditorLogic'
@@ -55,38 +55,61 @@ export function useNotebookQuerySQLEditorSync<T extends { query: QuerySchema }>(
const { queryInput, sourceQuery } = useValues(logic)
const { initialize, runQuery, setQueryInput, setSourceQuery } = useActions(logic)
+ // Sync Tiptap node attributes with sqlEditorLogic kea state. Two refs let us tell apart
+ // a remote attribute update (pull into kea) from a local edit (push to Tiptap).
+ const lastAttrRef = useRef<DataVisualizationNode | null>(null)
+ const lastLocalRef = useRef<DataVisualizationNode | null>(null)
+
useEffect(() => {
initialize()
}, [initialize])
useEffect(() => {
- if (!editorSourceQuery || queryInput !== null) {
+ if (!editorSourceQuery) {
return
}
- setQueryInput(editorSourceQuery.source.query)
- setSourceQuery(editorSourceQuery)
- runQuery(editorSourceQuery.source.query)
- }, [editorSourceQuery, queryInput, runQuery, setQueryInput, setSourceQuery])
-
- useEffect(() => {
- if (!editorSourceQuery || queryInput === null) {
- return
- }
-
- const nextQuery: DataVisualizationNode = {
+ const localQuery: DataVisualizationNode = {
...sourceQuery,
source: {
...sourceQuery.source,
- query: queryInput,
+ query: queryInput ?? '',
},
display: sourceQuery.display ?? editorSourceQuery.display ?? ChartDisplayType.ActionsTable,
}
- if (!equal(nextQuery, editorSourceQuery)) {
- updateAttributes({ query: nextQuery } as Partial<NotebookNodeAttributes<T>>)
+ if (equal(localQuery, editorSourceQuery)) {
+ lastAttrRef.current = editorSourceQuery
+ lastLocalRef.current = localQuery
+ return
+ }
+
+ if (!equal(editorSourceQuery, lastAttrRef.current)) {
+ // Attribute moved — pull (overrides any in-flight typing: last write wins).
+ // On first mount `lastAttrRef.current` is null, so we also seed kea here and
+ // kick off an initial `runQuery` so results show on load. We deliberately don't
+ // re-run on subsequent remote updates: that would fire a backend query every
+ // time another tab edits a keystroke through the collab stream.
+ const isFirstSeed = lastAttrRef.current === null
+ lastAttrRef.current = editorSourceQuery
+ lastLocalRef.current = editorSourceQuery
+ setQueryInput(editorSourceQuery.source.query)
+ setSourceQuery(editorSourceQuery)
+ if (isFirstSeed) {
+ runQuery(editorSourceQuery.source.query)
+ }
+ return
}
- }, [editorSourceQuery, queryInput, sourceQuery, updateAttributes])
+
+ if (!equal(localQuery, lastLocalRef.current)) {
+ // Editor moved — push to Tiptap.
+ lastLocalRef.current = localQuery
+ updateAttributes({ query: localQuery } as Partial<NotebookNodeAttributes<T>>)
+ return
+ }
+
+ // Tiptap hasn't propagated a push we already made — wait for the next render.
+ }, [editorSourceQuery, queryInput, sourceQuery, runQuery, setQueryInput, setSourceQuery, updateAttributes])
return editorSourceQuery
}
@@ -98,48 +121,53 @@ export function useNotebookCodeSQLEditorSync<T extends { code: string }>({
}: NotebookNodeAttributeProperties<T> & { tabId: string }): void {
const code = typeof attributes.code === 'string' ? attributes.code : ''
const logic = sqlEditorLogic({ tabId, mode: SQLEditorMode.Embedded })
- const { queryInput, sourceQuery } = useValues(logic)
+ const { queryInput } = useValues(logic)
const { initialize, setQueryInput, setSourceQuery } = useActions(logic)
+ // Tracks the last `code` and `queryInput` we observed in sync. A real attribute change
+ // (remote step, undo, programmatic) shows up as `code !== lastCodeRef` → pull. A real
+ // local edit shows up as `queryInput !== lastQueryRef` → push. A transient mismatch
+ // right after a push (Tiptap hasn't propagated yet) shows up as neither → wait.
+ // One ref isn't enough: after a push it would falsely classify the lag as a remote change.
+ const lastCodeRef = useRef<string | null>(null)
+ const lastQueryRef = useRef<string | null>(null)
+
useEffect(() => {
initialize()
}, [initialize])
useEffect(() => {
- if (queryInput !== null) {
+ if (queryInput === code) {
+ lastCodeRef.current = code
+ lastQueryRef.current = queryInput
return
}
- setQueryInput(code)
- setSourceQuery(buildSourceQuery(code))
- }, [code, queryInput, setQueryInput, setSourceQuery])
-
- useEffect(() => {
- if (queryInput === null || queryInput === code) {
+ if (code !== lastCodeRef.current) {
+ // Attribute moved — pull into kea (overrides any in-flight typing: last write wins).
+ lastCodeRef.current = code
+ lastQueryRef.current = code
+ setQueryInput(code)
+ setSourceQuery(buildSourceQuery(code))
return
}
- updateAttributes({ code: queryInput } as Partial<NotebookNodeAttributes<T>>)
- }, [code, queryInput, updateAttributes])
-
- useEffect(() => {
- if (queryInput === null) {
+ if (queryInput !== lastQueryRef.current) {
+ // Editor moved — push to Tiptap and keep sourceQuery in step with the new SQL so
+ // "Run query" uses what the user is actually looking at. Skip the push when
+ // queryInput resets to null (e.g. a re-initialize while `code` hasn't changed) —
+ // otherwise we'd write `code: null` and clear the cell on the next round-trip.
+ lastQueryRef.current = queryInput
+ if (queryInput !== null) {
+ setSourceQuery(buildSourceQuery(queryInput))
+ updateAttributes({ code: queryInput } as Partial<NotebookNodeAttributes<T>>)
+ }
return
}
- const nextSourceQuery = {
- ...sourceQuery,
- source: {
- ...sourceQuery.source,
- query: queryInput,
- },
- display: sourceQuery.display ?? ChartDisplayType.ActionsTable,
- }
-
- if (!equal(nextSourceQuery, sourceQuery)) {
- setSourceQuery(nextSourceQuery)
- }
- }, [queryInput, setSourceQuery, sourceQuery])
+ // Both sides match what we last observed but each other doesn't — Tiptap is still
+ // catching up to a push we already made. Do nothing; the next render will reconcile.
+ }, [code, queryInput, setQueryInput, setSourceQuery, updateAttributes])
}
export function NotebookSQLEditorOutput<T extends { query: QuerySchema }>({