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.

8.7 KB
Raw Blame
import { MakeLogicType, connect, kea, path } from 'kea'
import { loaders } from 'kea-loaders'

import api from 'lib/api'
import { featureFlagLogic } from 'lib/logic/featureFlagLogic'
import { parseGithubRepoURL } from 'lib/utils/url'
import { organizationLogic } from 'scenes/organizationLogic'
import { teamLogic } from 'scenes/teamLogic'
import { userLogic } from 'scenes/userLogic'

import { HogFunctionType, PluginConfigTypeNew, PluginType } from '~/types'

import { sourceWizardLogic } from 'products/data_warehouse/frontend/scenes/NewSourceScene/sourceWizardLogic'

import type { SourceConfigResponseApi } from '../../../../../products/warehouse_sources/frontend/generated/api.schemas'
import type { FeatureFlagsSet } from '../../../lib/logic/featureFlagLogic'
import type { UserType } from '../../../types'

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface nonHogFunctionsLogicValues {
    featureFlags: FeatureFlagsSet // featureFlagLogic
    currentOrganizationId: string // organizationLogic
    connectors: SourceConfigResponseApi[] // sourceWizardLogic
    currentProjectId: number | string // teamLogic
    user: UserType | null // userLogic
    hogFunctionPluginsDestinations: HogFunctionType[] | null
    hogFunctionPluginsDestinationsLoading: boolean
    hogFunctionPluginsSiteApps: HogFunctionType[] | null
    hogFunctionPluginsSiteAppsLoading: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface nonHogFunctionsLogicActions {
    loadHogFunctionPluginsDestinations: () => any
    loadHogFunctionPluginsDestinationsFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadHogFunctionPluginsDestinationsSuccess: (
        hogFunctionPluginsDestinations: HogFunctionType[],
        payload?: any
    ) => {
        hogFunctionPluginsDestinations: HogFunctionType[]
        payload?: any
    }
    loadHogFunctionPluginsSiteApps: () => any
    loadHogFunctionPluginsSiteAppsFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadHogFunctionPluginsSiteAppsSuccess: (
        hogFunctionPluginsSiteApps: HogFunctionType[],
        payload?: any
    ) => {
        hogFunctionPluginsSiteApps: HogFunctionType[]
        payload?: any
    }
}

export type nonHogFunctionsLogicType = MakeLogicType<nonHogFunctionsLogicValues, nonHogFunctionsLogicActions>

export const nonHogFunctionsLogic = kea<nonHogFunctionsLogicType>([
    path((key) => ['scenes', 'data-pipelines', 'utils', 'nonHogFunctionsLogic', key]),

    connect(() => ({
        values: [
            sourceWizardLogic,
            ['connectors'],
            featureFlagLogic,
            ['featureFlags'],
            userLogic,
            ['user'],
            teamLogic,
            ['currentProjectId'],
            organizationLogic,
            ['currentOrganizationId'],
        ],
    })),

    loaders(({ values }) => ({
        hogFunctionPluginsDestinations: [
            null as HogFunctionType[] | null,
            {
                // NOTE: This is super temporary until we have fully migrated off of plugins
                loadHogFunctionPluginsDestinations: async () => {
                    const [pluginConfigs, plugins] = await Promise.all([
                        // nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. No generated function covers this endpoint yet. Find out why the generated client skips it (no schema, no product tag, or excluded from the spec) and fix that first.
                        api.loadPaginatedResults<PluginConfigTypeNew>(
                            `api/projects/${values.currentProjectId}/pipeline_destination_configs`
                        ),
                        // nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. No generated function covers this endpoint yet. Find out why the generated client skips it (no schema, no product tag, or excluded from the spec) and fix that first.
                        api.loadPaginatedResults<PluginType>(
                            `api/organizations/${values.currentOrganizationId}/pipeline_destinations`
                        ),
                    ])

                    const pluginsById = Object.fromEntries(plugins.map((plugin) => [plugin.id, plugin]))

                    const hogfunctions: HogFunctionType[] = []

                    for (const pluginConfig of pluginConfigs) {
                        const plugin = pluginsById[pluginConfig.plugin]

                        let iconUrl = plugin.icon ?? 'static/images/plugin-default.png'

                        try {
                            const { user, repo, path } = parseGithubRepoURL(plugin.url || '')
                            iconUrl = `https://raw.githubusercontent.com/${user}/${repo}/${path || 'main'}/logo.png`
                        } catch {
                            // Do nothing
                        }

                        hogfunctions.push({
                            id: `plugin-${pluginConfig.id}`,
                            name: pluginConfig.name || plugin?.name || 'Unknown app',
                            description: pluginConfig.description || plugin?.description || '',
                            type: 'destination',
                            created_by: null,
                            created_at: '',
                            updated_at: pluginConfig.updated_at,
                            enabled: pluginConfig.enabled,
                            execution_order: undefined,
                            hog: '',
                            icon_url: iconUrl,
                        })
                    }

                    return hogfunctions
                },
            },
        ],

        hogFunctionPluginsSiteApps: [
            null as HogFunctionType[] | null,
            {
                // NOTE: This is super temporary until we have fully migrated off of plugins
                loadHogFunctionPluginsSiteApps: async () => {
                    const [pluginConfigs, plugins] = await Promise.all([
                        // nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. No generated function covers this endpoint yet. Find out why the generated client skips it (no schema, no product tag, or excluded from the spec) and fix that first.
                        api.loadPaginatedResults<PluginConfigTypeNew>(
                            `api/projects/${values.currentProjectId}/pipeline_frontend_apps_configs`
                        ),
                        // nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. No generated function covers this endpoint yet. Find out why the generated client skips it (no schema, no product tag, or excluded from the spec) and fix that first.
                        api.loadPaginatedResults<PluginType>(
                            `api/organizations/${values.currentOrganizationId}/pipeline_frontend_apps`
                        ),
                    ])

                    const pluginsById = Object.fromEntries(plugins.map((plugin) => [plugin.id, plugin]))

                    const hogfunctions: HogFunctionType[] = []

                    for (const pluginConfig of pluginConfigs) {
                        const plugin = pluginsById[pluginConfig.plugin]

                        let iconUrl = plugin.icon ?? 'static/images/plugin-default.png'

                        try {
                            const { user, repo, path } = parseGithubRepoURL(plugin.url || '')
                            iconUrl = `https://raw.githubusercontent.com/${user}/${repo}/${path || 'main'}/logo.png`
                        } catch {
                            // Do nothing
                        }

                        hogfunctions.push({
                            id: `plugin-${pluginConfig.id}`,
                            name: pluginConfig.name || plugin?.name || 'Unknown app',
                            description: pluginConfig.description || plugin?.description || '',
                            type: 'destination',
                            created_by: null,
                            created_at: '',
                            updated_at: pluginConfig.updated_at,
                            enabled: pluginConfig.enabled,
                            execution_order: undefined,
                            hog: '',
                            icon_url: iconUrl,
                        })
                    }

                    return hogfunctions
                },
            },
        ],
    })),
])