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): highlight edited insights in the files tree (#111035)

Marius Andrabe6f6fe48da3
diff --git a/frontend/src/layout/panel-layout/ProjectTree/ProjectTree.tsx b/frontend/src/layout/panel-layout/ProjectTree/ProjectTree.tsx
index 8c9617b0f7ee..e2323f4b37c7 100644
--- a/frontend/src/layout/panel-layout/ProjectTree/ProjectTree.tsx
+++ b/frontend/src/layout/panel-layout/ProjectTree/ProjectTree.tsx
@@ -46,7 +46,7 @@ import { projectTreeLogic } from './projectTreeLogic'
import { TreeFiltersDropdownMenu } from './TreeFiltersDropdownMenu'
import { TreeSearchField } from './TreeSearchField'
import { TreeSortMenuItems } from './TreeSortMenuItems'
-import { calculateMovePath, resolveProjectTreeDrop } from './utils'
+import { calculateMovePath, isProjectTreeItemActive, resolveProjectTreeDrop } from './utils'
interface ProjectTreeBaseProps {
layout?: 'panel' | 'inline'
@@ -96,35 +96,6 @@ let counter = 0
const SHORTCUT_DISMISSAL_LOCAL_STORAGE_KEY = 'shortcut-dismissal'
-// Show active state for items that are active in the URL
-const isItemActive = (item: TreeDataItem): boolean => {
- if (!item.record?.href) {
- return false
- }
-
- const currentPath = removeProjectIdIfPresent(window.location.pathname)
- const itemHref = typeof item.record.href === 'string' ? item.record.href : ''
-
- if (currentPath === itemHref) {
- return true
- }
-
- // Current path is a sub-path of item (e.g., /insights/new under /insights)
- if (currentPath.startsWith(itemHref + '/')) {
- return true
- }
-
- // Special handling for products with child pages on distinct paths (e.g., /replay/home and /replay/playlists)
- if (item.name === 'Session replay' && currentPath.startsWith('/replay/')) {
- return true
- }
- if (item.name === 'Workflows' && currentPath.startsWith('/workflows')) {
- return true
- }
-
- return false
-}
-
export function ProjectTree(props: ProjectTreeProps): JSX.Element {
const {
logicKey,
@@ -149,6 +120,7 @@ export function ProjectTree(props: ProjectTreeProps): JSX.Element {
const {
fullFileSystemFiltered,
lastViewedId,
+ projectTreeRef,
expandedFolders,
expandedSearchFolders,
searchTerm,
@@ -261,7 +233,9 @@ export function ProjectTree(props: ProjectTreeProps): JSX.Element {
data={treeData}
selectMode={selectMode}
defaultSelectedFolderOrNodeId={lastViewedId || undefined}
- isItemActive={isItemActive}
+ isItemActive={(item) =>
+ isProjectTreeItemActive(item, removeProjectIdIfPresent(window.location.pathname), projectTreeRef)
+ }
size={treeSize}
onItemChecked={onItemChecked}
checkedItemCount={checkedItemCountNumeric}
diff --git a/frontend/src/layout/panel-layout/ProjectTree/utils.test.ts b/frontend/src/layout/panel-layout/ProjectTree/utils.test.ts
index 1012ea6c85fc..5ed2154c8a6e 100644
--- a/frontend/src/layout/panel-layout/ProjectTree/utils.test.ts
+++ b/frontend/src/layout/panel-layout/ProjectTree/utils.test.ts
@@ -1,6 +1,9 @@
import { renderToStaticMarkup } from 'react-dom/server'
+import { TreeDataItem } from 'lib/lemon-ui/LemonTree/LemonTree'
+
import { FileSystemEntry } from '~/queries/schema/schema-general'
+import { ProjectTreeRef } from '~/types'
import { productsItemName } from '../navbar/tabs/productsCatalog'
import { getCustomIcon } from './customIconRegistry'
@@ -8,6 +11,7 @@ import { getDefaultTreeData, getDefaultTreeProducts, iconForType } from './defau
import {
convertFileSystemEntryToTreeDataItem,
escapePath,
+ isProjectTreeItemActive,
joinPath,
matchesRefType,
reparentPath,
@@ -20,6 +24,25 @@ const catalogProducts = [...getDefaultTreeProducts(), ...getDefaultTreeData()].f
)
describe('project tree utils', () => {
+ describe('isProjectTreeItemActive', () => {
+ const insight: TreeDataItem = {
+ id: 'project/sql-insight',
+ name: 'SQL insight',
+ record: { type: 'insight', ref: 'sql123', href: '/insights/sql123' },
+ }
+
+ it.each<[string, string, ProjectTreeRef | null, boolean]>([
+ ['view mode', '/insights/sql123', null, true],
+ ['edit mode', '/sql', { type: 'insight', ref: 'sql123' }, true],
+ ['another insight', '/sql', { type: 'insight', ref: 'other' }, false],
+ ['another resource with the same ID', '/sql', { type: 'dashboard', ref: 'sql123' }, false],
+ ['unsaved SQL', '/sql', null, false],
+ ['new insight', '/sql', { type: 'insight', ref: null }, false],
+ ])('highlights the current insight in %s', (_, pathname, ref, expected) => {
+ expect(isProjectTreeItemActive(insight, pathname, ref)).toBe(expected)
+ })
+ })
+
describe('resolveProjectTreeDrop', () => {
const note: FileSystemEntry = { id: 'note', type: 'notebook', ref: 'note-ref', path: 'Research/Notes' }
const folder: FileSystemEntry = { id: 'folder', type: 'folder', path: 'Research' }
diff --git a/frontend/src/layout/panel-layout/ProjectTree/utils.tsx b/frontend/src/layout/panel-layout/ProjectTree/utils.tsx
index fd7edb209ffe..4d53e7bc1cd3 100644
--- a/frontend/src/layout/panel-layout/ProjectTree/utils.tsx
+++ b/frontend/src/layout/panel-layout/ProjectTree/utils.tsx
@@ -6,7 +6,7 @@ import { TreeDataItem } from 'lib/lemon-ui/LemonTree/LemonTree'
import { SearchHighlightMultiple } from '~/layout/navigation-3000/components/SearchHighlight'
import { RecentResults, SearchResults } from '~/layout/panel-layout/ProjectTree/projectTreeLogic'
import { FileSystemEntry, FileSystemIconType, FileSystemImport } from '~/queries/schema/schema-general'
-import { UserBasicType } from '~/types'
+import { ProjectTreeRef, UserBasicType } from '~/types'
import { getCustomIcon } from './customIconRegistry'
import { ProductIconWrapper, getSidebarProduct, iconForType } from './defaultTree'
@@ -525,6 +525,44 @@ export function matchesRefType(rowType: string | undefined, type: string): boole
return type.endsWith('/') ? !!rowType?.startsWith(type) : rowType === type
}
+export function isProjectTreeItemActive(
+ item: TreeDataItem,
+ currentPath: string,
+ projectTreeRef: ProjectTreeRef | null
+): boolean {
+ if (
+ projectTreeRef?.ref &&
+ item.record?.ref === projectTreeRef.ref &&
+ matchesRefType(item.record?.type, projectTreeRef.type)
+ ) {
+ return true
+ }
+
+ if (!item.record?.href) {
+ return false
+ }
+
+ const itemHref = typeof item.record.href === 'string' ? item.record.href : ''
+ if (currentPath === itemHref) {
+ return true
+ }
+
+ // Current path is a sub-path of item (e.g., /insights/new under /insights)
+ if (currentPath.startsWith(itemHref + '/')) {
+ return true
+ }
+
+ // Special handling for products with child pages on distinct paths (e.g., /replay/home and /replay/playlists)
+ if (item.name === 'Session replay' && currentPath.startsWith('/replay/')) {
+ return true
+ }
+ if (item.name === 'Workflows' && currentPath.startsWith('/workflows')) {
+ return true
+ }
+
+ return false
+}
+
export function refTypeParams(type: string): { type?: string; type__startswith?: string } {
return type.endsWith('/') ? { type__startswith: type } : { type }
}
diff --git a/frontend/src/scenes/data-warehouse/editor/editorSceneLogic.tsx b/frontend/src/scenes/data-warehouse/editor/editorSceneLogic.tsx
index 08896913b98c..9f2983f8d4e5 100644
--- a/frontend/src/scenes/data-warehouse/editor/editorSceneLogic.tsx
+++ b/frontend/src/scenes/data-warehouse/editor/editorSceneLogic.tsx
@@ -14,7 +14,7 @@ import { urls } from 'scenes/urls'
import { SIDE_PANEL_CONTEXT_KEY, SidePanelSceneContext } from '~/layout/navigation-3000/sidepanel/types'
import { notebooksModel } from '~/models/notebooksModel'
import { DataVisualizationNode, FileSystemIconType, HogQLFilters, NodeKind } from '~/queries/schema/schema-general'
-import { Breadcrumb } from '~/types'
+import { Breadcrumb, ProjectTreeRef } from '~/types'
import type { FeatureFlagsSet } from '../../../lib/logic/featureFlagLogic'
import type { DataWarehouseSavedQuery, LinkBreadcrumb, InsightModel } from '../../../types'
@@ -112,6 +112,7 @@ export interface editorSceneLogicValues {
viewLoading: boolean // sqlEditorLogic
breadcrumbs: Breadcrumb[]
isHistoryModalOpen: boolean
+ projectTreeRef: ProjectTreeRef | null
saveAsMenuItems: {
primary: SaveAsMenuItem
secondary: SaveAsMenuItem[]
@@ -190,6 +191,9 @@ export interface editorSceneLogicActions {
export interface editorSceneLogicMeta {
key: string
__keaTypeGenInternalSelectorTypes: {
+ projectTreeRef: (
+ editingInsight: InsightModel<import('~/queries/schema/schema-general').Node<Record<string, any>>> | null
+ ) => ProjectTreeRef | null
breadcrumbs: (activeTab: QueryTab | null) => Breadcrumb[]
titleSectionProps: (
editingInsight: InsightModel<import('~/queries/schema/schema-general').Node<Record<string, any>>> | null,
@@ -288,6 +292,11 @@ export const editorSceneLogic = kea<editorSceneLogicType>([
],
}),
selectors({
+ projectTreeRef: [
+ (s) => [s.editingInsight],
+ (editingInsight: InsightModel | null): ProjectTreeRef | null =>
+ editingInsight ? { type: 'insight', ref: editingInsight.short_id } : null,
+ ],
breadcrumbs: [
(s) => [s.activeTab],
(activeTab: null | import('./sqlEditorLogic').QueryTab): Breadcrumb[] => {
diff --git a/frontend/src/scenes/data-warehouse/editor/sqlEditorLogic.test.ts b/frontend/src/scenes/data-warehouse/editor/sqlEditorLogic.test.ts
index 12d1f67a75ad..e867432d8c85 100644
--- a/frontend/src/scenes/data-warehouse/editor/sqlEditorLogic.test.ts
+++ b/frontend/src/scenes/data-warehouse/editor/sqlEditorLogic.test.ts
@@ -1053,6 +1053,7 @@ describe('sqlEditorLogic', () => {
expect(editorRootLogic.values.titleSectionProps).toMatchObject({
name: 'New SQL query',
})
+ expect(editorRootLogic.values.projectTreeRef).toBeNull()
expect(window.location.hash).not.toContain('insight')
expect(window.location.search).not.toContain('open_insight')
})
@@ -1207,24 +1208,31 @@ describe('sqlEditorLogic', () => {
})
describe('open_insight URL parameter', () => {
- it('sets editingInsight when opening an insight via open_insight search param', async () => {
- logic = sqlEditorLogic({
- tabId: TAB_ID,
- monaco: createMockMonaco(),
- editor: createMockEditor(),
- })
- logic.mount()
+ it.each([MOCK_INSIGHT_SHORT_ID, MOCK_DATA_TABLE_INSIGHT_SHORT_ID])(
+ 'sets the editing insight and file tree reference when opening %s',
+ async (shortId) => {
+ logic = sqlEditorLogic({
+ tabId: TAB_ID,
+ monaco: createMockMonaco(),
+ editor: createMockEditor(),
+ })
+ logic.mount()
+ editorRootLogic = editorSceneLogic({ tabId: TAB_ID })
+ editorRootLogic.mount()
- router.actions.push(urls.sqlEditor(), { open_insight: MOCK_INSIGHT_SHORT_ID })
+ expect(editorRootLogic.values.projectTreeRef).toBeNull()
+ router.actions.push(urls.sqlEditor(), { open_insight: shortId })
- await expectLogic(logic)
- .toDispatchActions(['editInsight', 'createTab', 'updateTab'])
- .toMatchValues({
- editingInsight: partial({
- short_id: MOCK_INSIGHT_SHORT_ID,
- }),
- })
- })
+ await expectLogic(logic)
+ .toDispatchActions(['editInsight', 'createTab', 'updateTab'])
+ .toMatchValues({
+ editingInsight: partial({
+ short_id: shortId,
+ }),
+ })
+ expect(editorRootLogic.values.projectTreeRef).toEqual({ type: 'insight', ref: shortId })
+ }
+ )
it('sets insightLoading to false after insight finishes loading', async () => {
logic = sqlEditorLogic({