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.

5.8 KB
Raw Blame
import { useActions, useValues } from 'kea'

import { LemonButton, LemonModal } from '@posthog/lemon-ui'

import { DataColorToken } from 'lib/colors'
import { EntityFilterInfo } from 'lib/components/EntityFilterInfo'
import { LemonColorList } from 'lib/lemon-ui/LemonColor/LemonColorList'
import { dataThemeLogic } from 'scenes/dataThemeLogic'
import { insightLogic } from 'scenes/insights/insightLogic'
import { insightVizDataLogic } from 'scenes/insights/insightVizDataLogic'
import { formatBreakdownLabel } from 'scenes/insights/utils'
import { getResultSeries } from 'scenes/insights/utils/resultSeries'

import { cohortsModel } from '~/models/cohortsModel'
import { propertyDefinitionsModel } from '~/models/propertyDefinitionsModel'
import { ResultCustomizationBy } from '~/queries/schema/schema-general'
import { FlattenedFunnelStepByBreakdown } from '~/types'

import { trendsDataLogic } from 'products/product_analytics/frontend/insights/trends/trendsDataLogic'
import { IndexedTrendResult } from 'products/product_analytics/frontend/insights/trends/types'

import { resultCustomizationsModalLogic } from './resultCustomizationsModalLogic'

export function ResultCustomizationsModal(): JSX.Element | null {
    const { insightProps } = useValues(insightLogic)

    const { modalVisible, dataset, colorToken, resultCustomizationBy } = useValues(
        resultCustomizationsModalLogic(insightProps)
    )
    const { closeModal, setColorToken, clearColorToken, save } = useActions(
        resultCustomizationsModalLogic(insightProps)
    )

    const { isTrends, isFunnels, querySource } = useValues(insightVizDataLogic)

    const { getTheme } = useValues(dataThemeLogic)
    const theme = getTheme(querySource?.dataColorTheme)

    if (dataset == null || theme == null) {
        return null
    }

    return (
        <LemonModal
            data-attr="legend-entry-modal"
            isOpen={modalVisible}
            title="Customize result color"
            width={520}
            footer={
                <>
                    <LemonButton type="secondary" onClick={closeModal}>
                        Cancel
                    </LemonButton>
                    <LemonButton type="primary" onClick={save}>
                        Save customizations
                    </LemonButton>
                </>
            }
            onClose={closeModal}
        >
            <p>
                Query results can be customized to provide a more{' '}
                <strong>meaningful appearance for you and your team members</strong>. The customizations are also shown
                on dashboards.
            </p>
            {isTrends && (
                <TrendsInfo dataset={dataset as IndexedTrendResult} resultCustomizationBy={resultCustomizationBy} />
            )}
            {isFunnels && <FunnelsInfo dataset={dataset as FlattenedFunnelStepByBreakdown} />}

            <h3 className="l4 mt-2 mb-2">Color</h3>
            <LemonColorList
                colorTokens={Object.keys(theme) as DataColorToken[]}
                selectedColorToken={colorToken}
                onSelectColorToken={setColorToken}
                onClearColorToken={clearColorToken}
            />
        </LemonModal>
    )
}

type TrendsInfoProps = {
    dataset: IndexedTrendResult
    resultCustomizationBy: ResultCustomizationBy
}

function TrendsInfo({ dataset, resultCustomizationBy }: TrendsInfoProps): JSX.Element {
    const { allCohorts } = useValues(cohortsModel)
    const { formatPropertyValueForDisplay } = useValues(propertyDefinitionsModel)
    const { breakdownFilter, querySource } = useValues(insightVizDataLogic)
    const { insightProps } = useValues(insightLogic)
    const { actionNames } = useValues(trendsDataLogic(insightProps))

    // A formula row has no series node, so fall back to the row label.
    const seriesNode = getResultSeries(querySource, dataset, actionNames).node
    const seriesName = seriesNode ? (
        <EntityFilterInfo filter={seriesNode} allowWrap={true} showSingleName={true} />
    ) : (
        dataset.label
    )

    return (
        <>
            {dataset.breakdown_value ? (
                <p className="mb-2">
                    You are customizing the appearance of series <b>{seriesName}</b> for the breakdown{' '}
                    <b>
                        {formatBreakdownLabel(
                            dataset.breakdown_value,
                            breakdownFilter,
                            allCohorts.results,
                            formatPropertyValueForDisplay
                        )}
                    </b>
                    .
                </p>
            ) : (
                <p className="mb-2">
                    You are customizing the appearance of series <b>{seriesName}</b>.
                </p>
            )}

            <p>
                Results are assigned by{' '}
                {resultCustomizationBy === ResultCustomizationBy.Position ? (
                    <>
                        their <strong>rank</strong> in the dataset
                    </>
                ) : (
                    <>
                        their <strong>name</strong> in the dataset
                    </>
                )}
                . You can change this in insight settings.
            </p>
        </>
    )
}

type FunnelsInfoProps = {
    dataset: FlattenedFunnelStepByBreakdown
}

function FunnelsInfo({ dataset }: FunnelsInfoProps): JSX.Element {
    return (
        <>
            You are customizing the appearance of the{' '}
            {/* Pure-compare rows carry no breakdown value but customize the baseline color. */}
            {dataset.breakdown_value?.[0] === 'Baseline' || !dataset.breakdown_value ? (
                <b>Baseline</b>
            ) : (
                <>
                    <b>{dataset.breakdown_value?.[0]}</b> breakdown
                </>
            )}
            .
        </>
    )
}