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

import api from 'lib/api'
import { isObject } from 'lib/utils/guards'
import { variantKeyToIndexFeatureFlagPayloads } from 'scenes/feature-flags/featureFlagLogic'

import type { FeatureFlagType } from '../../../../../types'
import { LinkedFeatureFlag } from '../../types'

export type FlagTriggerLogicProps = {
    logicKey: string
    flag: LinkedFeatureFlag | null
    onChange: (flag: LinkedFeatureFlag | null) => void
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface flagTriggerLogicValues {
    featureFlag: any
    featureFlagLoading: boolean
    flag: LinkedFeatureFlag | null
    flagHasVariants: boolean
    linkedFeatureFlagId: number | null
    linkedFlag: any
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface flagTriggerLogicActions {
    loadFeatureFlag: () => any
    loadFeatureFlagFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadFeatureFlagSuccess: (
        featureFlag: FeatureFlagType | null,
        payload?: any
    ) => {
        featureFlag: FeatureFlagType | null
        payload?: any
    }
    onChange: (flag: LinkedFeatureFlag | null) => {
        flag: LinkedFeatureFlag | null
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface flagTriggerLogicMeta {
    key: string
    __keaTypeGenInternalSelectorTypes: {
        linkedFeatureFlagId: (flag: LinkedFeatureFlag | null) => number | null
        flag: (flag: LinkedFeatureFlag | null) => LinkedFeatureFlag | null
        linkedFlag: (featureFlag: any, flag: LinkedFeatureFlag | null) => any
        flagHasVariants: (linkedFlag: any) => boolean
    }
}

export type flagTriggerLogicType = MakeLogicType<
    flagTriggerLogicValues,
    flagTriggerLogicActions,
    FlagTriggerLogicProps,
    flagTriggerLogicMeta
>

export const flagTriggerLogic = kea<flagTriggerLogicType>([
    props({} as FlagTriggerLogicProps),
    key((props) => props.logicKey),
    path((key) => ['lib', 'components', 'IngestionControls', 'triggers', 'FlagTrigger', 'flagTriggerLogic', key]),
    actions({
        onChange: (flag: LinkedFeatureFlag | null) => ({ flag }),
    }),
    loaders(({ values }) => ({
        featureFlag: {
            loadFeatureFlag: async () => {
                if (values.linkedFeatureFlagId) {
                    const retrievedFlag = await api.featureFlags.get(values.linkedFeatureFlagId)
                    return variantKeyToIndexFeatureFlagPayloads(retrievedFlag)
                }
                return null
            },
        },
    })),
    selectors({
        linkedFeatureFlagId: [(_, p) => [p.flag], (flag: LinkedFeatureFlag | null) => flag?.id || null],
        flag: [(_, p) => [p.flag], (flag: LinkedFeatureFlag | null) => flag],
        linkedFlag: [
            (s, p) => [s.featureFlag, p.flag],
            // an existing linked flag is loaded from the API,
            // a newly chosen flag is selected and can be passed in
            // the original value is used to ensure that we don't
            // show stale values as people change the selection
            (featureFlag, flag: LinkedFeatureFlag | null) => (flag?.id ? featureFlag : null),
        ],
        flagHasVariants: [(s) => [s.linkedFlag], (linkedFlag) => isObject(linkedFlag?.filters.multivariate)],
    }),
    listeners(({ props }) => ({
        onChange: ({ flag }) => {
            props.onChange(flag)
        },
    })),
    afterMount(({ actions }) => {
        actions.loadFeatureFlag()
    }),
])