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.

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

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

import api from 'lib/api'
import { ICONS } from 'lib/integrations/utils'

import { IntegrationKind, IntegrationType } from '~/types'

import type { InstallationStatusEnumApi } from '../../../../../products/integrations/frontend/generated/api.schemas'
import type { UserBasicType } from '../../../types'

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface organizationIntegrationsLogicValues {
    getOrganizationIntegrationsByKind: (
        kinds: (
            | 'apns'
            | 'apple-ads'
            | 'aws-redshift'
            | 'aws-s3'
            | 'azure-blob'
            | 'bing-ads'
            | 'clickup'
            | 'customerio-app'
            | 'customerio-track'
            | 'customerio-webhook'
            | 'databricks'
            | 'email'
            | 'firebase'
            | 'github'
            | 'gitlab'
            | 'google-ads'
            | 'google-analytics'
            | 'google-calendar'
            | 'google-cloud-service-account'
            | 'google-cloud-storage'
            | 'google-pubsub'
            | 'google-search-console'
            | 'google-sheets'
            | 'helpscout'
            | 'hubspot'
            | 'instagram'
            | 'intercom'
            | 'jira'
            | 'linear'
            | 'linkedin-ads'
            | 'meta-ads'
            | 'pardot'
            | 'pinterest-ads'
            | 'postgresql'
            | 'reddit-ads'
            | 's3-compatible'
            | 'salesforce'
            | 'slack'
            | 'snapchat'
            | 'snowflake'
            | 'stripe'
            | 'tiktok-ads'
            | 'twilio'
            | 'twitter-ads'
            | 'vercel'
            | 'youtube-analytics'
        )[]
    ) => IntegrationType[]
    organizationIntegrations: IntegrationType[] | null
    organizationIntegrationsLoading: boolean
    vercelIntegrations: IntegrationType[] | undefined
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface organizationIntegrationsLogicActions {
    deleteOrganizationIntegration: (id: IntegrationType['id']) => {
        id: number
    }
    loadOrganizationIntegrations: () => any
    loadOrganizationIntegrationsFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadOrganizationIntegrationsSuccess: (
        organizationIntegrations: {
            config: any
            created_at: string
            created_by?: UserBasicType | null | undefined
            display_name: string
            errors?: string | undefined
            files_write_requestable?: boolean | undefined
            icon_url: any
            id: number
            installation_shared?: boolean | null | undefined
            installation_status?: InstallationStatusEnumApi | null | undefined
            kind:
                | 'apns'
                | 'apple-ads'
                | 'aws-redshift'
                | 'aws-s3'
                | 'azure-blob'
                | 'bing-ads'
                | 'clickup'
                | 'customerio-app'
                | 'customerio-track'
                | 'customerio-webhook'
                | 'databricks'
                | 'email'
                | 'firebase'
                | 'github'
                | 'gitlab'
                | 'google-ads'
                | 'google-analytics'
                | 'google-calendar'
                | 'google-cloud-service-account'
                | 'google-cloud-storage'
                | 'google-pubsub'
                | 'google-search-console'
                | 'google-sheets'
                | 'helpscout'
                | 'hubspot'
                | 'instagram'
                | 'intercom'
                | 'jira'
                | 'linear'
                | 'linkedin-ads'
                | 'meta-ads'
                | 'pardot'
                | 'pinterest-ads'
                | 'postgresql'
                | 'reddit-ads'
                | 's3-compatible'
                | 'salesforce'
                | 'slack'
                | 'snapchat'
                | 'snowflake'
                | 'stripe'
                | 'tiktok-ads'
                | 'twilio'
                | 'twitter-ads'
                | 'vercel'
                | 'youtube-analytics'
            needs_webhook_reconnect?: boolean | null | undefined
        }[],
        payload?: any
    ) => {
        organizationIntegrations: {
            config: any
            created_at: string
            created_by?: UserBasicType | null | undefined
            display_name: string
            errors?: string | undefined
            files_write_requestable?: boolean | undefined
            icon_url: any
            id: number
            installation_shared?: boolean | null | undefined
            installation_status?: InstallationStatusEnumApi | null | undefined
            kind:
                | 'apns'
                | 'apple-ads'
                | 'aws-redshift'
                | 'aws-s3'
                | 'azure-blob'
                | 'bing-ads'
                | 'clickup'
                | 'customerio-app'
                | 'customerio-track'
                | 'customerio-webhook'
                | 'databricks'
                | 'email'
                | 'firebase'
                | 'github'
                | 'gitlab'
                | 'google-ads'
                | 'google-analytics'
                | 'google-calendar'
                | 'google-cloud-service-account'
                | 'google-cloud-storage'
                | 'google-pubsub'
                | 'google-search-console'
                | 'google-sheets'
                | 'helpscout'
                | 'hubspot'
                | 'instagram'
                | 'intercom'
                | 'jira'
                | 'linear'
                | 'linkedin-ads'
                | 'meta-ads'
                | 'pardot'
                | 'pinterest-ads'
                | 'postgresql'
                | 'reddit-ads'
                | 's3-compatible'
                | 'salesforce'
                | 'slack'
                | 'snapchat'
                | 'snowflake'
                | 'stripe'
                | 'tiktok-ads'
                | 'twilio'
                | 'twitter-ads'
                | 'vercel'
                | 'youtube-analytics'
            needs_webhook_reconnect?: boolean | null | undefined
        }[]
        payload?: any
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface organizationIntegrationsLogicMeta {
    __keaTypeGenInternalSelectorTypes: {
        vercelIntegrations: (organizationIntegrations: IntegrationType[] | null) => IntegrationType[] | undefined
        getOrganizationIntegrationsByKind: (
            organizationIntegrations: IntegrationType[] | null
        ) => (
            kinds: (
                | 'apns'
                | 'apple-ads'
                | 'aws-redshift'
                | 'aws-s3'
                | 'azure-blob'
                | 'bing-ads'
                | 'clickup'
                | 'customerio-app'
                | 'customerio-track'
                | 'customerio-webhook'
                | 'databricks'
                | 'email'
                | 'firebase'
                | 'github'
                | 'gitlab'
                | 'google-ads'
                | 'google-analytics'
                | 'google-calendar'
                | 'google-cloud-service-account'
                | 'google-cloud-storage'
                | 'google-pubsub'
                | 'google-search-console'
                | 'google-sheets'
                | 'helpscout'
                | 'hubspot'
                | 'instagram'
                | 'intercom'
                | 'jira'
                | 'linear'
                | 'linkedin-ads'
                | 'meta-ads'
                | 'pardot'
                | 'pinterest-ads'
                | 'postgresql'
                | 'reddit-ads'
                | 's3-compatible'
                | 'salesforce'
                | 'slack'
                | 'snapchat'
                | 'snowflake'
                | 'stripe'
                | 'tiktok-ads'
                | 'twilio'
                | 'twitter-ads'
                | 'vercel'
                | 'youtube-analytics'
            )[]
        ) => IntegrationType[]
    }
}

export type organizationIntegrationsLogicType = MakeLogicType<
    organizationIntegrationsLogicValues,
    organizationIntegrationsLogicActions,
    Record<string, any>,
    organizationIntegrationsLogicMeta
>

export const organizationIntegrationsLogic = kea<organizationIntegrationsLogicType>([
    path(['scenes', 'settings', 'organization', 'organizationIntegrationsLogic']),

    actions({
        deleteOrganizationIntegration: (id: IntegrationType['id']) => ({ id }),
    }),

    loaders(() => ({
        organizationIntegrations: [
            null as IntegrationType[] | null,
            {
                loadOrganizationIntegrations: async () => {
                    const res = await api.organizationIntegrations.list()

                    return res.results.map((integration) => {
                        return {
                            ...integration,
                            icon_url: ICONS[integration.kind],
                        }
                    })
                },
            },
        ],
    })),

    selectors({
        vercelIntegrations: [
            (s) => [s.organizationIntegrations],
            (organizationIntegrations: IntegrationType[] | null) => {
                return organizationIntegrations?.filter((x) => x.kind === 'vercel')
            },
        ],
        getOrganizationIntegrationsByKind: [
            (s) => [s.organizationIntegrations],
            (organizationIntegrations: IntegrationType[] | null) => {
                return (kinds: IntegrationKind[]) =>
                    organizationIntegrations?.filter((i) => kinds.includes(i.kind)) || []
            },
        ],
    }),

    listeners(({ actions, values }) => ({
        deleteOrganizationIntegration: ({ id }) => {
            const integration = values.organizationIntegrations?.find((x) => x.id === id)
            if (!integration) {
                return
            }

            LemonDialog.open({
                title: `Disconnect ${integration.kind} integration?`,
                description:
                    'This will remove the integration from your organization. PostHog resources configured to use this integration will stop working.',
                primaryButton: {
                    children: 'Yes, disconnect',
                    status: 'danger',
                    onClick: async () => {
                        try {
                            await api.organizationIntegrations.delete(id)
                            lemonToast.success('Integration disconnected.')
                            actions.loadOrganizationIntegrations()
                        } catch (error: any) {
                            lemonToast.error(error?.detail || 'Failed to disconnect integration. Please try again.')
                        }
                    },
                },
                secondaryButton: {
                    children: 'Cancel',
                },
            })
        },
    })),

    afterMount(({ actions }) => {
        actions.loadOrganizationIntegrations()
    }),
])