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.

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

import { lemonToast } from '@posthog/lemon-ui'

import api, { ApiError } from 'lib/api'
import { dayjs } from 'lib/dayjs'
import { fetchInsightsUsingVariable } from 'scenes/data-management/variables/insightsLoader'
import { teamLogic } from 'scenes/teamLogic'

import { InsightModel } from '~/types'

import type { HogQLVariable, Node } from '../../../../schema/schema-general'
import {
    BooleanVariable,
    DateVariable,
    ListVariable,
    NumberVariable,
    StringVariable,
    Variable,
    VariableType,
} from '../../types'
import { variableDataLogic } from './variableDataLogic'
import { variablesLogic } from './variablesLogic'

const DEFAULT_VARIABLE: StringVariable = {
    id: '',
    type: 'String',
    name: '',
    default_value: '',
    code_name: '',
}

export interface AddVariableLogicProps {
    key: string
}

const getDefaultVariableForType = (variableType: VariableType): Variable => {
    if (variableType === 'String') {
        return {
            id: '',
            type: 'String',
            name: '',
            default_value: '',
            code_name: '',
        } as StringVariable
    }

    if (variableType === 'Number') {
        return {
            id: '',
            type: 'Number',
            name: '',
            default_value: 0,
            code_name: '',
        } as NumberVariable
    }

    if (variableType === 'Boolean') {
        return {
            id: '',
            type: 'Boolean',
            name: '',
            default_value: false,
            code_name: '',
        } as BooleanVariable
    }

    if (variableType === 'List') {
        return {
            id: '',
            type: 'List',
            name: '',
            values: [],
            default_value: '',
            code_name: '',
            is_multi: false,
            values_query: null,
            values_query_connection_id: null,
        } as ListVariable
    }

    if (variableType === 'Date') {
        return {
            id: '',
            type: 'Date',
            name: '',
            default_value: dayjs().format('YYYY-MM-DD HH:mm:00'),
            code_name: '',
        } as DateVariable
    }

    throw new Error(`Unsupported variable type ${variableType}`)
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface variableModalLogicValues {
    currentTeamId: number | null // teamLogic
    insightsLoading: boolean
    insightsUsingVariable: InsightModel[]
    isModalOpen: boolean
    isSaving: boolean
    modalType: 'existing' | 'new'
    variable: Variable
    variableType: VariableType
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface variableModalLogicActions {
    loadVariables: () => any // variableDataLogic
    addVariable: (variable: HogQLVariable) => {
        variable: HogQLVariable
    } // variablesLogic
    updateInternalSelectedVariable: (variable: HogQLVariable) => {
        variable: HogQLVariable
    } // variablesLogic
    changeTypeExistingVariable: (variableType: VariableType) => {
        variableType: VariableType
    }
    closeModal: () => {
        value: true
    }
    openExistingVariableModal: (variable: Variable) => {
        variable: Variable
    }
    openNewVariableModal: (variableType: VariableType) => {
        variableType: VariableType
    }
    save: () => {
        value: true
    }
    saveFailure: () => {
        value: true
    }
    saveSuccess: () => {
        value: true
    }
    setInsightsLoading: (loading: boolean) => {
        loading: boolean
    }
    setInsightsUsingVariable: (insights: InsightModel[]) => {
        insights: InsightModel<Node<Record<string, any>>>[]
    }
    updateVariable: (variable: Variable) => {
        variable: Variable
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface variableModalLogicMeta {
    key: string
}

export type variableModalLogicType = MakeLogicType<
    variableModalLogicValues,
    variableModalLogicActions,
    AddVariableLogicProps,
    variableModalLogicMeta
>

export const variableModalLogic = kea<variableModalLogicType>([
    path(['queries', 'nodes', 'DataVisualization', 'Components', 'Variables', 'variableLogic']),
    props({ key: '' } as AddVariableLogicProps),
    key((props) => props.key),
    connect(() => ({
        values: [teamLogic, ['currentTeamId']],
        actions: [
            variableDataLogic,
            ['loadVariables'],
            variablesLogic,
            ['addVariable', 'updateInternalSelectedVariable'],
        ],
    })),
    actions({
        openNewVariableModal: (variableType: VariableType) => ({ variableType }),
        openExistingVariableModal: (variable: Variable) => ({ variable }),
        closeModal: true,
        updateVariable: (variable: Variable) => ({ variable }),
        save: true,
        saveSuccess: true,
        saveFailure: true,
        changeTypeExistingVariable: (variableType: VariableType) => ({ variableType }),
        setInsightsUsingVariable: (insights: InsightModel[]) => ({ insights }),
        setInsightsLoading: (loading: boolean) => ({ loading }),
    }),
    reducers({
        modalType: [
            'new' as 'new' | 'existing',
            {
                openNewVariableModal: () => 'new',
                openExistingVariableModal: () => 'existing',
            },
        ],
        variableType: [
            'string' as VariableType,
            {
                openNewVariableModal: (_, { variableType }) => variableType,
                openExistingVariableModal: (_, { variable }) => variable.type,
            },
        ],
        isModalOpen: [
            false as boolean,
            {
                openNewVariableModal: () => true,
                openExistingVariableModal: () => true,
                closeModal: () => false,
            },
        ],
        variable: [
            DEFAULT_VARIABLE as Variable,
            {
                openExistingVariableModal: (_, { variable }) => ({ ...variable }),
                openNewVariableModal: (_, { variableType }) => {
                    return getDefaultVariableForType(variableType)
                },
                updateVariable: (state, { variable }) =>
                    ({
                        ...state,
                        ...variable,
                    }) as Variable,
                closeModal: () => DEFAULT_VARIABLE,
                changeTypeExistingVariable: (state, { variableType }) => {
                    const defaultVariable = getDefaultVariableForType(variableType)
                    return {
                        ...defaultVariable,
                        id: state.id,
                        name: state.name,
                        code_name: state.code_name,
                    }
                },
            },
        ],
        insightsUsingVariable: [
            [] as InsightModel[],
            {
                setInsightsUsingVariable: (_, { insights }) => insights,
                closeModal: () => [],
            },
        ],
        insightsLoading: [
            false as boolean,
            {
                setInsightsLoading: (_, { loading }) => loading,
                openExistingVariableModal: () => true,
                closeModal: () => false,
            },
        ],
        isSaving: [
            false as boolean,
            {
                save: () => true,
                saveSuccess: () => false,
                saveFailure: () => false,
                closeModal: () => false,
            },
        ],
    }),
    listeners(({ values, actions }) => ({
        openExistingVariableModal: async ({ variable }) => {
            // Load insights that use this variable
            if (variable.id && values.currentTeamId) {
                actions.setInsightsLoading(true)
                try {
                    const matchingInsights = await fetchInsightsUsingVariable(values.currentTeamId, variable.id)
                    actions.setInsightsUsingVariable(matchingInsights)
                } catch {
                    // Error already handled by fetchInsightsUsingVariable
                } finally {
                    actions.setInsightsLoading(false)
                }
            }
        },
        save: async () => {
            try {
                if (values.modalType === 'new') {
                    const variable = await api.insightVariables.create(values.variable)
                    actions.addVariable({ variableId: variable.id, code_name: variable.code_name })
                } else {
                    const variable = await api.insightVariables.update(values.variable.id, values.variable)
                    if (values.variableType !== values.variable.type) {
                        actions.updateInternalSelectedVariable({
                            variableId: variable.id,
                            code_name: variable.code_name,
                        })
                    }
                }
                actions.loadVariables()
                actions.saveSuccess()
                actions.closeModal()
            } catch (e: any) {
                const error = e as ApiError
                actions.saveFailure()
                lemonToast.error(error.detail ?? error.message)
            }
        },
    })),
])