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.

6.9 KB
Raw Blame
import { useState } from 'react'

import { IconX } from '@posthog/icons'

import { IconOpenInNew } from 'lib/lemon-ui/icons'
import { LemonButton } from 'lib/lemon-ui/LemonButton'
import { LemonTag } from 'lib/lemon-ui/LemonTag'
import { Link } from 'lib/lemon-ui/Link'
import { urls } from 'scenes/urls'

import type { FeatureFlagType } from '~/types'

import { getFlagVariants } from '../utils'

interface VariantsPanelLinkFeatureFlagProps {
    linkedFeatureFlag: FeatureFlagType | null
    setShowFeatureFlagSelector: () => void
    disabled?: boolean
    onRemove?: () => void
}

const getTargetingSummary = (flag: FeatureFlagType): string[] => {
    if (!flag.filters.groups || flag.filters.groups.length === 0) {
        return ['All users']
    }

    return flag.filters.groups.map((group) => {
        const rollout = group.rollout_percentage != null ? `${group.rollout_percentage}% of ` : ''
        const users = group.properties?.length
            ? `users matching ${group.properties.length} ${group.properties.length === 1 ? 'condition' : 'conditions'}`
            : 'all users'
        const variant = group.variant ? ` → variant "${group.variant}"` : ''

        return `${rollout}${users}${variant}`
    })
}

const TargetingSummary = ({ flag }: { flag: FeatureFlagType }): JSX.Element => {
    const [expanded, setExpanded] = useState(false)
    const conditions = getTargetingSummary(flag)
    const hasMultipleConditions = conditions.length > 2

    const displayConditions = expanded ? conditions : conditions.slice(0, 2)

    return (
        <div className="space-y-2">
            <div className="text-xs uppercase tracking-wide font-semibold text-muted">Targeting</div>
            <ul className="list-disc pl-4 text-sm space-y-1">
                {displayConditions.map((condition, index) => (
                    <li key={index}>{condition}</li>
                ))}
            </ul>
            {hasMultipleConditions && !expanded && (
                <button
                    onClick={() => setExpanded(true)}
                    className="text-sm text-link hover:underline cursor-pointer pl-4"
                >
                    +{conditions.length - 2} more {conditions.length - 2 === 1 ? 'condition' : 'conditions'}
                </button>
            )}
            {expanded && hasMultipleConditions && (
                <button
                    onClick={() => setExpanded(false)}
                    className="text-sm text-link hover:underline cursor-pointer pl-4"
                >
                    Show less
                </button>
            )}
        </div>
    )
}

export const VariantsPanelLinkFeatureFlag = ({
    linkedFeatureFlag,
    setShowFeatureFlagSelector,
    disabled = false,
    onRemove,
}: VariantsPanelLinkFeatureFlagProps): JSX.Element => {
    if (!linkedFeatureFlag) {
        if (disabled) {
            return (
                <div className="text-danger text-sm">
                    You cannot change the feature flag when editing an experiment.
                </div>
            )
        }
        return (
            <div>
                <label className="text-sm font-semibold">Selected Feature Flag</label>
                <div className="mt-2 p-8 border border-dashed rounded-lg bg-bg-light flex flex-col items-center gap-3">
                    <div className="text-base font-semibold">No feature flag selected</div>
                    <div className="text-sm text-muted-alt text-center">
                        Select an existing multivariate feature flag to use with this experiment
                    </div>
                    <LemonButton type="primary" size="small" onClick={setShowFeatureFlagSelector}>
                        Select Feature Flag
                    </LemonButton>
                </div>
            </div>
        )
    }

    const variants = getFlagVariants(linkedFeatureFlag)

    return (
        <div>
            <label className="text-sm font-semibold">Linked feature flag</label>
            <div className="mt-2 border rounded-lg bg-bg-light p-4 space-y-2">
                {/* Header: Flag key + link + change button */}
                <div className="flex flex-row gap-4">
                    <div className="flex items-center gap-2 min-w-0 flex-1">
                        <div className="font-semibold text-base truncate">{linkedFeatureFlag.key}</div>
                        {!linkedFeatureFlag.active && <LemonTag type="default">Inactive</LemonTag>}
                        <Link
                            to={urls.featureFlag(linkedFeatureFlag.id as number)}
                            target="_blank"
                            className="flex items-center hover:text-link flex-shrink-0"
                            title="View feature flag"
                        >
                            <IconOpenInNew />
                        </Link>
                    </div>
                    {/* Only show Change button when not read-only */}

                    <div className="flex items-center gap-1">
                        <LemonButton
                            type="secondary"
                            size="small"
                            onClick={setShowFeatureFlagSelector}
                            disabledReason={
                                disabled ? 'You cannot change the feature flag when editing an experiment.' : undefined
                            }
                        >
                            Change
                        </LemonButton>
                        {onRemove && (
                            <LemonButton
                                size="small"
                                icon={<IconX />}
                                onClick={onRemove}
                                tooltip="Remove linked flag"
                            />
                        )}
                    </div>
                </div>

                {/* Description */}
                {linkedFeatureFlag.name && (
                    <div className="text-sm text-muted-alt -mt-2 mb-3">{linkedFeatureFlag.name}</div>
                )}

                <div className="flex gap-10">
                    {/* Variants */}
                    <div className="space-y-2">
                        <div className="text-xs uppercase tracking-wide font-semibold text-muted">Variants</div>
                        <div className="flex flex-wrap gap-1.5">
                            {variants.map(({ key }) => (
                                <LemonTag key={key} type={key === 'control' ? 'primary' : 'default'}>
                                    {key}
                                </LemonTag>
                            ))}
                        </div>
                    </div>

                    {/* Targeting */}
                    <div className="flex-1">
                        <TargetingSummary flag={linkedFeatureFlag} />
                    </div>
                </div>
            </div>
        </div>
    )
}