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.

2.0 KB
Raw Blame
import { LogicWrapper, MakeLogicType, afterMount, connect, kea, key, path, props } from 'kea'
import { loaders } from 'kea-loaders'

import { projectLogic } from 'scenes/projectLogic'

import { featureFlagsRetrieve } from 'products/feature_flags/frontend/generated/api'

export interface FlagSelectorLogicProps {
    id: number | undefined
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface flagSelectorLogicValues {
    currentProjectId: number | null // projectLogic
    flagKey: string
    flagKeyLoading: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface flagSelectorLogicActions {
    loadFlagKey: () => any
    loadFlagKeyFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadFlagKeySuccess: (
        flagKey: string,
        payload?: any
    ) => {
        flagKey: string
        payload?: any
    }
}

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

export type flagSelectorLogicType = MakeLogicType<
    flagSelectorLogicValues,
    flagSelectorLogicActions,
    FlagSelectorLogicProps,
    flagSelectorLogicMeta
>

/** The key of the flag a FlagSelector holds, without loading the whole feature flag editor logic. */
export const flagSelectorLogic: LogicWrapper<flagSelectorLogicType> = kea<flagSelectorLogicType>([
    props({} as FlagSelectorLogicProps),
    key((props) => props.id ?? 'none'),
    path((key) => ['lib', 'components', 'flagSelectorLogic', key]),
    connect(() => ({ values: [projectLogic, ['currentProjectId']] })),
    loaders(({ props, values }) => ({
        flagKey: [
            '',
            {
                loadFlagKey: async () =>
                    props.id ? (await featureFlagsRetrieve(String(values.currentProjectId), props.id)).key : '',
            },
        ],
    })),
    afterMount(({ actions }) => {
        actions.loadFlagKey()
    }),
])