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.

4.1 KB
Raw Blame
import { MakeLogicType, actions, afterMount, kea, listeners, path, reducers, selectors } from 'kea'
import { loaders } from 'kea-loaders'

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

import api from 'lib/api'

import { Variable } from '~/queries/nodes/DataVisualization/types'

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

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

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface sqlVariablesLogicMeta {
    __keaTypeGenInternalSelectorTypes: {
        filteredVariables: (variables: Variable[], searchTerm: string) => Variable[]
    }
}

export type sqlVariablesLogicType = MakeLogicType<
    sqlVariablesLogicValues,
    sqlVariablesLogicActions,
    Record<string, any>,
    sqlVariablesLogicMeta
>

export const sqlVariablesLogic = kea<sqlVariablesLogicType>([
    path(['scenes', 'data-management', 'variables', 'sqlVariablesLogic']),
    actions({
        setSearchTerm: (searchTerm: string) => ({ searchTerm }),
        deleteVariable: (variableId: string) => ({ variableId }),
    }),
    reducers({
        searchTerm: [
            '' as string,
            {
                setSearchTerm: (_, { searchTerm }) => searchTerm,
            },
        ],
    }),
    loaders(({ values }) => ({
        variables: [
            [] as Variable[],
            {
                loadVariables: async () => {
                    const response = await api.insightVariables.list()
                    return response.results
                },
                deleteVariableSuccess: async ({ variableId }: { variableId: string }) => {
                    try {
                        await api.insightVariables.delete(variableId)
                        lemonToast.success('Variable deleted')
                        return values.variables.filter((variable: Variable) => variable.id !== variableId)
                    } catch {
                        lemonToast.error('Failed to delete variable')
                        return values.variables
                    }
                },
            },
        ],
    })),
    selectors({
        filteredVariables: [
            (s) => [s.variables, s.searchTerm],
            (variables: Variable[], searchTerm: string): Variable[] => {
                if (!searchTerm) {
                    return variables
                }
                const lowerSearchTerm = searchTerm.toLowerCase()
                return variables.filter(
                    (variable: Variable) =>
                        variable.name.toLowerCase().includes(lowerSearchTerm) ||
                        variable.code_name.toLowerCase().includes(lowerSearchTerm)
                )
            },
        ],
    }),
    listeners(({ actions }) => ({
        deleteVariable: ({ variableId }: { variableId: string }) => {
            actions.deleteVariableSuccess({ variableId })
        },
    })),
    afterMount(({ actions }) => {
        actions.loadVariables()
    }),
])