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.

1.6 KB
Raw Blame
import { createContext, useContext, useEffect } from 'react'

/** `disabledReason` mirrors the run button's own gate, so a shortcut can never start a run the
 * button would refuse. */
export type NotebookComponentRunHandler = {
    run: () => void
    disabledReason?: string | null
    isRunning?: boolean
    /** Waiting behind another cell. It counts as running for the prompt, as Jupyter shows In [*]. */
    isQueued?: boolean
    executionCount?: number | null
    hasError?: boolean
    interrupt?: () => void
}

// Lets the shell own the cell's key handling while knowing nothing about SQL or Python.
export const NotebookComponentRunHandlerContext = createContext<
    ((handler: NotebookComponentRunHandler | null) => void) | null
>(null)

export function usePublishNotebookComponentRunHandler(handler: NotebookComponentRunHandler | null): void {
    const publishRunHandler = useContext(NotebookComponentRunHandlerContext)
    const run = handler?.run
    const disabledReason = handler?.disabledReason ?? null
    const isRunning = handler?.isRunning ?? false
    const isQueued = handler?.isQueued ?? false
    const executionCount = handler?.executionCount ?? null
    const hasError = handler?.hasError ?? false
    const interrupt = handler?.interrupt

    useEffect(() => {
        if (!publishRunHandler) {
            return
        }

        publishRunHandler(
            run ? { run, disabledReason, isRunning, isQueued, executionCount, hasError, interrupt } : null
        )

        return () => publishRunHandler(null)
    }, [publishRunHandler, run, disabledReason, isRunning, isQueued, executionCount, hasError, interrupt])
}