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 { useValues } from 'kea'
import { useMemo } from 'react'

import { LemonTag } from '@posthog/lemon-ui'

import { featureFlagReleaseConditionsLogic } from 'scenes/feature-flags/featureFlagReleaseConditionsLogic'

import { FeatureFlagFilters } from '~/types'

import { FeatureFlagConditionSetCard } from './FeatureFlagConditionSetCard'
import { ConditionSetChange, diffReleaseConditionSets, rolloutOf } from './releaseConditionsDiff'

export interface FeatureFlagReleaseConditionsChangeProps {
    flagId: string
    activityId: string
    before?: FeatureFlagFilters | null
    after: FeatureFlagFilters
}

function statusTag(set: ConditionSetChange): JSX.Element | null {
    if (set.status === 'added') {
        return (
            <LemonTag type="success" size="small">
                Added
            </LemonTag>
        )
    }
    if (set.status === 'changed') {
        return (
            <LemonTag type="warning" size="small">
                Changed
            </LemonTag>
        )
    }
    return null
}

export function FeatureFlagReleaseConditionsChange({
    flagId,
    activityId,
    before,
    after,
}: FeatureFlagReleaseConditionsChangeProps): JSX.Element {
    // The logic resolves distinct ids and flag ids to names from the groups it is given, and this view
    // also renders the sets that the change removed, so it needs both sides.
    const filtersForNameResolution = useMemo(
        (): FeatureFlagFilters => ({ ...after, groups: [...(after.groups ?? []), ...(before?.groups ?? [])] }),
        [after, before]
    )
    // The logic is keyed by id, and every history entry for one flag carries different filters,
    // so each entry needs its own key or they would all share one state.
    const logic = featureFlagReleaseConditionsLogic({
        id: `${flagId}-activity-${activityId}`,
        readOnly: true,
        filters: filtersForNameResolution,
    })
    const { aggregationTargetName, getDistinctIdName, getFlagKey } = useValues(logic)
    const diff = diffReleaseConditionSets(before, after)

    return (
        <div className="flex flex-col gap-2 py-2">
            <p className="text-xs text-muted mb-0">
                Release conditions after this change. Condition sets are evaluated top to bottom, the first match wins.
            </p>

            {diff.sets.length === 0 && (
                <p className="text-sm text-muted mb-0">No condition sets. The flag matches no one.</p>
            )}

            {diff.sets.map((set) => (
                <div key={set.index}>
                    {set.index > 0 && (
                        <div className="condition-set-separator my-2 py-0 text-center text-xs font-semibold text-muted">
                            OR
                        </div>
                    )}
                    <FeatureFlagConditionSetCard
                        group={set.group}
                        index={set.index}
                        aggregationTargetName={aggregationTargetName(set.group.aggregation_group_type_index)}
                        getDistinctIdName={getDistinctIdName}
                        getFlagKey={getFlagKey}
                        tag={statusTag(set)}
                        previousRolloutPercentage={set.previous ? rolloutOf(set.previous) : undefined}
                    />
                </div>
            ))}

            {diff.removed.length > 0 && (
                <>
                    <div className="mt-2 text-xs font-semibold text-muted">Removed by this change</div>
                    {diff.removed.map(({ group, index }) => (
                        <div key={index} className="opacity-60">
                            <FeatureFlagConditionSetCard
                                group={group}
                                index={index}
                                label={`Was set ${index + 1}`}
                                aggregationTargetName={aggregationTargetName(group.aggregation_group_type_index)}
                                getDistinctIdName={getDistinctIdName}
                                getFlagKey={getFlagKey}
                                tag={
                                    <LemonTag type="danger" size="small">
                                        Removed
                                    </LemonTag>
                                }
                            />
                        </div>
                    ))}
                </>
            )}
        </div>
    )
}