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.5 KB
Raw Blame
import { MakeLogicType, actions, afterMount, connect, kea, key, listeners, path, props, reducers, selectors } from 'kea'
import type { DeepPartial } from 'kea-forms/lib/index'

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

import api from 'lib/api'

import { HogFunctionConfigurationType } from '~/types'

import type { HogFunctionType } from '../../../types'
import { hogFunctionConfigurationLogic } from '../configuration/hogFunctionConfigurationLogic'

export interface HogFunctionBackfillsLogicProps {
    id: string
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface hogFunctionBackfillsLogicValues {
    configuration: HogFunctionConfigurationType // hogFunctionConfigurationLogic
    hasTriedEnabling: boolean
    isLoading: boolean
    isReady: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface hogFunctionBackfillsLogicActions {
    loadHogFunction: () => any // hogFunctionConfigurationLogic
    loadHogFunctionSuccess: (
        hogFunction: HogFunctionType | null,
        payload?: any
    ) => {
        hogFunction: HogFunctionType | null
        payload?: any
    } // hogFunctionConfigurationLogic
    setConfigurationValues: (values: DeepPartial<HogFunctionConfigurationType>) => {
        values: DeepPartial<HogFunctionConfigurationType>
    } // hogFunctionConfigurationLogic
    enableHogFunctionBackfills: () => boolean
    setLoading: (loading: boolean) => {
        loading: boolean
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface hogFunctionBackfillsLogicMeta {
    key: string
    __keaTypeGenInternalSelectorTypes: {
        isReady: (configuration: HogFunctionConfigurationType, isLoading: boolean) => boolean
    }
}

export type hogFunctionBackfillsLogicType = MakeLogicType<
    hogFunctionBackfillsLogicValues,
    hogFunctionBackfillsLogicActions,
    HogFunctionBackfillsLogicProps,
    hogFunctionBackfillsLogicMeta
>

export const hogFunctionBackfillsLogic = kea<hogFunctionBackfillsLogicType>([
    props({} as HogFunctionBackfillsLogicProps),
    key(({ id }: HogFunctionBackfillsLogicProps) => id),
    path((key) => ['scenes', 'pipeline', 'hogFunctionBackfillsLogic', key]),
    connect((props: HogFunctionBackfillsLogicProps) => ({
        values: [hogFunctionConfigurationLogic(props), ['configuration']],
        actions: [
            hogFunctionConfigurationLogic(props),
            ['setConfigurationValues', 'loadHogFunction', 'loadHogFunctionSuccess'],
        ],
    })),
    actions({
        enableHogFunctionBackfills: () => true,
        setLoading: (loading: boolean) => ({ loading }),
    }),
    reducers({
        isLoading: [
            false,
            {
                setLoading: (_, { loading }) => loading,
            },
        ],
        hasTriedEnabling: [
            false,
            {
                enableHogFunctionBackfills: () => true,
            },
        ],
    }),
    selectors({
        isReady: [
            (s) => [s.configuration, s.isLoading],
            (configuration: HogFunctionConfigurationType, isLoading: boolean) => {
                return !!configuration.batch_export_id && !isLoading
            },
        ],
    }),
    listeners(({ actions, props, values }) => ({
        enableHogFunctionBackfills: async () => {
            try {
                actions.setLoading(true)
                await api.hogFunctions.enableBackfills(props.id)

                // Reload to get the updated config and render <BatchExportBackfills />
                actions.loadHogFunction()

                lemonToast.success('Backfills enabled for this destination.')
            } catch {
                lemonToast.error('Failed to enable backfills for this destination.')
            } finally {
                actions.setLoading(false)
            }
        },
        loadHogFunctionSuccess: () => {
            // Handle the case where config loads after this logic mounts.
            if (!values.configuration.batch_export_id && !values.hasTriedEnabling) {
                actions.enableHogFunctionBackfills()
            }
        },
    })),
    afterMount(({ actions, values }) => {
        // If hogFunctionConfigurationLogic has already loaded (e.g. user clicked
        // the Backfills tab after the page loaded), check immediately.
        if (values.configuration?.name && !values.configuration.batch_export_id && !values.hasTriedEnabling) {
            actions.enableHogFunctionBackfills()
        }
    }),
])