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 pasted chart nodes from sharing state (#76070)

Sean Mallia8ca6ee929ce2
diff --git a/frontend/src/lib/components/MarkdownNotebook/documentModel.test.ts b/frontend/src/lib/components/MarkdownNotebook/documentModel.test.ts
new file mode 100644
index 000000000000..5538a6f7f646
--- /dev/null
+++ b/frontend/src/lib/components/MarkdownNotebook/documentModel.test.ts
@@ -0,0 +1,42 @@
+import { rekeyNotebookNodes } from './documentModel'
+import { NotebookBlockNode } from './types'
+
+describe('documentModel', () => {
+ describe('rekeyNotebookNodes', () => {
+ it('gives a pasted component node a fresh nodeId so it no longer shares logic with the original', () => {
+ // A trend chart carrying a persisted nodeId, as it exists once saved and copied.
+ const copied: NotebookBlockNode = {
+ type: 'component',
+ tagName: 'Query',
+ id: 'original-block',
+ props: {
+ nodeId: 'shared-node-id',
+ query: { kind: 'InsightVizNode', source: { kind: 'TrendsQuery', series: [] } },
+ },
+ } as NotebookBlockNode
+
+ const [pasted] = rekeyNotebookNodes([copied], 'paste-seed')
+
+ expect(pasted.id).not.toBe('original-block')
+ // The nodeId prop keys the node's logic; a shared value is what let edits leak between copies.
+ expect((pasted as any).props.nodeId).not.toBe('shared-node-id')
+ expect((pasted as any).props.nodeId).toBe(pasted.id)
+ // The rest of the node is preserved.
+ expect((pasted as any).props.query).toEqual((copied as any).props.query)
+ })
+
+ it('leaves a component node without a persisted nodeId untouched beyond the block id', () => {
+ const copied: NotebookBlockNode = {
+ type: 'component',
+ tagName: 'Query',
+ id: 'original-block',
+ props: { query: { kind: 'InsightVizNode', source: { kind: 'TrendsQuery', series: [] } } },
+ } as NotebookBlockNode
+
+ const [pasted] = rekeyNotebookNodes([copied], 'paste-seed')
+
+ expect(pasted.id).not.toBe('original-block')
+ expect((pasted as any).props.nodeId).toBeUndefined()
+ })
+ })
+})
diff --git a/frontend/src/lib/components/MarkdownNotebook/documentModel.ts b/frontend/src/lib/components/MarkdownNotebook/documentModel.ts
index 4b8c93f6c749..3457aee39dca 100644
--- a/frontend/src/lib/components/MarkdownNotebook/documentModel.ts
+++ b/frontend/src/lib/components/MarkdownNotebook/documentModel.ts
@@ -973,6 +973,18 @@ export function rekeyNotebookNodes(nodes: NotebookBlockNode[], seed: string): No
}
}
+ // A component node's `nodeId` prop is its per-instance identity: it keys the node's
+ // logic and cached results. If it survives a paste unchanged, the pasted copy shares
+ // that logic with the original, so editing one edits the other. Refresh it to match the
+ // fresh block id (the same fallback a node with no persisted nodeId already uses).
+ if (clonedNode.type === 'component' && typeof clonedNode.props.nodeId === 'string') {
+ return {
+ ...clonedNode,
+ id,
+ props: { ...clonedNode.props, nodeId: id },
+ }
+ }
+
return {
...clonedNode,
id,