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.

17.2 KB
Raw Blame
import clsx from 'clsx'
import { useActions, useValues } from 'kea'
import { useState } from 'react'

import { IconCopy, IconEllipsis, IconPencil, IconSort, IconStack, IconTarget, IconTrash } from '@posthog/icons'
import { LemonButton, LemonDialog, LemonDropdown, LemonMenu, LemonTag, Tooltip } from '@posthog/lemon-ui'

import { TaxonomicFilter } from 'lib/components/TaxonomicFilter/TaxonomicFilter'
import { TaxonomicFilterGroupType } from 'lib/components/TaxonomicFilter/types'
import { Spinner } from 'lib/lemon-ui/Spinner'
import { experimentLogic, getSectionMetricUuids } from 'scenes/experiments/experimentLogic'
import { experimentMetricsLogic } from 'scenes/experiments/experimentMetricsLogic'
import { isMetricThresholdCueVisible } from 'scenes/experiments/ExperimentMetricThreshold'
import {
    EXPOSURE_DEFAULT_EVENT,
    getActivationConfig,
    getExposureEventAndProperty,
    resolvedExposureEvent,
} from 'scenes/experiments/exposureContract'
import { urls } from 'scenes/urls'

import type { Breakdown, EventsNode, ExperimentMetric } from '~/queries/schema/schema-general'
import { NodeKind } from '~/queries/schema/schema-general'
import type { Experiment } from '~/types'

import {
    METRIC_CONTEXTS,
    experimentMetricModalLogic,
} from 'products/experiments/frontend/modals/ExperimentMetricModal/experimentMetricModalLogic'
import { sharedMetricDetailsModalLogic } from 'products/experiments/frontend/modals/SharedMetricDetailsModal/sharedMetricDetailsModalLogic'

import { MetricRetryDetails } from './MetricRetryState'
import { MetricTitle } from './MetricTitle'
import { MetricTypeTag } from './MetricTypeTag'

const MAX_BREAKDOWNS = 3

// Helper function to get the exposure event from experiment. In activation mode breakdowns are
// attributed from the activation event, so property suggestions should come from it too.
const getExposureEvent = (experiment: Experiment): string => {
    const activationConfig = getActivationConfig(experiment.exposure_criteria)
    if (activationConfig && 'event' in activationConfig && activationConfig.event) {
        return activationConfig.event
    }
    return (
        getExposureEventAndProperty({
            featureFlagKey: experiment.feature_flag_key,
            exposureCriteria: experiment.exposure_criteria,
            resolvedExposureEvent: resolvedExposureEvent(experiment),
        }).event ?? EXPOSURE_DEFAULT_EVENT
    )
}

const AddBreakdownMenuItem = ({
    experiment,
    onChange,
}: {
    experiment: Experiment
    onChange: (breakdown: Breakdown) => void
}): JSX.Element => {
    const [dropdownOpen, setDropdownOpen] = useState(false)

    const exposureEvent = getExposureEvent(experiment)
    const metadataSource: EventsNode = {
        kind: NodeKind.EventsNode,
        event: exposureEvent,
    }
    const taxonomicGroupTypes = [TaxonomicFilterGroupType.EventProperties, TaxonomicFilterGroupType.PersonProperties]

    return (
        <LemonDropdown
            placement="left-start"
            overlay={
                <TaxonomicFilter
                    onChange={(group, value) => {
                        const breakdownType =
                            group.type === TaxonomicFilterGroupType.PersonProperties ? 'person' : 'event'
                        onChange({ type: breakdownType, property: value?.toString() || '' })
                        setDropdownOpen(false)
                    }}
                    taxonomicGroupTypes={taxonomicGroupTypes}
                    metadataSource={metadataSource}
                />
            }
            visible={dropdownOpen}
            onClickOutside={() => setDropdownOpen(false)}
        >
            <LemonButton size="small" fullWidth icon={<IconStack />} onClick={() => setDropdownOpen(!dropdownOpen)}>
                Add breakdown
            </LemonButton>
        </LemonDropdown>
    )
}

