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.

9.4 KB
Raw Blame
import type { ReactElement } from 'react'

import { Badge, Card, CardContent, CardHeader, CardTitle, CardDescription } from '@posthog/quill'

import { PropertyFilterList, type PropertyFilter } from './PropertyFilterList'

// Matches SUPER_CONDITION_INDEX in rust/feature-flags/src/api/types.rs: the early-access
// enrollment super condition has no position among the zero-based release conditions.
export const SUPER_CONDITION_INDEX = -1
// Matches HOLDOUT_CONDITION_INDEX in rust/feature-flags/src/api/types.rs: the holdout has no
// position among the zero-based release conditions either.
export const HOLDOUT_CONDITION_INDEX = -2

const SYNTHETIC_CONDITION_LABELS: Record<number, string> = {
    [SUPER_CONDITION_INDEX]: 'Early access enrollment',
    [HOLDOUT_CONDITION_INDEX]: 'Holdout',
}

export interface ConditionAnalysis {
    index: number
    matched: boolean
    rollout_percentage: number
    variant?: string | null
    properties?: Array<{ key: string; value: unknown; operator?: string; type?: string }>
    reason?: string
    explanation?: string
}

export interface FeatureFlagTestEvaluationResult {
    flag_key: string
    result: boolean | string
    reason: string
    condition_index: number | null
    payload: unknown | null
    person_properties: Record<string, unknown>
    conditions: ConditionAnalysis[]
}

// Keep the original name for backward compatibility with the generated UI app
export type FeatureFlagTestingData = FeatureFlagTestEvaluationResult

export interface FeatureFlagTestingViewProps {
    flag: FeatureFlagTestingData
}

export function FeatureFlagTestingView({ flag }: FeatureFlagTestingViewProps): ReactElement {
    const getResultBadgeVariant = (): 'success' | 'destructive' | 'info' => {
        if (typeof flag.result === 'boolean') {
            return flag.result ? 'success' : 'destructive'
        }
        return 'info'
    }

    const formatResult = (): string => {
        if (typeof flag.result === 'boolean') {
            return flag.result ? 'True' : 'False'
        }
        return String(flag.result)
    }

    const matchedConditionLabel = (): string => {
        // Reuse the same signal and label map the condition list below uses (a matched entry at
        // the sentinel index) instead of re-deriving "was this enrollment/holdout" from flag.reason.
        const matchedCondition = flag.conditions.find((condition) => condition.matched)
        const syntheticLabel = matchedCondition && SYNTHETIC_CONDITION_LABELS[matchedCondition.index]
        if (syntheticLabel) {
            return syntheticLabel
        }
        // Falls back to flag.reason if the backend hasn't started emitting the synthetic holdout
        // entry yet (the Rust service and this frontend deploy independently).
        if (flag.reason === 'holdout_condition_value') {
            return 'Holdout'
        }
        return `#${flag.condition_index! + 1}`
    }

    return (
        <div className="p-4">
            <div className="flex flex-col gap-2">
                {/* Header */}
                <div className="flex flex-col gap-1">
                    <div className="flex items-center gap-2 flex-wrap">
                        <span className="text-lg font-semibold text-primary">{flag.flag_key}</span>
                        <Badge>Test Result</Badge>
                    </div>
                    <span className="text-sm text-secondary">Feature flag evaluation result for the tested user</span>
                </div>

                {/* Evaluation Result */}
                <Card>
                    <CardHeader>
                        <CardTitle>Test Evaluation Results</CardTitle>
                        <CardDescription>
                            This shows how the flag evaluated for the specified user, including which condition matched
                            and the person properties that were considered.
                        </CardDescription>
                    </CardHeader>
                    <CardContent>
                        <div className="flex items-center gap-2">
                            <span className="text-sm font-medium">Result:</span>
                            <Badge variant={getResultBadgeVariant()}>{formatResult()}</Badge>
                        </div>
                        <div className="text-sm">
                            <span className="font-medium">Reason: </span>
                            <span className="text-secondary">{flag.reason}</span>
                        </div>
                        {(flag.condition_index !== null || flag.reason === 'holdout_condition_value') && (
                            <div className="text-sm">
                                <span className="font-medium">Matched condition: </span>
                                <span className="text-secondary">{matchedConditionLabel()}</span>
                            </div>
                        )}
                        {flag.payload != null && (
                            <div className="text-sm">
                                <span className="font-medium">Payload: </span>
                                <code className="text-xs bg-bg-light p-1 rounded">
                                    {JSON.stringify(flag.payload) || 'null'}
                                </code>
                            </div>
                        )}
                    </CardContent>
                    <div className="p-1 flex flex-col gap-1" />
                </Card>

                {/* Person Properties */}
                {Object.keys(flag.person_properties).length > 0 && (
                    <Card>
                        <CardHeader>
                            <CardTitle>Person Properties</CardTitle>{' '}
                        </CardHeader>
                        <CardContent>
                            <div className="space-y-1">
                                {Object.entries(flag.person_properties).map(([key, value]) => (
                                    <div key={key} className="flex items-center gap-2 text-sm">
                                        <span className="font-mono text-primary">{key}:</span>
                                        <span className="text-secondary">
                                            {typeof value === 'object' ? String(JSON.stringify(value)) : String(value)}
                                        </span>
                                    </div>
                                ))}
                            </div>
                        </CardContent>
                    </Card>
                )}

                {/* Condition Analysis */}
                {flag.conditions.length > 0 && (
                    <Card>
                        <CardHeader>
                            <CardTitle>Condition Analysis</CardTitle>
                            <CardDescription>Detailed breakdown of how each condition was evaluated</CardDescription>
                        </CardHeader>
                        <CardContent>
                            <div className="flex flex-col gap-1">
                                {flag.conditions.map((condition) => (
                                    <Card key={condition.index}>
                                        <CardContent>
                                            <div className="flex items-center gap-2">
                                                <span className="text-sm font-medium">
                                                    {`${
                                                        SYNTHETIC_CONDITION_LABELS[condition.index] ??
                                                        `Condition #${condition.index + 1}`
                                                    }:`}
                                                </span>
                                                <Badge variant={condition.matched ? 'success' : 'destructive'}>
                                                    {condition.matched ? 'Matched' : 'No match'}
                                                </Badge>
                                                {/* synthetic (enrollment/holdout) entries have no release rollout */}
                                                {condition.index >= 0 && (
                                                    <Badge>{condition.rollout_percentage}% rollout</Badge>
                                                )}
                                                {condition.variant && <Badge variant="info">{condition.variant}</Badge>}
                                            </div>
                                            {(condition.explanation || condition.reason) && (
                                                <div className="text-sm text-secondary">
                                                    {condition.explanation || condition.reason}
                                                </div>
                                            )}
                                            {condition.properties && condition.properties.length > 0 && (
                                                <PropertyFilterList
                                                    filters={condition.properties as PropertyFilter[]}
                                                />
                                            )}
                                        </CardContent>
                                    </Card>
                                ))}
                            </div>
                        </CardContent>
                    </Card>
                )}
            </div>
        </div>
    )
}