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.

7.1 KB
Raw Blame
import { BindLogic, useActions, useValues } from 'kea'
import { useEffect } from 'react'

import { IconFlag, IconRocket } from '@posthog/icons'

import { NotFound } from 'lib/components/NotFound'
import { JSONContent } from 'lib/components/RichContentEditor/types'
import { IconRecording, IconSurveys } from 'lib/lemon-ui/icons'
import { FeatureFlagLoadError } from 'scenes/feature-flags/FeatureFlagLoadError'
import { FeatureFlagLogicProps, featureFlagLogic } from 'scenes/feature-flags/featureFlagLogic'
import {
    FEATURE_FLAG_NOTEBOOK_WIDGET_VIEWS,
    FeatureFlagNotebookWidgetAttributes,
    withFeatureFlagNotebookMetadata,
} from 'scenes/feature-flags/featureFlagNotebookWidgetViews'
import { FeatureFlagReleaseConditions } from 'scenes/feature-flags/FeatureFlagReleaseConditions'
import { createPostHogWidgetNode } from 'scenes/notebooks/Nodes/NodeWrapper'
import { getNotebookWidgetDefaultView } from 'scenes/notebooks/notebookWidgetCatalog'
import { urls } from 'scenes/urls'

import { isV1FeatureFlagConfig } from 'products/feature_flags/frontend/featureFlagConfigFormat'
import { FeatureFlagConfigReadonlyNotice } from 'products/feature_flags/frontend/FeatureFlagConfigReadonlyNotice'

import { NotebookNodeProps, NotebookNodeType } from '../types'
import { buildEarlyAccessFeatureContent } from './NotebookNodeEarlyAccessFeature'
import { buildCodeExampleContent } from './NotebookNodeFlagCodeExample'
import { notebookNodeFlagLogic } from './NotebookNodeFlagLogic'
import { notebookNodeLogic } from './notebookNodeLogic'
import { buildPlaylistContent } from './NotebookNodePlaylist'
import { buildSurveyContent } from './NotebookNodeSurvey'

function FeatureFlagNotebookActions({ attributes }: NotebookNodeProps<FeatureFlagNotebookWidgetAttributes>): null {
    const { id } = attributes
    const {
        featureFlag,
        recordingFilterForFlag,
        hasEarlyAccessFeatures,
        canCreateEarlyAccessFeature,
        hasSurveys,
        newEarlyAccessFeatureLoading,
        newSurveyLoading,
    } = useValues(featureFlagLogic({ id }))
    const { createEarlyAccessFeature, createSurvey } = useActions(featureFlagLogic({ id }))
    const { nextNode } = useValues(notebookNodeLogic)
    const { insertAfter, setActions } = useActions(notebookNodeLogic)

    const { shouldDisableInsertEarlyAccessFeature, shouldDisableInsertSurvey } = useValues(
        notebookNodeFlagLogic({ id, insertAfter })
    )

    useEffect(() => {
        setActions([
            {
                icon: <IconSurveys />,
                text: `${hasSurveys ? 'View' : 'Create'} survey`,
                disabledReason: !hasSurveys && newSurveyLoading ? 'Creating survey' : undefined,
                onClick: () => {
                    if (!hasSurveys) {
                        return createSurvey()
                    }
                    if ((featureFlag?.surveys?.length || 0) <= 0) {
                        return
                    }
                    if (!shouldDisableInsertSurvey(nextNode) && featureFlag.surveys) {
                        insertAfter(buildSurveyContent(featureFlag.surveys[0].id))
                    }
                },
            },
            {
                icon: <IconFlag />,
                text: 'Show implementation',
                onClick: () => {
                    if (nextNode?.type.name !== NotebookNodeType.FeatureFlagCodeExample) {
                        insertAfter(buildCodeExampleContent(id))
                    }
                },
            },
            {
                icon: <IconRecording />,
                text: 'View replays',
                onClick: () => {
                    if (nextNode?.type.name !== NotebookNodeType.RecordingPlaylist) {
                        insertAfter(buildPlaylistContent(recordingFilterForFlag))
                    }
                },
            },
            // canCreateEarlyAccessFeature is false for a non-v1 flag, so the notebook never offers Create.
            // A non-v1 flag that already has an early access feature still gets the View action.
            canCreateEarlyAccessFeature || (hasEarlyAccessFeatures && !isV1FeatureFlagConfig(featureFlag.filters))
                ? {
                      text: `${hasEarlyAccessFeatures ? 'View' : 'Create'} early access feature`,
                      icon: <IconRocket />,
                      disabledReason:
                          !hasEarlyAccessFeatures && newEarlyAccessFeatureLoading
                              ? 'Creating early access feature'
                              : undefined,
                      onClick: () => {
                          if (!hasEarlyAccessFeatures) {
                              createEarlyAccessFeature()
                          } else {
                              if ((featureFlag?.features?.length || 0) <= 0) {
                                  return
                              }
                              if (!shouldDisableInsertEarlyAccessFeature(nextNode) && featureFlag.features) {
                                  insertAfter(buildEarlyAccessFeatureContent(featureFlag.features[0].id))
                              }
                          }
                      },
                  }
                : undefined,
        ])
        // oxlint-disable-next-line exhaustive-deps
    }, [featureFlag, newEarlyAccessFeatureLoading, newSurveyLoading])

    return null
}

const Component = ({ attributes }: NotebookNodeProps<FeatureFlagNotebookWidgetAttributes>): JSX.Element => {
    const { id } = attributes
    const { featureFlag, featureFlagMissing, featureFlagLoadFailed } = useValues(featureFlagLogic({ id }))
    const { expanded } = useValues(notebookNodeLogic)

    if (featureFlagMissing) {
        return <NotFound object="feature flag" />
    }
    if (featureFlagLoadFailed) {
        return <FeatureFlagLoadError id={id} />
    }

    return (
        <>
            <BindLogic logic={featureFlagLogic} props={{ id }}>
                {expanded ? (
                    <div className="p-2">
                        {isV1FeatureFlagConfig(featureFlag.filters) ? (
                            <FeatureFlagReleaseConditions readOnly filters={featureFlag.filters} />
                        ) : (
                            <FeatureFlagConfigReadonlyNotice filters={featureFlag.filters} />
                        )}
                    </div>
                ) : null}
            </BindLogic>
        </>
    )
}

export const NotebookNodeFlag = createPostHogWidgetNode<FeatureFlagNotebookWidgetAttributes>({
    nodeType: NotebookNodeType.FeatureFlag,
    titlePlaceholder: 'Feature flag',
    editableTitle: false,
    Component: withFeatureFlagNotebookMetadata(Component),
    ToolbarComponent: FeatureFlagNotebookActions,
    heightEstimate: '3rem',
    href: (attrs) => urls.featureFlag(attrs.id),
    resizeable: false,
    attributes: {
        id: {},
        view: {},
    },
    defaultView: getNotebookWidgetDefaultView('FeatureFlag'),
    views: FEATURE_FLAG_NOTEBOOK_WIDGET_VIEWS,
})

export function buildFlagContent(id: FeatureFlagLogicProps['id']): JSONContent {
    return {
        type: NotebookNodeType.FeatureFlag,
        attrs: { id },
    }
}