: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(data-modeling): zoom the lineage canvas to the closest search match (#106844)
Jovan Sakovic252a5a5239bc
diff --git a/products/data_modeling/frontend/lineage/ModelsLineageTab.tsx b/products/data_modeling/frontend/lineage/ModelsLineageTab.tsxindex f0e1f52b7bfa..da72007ca929 100644--- a/products/data_modeling/frontend/lineage/ModelsLineageTab.tsx+++ b/products/data_modeling/frontend/lineage/ModelsLineageTab.tsx@@ -1,6 +1,5 @@import { useActions, useValues } from 'kea'import { router } from 'kea-router'-import { useMemo } from 'react'import { IconInfo } from '@posthog/icons'import { LemonButton, LemonInput, Tooltip } from '@posthog/lemon-ui'@@ -30,19 +29,13 @@ export function ModelsLineageTab(): JSX.Element {searchTerm,typeFilter,legendCollapsed,- parsedSearch,highlightedNodeIds,+ focusNodeIds,visibleNodes,visibleEdges,isFiltered,} = useValues(modelsLineageLogic)const { setSearchTerm, setTypeFilter, toggleLegendCollapsed, resetFilters } = useActions(modelsLineageLogic)- // A fresh Set on every render would restart the graph's fitView animation each keystroke,- // so keep the identity stable while the underlying selectors are unchanged.- const focusNodeIds = useMemo(- () => (parsedSearch.mode === 'search' ? highlightedNodeIds : new Set(visibleNodes.map((node) => node.id))),- [parsedSearch.mode, highlightedNodeIds, visibleNodes]- )return (<div className="flex flex-col gap-2">diff --git a/products/data_modeling/frontend/lineage/modelsLineageLogic.test.ts b/products/data_modeling/frontend/lineage/modelsLineageLogic.test.tsnew file mode 100644index 000000000000..2e75d1703b24--- /dev/null+++ b/products/data_modeling/frontend/lineage/modelsLineageLogic.test.ts@@ -0,0 +1,71 @@+import { expectLogic } from 'kea-test-utils'++import { useMocks } from '~/mocks/jest'+import { initKeaTests } from '~/test/init'+import { DataModelingEdge, DataModelingNode } from '~/types'++import { lineageDataLogic } from './lineageDataLogic'+import { modelsLineageLogic } from './modelsLineageLogic'++const NODES: DataModelingNode[] = [+ { id: '1', name: 'website_leads', type: 'table' },+ { id: '2', name: 'website_leads_daily', type: 'view' },+ { id: '3', name: 'app_onboarding_leads', type: 'view' },+ { id: '4', name: 'customers', type: 'view' },+] as DataModelingNode[]++// customers feeds website_leads, so an upstream selector keeps a cone of two nodes rather than one.+const EDGES: DataModelingEdge[] = [{ id: 'e1', source_id: '4', target_id: '1' }] as DataModelingEdge[]++describe('modelsLineageLogic', () => {+ let logic: ReturnType<typeof modelsLineageLogic.build>++ beforeEach(async () => {+ useMocks({+ get: {+ '/api/environments/:team_id/data_modeling_nodes/': () => [200, { results: NODES, next: null }],+ '/api/environments/:team_id/data_modeling_edges/': () => [200, { results: EDGES, next: null }],+ },+ })+ initKeaTests()+ logic = modelsLineageLogic()+ logic.mount()+ await expectLogic(lineageDataLogic).toDispatchActions(['loadNodesSuccess', 'loadEdgesSuccess'])+ })++ afterEach(() => {+ logic.unmount()+ })++ // A big DAG has many substring hits for a term; flying to all of them zooms back out to the+ // unreadable overview. Every hit keeps its ring, but the viewport lands on the closest name only.+ it.each([+ ['leads', new Set(['1', '2', '3']), new Set(['1'])],+ ['website_leads', new Set(['1', '2']), new Set(['1'])],+ ])('rings every %p match but focuses the single closest name', async (term, highlighted, focused) => {+ await expectLogic(logic, () => {+ logic.actions.setDebouncedSearchTerm(term)+ }).toMatchValues({+ highlightedNodeIds: highlighted,+ focusNodeIds: focused,+ })+ })++ it('skips a closest match the type filter hid, so the viewport still moves', async () => {+ await expectLogic(logic, () => {+ logic.actions.setTypeFilter(['view'])+ logic.actions.setDebouncedSearchTerm('website_leads')+ }).toMatchValues({+ focusNodeIds: new Set(['2']),+ })+ })++ it('fits the whole surviving cone for a lineage selector instead of one node', async () => {+ await expectLogic(logic, () => {+ logic.actions.setDebouncedSearchTerm('+website_leads')+ }).toMatchValues({+ highlightedNodeIds: new Set(),+ focusNodeIds: new Set(['1', '4']),+ })+ })+})diff --git a/products/data_modeling/frontend/lineage/modelsLineageLogic.ts b/products/data_modeling/frontend/lineage/modelsLineageLogic.tsindex 30e564666c8e..245edc5201ea 100644--- a/products/data_modeling/frontend/lineage/modelsLineageLogic.ts+++ b/products/data_modeling/frontend/lineage/modelsLineageLogic.ts@@ -25,6 +25,7 @@ export interface modelsLineageLogicValues {legendCollapsed: booleanparsedSearch: ParsedLineageSearchhighlightedNodeIds: Set<string>+ focusNodeIds: Set<string>visibleNodes: DataModelingNode[]visibleEdges: DataModelingEdge[]isFiltered: boolean@@ -126,6 +127,23 @@ export const modelsLineageLogic = kea<modelsLineageLogicType>([},],+ // The viewport flies here. A plain term matches by substring, so on a big DAG it hits many+ // scattered nodes, and fitting all of them zooms back out to the unreadable overview. Fly to+ // the single closest match instead, because the rest keep their ring and show in the minimap.+ // Match within the visible nodes, since the canvas only lays out those: a closest name that+ // the type filter hides would leave the graph nothing to fit and the viewport would not move.+ // Lineage selectors have already pruned the graph, so there we fit the whole surviving cone.+ focusNodeIds: [+ (s) => [s.parsedSearch, s.visibleNodes],+ (parsedSearch: ParsedLineageSearch, visibleNodes: DataModelingNode[]): Set<string> => {+ if (parsedSearch.mode === 'search') {+ const best = matchNodesByName(visibleNodes, parsedSearch.term)[0]+ return best ? new Set([best.id]) : new Set()+ }+ return new Set(visibleNodes.map((node) => node.id))+ },+ ],+visibleEdges: [(s) => [s.edges, s.visibleNodes],(edges: DataModelingEdge[], visibleNodes: DataModelingNode[]): DataModelingEdge[] =>