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.

12.3 KB
Raw Blame
import { MakeLogicType, actions, connect, kea, key, listeners, path, props, reducers, selectors } from 'kea'

import { DataColorToken } from 'lib/colors'
import { keyForInsightLogicProps } from 'lib/insights/sharedUtils'
import { insightVizDataLogic } from 'scenes/insights/insightVizDataLogic'
import {
    getFunnelDatasetKey,
    getFunnelResultCustomization,
    getTrendResultCustomization,
    getTrendResultCustomizationKey,
} from 'scenes/insights/utils'

import { ResultCustomizationBy, TrendsFilter } from '~/queries/schema/schema-general'
import { FlattenedFunnelStepByBreakdown, InsightLogicProps } from '~/types'

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

import type { DataColorTheme } from '../../../lib/colors'
import type { FunnelStepWithConversionMetrics } from '../../../types'
import type {
    FunnelsQuery,
    InsightFilter,
    LifecycleQuery,
    PathsQuery,
    PathsV2Query,
    ResultCustomizationByPosition,
    ResultCustomizationByValue,
    RetentionQuery,
    StickinessQuery,
    TrendsQuery,
    WebOverviewQuery,
    WebStatsTableQuery,
} from '../../schema/schema-general'

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface resultCustomizationsModalLogicValues {
    funnelsResultCustomizations: Record<string, ResultCustomizationByValue> | undefined // funnelDataLogic
    getFunnelsColorToken: (
        dataset: FlattenedFunnelStepByBreakdown | FunnelStepWithConversionMetrics
    ) => [DataColorTheme | null, DataColorToken | null] // funnelDataLogic
    insightFilter: InsightFilter | null | undefined // insightVizDataLogic
    isFunnels: boolean // insightVizDataLogic
    isStickiness: boolean // insightVizDataLogic
    isTrends: boolean // insightVizDataLogic
    querySource:
        | FunnelsQuery
        | LifecycleQuery
        | PathsQuery
        | PathsV2Query
        | RetentionQuery
        | StickinessQuery
        | TrendsQuery
        | WebOverviewQuery
        | WebStatsTableQuery
        | null // insightVizDataLogic
    getTrendsColorToken: (dataset: IndexedTrendResult) => [DataColorTheme | null, DataColorToken | null] // trendsDataLogic
    resultCustomizationBy: ResultCustomizationBy // trendsDataLogic
    trendsResultCustomizations:
        | Record<string, ResultCustomizationByValue>
        | Record<number, ResultCustomizationByPosition>
        | undefined // trendsDataLogic
    colorToken: DataColorToken | null
    colorTokenFromQuery: DataColorToken | null
    dataset: FlattenedFunnelStepByBreakdown | IndexedTrendResult | null
    localColorToken: DataColorToken | null
    localColorTokenTouched: boolean
    modalVisible: boolean
    resultCustomizations:
        | Record<string, ResultCustomizationByValue>
        | Record<number, ResultCustomizationByPosition>
        | null
        | undefined
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface resultCustomizationsModalLogicActions {
    updateInsightFilter: (insightFilter: InsightFilter) => {
        insightFilter: InsightFilter
    } // insightVizDataLogic
    clearColorToken: () => {
        value: true
    }
    closeModal: () => {
        value: true
    }
    openModal: (dataset: FlattenedFunnelStepByBreakdown | IndexedTrendResult) => {
        dataset: FlattenedFunnelStepByBreakdown | IndexedTrendResult
    }
    save: () => {
        value: true
    }
    setColorToken: (token: DataColorToken) => {
        token: DataColorToken
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface resultCustomizationsModalLogicMeta {
    key: string
    __keaTypeGenInternalSelectorTypes: {
        modalVisible: (dataset: FlattenedFunnelStepByBreakdown | IndexedTrendResult | null) => boolean
        colorToken: (
            localColorToken: DataColorToken | null,
            localColorTokenTouched: boolean,
            colorTokenFromQuery: DataColorToken | null
        ) => DataColorToken | null
        colorTokenFromQuery: (
            isTrends: boolean,
            isStickiness: boolean,
            isFunnels: boolean,
            getTrendsColorToken: (dataset: IndexedTrendResult) => [DataColorTheme | null, DataColorToken | null],
            getFunnelsColorToken: (
                dataset: FlattenedFunnelStepByBreakdown | FunnelStepWithConversionMetrics
            ) => [DataColorTheme | null, DataColorToken | null],
            dataset: FlattenedFunnelStepByBreakdown | IndexedTrendResult | null
        ) => DataColorToken | null
        resultCustomizations: (
            isTrends: boolean,
            isStickiness: boolean,
            isFunnels: boolean,
            trendsResultCustomizations:
                | Record<string, ResultCustomizationByValue>
                | Record<number, ResultCustomizationByPosition>
                | undefined,
            funnelsResultCustomizations: Record<string, ResultCustomizationByValue> | undefined
        ) =>
            | Record<string, ResultCustomizationByValue>
            | Record<number, ResultCustomizationByPosition>
            | null
            | undefined
    }
}

export type resultCustomizationsModalLogicType = MakeLogicType<
    resultCustomizationsModalLogicValues,
    resultCustomizationsModalLogicActions,
    InsightLogicProps,
    resultCustomizationsModalLogicMeta
>

export const resultCustomizationsModalLogic = kea<resultCustomizationsModalLogicType>([
    props({} as InsightLogicProps),
    key(keyForInsightLogicProps('new')),
    path((key) => ['scenes', 'insights', 'views', 'InsightsTable', 'resultCustomizationsModalLogic', key]),

    connect((props: InsightLogicProps) => ({
        values: [
            insightVizDataLogic,
            ['isTrends', 'isStickiness', 'isFunnels', 'insightFilter', 'querySource'],
            trendsDataLogic(props),
            ['resultCustomizationBy', 'resultCustomizations as trendsResultCustomizations', 'getTrendsColorToken'],
            funnelDataLogic(props),
            ['resultCustomizations as funnelsResultCustomizations', 'getFunnelsColorToken'],
        ],
        actions: [insightVizDataLogic, ['updateInsightFilter']],
    })),

    actions({
        openModal: (dataset: IndexedTrendResult | FlattenedFunnelStepByBreakdown) => ({ dataset }),
        closeModal: true,

        setColorToken: (token: DataColorToken) => ({ token }),
        clearColorToken: true,

        save: true,
    }),

    reducers({
        dataset: [
            null as IndexedTrendResult | FlattenedFunnelStepByBreakdown | null,
            {
                openModal: (_, { dataset }) => dataset,
                closeModal: () => null,
            },
        ],
        localColorToken: [
            null as DataColorToken | null,
            {
                setColorToken: (_, { token }) => token,
                clearColorToken: () => null,
                closeModal: () => null,
            },
        ],
        localColorTokenTouched: [
            false,
            {
                setColorToken: () => true,
                clearColorToken: () => true,
                closeModal: () => false,
            },
        ],
    }),

    selectors({
        modalVisible: [
            (s) => [s.dataset],
            (dataset: FlattenedFunnelStepByBreakdown | IndexedTrendResult | null): boolean => dataset !== null,
        ],
        colorToken: [
            (s) => [s.localColorToken, s.localColorTokenTouched, s.colorTokenFromQuery],
            (
                localColorToken: DataColorToken | null,
                localColorTokenTouched: boolean,
                colorTokenFromQuery: DataColorToken | null
            ): DataColorToken | null => (localColorTokenTouched ? localColorToken : colorTokenFromQuery),
        ],
        colorTokenFromQuery: [
            (s) => [s.isTrends, s.isStickiness, s.isFunnels, s.getTrendsColorToken, s.getFunnelsColorToken, s.dataset],
            (
                isTrends: boolean,
                isStickiness: boolean,
                isFunnels: boolean,
                getTrendsColorToken: (
                    dataset: IndexedTrendResult
                ) => [null | import('lib/colors').DataColorTheme, DataColorToken | null],
                getFunnelsColorToken: (
                    dataset: FlattenedFunnelStepByBreakdown | import('~/types').FunnelStepWithConversionMetrics
                ) => [null | import('lib/colors').DataColorTheme, DataColorToken | null],
                dataset: FlattenedFunnelStepByBreakdown | IndexedTrendResult | null
            ): DataColorToken | null => {
                if (!dataset) {
                    return null
                }

                if (isTrends || isStickiness) {
                    return getTrendsColorToken(dataset as IndexedTrendResult)[1]
                } else if (isFunnels) {
                    return getFunnelsColorToken(dataset as FlattenedFunnelStepByBreakdown)[1]
                }

                return null
            },
        ],
        resultCustomizations: [
            (s) => [
                s.isTrends,
                s.isStickiness,
                s.isFunnels,
                s.trendsResultCustomizations,
                s.funnelsResultCustomizations,
            ],
            (
                isTrends: boolean,
                isStickiness: boolean,
                isFunnels: boolean,
                trendsResultCustomizations:
                    | Record<string, import('~/queries/schema/schema-general').ResultCustomizationByValue>
                    | Record<number, import('~/queries/schema/schema-general').ResultCustomizationByPosition>
                    | undefined,
                funnelsResultCustomizations:
                    | Record<string, import('~/queries/schema/schema-general').ResultCustomizationByValue>
                    | undefined
            ) => {
                if (isTrends || isStickiness) {
                    return trendsResultCustomizations
                } else if (isFunnels) {
                    return funnelsResultCustomizations
                }

                return null
            },
        ],
    }),

    listeners(({ actions, values }) => ({
        save: () => {
            if (!values.localColorTokenTouched || values.dataset == null) {
                actions.closeModal()
                return
            }

            const color = values.localColorToken ?? undefined

            if (values.isTrends || values.isStickiness) {
                const resultCustomizationKey = getTrendResultCustomizationKey(
                    values.resultCustomizationBy,
                    values.dataset as IndexedTrendResult,
                    values.querySource
                )
                const resultCustomization = getTrendResultCustomization(
                    values.resultCustomizationBy,
                    values.dataset as IndexedTrendResult,
                    values.resultCustomizations,
                    values.querySource
                )
                actions.updateInsightFilter({
                    resultCustomizations: {
                        ...values.trendsResultCustomizations,
                        [resultCustomizationKey]: {
                            ...resultCustomization,
                            assignmentBy: values.resultCustomizationBy,
                            color,
                        },
                    },
                } as Partial<TrendsFilter>)
            }

            if (values.isFunnels) {
                const resultCustomizationKey = getFunnelDatasetKey(values.dataset as FlattenedFunnelStepByBreakdown)
                const resultCustomization = getFunnelResultCustomization(
                    values.dataset as FlattenedFunnelStepByBreakdown,
                    values.funnelsResultCustomizations
                )
                actions.updateInsightFilter({
                    resultCustomizations: {
                        ...values.funnelsResultCustomizations,
                        [resultCustomizationKey]: {
                            ...resultCustomization,
                            assignmentBy: ResultCustomizationBy.Value,
                            color,
                        },
                    },
                })
            }

            actions.closeModal()
        },
    })),
])