: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(notebooks): Let users pick/choose an experiment from a list in markdown notebooks, similar to V1 notebooks (#65489)
Sinan Kucukkoseler05dbbbf21b24
diff --git a/frontend/src/lib/components/MarkdownNotebook/InsertMenu.tsx b/frontend/src/lib/components/MarkdownNotebook/InsertMenu.tsxindex 5393fc9a0d3a..5d3e66e12429 100644--- a/frontend/src/lib/components/MarkdownNotebook/InsertMenu.tsx+++ b/frontend/src/lib/components/MarkdownNotebook/InsertMenu.tsx@@ -341,16 +341,6 @@ export function buildInsertCommands(},]- const experimentCommands: InsertCommand[] = [- {- key: 'experiment',- label: 'Experiment',- category: 'Experiment',- icon: <IconGraph />,- run: (targetNodeId) => insertRegisteredComponent(targetNodeId, 'Experiment'),- },- ]-const mediaCommands: InsertCommand[] = [{key: 'media-image',@@ -496,7 +486,6 @@ export function buildInsertCommands(...sqlCommands,...queryCommands,...dataCommands,- ...experimentCommands,...mediaCommands,...componentCommands,...textStyleCommands,diff --git a/frontend/src/scenes/notebooks/AddExperimentsToNotebookModal/ExperimentsNotebookTable.tsx b/frontend/src/scenes/notebooks/AddExperimentsToNotebookModal/ExperimentsNotebookTable.tsxindex 06e52bedf8ec..d67e32c91d8f 100644--- a/frontend/src/scenes/notebooks/AddExperimentsToNotebookModal/ExperimentsNotebookTable.tsx+++ b/frontend/src/scenes/notebooks/AddExperimentsToNotebookModal/ExperimentsNotebookTable.tsx@@ -16,10 +16,11 @@ import { Experiment } from '~/types'import { addExperimentsToNotebookModalLogic } from './addExperimentsToNotebookModalLogic'type ExperimentsNotebookTableProps = {- insertionPosition: number | null+ insertionPosition?: number | null+ onSelect?: (experimentId: number) => void}-export function ExperimentsNotebookTable({ insertionPosition }: ExperimentsNotebookTableProps): JSX.Element {+export function ExperimentsNotebookTable({ insertionPosition, onSelect }: ExperimentsNotebookTableProps): JSX.Element {const { experiments, experimentsLoading, filters, sorting, experimentsPerPage, count, modalPage } = useValues(addExperimentsToNotebookModalLogic)@@ -29,10 +30,17 @@ export function ExperimentsNotebookTable({ insertionPosition }: ExperimentsNotebconst { addExperimentToNotebook } = useActions(notebookLogic)const isSelected = (experiment: Experiment): boolean => {+ if (onSelect) {+ return false+ }return experimentIdsInNotebook?.includes(experiment.id as number) ?? false}const onToggle = (experiment: Experiment): void => {+ if (onSelect) {+ onSelect(experiment.id as number)+ return+ }// If already in notebook, remove itif (isSelected(experiment)) {const nodeLogic = findNodeLogic(NotebookNodeType.Experiment, { id: experiment.id })diff --git a/frontend/src/scenes/notebooks/Notebook/MarkdownNotebookExperimentPicker.tsx b/frontend/src/scenes/notebooks/Notebook/MarkdownNotebookExperimentPicker.tsxnew file mode 100644index 000000000000..608f74d4f051--- /dev/null+++ b/frontend/src/scenes/notebooks/Notebook/MarkdownNotebookExperimentPicker.tsx@@ -0,0 +1,51 @@+import { useActions } from 'kea'+import { useEffect } from 'react'++import { LemonButton } from '@posthog/lemon-ui'++import { LemonModal } from 'lib/lemon-ui/LemonModal'++import { addExperimentsToNotebookModalLogic } from '../AddExperimentsToNotebookModal/addExperimentsToNotebookModalLogic'+import { ExperimentsNotebookTable } from '../AddExperimentsToNotebookModal/ExperimentsNotebookTable'++export type MarkdownNotebookExperimentPickerProps = {+ isOpen: boolean+ onClose: () => void+ onSelect: (experimentId: number) => void+}++export function MarkdownNotebookExperimentPicker({+ isOpen,+ onClose,+ onSelect,+}: MarkdownNotebookExperimentPickerProps): JSX.Element {+ const { loadExperiments, closeModal } = useActions(addExperimentsToNotebookModalLogic)++ // The table reads from the shared experiments logic; the legacy modal seeds it on open, so this+ // picker (which doesn't toggle that modal's open state) must drive that lifecycle itself: load on+ // open, and reset filters on close so a stale search query doesn't carry over to the next open.+ // `closeModal` resets the filters without triggering a fetch (no listener), and the shared modal's+ // open flag is already unused here.+ useEffect(() => {+ if (isOpen) {+ loadExperiments()+ } else {+ closeModal()+ }+ }, [isOpen, loadExperiments, closeModal])++ return (+ <LemonModal+ title="Add experiment to notebook"+ onClose={onClose}+ isOpen={isOpen}+ footer={+ <LemonButton type="secondary" data-attr="markdown-notebook-experiment-cancel" onClick={onClose}>+ Close+ </LemonButton>+ }+ >+ <ExperimentsNotebookTable onSelect={onSelect} />+ </LemonModal>+ )+}diff --git a/frontend/src/scenes/notebooks/Notebook/MarkdownNotebookV2Renderer.tsx b/frontend/src/scenes/notebooks/Notebook/MarkdownNotebookV2Renderer.tsxindex a86b49a91a9e..694c2ad1f8ec 100644--- a/frontend/src/scenes/notebooks/Notebook/MarkdownNotebookV2Renderer.tsx+++ b/frontend/src/scenes/notebooks/Notebook/MarkdownNotebookV2Renderer.tsx@@ -1,7 +1,7 @@import { useActions, useValues } from 'kea'import { useCallback, useEffect, useMemo, useRef, useState } from 'react'-import { IconGraph } from '@posthog/icons'+import { IconFlask, IconGraph } from '@posthog/icons'import { MarkdownNotebook, parseMarkdownNotebook } from 'lib/components/MarkdownNotebook'import type {@@ -22,6 +22,7 @@ import { uuid } from 'lib/utils/dom'import type { NotebookArtifactContent } from '~/queries/schema/schema-assistant-messages'+import { MarkdownNotebookExperimentPicker } from './MarkdownNotebookExperimentPicker'import { InlineAIAssistantMessage, InlineAICompletion, InlineNotebookAIRunner } from './MarkdownNotebookInlineAI'import { NOTEBOOK_MARKDOWN_REGISTRY } from './markdownNotebookRegistry'import {@@ -394,11 +395,13 @@ export function MarkdownNotebookV2({ debugOpen, onDebugOpenChange }: MarkdownNot)const [savedInsightPickerTargetNodeId, setSavedInsightPickerTargetNodeId] = useState<string | null>(null)- // Insert API + target node captured when "Saved insight" is picked, so the modal's async selection- // can insert into the right node once an insight is chosen.+ const [experimentPickerTargetNodeId, setExperimentPickerTargetNodeId] = useState<string | null>(null)+ // Insert API + target node captured when "Saved insight" / "Experiment" is picked, so the modal's+ // async selection can insert into the right node once an entity is chosen.const savedInsightInsertRef = useRef<{ api: MarkdownNotebookInsertMenuApi; targetNodeId: string } | null>(null)+ const experimentInsertRef = useRef<{ api: MarkdownNotebookInsertMenuApi; targetNodeId: string } | null>(null)- const buildSavedInsightInsertCommands = useCallback(+ const buildExtraInsertCommands = useCallback((api: MarkdownNotebookInsertMenuApi): InsertCommand[] => [{key: 'query-saved-insight',@@ -410,6 +413,16 @@ export function MarkdownNotebookV2({ debugOpen, onDebugOpenChange }: MarkdownNotsetSavedInsightPickerTargetNodeId(targetNodeId)},},+ {+ key: 'experiment',+ label: 'Experiment',+ category: 'Experiment',+ icon: <IconFlask />,+ run: (targetNodeId) => {+ experimentInsertRef.current = { api, targetNodeId }+ setExperimentPickerTargetNodeId(targetNodeId)+ },+ },],[])@@ -419,6 +432,11 @@ export function MarkdownNotebookV2({ debugOpen, onDebugOpenChange }: MarkdownNotsetSavedInsightPickerTargetNodeId(null)}, [])+ const closeExperimentPicker = useCallback((): void => {+ experimentInsertRef.current = null+ setExperimentPickerTargetNodeId(null)+ }, [])+const handleSavedInsightPicked = useCallback((shortId: string, title: string): void => {const pending = savedInsightInsertRef.currentif (pending) {@@ -435,6 +453,15 @@ export function MarkdownNotebookV2({ debugOpen, onDebugOpenChange }: MarkdownNotsetSavedInsightPickerTargetNodeId(null)}, [])+ const handleExperimentPicked = useCallback((experimentId: number): void => {+ const pending = experimentInsertRef.current+ if (pending) {+ pending.api.insertComponent(pending.targetNodeId, 'Experiment', { id: experimentId })+ }+ experimentInsertRef.current = null+ setExperimentPickerTargetNodeId(null)+ }, [])+const runtimeContext = useMemo<MarkdownNotebookRuntimeContextValue>(() => ({notebookShortId: notebook?.short_id ?? null,@@ -550,7 +577,7 @@ export function MarkdownNotebookV2({ debugOpen, onDebugOpenChange }: MarkdownNotremoteVersion={notebook?.version}mode={isEditable ? 'edit' : 'view'}registry={NOTEBOOK_MARKDOWN_REGISTRY}- extraInsertCommands={isEditable ? buildSavedInsightInsertCommands : undefined}+ extraInsertCommands={isEditable ? buildExtraInsertCommands : undefined}onChange={isEditable ? handleMarkdownNotebookChange : undefined}onConflict={reportMarkdownMergeConflicts}remoteCarets={remoteCarets}@@ -585,6 +612,13 @@ export function MarkdownNotebookV2({ debugOpen, onDebugOpenChange }: MarkdownNotonSelect={handleSavedInsightPicked}/>)}+ {isEditable && (+ <MarkdownNotebookExperimentPicker+ isOpen={experimentPickerTargetNodeId !== null}+ onClose={closeExperimentPicker}+ onSelect={handleExperimentPicked}+ />+ )}</MarkdownNotebookRuntimeContext.Provider>)}