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.

5.9 KB
Raw Blame
import {
    MakeLogicType,
    LogicWrapper,
    actions,
    connect,
    kea,
    key,
    listeners,
    path,
    props,
    reducers,
    selectors,
} from 'kea'

import { databaseTableListLogic } from 'scenes/data-management/database/databaseTableListLogic'
import type { TableFieldsStatus } from 'scenes/data-management/database/databaseTableListLogic'

import { BIConfig } from '~/queries/schema/schema-business-intelligence'
import type { DatabaseSchemaTable } from '~/queries/schema/schema-general'

import {
    buildBIConnections,
    filterBIConnections,
    getPendingBIConnectionTables,
} from 'products/business_intelligence/frontend/biConnectionTree'
import type { BIConnection } from 'products/business_intelligence/frontend/biConnectionTree'
import { biEditorLogic } from 'products/business_intelligence/frontend/biEditorLogic'

import { captureBIWorksheetAction } from './biEditorAnalytics'

export interface BIConnectionsLogicProps {
    tabId: string
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface biConnectionsLogicValues {
    config: BIConfig // biEditorLogic
    dataPaneSearch: string // biEditorLogic
    databaseConnectionId: string | null // biEditorLogic
    allTablesMap: Record<string, DatabaseSchemaTable> // databaseTableListLogic
    databaseFieldsComplete: boolean // databaseTableListLogic
    tableFieldsStatus: TableFieldsStatus // databaseTableListLogic
    connections: BIConnection[]
    expandedIds: string[]
    filteredConnections: BIConnection[]
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface biConnectionsLogicActions {
    resetConfig: () => {
        value: true
    } // biEditorLogic
    setDataSource: (source: import('~/queries/schema').BIDataSource) => {
        source: import('~/queries/schema').BIDataSource
    } // biEditorLogic
    hydrateTableFields: (tableNames: string[]) => {
        tableNames: string[]
    } // databaseTableListLogic
    hydrateTableFieldsSuccess: (
        tableNames: string[],
        tables: Record<string, DatabaseSchemaTable>
    ) => {
        tableNames: string[]
        tables: Record<string, DatabaseSchemaTable>
    } // databaseTableListLogic
    toggleConnection: (
        id: string,
        tableName?: string
    ) => {
        id: string
        tableName: string | undefined
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface biConnectionsLogicMeta {
    key: string
    __keaTypeGenInternalSelectorTypes: {
        connections: (
            config: BIConfig,
            allTablesMap: Record<string, DatabaseSchemaTable>,
            expandedIds: string[],
            tableFieldsStatus: TableFieldsStatus,
            databaseFieldsComplete: boolean,
            databaseConnectionId: string | null
        ) => BIConnection[]
        filteredConnections: (connections: BIConnection[], dataPaneSearch: string) => BIConnection[]
    }
}

export type biConnectionsLogicType = MakeLogicType<
    biConnectionsLogicValues,
    biConnectionsLogicActions,
    BIConnectionsLogicProps,
    biConnectionsLogicMeta
>

export const biConnectionsLogic: LogicWrapper<biConnectionsLogicType> = kea<biConnectionsLogicType>([
    props({} as BIConnectionsLogicProps),
    key((props) => props.tabId),
    path((key) => ['products', 'business_intelligence', 'frontend', 'biConnectionsLogic', key]),
    connect((props: BIConnectionsLogicProps) => ({
        values: [
            biEditorLogic({ tabId: props.tabId }),
            ['config', 'dataPaneSearch', 'databaseConnectionId'],
            databaseTableListLogic,
            ['allTablesMap', 'tableFieldsStatus', 'databaseFieldsComplete'],
        ],
        actions: [
            biEditorLogic({ tabId: props.tabId }),
            ['setDataSource', 'resetConfig'],
            databaseTableListLogic,
            ['hydrateTableFields', 'hydrateTableFieldsSuccess'],
        ],
    })),
    actions({ toggleConnection: (id: string, tableName?: string) => ({ id, tableName }) }),
    reducers({
        expandedIds: [
            [] as string[],
            {
                toggleConnection: (state, { id }) =>
                    state.includes(id) ? state.filter((key) => key !== id) : [...state, id],
                setDataSource: () => [],
                resetConfig: () => [],
            },
        ],
    }),
    selectors({
        connections: [
            (s) => [
                s.config,
                s.allTablesMap,
                s.expandedIds,
                s.tableFieldsStatus,
                s.databaseFieldsComplete,
                s.databaseConnectionId,
            ],
            (
                config: BIConfig,
                tables: Record<string, DatabaseSchemaTable>,
                expanded: string[],
                status: TableFieldsStatus,
                complete: boolean,
                connectionId: string | null
            ): BIConnection[] =>
                config.source && (config.source.connectionId ?? null) === connectionId
                    ? buildBIConnections(config.source, tables, expanded, status, complete)
                    : [],
        ],
        filteredConnections: [
            (s) => [s.connections, s.dataPaneSearch],
            (connections: BIConnection[], search: string): BIConnection[] => filterBIConnections(connections, search),
        ],
    }),
    listeners(({ actions, values }) => ({
        hydrateTableFieldsSuccess: () => {
            const tables = getPendingBIConnectionTables(values.connections)
            if (tables.length) {
                actions.hydrateTableFields(tables)
            }
        },
        toggleConnection: ({ id, tableName }) => {
            if (values.expandedIds.includes(id)) {
                captureBIWorksheetAction('related_table_expanded', values.config)
            }
            if (tableName && values.expandedIds.includes(id)) {
                actions.hydrateTableFields([tableName])
            }
        },
    })),
])