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.

3.0 KB
Raw Blame
import { MakeLogicType, connect, kea, key, listeners, path, props } from 'kea'
import { loaders } from 'kea-loaders'

import { FEATURE_FLAGS } from 'lib/constants'
import { featureFlagLogic } from 'lib/logic/featureFlagLogic'
import type { FeatureFlagsSet } from 'lib/logic/featureFlagLogic'
import { projectLogic } from 'scenes/projectLogic'

import { experimentsFlagCleanupTargetRetrieve } from 'products/experiments/frontend/generated/api'
import type { ExperimentFlagCleanupTargetApi } from 'products/experiments/frontend/generated/api.schemas'

import { modalsLogic } from '../modalsLogic'

export interface FlagCleanupTargetLogicProps {
    experimentId: number
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface flagCleanupTargetLogicValues {
    featureFlags: FeatureFlagsSet // featureFlagLogic
    currentProjectId: number | null // projectLogic
    cleanupTarget: ExperimentFlagCleanupTargetApi | null
    cleanupTargetLoading: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface flagCleanupTargetLogicActions {
    openFinishExperimentModal: () => {
        value: true
    } // modalsLogic
    loadCleanupTarget: () => any
    loadCleanupTargetFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadCleanupTargetSuccess: (
        cleanupTarget: ExperimentFlagCleanupTargetApi,
        payload?: any
    ) => {
        cleanupTarget: ExperimentFlagCleanupTargetApi
        payload?: any
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface flagCleanupTargetLogicMeta {
    key: number
}

export type flagCleanupTargetLogicType = MakeLogicType<
    flagCleanupTargetLogicValues,
    flagCleanupTargetLogicActions,
    FlagCleanupTargetLogicProps,
    flagCleanupTargetLogicMeta
>

export const flagCleanupTargetLogic = kea<flagCleanupTargetLogicType>([
    props({} as FlagCleanupTargetLogicProps),
    key((props) => props.experimentId),
    path((key) => ['scenes', 'experiments', 'flagCleanupTargetLogic', key]),
    connect(() => ({
        values: [projectLogic, ['currentProjectId'], featureFlagLogic, ['featureFlags']],
        actions: [modalsLogic, ['openFinishExperimentModal']],
    })),
    loaders(({ props, values }) => ({
        cleanupTarget: [
            null as ExperimentFlagCleanupTargetApi | null,
            {
                loadCleanupTarget: async () =>
                    await experimentsFlagCleanupTargetRetrieve(String(values.currentProjectId), props.experimentId),
            },
        ],
    })),
    listeners(({ actions, values }) => ({
        // Loaded on modal open, not on mount: the modal component mounts with the scene,
        // and most experiment page visits never open it.
        openFinishExperimentModal: () => {
            if (values.featureFlags[FEATURE_FLAGS.TASKS]) {
                actions.loadCleanupTarget()
            }
        },
    })),
])