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): stop dataframe column clicks leaving the notebook (#77349)

Sinan Kucukkoseler88c3d943b71e
diff --git a/frontend/src/scenes/data-warehouse/editor/sidebar/QueryDatabase.test.ts b/frontend/src/scenes/data-warehouse/editor/sidebar/QueryDatabase.test.ts
index e212c1b8961b..da8eef6d44cb 100644
--- a/frontend/src/scenes/data-warehouse/editor/sidebar/QueryDatabase.test.ts
+++ b/frontend/src/scenes/data-warehouse/editor/sidebar/QueryDatabase.test.ts
@@ -1,6 +1,21 @@
-import { getSidebarAddJoinSourceTableName } from './QueryDatabase'
+import { getColumnInsertText, getSidebarAddJoinSourceTableName } from './QueryDatabase'
describe('QueryDatabase', () => {
+ describe('getColumnInsertText', () => {
+ test.each([
+ ['a column inserts its name at the cursor', { type: 'column', columnName: 'id' }, 'id'],
+ // A row is free to render as a column without carrying a name — the notebook's dataframe
+ // section did for a while. Escaping the missing name throws, and the throw escapes the
+ // row's <a> handler before it can preventDefault, so the browser follows the placeholder
+ // href and drops the user on the project home page.
+ ['a column with no name inserts nothing', { type: 'column' }, null],
+ ['a row that is not a column inserts nothing', { type: 'table', columnName: 'id' }, null],
+ ['a row with no record inserts nothing', undefined, null],
+ ])('%s', (_name, record, expected) => {
+ expect(getColumnInsertText(record)).toEqual(expected)
+ })
+ })
+
describe('getSidebarAddJoinSourceTableName', () => {
test.each([
['table rows keep add join in the table-specific menu', 'table', 'events', undefined, null],
diff --git a/frontend/src/scenes/data-warehouse/editor/sidebar/QueryDatabase.tsx b/frontend/src/scenes/data-warehouse/editor/sidebar/QueryDatabase.tsx
index e5909952b35a..e44d80f3596d 100644
--- a/frontend/src/scenes/data-warehouse/editor/sidebar/QueryDatabase.tsx
+++ b/frontend/src/scenes/data-warehouse/editor/sidebar/QueryDatabase.tsx
@@ -73,6 +73,20 @@ export function getSidebarAddJoinSourceTableName(
}
}
+/**
+ * The text a column row inserts at the cursor, or null when there is nothing to insert.
+ *
+ * The name has to be checked rather than assumed: escaping an undefined one throws, and a throw
+ * escapes the tree row's `<a>` click handler before it can preventDefault, so the browser follows
+ * the row's placeholder href and leaves the scene the tree is embedded in.
+ */
+export function getColumnInsertText(record: Record<string, any> | undefined): string | null {
+ if (record?.type !== 'column' || !record.columnName) {
+ return null
+ }
+ return escapeDottedHogQLIdentifier(record.columnName)
+}
+
/**
* Keep a section whose folder, or any of its children, matches the search term — the same
* name-and-field matching the database tree does for its own tables.
@@ -403,8 +417,9 @@ export const QueryDatabase = ({
}
// Insert the column at the cursor, preserving the rest of the query the user has typed
- if (item && item.record?.type === 'column') {
- insertTextAtCursor(escapeDottedHogQLIdentifier(item.record.columnName))
+ const columnInsertText = getColumnInsertText(item?.record)
+ if (columnInsertText) {
+ insertTextAtCursor(columnInsertText)
}
if (item && item.record?.type === 'unsaved-query') {
diff --git a/frontend/src/scenes/notebooks/Nodes/components/notebookDataframeTree.test.tsx b/frontend/src/scenes/notebooks/Nodes/components/notebookDataframeTree.test.tsx
index 60d4d28d9588..e09f542fd0ff 100644
--- a/frontend/src/scenes/notebooks/Nodes/components/notebookDataframeTree.test.tsx
+++ b/frontend/src/scenes/notebooks/Nodes/components/notebookDataframeTree.test.tsx
@@ -110,6 +110,13 @@ describe('buildDataframeTreeSection', () => {
])
})
+ it('gives each column the name QueryDatabase inserts at the cursor', () => {
+ // Without it, the click handler throws before the tree item's link can preventDefault and
+ // the browser follows the placeholder href, dropping the user on the project home page.
+ const column = buildDataframeTreeSection([sqlNode('events_df')], [])[0].children?.[0].children?.[0]
+ expect(column?.record).toMatchObject({ type: 'column', columnName: 'id' })
+ })
+
it('still lists a written cell that has not run', () => {
// Written but un-run is a real intent to bind that name, so it stays with a nudge to run.
const section = buildDataframeTreeSection([sqlNode('draft', false, 'SELECT 1')], [])
diff --git a/frontend/src/scenes/notebooks/Nodes/components/notebookDataframeTree.tsx b/frontend/src/scenes/notebooks/Nodes/components/notebookDataframeTree.tsx
index 91087cf21fe2..dde3507edd43 100644
--- a/frontend/src/scenes/notebooks/Nodes/components/notebookDataframeTree.tsx
+++ b/frontend/src/scenes/notebooks/Nodes/components/notebookDataframeTree.tsx
@@ -119,12 +119,10 @@ const entryNode = (entry: DataframeEntry, index: number): TreeDataItem => ({
id: `${frameId(index)}:col:${columnIndex}`,
name: columnName,
type: 'node' as const,
- // The shape QueryDatabase's renderItem expects for a column: monospaced name plus a
- // muted type suffix, so a dataframe's columns read like a table's. Only the rendering
- // is shared — its click handler needs `table`/`columnName`, which a kernel frame has no
- // equivalent of, so selection is disabled rather than left as a click that does nothing.
- record: { type: 'column', field: { name: columnName, type: columnType } },
- disableSelect: true,
+ // The shape QueryDatabase expects for a column: monospaced name plus a muted type
+ // suffix, and `columnName` for the click that inserts it at the cursor. A dataframe is
+ // referenceable by name from SQL, so its columns insert exactly like a table's.
+ record: { type: 'column', columnName, field: { name: columnName, type: columnType } },
})),
})