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 } from 'kea'
import { loaders } from 'kea-loaders'

import { ApiConfig, ApiError } from 'lib/api'
import { lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'

import {
    dataWarehouseDataQualityGatePartialUpdate,
    dataWarehouseDataQualityGateRetrieve,
} from 'products/data_warehouse/frontend/generated/api'
import type { DataQualityGateConfigApi } from 'products/data_warehouse/frontend/generated/api.schemas'

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface dataQualityGateLogicValues {
    gateConfig: DataQualityGateConfigApi | null
    gateConfigLoading: boolean
    gateReadable: boolean
    gateSaving: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface dataQualityGateLogicActions {
    loadGateConfig: () => any
    loadGateConfigFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadGateConfigSuccess: (
        gateConfig: DataQualityGateConfigApi,
        payload?: any
    ) => {
        gateConfig: DataQualityGateConfigApi
        payload?: any
    }
    setGateEnabled: (enabled: boolean) => {
        enabled: boolean
    }
    setGateReadable: (readable: boolean) => {
        readable: boolean
    }
}

export type dataQualityGateLogicType = MakeLogicType<dataQualityGateLogicValues, dataQualityGateLogicActions>

export const dataQualityGateLogic = kea<dataQualityGateLogicType>([
    path(['products', 'data_quality', 'frontend', 'dataQualityGateLogic']),
    actions({
        setGateEnabled: (enabled: boolean) => ({ enabled }),
        setGateReadable: (readable: boolean) => ({ readable }),
    }),
    loaders(() => ({
        gateConfig: [
            null as DataQualityGateConfigApi | null,
            {
                loadGateConfig: async () =>
                    await dataWarehouseDataQualityGateRetrieve(String(ApiConfig.getCurrentTeamId())),
            },
        ],
    })),
    reducers({
        gateConfig: {
            setGateEnabled: (_: DataQualityGateConfigApi | null, { enabled }: { enabled: boolean }) => ({
                gate_materialization_on_checks: enabled,
            }),
        },
        // A viewer, or a token without the warehouse scope, cannot read the setting at all.
        gateReadable: [
            true,
            {
                setGateReadable: (_, { readable }) => readable,
            },
        ],
        // In-flight lock for the PATCH, so the switch cannot start a second overlapping write.
        gateSaving: [
            false,
            {
                setGateEnabled: () => true,
                loadGateConfigSuccess: () => false,
                loadGateConfigFailure: () => false,
            },
        ],
    }),
    listeners(({ actions }) => ({
        loadGateConfigFailure: ({ errorObject }) => {
            if (errorObject instanceof ApiError && errorObject.status === 403) {
                actions.setGateReadable(false)
            }
        },
        setGateEnabled: async ({ enabled }) => {
            try {
                const updated = await dataWarehouseDataQualityGatePartialUpdate(String(ApiConfig.getCurrentTeamId()), {
                    gate_materialization_on_checks: enabled,
                })
                // Adopt the value the server stored, and release the in-flight lock, through the
                // loader's own success action so we do not re-enter this listener.
                actions.loadGateConfigSuccess(updated)
            } catch (error) {
                // Re-read the stored value rather than assuming the previous one; this also releases
                // the in-flight lock once the retrieve settles.
                actions.loadGateConfig()
                lemonToast.error(
                    (error instanceof ApiError && error.detail) || 'Could not change the setting. Try again.'
                )
            }
        },
    })),
    afterMount(({ actions, values }) => {
        if (!values.gateConfig) {
            actions.loadGateConfig()
        }
    }),
])