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.4 KB
Raw Blame
import { MakeLogicType, connect, kea, key, listeners, path, props, selectors } from 'kea'

import { JSONContent, RichContentNode } from 'lib/components/RichContentEditor/types'
import { FeatureFlagLogicProps, featureFlagLogic } from 'scenes/feature-flags/featureFlagLogic'

import type { EarlyAccessFeatureType, FeatureFlagType, Survey } from '../../../types'
import { NotebookNodeType } from '../types'
import { buildEarlyAccessFeatureContent } from './NotebookNodeEarlyAccessFeature'
import { buildSurveyContent } from './NotebookNodeSurvey'

export type NotebookNodeFlagLogicProps = {
    id: FeatureFlagLogicProps['id']
    insertAfter: (content: JSONContent) => void
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface notebookNodeFlagLogicValues {
    featureFlag: FeatureFlagType // featureFlagLogic
    hasEarlyAccessFeatures: boolean // featureFlagLogic
    hasSurveys: boolean | null // featureFlagLogic
    shouldDisableInsertEarlyAccessFeature: (nextNode: RichContentNode | null) => boolean
    shouldDisableInsertSurvey: (nextNode: RichContentNode | null) => boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface notebookNodeFlagLogicActions {
    createEarlyAccessFeatureSuccess: (
        newEarlyAccessFeature: EarlyAccessFeatureType,
        payload?: any
    ) => {
        newEarlyAccessFeature: EarlyAccessFeatureType
        payload?: any
    } // featureFlagLogic
    createSurveySuccess: (
        newSurvey: Survey,
        payload?: any
    ) => {
        newSurvey: Survey
        payload?: any
    } // featureFlagLogic
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface notebookNodeFlagLogicMeta {
    key: number | 'link' | 'new'
    __keaTypeGenInternalSelectorTypes: {
        shouldDisableInsertEarlyAccessFeature: (
            featureFlag: FeatureFlagType,
            hasEarlyAccessFeatures: boolean
        ) => (nextNode: RichContentNode | null) => boolean
        shouldDisableInsertSurvey: (
            featureFlag: FeatureFlagType,
            hasSurveys: boolean | null
        ) => (nextNode: RichContentNode | null) => boolean
    }
}

export type notebookNodeFlagLogicType = MakeLogicType<
    notebookNodeFlagLogicValues,
    notebookNodeFlagLogicActions,
    NotebookNodeFlagLogicProps,
    notebookNodeFlagLogicMeta
>

export const notebookNodeFlagLogic = kea<notebookNodeFlagLogicType>([
    props({} as NotebookNodeFlagLogicProps),
    path((key) => ['scenes', 'notebooks', 'Notebook', 'Nodes', 'notebookNodeFlagLogic', key]),
    key(({ id }) => id),

    connect((props: NotebookNodeFlagLogicProps) => ({
        actions: [featureFlagLogic({ id: props.id }), ['createEarlyAccessFeatureSuccess', 'createSurveySuccess']],
        values: [featureFlagLogic({ id: props.id }), ['featureFlag', 'hasEarlyAccessFeatures', 'hasSurveys']],
    })),
    listeners(({ props }) => ({
        createEarlyAccessFeatureSuccess: async ({ newEarlyAccessFeature }) => {
            props.insertAfter(buildEarlyAccessFeatureContent(newEarlyAccessFeature.id))
        },
        createSurveySuccess: async ({ newSurvey }) => {
            props.insertAfter(buildSurveyContent(newSurvey.id))
        },
    })),
    selectors({
        shouldDisableInsertEarlyAccessFeature: [
            (s) => [s.featureFlag, s.hasEarlyAccessFeatures],
            (featureFlag: FeatureFlagType, hasEarlyAccessFeatures: boolean) =>
                (nextNode: RichContentNode | null): boolean => {
                    return (
                        (nextNode?.type.name === NotebookNodeType.EarlyAccessFeature &&
                            hasEarlyAccessFeatures &&
                            featureFlag.features &&
                            nextNode?.attrs.id === featureFlag.features[0].id) ||
                        false
                    )
                },
        ],
        shouldDisableInsertSurvey: [
            (s) => [s.featureFlag, s.hasSurveys],
            (featureFlag: FeatureFlagType, hasSurveys: boolean | null) =>
                (nextNode: RichContentNode | null): boolean => {
                    return (
                        (nextNode?.type.name === NotebookNodeType.Survey &&
                            hasSurveys &&
                            featureFlag.surveys &&
                            nextNode?.attrs.id === featureFlag.surveys[0].id) ||
                        false
                    )
                },
        ],
    }),
])