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.

2.5 KB
Raw Blame
import { useActions, useValues } from 'kea'

import { IconPlay, IconStopFilled } from '@posthog/icons'
import { LemonButton, LemonButtonProps } from '@posthog/lemon-ui'

import { featureFlagLogic } from 'lib/logic/featureFlagLogic'

import { isKernelUiEnabled } from '../utils'
import { isMarkdownNotebookContent } from './markdownNotebookV2'
import { notebookLogic } from './notebookLogic'
import { notebookRunLogic } from './notebookRunLogic'

/**
 * Runs every SQL and Python cell of the open notebook, in document order. While a run is
 * active the same button stops it, so the toolbar keeps one slot for the whole action.
 */
export const NotebookRunAllButton = (
    props: Pick<LemonButtonProps, 'children' | 'size' | 'type'>
): JSX.Element | null => {
    const { featureFlags } = useValues(featureFlagLogic)
    const { content, shortId, isShared, runnableCellNodeIds } = useValues(notebookLogic)
    const { isRunning, isStarting, isInterrupting, progressLabel } = useValues(notebookRunLogic({ shortId }))
    const { startRun, interruptRun } = useActions(notebookRunLogic({ shortId }))

    // Only a markdown notebook with something to run gets the button at all. Runnable means
    // SQL or Python, the same pair the backend plan walks — a notebook of Python cells alone
    // runs perfectly well, and reading the SQL summaries hid the button from it.
    if (!isKernelUiEnabled(featureFlags) || !isMarkdownNotebookContent(content) || !runnableCellNodeIds.length) {
        return null
    }

    if (isRunning) {
        return (
            <LemonButton
                {...props}
                onClick={() => interruptRun()}
                icon={<IconStopFilled />}
                loading={isInterrupting}
                disabledReason={isInterrupting ? 'Stopping the run' : undefined}
                tooltip={progressLabel ?? 'Stop the run'}
                data-attr="notebook-run-all-stop"
            >
                Stop
            </LemonButton>
        )
    }

    return (
        <LemonButton
            {...props}
            onClick={() => startRun()}
            icon={<IconPlay />}
            loading={isStarting}
            disabledReason={
                isShared ? 'You can only run cells in the notebook itself' : isStarting ? 'Starting the run' : undefined
            }
            tooltip="Run every SQL and Python cell in order, stopping at the first one that fails"
            data-attr="notebook-run-all"
        >
            Run all
        </LemonButton>
    )
}