export const MetricHeader = ({
    dragHandle,
    displayOrder,
    metric,
    isPrimaryMetric,
    experiment,
    onDuplicateMetricClick,
    onDuplicateAsSingleUseMetricClick,
    onBreakdownChange,
    onDeleteMetricClick,
    readOnly,
}: {
    dragHandle?: JSX.Element | null
    displayOrder?: number
    metric: ExperimentMetric
    isPrimaryMetric: boolean
    experiment: Experiment
    onDuplicateMetricClick: (metric: ExperimentMetric) => void
    onDuplicateAsSingleUseMetricClick: (metric: ExperimentMetric) => void
    onBreakdownChange: (breakdown: Breakdown) => void
    onDeleteMetricClick?: (metric: ExperimentMetric) => void
    readOnly?: boolean
}): JSX.Element => {
    const { moveMetricsBetweenSections } = useActions(experimentLogic)
    const { openExperimentMetricModal } = useActions(experimentMetricModalLogic)
    const { openSharedMetricDetailModal } = useActions(sharedMetricDetailsModalLogic)

    const [menuVisible, setMenuVisible] = useState(false)
    const closeMenu = (): void => setMenuVisible(false)

    const isSharedMetric = !!metric.isSharedMetric && !!metric.sharedMetricId

    const openEditModal = (): void => {
        if (isSharedMetric) {
            openSharedMetricDetailModal(metric, METRIC_CONTEXTS[isPrimaryMetric ? 'primary' : 'secondary'])
            return
        }

        openExperimentMetricModal(METRIC_CONTEXTS[isPrimaryMetric ? 'primary' : 'secondary'], metric)
    }

    const handleDuplicate = (): void => {
        /**
         * For shared metrics we open the duplicate form
         * after a confirmation.
         */
        if (isSharedMetric) {
            LemonDialog.open({
                title: 'Duplicate this shared metric?',
                content: (
                    <div className="text-sm text-secondary max-w-lg deprecated-space-y-2">
                        <p>
                            <b>As a single-use metric</b> adds an editable copy to this experiment only. Other
                            experiments using the shared metric are unaffected.
                        </p>
                        <p>
                            <b>As a shared metric</b> takes you to the form to customize and save a new shared metric,
                            ready to be added to any experiment.
                        </p>
                    </div>
                ),
                primaryButton: {
                    children: 'Duplicate as single-use metric',
                    size: 'small',
                    onClick: () => onDuplicateAsSingleUseMetricClick(metric),
                },
                secondaryButton: {
                    children: 'Duplicate as shared metric',
                    to: urls.experimentsSharedMetric(metric.sharedMetricId!, 'duplicate'),
                    type: 'secondary',
                    size: 'small',
                },
                tertiaryButton: {
                    children: 'Cancel',
                    type: 'tertiary',
                    size: 'small',
                },
            })
            return
        }

        // regular metrics just get duplicated
        onDuplicateMetricClick(metric)
    }

    const handleDelete = (): void => {
        if (!onDeleteMetricClick) {
            return
        }

        const deleteLabel = isSharedMetric ? 'Remove from experiment' : 'Delete metric'
        const description = isSharedMetric
            ? 'This will remove the shared metric from this experiment. The shared metric itself will not be deleted.'
            : 'This will permanently remove this metric from the experiment. This action cannot be undone.'

        LemonDialog.open({
            title: isSharedMetric ? 'Remove this metric from the experiment?' : 'Delete this metric?',
            content: <div className="text-sm text-secondary max-w-lg">{description}</div>,
            primaryButton: {
                children: deleteLabel,
                status: 'danger',
                type: 'primary',
                size: 'small',
                onClick: () => onDeleteMetricClick(metric),
            },
            secondaryButton: {
                children: 'Cancel',
                type: 'tertiary',
                size: 'small',
            },
        })
    }

    const canAddBreakdown = (metric.breakdownFilter?.breakdowns || []).length < MAX_BREAKDOWNS

    const metricUuid = metric.uuid
    const sectionUuids =
        (isPrimaryMetric ? experiment.primary_metrics_ordered_uuids : experiment.secondary_metrics_ordered_uuids) ?? []
    // An experiment still has to measure something, so the last primary metric can't leave. Counted
    // from the metrics themselves, because the ordering array is a display hint that can hold stale uuids.
    const isLastPrimaryMetric = isPrimaryMetric && getSectionMetricUuids(experiment, false).length <= 1

    const handleMoveSection = (): void => {
        // The menu item only renders when the metric has a uuid.
        if (!metricUuid) {
            return
        }

        moveMetricsBetweenSections(isPrimaryMetric === false, sectionUuids, [], [metricUuid])
    }

    const { isMetricRecalculating, metricRetries } = useValues(experimentMetricsLogic({ experiment }))
    const showRecalculatingTag = isMetricRecalculating(metric.uuid)
    const metricRetry = metric.uuid ? metricRetries[metric.uuid] : undefined

    return (
        // The handle and the order number are their own columns, so the title and the tags below it
        // share one left edge instead of the tags starting back at the cell edge.
        <div className="text-xs font-semibold flex items-start gap-1">
            {dragHandle}
            {displayOrder !== undefined && <span className="flex-shrink-0">{displayOrder + 1}.</span>}
            <div className="flex flex-col flex-1 min-w-0 deprecated-space-y-1">
                <div className="flex items-start justify-between gap-2 min-w-0">
                    <div className="text-xs font-semibold flex items-start min-w-0 flex-1">
                        <div className="min-w-0 flex-1">
                            <MetricTitle metric={metric} />
                        </div>
                    </div>
                    {!readOnly && (
                        <div
                            className={clsx(
                                'flex flex-shrink-0 gap-1 transition-opacity',
                                menuVisible
                                    ? 'opacity-100'
                                    : 'opacity-0 group-hover/metric-cell:opacity-100 focus-within:opacity-100 pointer-coarse:opacity-100'
                            )}
                        >
                            <LemonButton
                                type="tertiary"
                                size="xsmall"
                                icon={<IconPencil />}
                                tooltip="Edit"
                                aria-label="Edit metric"
                                onClick={openEditModal}
                            />
                            <LemonMenu
                                placement="bottom-end"
                                visible={menuVisible}
                                onVisibilityChange={setMenuVisible}
                                closeOnClickInside={false}
                                items={
                                    [
                                        {
                                            items: [
                                                canAddBreakdown && {
                                                    label: () => (
                                                        <AddBreakdownMenuItem
                                                            experiment={experiment}
                                                            onChange={(breakdown) => {
                                                                onBreakdownChange(breakdown)
                                                                closeMenu()
                                                            }}
                                                        />
                                                    ),
                                                    custom: true,
                                                },
                                                {
                                                    label: 'Duplicate',
                                                    icon: <IconCopy />,
                                                    onClick: () => {
                                                        closeMenu()
                                                        handleDuplicate()
                                                    },
                                                },
                                                !!metricUuid && {
                                                    label: isPrimaryMetric
                                                        ? 'Make secondary metric'
                                                        : 'Make primary metric',
                                                    icon: <IconSort />,
                                                    disabledReason: isLastPrimaryMetric
                                                        ? 'An experiment needs at least one primary metric'
                                                        : undefined,
                                                    onClick: () => {
                                                        closeMenu()
                                                        handleMoveSection()
                                                    },
                                                },
                                            ].filter(Boolean) as any,
                                        },
                                        onDeleteMetricClick && {
                                            items: [
                                                {
                                                    label: isSharedMetric ? 'Remove from experiment' : 'Delete',
                                                    icon: <IconTrash />,
                                                    status: 'danger',
                                                    onClick: () => {
                                                        closeMenu()
                                                        handleDelete()
                                                    },
                                                },
                                            ],
                                        },
                                    ].filter(Boolean) as any
                                }
                            >
                                <LemonButton
                                    type="tertiary"
                                    size="xsmall"
                                    icon={<IconEllipsis />}
                                    tooltip="More actions"
                                    aria-label="More actions"
                                />
                            </LemonMenu>
                        </div>
                    )}
                </div>
                <div className="flex flex-wrap items-center gap-1">
                    {(showRecalculatingTag || metricRetry) &&
                        (metricRetry ? (
                            <LemonDropdown
                                placement="bottom-start"
                                showArrow
                                trigger="hover"
                                closeOnClickInside={false}
                                overlay={<MetricRetryDetails retry={metricRetry} className="max-w-100 p-2" />}
                            >
                                <LemonTag type="warning" size="medium" icon={<Spinner textColored />}>
                                    Retry {metricRetry.attempt} of {metricRetry.max_attempts}
                                </LemonTag>
                            </LemonDropdown>
                        ) : (
                            <LemonTag type="highlight" size="medium" icon={<Spinner textColored />}>
                                Recalculating
                            </LemonTag>
                        ))}
                    <MetricTypeTag metric={metric} />
                    {isMetricThresholdCueVisible(metric) && (
                        <Tooltip
                            title={`Reports the percentage of users whose value reaches or exceeds ${metric.threshold}.`}
                        >
                            <LemonTag type="muted" size="small" icon={<IconTarget />}>
                                ≥ {metric.threshold}
                            </LemonTag>
                        </Tooltip>
                    )}
                    {experiment.parameters?.prompt_metadata && (
                        <LemonTag type="completion" size="small">
                            LLM
                        </LemonTag>
                    )}
                    {metric.goal === 'decrease' && (
                        <LemonTag type="highlight" size="small">
                            Goal: Decrease
                        </LemonTag>
                    )}
                    {metric.isSharedMetric && (
                        <LemonTag type="option" size="small">
                            Shared
                        </LemonTag>
                    )}
                </div>
            </div>
        </div>
    )
}