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.3 KB
Raw Blame
import { MakeLogicType, actions, kea, key, listeners, path, props, reducers } from 'kea'
import { loaders } from 'kea-loaders'

import { ApiConfig } from 'lib/api'

import { externalDataSourcesOauthAccountsRetrieve } from 'products/warehouse_sources/frontend/generated/api'
import type { IntegrationAccountApi } from 'products/warehouse_sources/frontend/generated/api.schemas'

const SEARCH_DEBOUNCE_MS = 300

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface integrationAccountsLogicValues {
    accounts: IntegrationAccountApi[]
    accountsError: string | null
    accountsLoaded: boolean
    accountsLoading: boolean
    search: string
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface integrationAccountsLogicActions {
    loadIntegrationAccounts: () => any
    loadIntegrationAccountsFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadIntegrationAccountsSuccess: (
        accounts: IntegrationAccountApi[],
        payload?: any
    ) => {
        accounts: IntegrationAccountApi[]
        payload?: any
    }
    setSearch: (search: string) => {
        search: string
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface integrationAccountsLogicProps {
    id: number
    sourceType: string
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface integrationAccountsLogicMeta {
    key: string
}

export type integrationAccountsLogicType = MakeLogicType<
    integrationAccountsLogicValues,
    integrationAccountsLogicActions,
    integrationAccountsLogicProps,
    integrationAccountsLogicMeta
>

export const integrationAccountsLogic = kea<integrationAccountsLogicType>([
    props({} as { id: number; sourceType: string }),
    key((props) => `${props.sourceType}/${props.id}`),
    path((key) => ['lib', 'integrations', 'integrationAccountsLogic', key]),

    actions({
        setSearch: (search: string) => ({ search }),
    }),

    loaders(({ props, values }) => ({
        accounts: [
            [] as IntegrationAccountApi[],
            {
                loadIntegrationAccounts: async () => {
                    const response = await externalDataSourcesOauthAccountsRetrieve(
                        String(ApiConfig.getCurrentProjectId()),
                        {
                            source_type: props.sourceType,
                            integration_id: props.id,
                            // Sources with large resource lists (e.g. GitHub repositories) filter server-side;
                            // small-list sources ignore it and the endpoint filters their returned list.
                            search: values.search || undefined,
                        }
                    )
                    return response.accounts
                },
            },
        ],
    })),

    reducers({
        search: ['', { setSearch: (_, { search }) => search }],
        // Whether a load has finished at least once, so the UI can distinguish "not fetched yet" from
        // "fetched and genuinely empty" without flashing an empty state on first paint.
        accountsLoaded: [
            false,
            {
                loadIntegrationAccountsSuccess: () => true,
                loadIntegrationAccountsFailure: () => true,
            },
        ],
        // Surface the backend's actionable 400 message (e.g. "reconnect the integration") instead of
        // falling back to a generic "no accounts" empty state. Cleared when a fresh load starts or succeeds.
        accountsError: [
            null as string | null,
            {
                loadIntegrationAccounts: () => null,
                loadIntegrationAccountsSuccess: () => null,
                loadIntegrationAccountsFailure: (_, { error, errorObject }) =>
                    errorObject?.data?.detail ?? errorObject?.detail ?? error ?? null,
            },
        ],
    }),

    listeners(({ actions }) => ({
        setSearch: async (_, breakpoint) => {
            // Debounce keystrokes into a single server-side query.
            await breakpoint(SEARCH_DEBOUNCE_MS)
            actions.loadIntegrationAccounts()
        },
    })),
])