: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
import { MarkerType, Position, type Edge as ReactFlowEdge, type Node as ReactFlowNode } from '@xyflow/react'
import { MakeLogicType, actions, afterMount, kea, key, listeners, path, props, propsChanged, reducers } from 'kea'
import { ElkDirection, NodeHandle, getFormattedNodes } from './autolayout'
import { LineageEdge, LineageGraphNode } from './lineageHiddenNodes'
import { LineageNodeData, LineageVariant } from './LineageNode'
const NODE_SIZES: Record<LineageVariant, { width: number; height: number }> = {
full: { width: 200, height: 90 },
canvas: { width: 180, height: 120 },
}
const COLLAPSED_NODE_SIZE = { width: 40, height: 24 }
const ARROW_MARKER = { type: MarkerType.ArrowClosed, width: 20, height: 20 }
const NODE_GAP = 60
const THROUGH_HIDDEN_NODES_EDGE: Partial<ReactFlowEdge> = {
label: '⋯',
ariaLabel: 'Connected through hidden models',
style: { strokeDasharray: '4 4' },
labelStyle: { fill: 'var(--color-text-secondary)' },
labelBgStyle: { fill: 'var(--color-bg-surface-primary)', fillOpacity: 0.8 },
labelBgPadding: [2, 0],
labelBgBorderRadius: 4,
}
export interface LineageGraphLayout {
nodes: ReactFlowNode<LineageNodeData>[]
edges: ReactFlowEdge[]
}
export interface LineageGraphLogicProps {
nodes: LineageGraphNode[]
edges: LineageEdge[]
variant: LineageVariant
direction: ElkDirection
}
function handlesForDirection(nodeId: string, direction: ElkDirection): NodeHandle[] {
const isDown = direction === 'DOWN'
return [
{ id: `target_${nodeId}`, type: 'target', position: isDown ? Position.Top : Position.Left },
{ id: `source_${nodeId}`, type: 'source', position: isDown ? Position.Bottom : Position.Right },
]
}
export function initialLineageGraphLayout(
nodes: LineageGraphNode[],
edges: LineageEdge[],
variant: LineageVariant,
direction: ElkDirection
): LineageGraphLayout {
const { width, height } = NODE_SIZES[variant]
const rfNodes: ReactFlowNode<LineageNodeData>[] = nodes.map((node, index) => ({
id: node.id,
type: 'lineage',
position:
direction === 'DOWN' ? { x: 0, y: index * (height + NODE_GAP) } : { x: index * (width + NODE_GAP), y: 0 },
...(node.collapsed ? COLLAPSED_NODE_SIZE : { width, height }),
data: {
node,
variant,
direction,
state: {},
callbacks: {},
handles: handlesForDirection(node.id, direction),
},
}))
const collapsedNodeIds = new Set(nodes.filter((node) => node.collapsed).map((node) => node.id))
const rfEdges: ReactFlowEdge[] = edges.map((edge) => ({
id: edge.id,
source: edge.source_id,
target: edge.target_id,
sourceHandle: `source_${edge.source_id}`,
targetHandle: `target_${edge.target_id}`,
markerEnd: collapsedNodeIds.has(edge.target_id) ? undefined : ARROW_MARKER,
...(edge.throughHiddenNodes && THROUGH_HIDDEN_NODES_EDGE),
}))
return { nodes: rfNodes, edges: rfEdges }
}
// Layout depends only on the graph's structure — per-node state/callbacks are decorated onto the
// laid-out nodes at render time, so caller-supplied inline callbacks never retrigger the ELK pass.
async function layoutGraph(
nodes: LineageGraphNode[],
edges: LineageEdge[],
variant: LineageVariant,
direction: ElkDirection
): Promise<LineageGraphLayout> {
const initialLayout = initialLineageGraphLayout(nodes, edges, variant, direction)
const laidOut = await getFormattedNodes(initialLayout.nodes as any, initialLayout.edges, direction)
return { nodes: laidOut as unknown as ReactFlowNode<LineageNodeData>[], edges: initialLayout.edges }
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface lineageGraphLogicValues {
layout: LineageGraphLayout | null
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface lineageGraphLogicActions {
computeLayout: () => {
value: true
}
layoutComputed: (layout: LineageGraphLayout) => {
layout: LineageGraphLayout
}
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface lineageGraphLogicMeta {
key: string
}
export type lineageGraphLogicType = MakeLogicType<
lineageGraphLogicValues,
lineageGraphLogicActions,
LineageGraphLogicProps,
lineageGraphLogicMeta
>
export const lineageGraphLogic = kea<lineageGraphLogicType>([
path(['products', 'data_modeling', 'frontend', 'lineage', 'lineageGraphLogic']),
props({} as LineageGraphLogicProps),
key((props) => `${props.variant}-${props.direction}-${props.nodes.map((n) => n.id).join(',')}`),
actions({
computeLayout: true,
layoutComputed: (layout: LineageGraphLayout) => ({ layout }),
}),
reducers({
layout: [
null as LineageGraphLayout | null,
{
layoutComputed: (_, { layout }) => layout,
},
],
}),
listeners((logic) => ({
computeLayout: async (_, breakpoint) => {
// Read logic.props, not a destructured copy: the copy is frozen at build time, so a
// relayout after propsChanged would still lay out the original nodes and edges
const { nodes, edges, variant, direction } = logic.props
const layout = await layoutGraph(nodes, edges, variant, direction)
breakpoint()
logic.actions.layoutComputed(layout)
},
})),
propsChanged(({ actions, props }, oldProps) => {
if (
props.nodes !== oldProps.nodes ||
props.edges !== oldProps.edges ||
props.variant !== oldProps.variant ||
props.direction !== oldProps.direction
) {
actions.computeLayout()
}
}),
afterMount(({ actions }) => {
actions.computeLayout()
}),
])