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.

4.1 KB
Raw Blame
import { MakeLogicType, actions, connect, kea, key, listeners, path, props, reducers, selectors } from 'kea'

import { FEATURE_FLAGS } from 'lib/constants'
import { FeatureFlagsSet, featureFlagLogic } from 'lib/logic/featureFlagLogic'
import { userLogic } from 'scenes/userLogic'

import { UserType } from '~/types'

// pinned: persisted per user in `has_seen_product_intro_for`, so renaming it reopens the hint for
// everyone who already dismissed it.
export const NOTEBOOK_DATAFRAME_HINT_SEEN_KEY = 'notebooks-sql-dataframe-hint'

export interface NotebookDataframeHintLogicProps {
    shortId: string
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface notebookDataframeHintLogicValues {
    featureFlags: FeatureFlagsSet // featureFlagLogic
    user: UserType | null // userLogic
    canShowHint: boolean
    hintNodeId: string | null
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface notebookDataframeHintLogicActions {
    updateHasSeenProductIntroFor: (
        productKey: string,
        value?: boolean | undefined
    ) => {
        productKey: string
        value: boolean
    } // userLogic
    dismissHint: () => {
        value: true
    }
    reportRunFinished: (
        nodeId: string,
        hasReturnVariable: boolean
    ) => {
        hasReturnVariable: boolean
        nodeId: string
    }
    showHint: (nodeId: string) => {
        nodeId: string
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface notebookDataframeHintLogicMeta {
    key: string
    __keaTypeGenInternalSelectorTypes: {
        canShowHint: (featureFlags: FeatureFlagsSet, user: UserType | null) => boolean
    }
}

export type notebookDataframeHintLogicType = MakeLogicType<
    notebookDataframeHintLogicValues,
    notebookDataframeHintLogicActions,
    NotebookDataframeHintLogicProps,
    notebookDataframeHintLogicMeta
>

/** Decides when to point a first-time user at the output dataframe name on a SQL cell, and records
 * the dismissal against the user rather than the browser. */
export const notebookDataframeHintLogic = kea<notebookDataframeHintLogicType>([
    path((key) => ['scenes', 'notebooks', 'Nodes', 'components', 'notebookDataframeHintLogic', key]),
    props({} as NotebookDataframeHintLogicProps),
    key((props) => props.shortId),
    connect(() => ({
        values: [featureFlagLogic, ['featureFlags'], userLogic, ['user']],
        actions: [userLogic, ['updateHasSeenProductIntroFor']],
    })),
    actions({
        reportRunFinished: (nodeId: string, hasReturnVariable: boolean) => ({ nodeId, hasReturnVariable }),
        showHint: (nodeId: string) => ({ nodeId }),
        dismissHint: true,
    }),
    reducers({
        hintNodeId: [
            null as string | null,
            {
                showHint: (_, { nodeId }) => nodeId,
                dismissHint: () => null,
            },
        ],
    }),
    selectors({
        canShowHint: [
            (s) => [s.featureFlags, s.user],
            // An unloaded user is not a user who has seen nothing, so the hint waits rather than
            // opening once on every page load and closing again when the user arrives.
            (featureFlags: FeatureFlagsSet, user: UserType | null): boolean =>
                !!featureFlags[FEATURE_FLAGS.REVAMPED_PY_NOTEBOOKS] &&
                !!user &&
                !user.has_seen_product_intro_for?.[NOTEBOOK_DATAFRAME_HINT_SEEN_KEY],
        ],
    }),
    listeners(({ actions, values }) => ({
        reportRunFinished: ({ nodeId, hasReturnVariable }) => {
            // A cell that already names its output has nothing left to explain. One hint at a time
            // too: a chain run finishes several cells, and the popover would hop between them.
            if (!values.canShowHint || hasReturnVariable || values.hintNodeId) {
                return
            }
            actions.showHint(nodeId)
        },
        dismissHint: () => {
            actions.updateHasSeenProductIntroFor(NOTEBOOK_DATAFRAME_HINT_SEEN_KEY)
        },
    })),
])