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.2 KB
Raw Blame
import { MakeLogicType, actions, connect, kea, key, listeners, path, props, reducers, selectors } from 'kea'
import { loaders } from 'kea-loaders'
import { subscriptions } from 'kea-subscriptions'

import api from 'lib/api'

import { ConditionalFormattingRule } from '~/queries/schema/schema-general'

import { dataVisualizationLogic } from '../../dataVisualizationLogic'
import { FORMATTING_TEMPLATES, FormattingTemplate } from '../../types'

export interface ConditionalFormattingLogicProps {
    rule: ConditionalFormattingRule
    key: string
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface conditionalFormattingLogicValues {
    hog: any[] | null
    hogLoading: boolean
    rule: ConditionalFormattingRule
    template: FormattingTemplate
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface conditionalFormattingLogicActions {
    updateConditionalFormattingRule: (
        rule: ConditionalFormattingRule,
        deleteRule?: boolean | undefined
    ) => {
        colorMode: string
        deleteRule: boolean | undefined
        rule: ConditionalFormattingRule
    } // dataVisualizationLogic
    compileHog: ({ hog }: any) => any
    compileHogFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    compileHogSuccess: (
        hog: any[],
        payload?: any
    ) => {
        hog: any[]
        payload?: any
    }
    deleteRule: () => {
        value: true
    }
    selectColor: (color: string) => {
        color: string
    }
    selectColumn: (columnName: string) => {
        columnName: string
    }
    selectTemplate: (templateId: string) => {
        templateId: string
    }
    updateBytecode: (bytecode: any[]) => {
        bytecode: any[]
    }
    updateInput: (input: string) => {
        input: string
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface conditionalFormattingLogicMeta {
    key: string
    __keaTypeGenInternalSelectorTypes: {
        template: (rule: ConditionalFormattingRule) => FormattingTemplate
    }
}

export type conditionalFormattingLogicType = MakeLogicType<
    conditionalFormattingLogicValues,
    conditionalFormattingLogicActions,
    ConditionalFormattingLogicProps,
    conditionalFormattingLogicMeta
>

export const conditionalFormattingLogic = kea<conditionalFormattingLogicType>([
    key((props) => props.rule.id),
    path(['queries', 'nodes', 'DataVisualization', 'Components', 'conditionalFormattingLogic']),
    props({ rule: { id: '' }, key: '' } as ConditionalFormattingLogicProps),
    connect(() => ({
        actions: [dataVisualizationLogic, ['updateConditionalFormattingRule']],
    })),
    actions({
        selectColumn: (columnName: string) => ({ columnName }),
        updateInput: (input: string) => ({ input }),
        selectTemplate: (templateId: string) => ({ templateId }),
        updateBytecode: (bytecode: any[]) => ({ bytecode }),
        selectColor: (color: string) => ({ color }),
        deleteRule: true,
    }),
    reducers(({ props }) => ({
        rule: [
            props.rule,
            {
                selectColumn: (state, { columnName }) => {
                    return { ...state, columnName }
                },
                updateInput: (state, { input }) => {
                    return { ...state, input }
                },
                selectTemplate: (state, { templateId }) => {
                    return { ...state, templateId }
                },
                updateBytecode: (state, { bytecode }) => {
                    return { ...state, bytecode }
                },
                selectColor: (state, { color }) => {
                    return { ...state, color }
                },
            },
        ],
    })),
    selectors({
        template: [
            (s) => [s.rule],
            (rule: ConditionalFormattingRule): FormattingTemplate => {
                const template = FORMATTING_TEMPLATES.find((n) => n.id === rule.templateId)
                if (!template) {
                    return FORMATTING_TEMPLATES[0]
                }

                return template
            },
        ],
    }),
    loaders({
        hog: [
            null as null | any[],
            {
                compileHog: async ({ hog }) => {
                    const res = await api.hog.create(hog)
                    return res.bytecode
                },
            },
        ],
    }),
    listeners(({ actions, values }) => ({
        compileHogSuccess: ({ hog }) => {
            actions.updateBytecode(hog)
        },
        deleteRule: () => {
            actions.updateConditionalFormattingRule(values.rule, true)
        },
    })),
    subscriptions(({ actions }) => ({
        template: (template: FormattingTemplate, oldTemplate: FormattingTemplate | undefined) => {
            actions.compileHog({ hog: template.hog })

            // If we've changed to a template with a disabled `input` field, then clear the input
            if (!oldTemplate?.hideInput && template.hideInput) {
                actions.updateInput('')
            }
        },
        rule: (rule: ConditionalFormattingRule) => {
            actions.updateConditionalFormattingRule(rule)
        },
    })),
])