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.

14.2 KB
Raw Blame
import { MakeLogicType, actions, connect, kea, key, path, props, reducers, selectors } from 'kea'
import { forms } from 'kea-forms'
import type { DeepPartial, DeepPartialMap, FieldName, ValidationErrorType } from 'kea-forms'
import posthog from 'posthog-js'

import { getSeriesColor } from 'lib/colors'

import {
    AxisSeries,
    DataVisualizationLogicProps,
    EmptyYAxisSeries,
    dataVisualizationLogic,
} from '../dataVisualizationLogic'
import type { AxisSeriesSettings } from '../dataVisualizationLogic'

export interface YSeriesLogicProps {
    series: AxisSeries<number | null>
    seriesIndex: number
    dataVisualizationProps: DataVisualizationLogicProps
}

export enum YSeriesSettingsTab {
    Formatting = 'formatting',
    Display = 'display',
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface ySeriesLogicValues {
    activeSettingsTab: YSeriesSettingsTab
    canOpenSettings: boolean
    display: {
        color: string
        description: string
        displayType: 'area' | 'auto' | 'bar' | 'line'
        excludeFromTotal: boolean
        label: string
        trendLine: boolean
        valueLabels: 'auto' | 'hide' | 'show'
        yAxisPosition: 'left' | 'right'
    }
    displayAllErrors: Record<string, any>
    displayChanged: boolean
    displayErrors: DeepPartialMap<
        {
            color: string
            description: string
            displayType: 'area' | 'auto' | 'bar' | 'line'
            excludeFromTotal: boolean
            label: string
            trendLine: boolean
            valueLabels: 'auto' | 'hide' | 'show'
            yAxisPosition: 'left' | 'right'
        },
        ValidationErrorType
    >
    displayHasErrors: boolean
    displayManualErrors: Record<string, any>
    displayTouched: boolean
    displayTouches: Record<string, boolean>
    displayValidationErrors: DeepPartialMap<
        {
            color: string
            description: string
            displayType: 'area' | 'auto' | 'bar' | 'line'
            excludeFromTotal: boolean
            label: string
            trendLine: boolean
            valueLabels: 'auto' | 'hide' | 'show'
            yAxisPosition: 'left' | 'right'
        },
        ValidationErrorType
    >
    formatting: {
        decimalPlaces: number | undefined
        prefix: string
        style: 'none' | 'number' | 'percent' | 'short'
        suffix: string
    }
    formattingAllErrors: Record<string, any>
    formattingChanged: boolean
    formattingErrors: DeepPartialMap<
        {
            decimalPlaces: number | undefined
            prefix: string
            style: 'none' | 'number' | 'percent' | 'short'
            suffix: string
        },
        ValidationErrorType
    >
    formattingHasErrors: boolean
    formattingManualErrors: Record<string, any>
    formattingTouched: boolean
    formattingTouches: Record<string, boolean>
    formattingValidationErrors: DeepPartialMap<
        {
            decimalPlaces: number | undefined
            prefix: string
            style: 'none' | 'number' | 'percent' | 'short'
            suffix: string
        },
        ValidationErrorType
    >
    isDisplaySubmitting: boolean
    isDisplayValid: boolean
    isFormattingSubmitting: boolean
    isFormattingValid: boolean
    isSettingsOpen: boolean
    showDisplayErrors: boolean
    showFormattingErrors: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface ySeriesLogicActions {
    updateSeriesIndex: (
        seriesIndex: number,
        columnName: string,
        settings?: AxisSeriesSettings | undefined
    ) => {
        columnName: string
        seriesIndex: number
        settings: AxisSeriesSettings | undefined
    } // dataVisualizationLogic
    resetDisplay: (values?: {
        color: string
        description: string
        displayType: 'area' | 'auto' | 'bar' | 'line'
        excludeFromTotal: boolean
        label: string
        trendLine: boolean
        valueLabels: 'auto' | 'hide' | 'show'
        yAxisPosition: 'left' | 'right'
    }) => {
        values?: {
            color: string
            description: string
            displayType: 'area' | 'auto' | 'bar' | 'line'
            excludeFromTotal: boolean
            label: string
            trendLine: boolean
            valueLabels: 'auto' | 'hide' | 'show'
            yAxisPosition: 'left' | 'right'
        }
    }
    resetFormatting: (values?: {
        decimalPlaces: number | undefined
        prefix: string
        style: 'none' | 'number' | 'percent' | 'short'
        suffix: string
    }) => {
        values?: {
            decimalPlaces: number | undefined
            prefix: string
            style: 'none' | 'number' | 'percent' | 'short'
            suffix: string
        }
    }
    setDisplayManualErrors: (errors: Record<string, any>) => {
        errors: Record<string, any>
    }
    setDisplayValue: (
        key: FieldName,
        value: any
    ) => {
        name: FieldName
        value: any
    }
    setDisplayValues: (
        values: DeepPartial<{
            color: string
            description: string
            displayType: 'area' | 'auto' | 'bar' | 'line'
            excludeFromTotal: boolean
            label: string
            trendLine: boolean
            valueLabels: 'auto' | 'hide' | 'show'
            yAxisPosition: 'left' | 'right'
        }>
    ) => {
        values: DeepPartial<{
            color: string
            description: string
            displayType: 'area' | 'auto' | 'bar' | 'line'
            excludeFromTotal: boolean
            label: string
            trendLine: boolean
            valueLabels: 'auto' | 'hide' | 'show'
            yAxisPosition: 'left' | 'right'
        }>
    }
    setFormattingManualErrors: (errors: Record<string, any>) => {
        errors: Record<string, any>
    }
    setFormattingValue: (
        key: FieldName,
        value: any
    ) => {
        name: FieldName
        value: any
    }
    setFormattingValues: (
        values: DeepPartial<{
            decimalPlaces: number | undefined
            prefix: string
            style: 'none' | 'number' | 'percent' | 'short'
            suffix: string
        }>
    ) => {
        values: DeepPartial<{
            decimalPlaces: number | undefined
            prefix: string
            style: 'none' | 'number' | 'percent' | 'short'
            suffix: string
        }>
    }
    setSettingsOpen: (open: boolean) => {
        open: boolean
    }
    setSettingsTab: (tab: YSeriesSettingsTab) => {
        tab: YSeriesSettingsTab
    }
    submitDisplay: () => {
        value: boolean
    }
    submitDisplayFailure: (
        error: Error,
        errors: Record<string, any>
    ) => {
        error: Error
        errors: Record<string, any>
    }
    submitDisplayRequest: (display: {
        color: string
        description: string
        displayType: 'area' | 'auto' | 'bar' | 'line'
        excludeFromTotal: boolean
        label: string
        trendLine: boolean
        valueLabels: 'auto' | 'hide' | 'show'
        yAxisPosition: 'left' | 'right'
    }) => {
        display: {
            color: string
            description: string
            displayType: 'area' | 'auto' | 'bar' | 'line'
            excludeFromTotal: boolean
            label: string
            trendLine: boolean
            valueLabels: 'auto' | 'hide' | 'show'
            yAxisPosition: 'left' | 'right'
        }
    }
    submitDisplaySuccess: (display: {
        color: string
        description: string
        displayType: 'area' | 'auto' | 'bar' | 'line'
        excludeFromTotal: boolean
        label: string
        trendLine: boolean
        valueLabels: 'auto' | 'hide' | 'show'
        yAxisPosition: 'left' | 'right'
    }) => {
        display: {
            color: string
            description: string
            displayType: 'area' | 'auto' | 'bar' | 'line'
            excludeFromTotal: boolean
            label: string
            trendLine: boolean
            valueLabels: 'auto' | 'hide' | 'show'
            yAxisPosition: 'left' | 'right'
        }
    }
    submitFormatting: () => {
        value: boolean
    }
    submitFormattingFailure: (
        error: Error,
        errors: Record<string, any>
    ) => {
        error: Error
        errors: Record<string, any>
    }
    submitFormattingRequest: (formatting: {
        decimalPlaces: number | undefined
        prefix: string
        style: 'none' | 'number' | 'percent' | 'short'
        suffix: string
    }) => {
        formatting: {
            decimalPlaces: number | undefined
            prefix: string
            style: 'none' | 'number' | 'percent' | 'short'
            suffix: string
        }
    }
    submitFormattingSuccess: (formatting: {
        decimalPlaces: number | undefined
        prefix: string
        style: 'none' | 'number' | 'percent' | 'short'
        suffix: string
    }) => {
        formatting: {
            decimalPlaces: number | undefined
            prefix: string
            style: 'none' | 'number' | 'percent' | 'short'
            suffix: string
        }
    }
    touchDisplayField: (key: string) => {
        key: string
    }
    touchFormattingField: (key: string) => {
        key: string
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface ySeriesLogicMeta {
    key: string
    __keaTypeGenInternalSelectorTypes: {
        canOpenSettings: (series: AxisSeries<number | null>) => boolean
    }
}

export type ySeriesLogicType = MakeLogicType<
    ySeriesLogicValues,
    ySeriesLogicActions,
    YSeriesLogicProps,
    ySeriesLogicMeta
>

export const ySeriesLogic = kea<ySeriesLogicType>([
    path(['queries', 'nodes', 'DataVisualization', 'Components', 'ySeriesLogic']),
    key((props) => `${props.series?.column?.name ?? 'new'}-${props.seriesIndex ?? 0}`),
    connect((props: YSeriesLogicProps) => ({
        actions: [dataVisualizationLogic(props.dataVisualizationProps), ['updateSeriesIndex']],
    })),
    props({ series: EmptyYAxisSeries } as YSeriesLogicProps),
    actions({
        setSettingsOpen: (open: boolean) => ({ open }),
        setSettingsTab: (tab: YSeriesSettingsTab) => ({ tab }),
    }),
    reducers({
        isSettingsOpen: [
            false as boolean,
            {
                setSettingsOpen: (_, { open }) => open,
            },
        ],
        activeSettingsTab: [
            YSeriesSettingsTab.Formatting as YSeriesSettingsTab,
            {
                setSettingsTab: (_state, { tab }) => tab,
            },
        ],
    }),
    selectors({
        canOpenSettings: [
            (_s, p) => [p.series],
            (series: AxisSeries<number | null>) => {
                return series !== EmptyYAxisSeries
            },
        ],
    }),
    forms(({ actions, props, values }) => ({
        formatting: {
            defaults: {
                prefix: props.series?.settings?.formatting?.prefix ?? '',
                suffix: props.series?.settings?.formatting?.suffix ?? '',
                style: props.series?.settings?.formatting?.style ?? 'none',
                decimalPlaces: props.series?.settings?.formatting?.decimalPlaces,
            },
            submit: async (format) => {
                actions.updateSeriesIndex(props.seriesIndex, props.series.column.name, {
                    formatting: {
                        prefix: format.prefix,
                        suffix: format.suffix,
                        style: format.style,
                        decimalPlaces: Number.isNaN(format.decimalPlaces) ? undefined : format.decimalPlaces,
                    },
                })
                if (values.formattingChanged) {
                    posthog.capture('sql column settings updated', {
                        settings_tab: 'formatting',
                        query_kind: props.dataVisualizationProps.query.kind,
                    })
                    // Clear the change flag, so reopening and closing without edits is not tracked again
                    actions.resetFormatting(format)
                }
                actions.setSettingsOpen(false)
            },
        },
        display: {
            defaults: {
                color: props.series?.settings?.display?.color ?? getSeriesColor(props.seriesIndex),
                label: props.series?.settings?.display?.label ?? '',
                description: props.series?.settings?.display?.description ?? '',
                trendLine: props.series?.settings?.display?.trendLine ?? false,
                excludeFromTotal: props.series?.settings?.display?.excludeFromTotal ?? false,
                valueLabels: props.series?.settings?.display?.valueLabels ?? 'auto',
                yAxisPosition: props.series?.settings?.display?.yAxisPosition ?? 'left',
                displayType: props.series?.settings?.display?.displayType ?? 'auto',
            },
            submit: async (display) => {
                const savedDisplay = props.series.settings?.display
                actions.updateSeriesIndex(props.seriesIndex, props.series.column.name, {
                    display: {
                        color: display.color,
                        label: display.label,
                        trendLine: display.trendLine,
                        ...(display.excludeFromTotal || savedDisplay?.excludeFromTotal !== undefined
                            ? { excludeFromTotal: display.excludeFromTotal }
                            : {}),
                        ...(display.valueLabels !== 'auto' || savedDisplay?.valueLabels !== undefined
                            ? { valueLabels: display.valueLabels }
                            : {}),
                        yAxisPosition: display.yAxisPosition,
                        displayType: display.displayType,
                    },
                })
                if (values.displayChanged) {
                    posthog.capture('sql column settings updated', {
                        settings_tab: 'display',
                        query_kind: props.dataVisualizationProps.query.kind,
                        has_description: Boolean(display.description.trim()),
                    })
                    // Clear the change flag, so reopening and closing without edits is not tracked again
                    actions.resetDisplay(display)
                }
                actions.setSettingsOpen(false)
            },
        },
    })),
])