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.

2.4 KB
Raw Blame
import { MakeLogicType, afterMount, kea, path } from 'kea'
import { loaders } from 'kea-loaders'

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

import api from 'lib/api'

import { isSharedView } from '~/exporter/exporterViewLogic'

import { Variable } from '../../types'

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface variableDataLogicValues {
    variables: Variable[]
    variablesLoading: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface variableDataLogicActions {
    deleteVariable: (variableId: string) => string
    deleteVariableFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    deleteVariableSuccess: (
        variables: Variable[],
        payload?: string
    ) => {
        variables: Variable[]
        payload?: string
    }
    loadVariables: () => any
    loadVariablesFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadVariablesSuccess: (
        variables: Variable[],
        payload?: any
    ) => {
        variables: Variable[]
        payload?: any
    }
}

export type variableDataLogicType = MakeLogicType<variableDataLogicValues, variableDataLogicActions>

export const variableDataLogic = kea<variableDataLogicType>([
    path(['queries', 'nodes', 'DataVisualization', 'Components', 'Variables', 'variableDataLogic']),
    loaders(({ values }) => ({
        variables: [
            [] as Variable[],
            {
                loadVariables: async () => {
                    if (isSharedView()) {
                        return []
                    }
                    const insights = await api.insightVariables.list()
                    return insights.results
                },
                deleteVariable: async (variableId: string) => {
                    try {
                        await api.insightVariables.delete(variableId)
                        lemonToast.success('Variable deleted successfully')
                    } catch {
                        lemonToast.error('Failed to delete variable')
                    }
                    return values.variables.filter((variable) => variable.id !== variableId)
                },
            },
        ],
    })),
    afterMount(({ actions }) => {
        actions.loadVariables()
    }),
])