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.

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

import { dataNodeLogic } from '~/queries/nodes/DataNode/dataNodeLogic'
import { insightVizDataNodeKey } from '~/queries/nodes/InsightViz/InsightViz'
import { BIVisualizationNode } from '~/queries/schema/schema-business-intelligence'
import { DataVisualizationNode } from '~/queries/schema/schema-general'

import { buildBISourcePreview } from './biSourcePreview'

interface BISourcePreviewProps {
    tabId: string
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface biSourcePreviewLogicValues {
    filtered: boolean
    limit: number
    query: DataVisualizationNode | null
    worksheet: BIVisualizationNode | null
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface biSourcePreviewLogicActions {
    close: () => {
        value: true
    }
    loadMore: () => {
        value: true
    }
    open: (worksheet: BIVisualizationNode) => {
        worksheet: BIVisualizationNode
    }
    setFiltered: (filtered: boolean) => {
        filtered: boolean
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface biSourcePreviewLogicMeta {
    key: string
    __keaTypeGenInternalSelectorTypes: {
        query: (worksheet: BIVisualizationNode | null, filtered: boolean, limit: number) => DataVisualizationNode | null
    }
}

export type biSourcePreviewLogicType = MakeLogicType<
    biSourcePreviewLogicValues,
    biSourcePreviewLogicActions,
    BISourcePreviewProps,
    biSourcePreviewLogicMeta
>

export const biSourcePreviewLogic: LogicWrapper<biSourcePreviewLogicType> = kea<biSourcePreviewLogicType>([
    props({} as BISourcePreviewProps),
    key(({ tabId }) => tabId),
    path((key) => ['products', 'business_intelligence', 'biSourcePreviewLogic', key]),
    actions({
        open: (worksheet: BIVisualizationNode) => ({ worksheet }),
        close: true,
        setFiltered: (filtered: boolean) => ({ filtered }),
        loadMore: true,
    }),
    reducers({
        worksheet: [null as BIVisualizationNode | null, { open: (_, { worksheet }) => worksheet, close: () => null }],
        filtered: [false, { open: () => false, setFiltered: (_, { filtered }) => filtered }],
        limit: [
            1000,
            { open: () => 1000, setFiltered: () => 1000, loadMore: (limit) => Math.min(limit + 1000, 10000) },
        ],
    }),
    listeners(({ props }) => ({
        close: () =>
            dataNodeLogic
                .findMounted({
                    key: insightVizDataNodeKey({ dashboardItemId: `new-SQL-bi-source-preview-${props.tabId}` }),
                })
                ?.actions.cancelQuery(),
    })),
    selectors({
        query: [
            (s) => [s.worksheet, s.filtered, s.limit],
            (worksheet: BIVisualizationNode | null, filtered: boolean, limit: number): DataVisualizationNode | null =>
                worksheet ? buildBISourcePreview(worksheet, filtered, limit) : null,
        ],
    }),
])