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, path, props, selectors } from 'kea'

import { isPropertyFilterWithOperator } from 'lib/components/PropertyFilters/utils'
import { stringifyWithBigInts } from 'lib/utils/json'

import { cohortsModel } from '~/models/cohortsModel'
import { AnyPropertyFilter, CohortType, FeatureFlagEvaluationRuntime, PropertyFilterType } from '~/types'

export interface FeatureFlagConditionWarningLogicProps {
    properties: AnyPropertyFilter[]
    evaluationRuntime: FeatureFlagEvaluationRuntime
}

const REGEX_LOOKAHEAD = /(?<!\\)\(\?[=!]/ // (?= or (?!
const REGEX_LOOKBEHIND = /(?<!\\)\(\?<[=!]/ //  or (?<!
const REGEX_BACKREFERENCE = /(?<!\\)\\[1-9]/ // \1 through \9

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface featureFlagConditionWarningLogicValues {
    cohortsById: Partial<Record<number | string, CohortType>> // cohortsModel
    hasStaticCohort: boolean
    issues: string[]
    warning: string | undefined
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface featureFlagConditionWarningLogicMeta {
    key: string
    __keaTypeGenInternalSelectorTypes: {
        issues: (
            cohortsById: Partial<Record<number | string, CohortType>>,
            properties: AnyPropertyFilter[],
            evaluationRuntime: FeatureFlagEvaluationRuntime
        ) => string[]
        warning: (issues: string[]) => string | undefined
        hasStaticCohort: (issues: string[]) => boolean
    }
}

export type featureFlagConditionWarningLogicType = MakeLogicType<
    featureFlagConditionWarningLogicValues,
    {},
    FeatureFlagConditionWarningLogicProps,
    featureFlagConditionWarningLogicMeta
>

export const featureFlagConditionWarningLogic = kea<featureFlagConditionWarningLogicType>([
    path(['scenes', 'feature-flags', 'featureFlagConditionWarningLogic']),
    props({} as FeatureFlagConditionWarningLogicProps),
    key((props) => stringifyWithBigInts(props.properties)),
    connect(() => ({
        values: [cohortsModel, ['cohortsById']],
    })),

    selectors({
        issues: [
            (s, p) => [s.cohortsById, p.properties, p.evaluationRuntime],
            (
                cohortsById: Partial<Record<string | number, CohortType>>,
                properties: AnyPropertyFilter[],
                evaluationRuntime: FeatureFlagEvaluationRuntime
            ): string[] => {
                // Local evaluation is only relevant for server-side SDKs, so only show the warning
                // for flags that can be evaluated server-side (ALL or SERVER)
                if (evaluationRuntime === FeatureFlagEvaluationRuntime.CLIENT) {
                    return []
                }

                const issues: string[] = []

                properties.forEach((property) => {
                    if (isPropertyFilterWithOperator(property) && property.operator === 'is_not_set') {
                        issues.push('is_not_set operator')
                    }

                    if (property.type === PropertyFilterType.Cohort) {
                        const cohortId = property.value
                        // Try both numeric and string keys since cohort IDs can be stored as either type
                        const cohort = cohortsById[cohortId] ?? cohortsById[String(cohortId)]
                        if (cohort?.is_static) {
                            issues.push('static cohorts')
                        }
                    }

                    if (isPropertyFilterWithOperator(property) && property.operator === 'regex') {
                        const pattern = String(property.value)

                        if (REGEX_LOOKAHEAD.test(pattern)) {
                            issues.push('lookahead in regex')
                        }

                        if (REGEX_LOOKBEHIND.test(pattern)) {
                            issues.push('lookbehind in regex')
                        }

                        if (REGEX_BACKREFERENCE.test(pattern)) {
                            issues.push('backreferences in regex')
                        }
                    }
                })

                return [...new Set(issues)]
            },
        ],

        warning: [
            (s) => [s.issues],
            (issues: string[]): string | undefined => (issues.length === 0 ? undefined : issues.join(', ')),
        ],

        hasStaticCohort: [(s) => [s.issues], (issues: string[]): boolean => issues.includes('static cohorts')],
    }),
])