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.5 KB
Raw Blame
import { MakeLogicType, afterMount, kea, key, path, props, propsChanged } from 'kea'
import { loaders } from 'kea-loaders'
import posthog from 'posthog-js'

import { hogql } from 'lib/utils/hogql'

import { hogqlQuery } from '~/queries/query'

import type { TablePreviewExpressionColumn } from './types'

export interface DatabaseTablePreviewLogicProps {
    logicKey: string
    tableName?: string
    limit?: number
    whereClause?: string | null
    expressionColumns?: TablePreviewExpressionColumn[]
}

const DEFAULT_LIMIT = 10

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface databaseTablePreviewLogicValues {
    previewData: Record<string, any>[]
    previewDataLoading: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface databaseTablePreviewLogicActions {
    loadPreviewData: () => any
    loadPreviewDataFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadPreviewDataSuccess: (
        previewData: {
            [k: string]: any
        }[],
        payload?: any
    ) => {
        previewData: {
            [k: string]: any
        }[]
        payload?: any
    }
}

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

export type databaseTablePreviewLogicType = MakeLogicType<
    databaseTablePreviewLogicValues,
    databaseTablePreviewLogicActions,
    DatabaseTablePreviewLogicProps,
    databaseTablePreviewLogicMeta
>

export const databaseTablePreviewLogic = kea<databaseTablePreviewLogicType>([
    path((logicKey) => ['lib', 'components', 'TablePreview', 'databaseTablePreviewLogic', logicKey]),
    props({} as DatabaseTablePreviewLogicProps),
    key((props) => props.logicKey),
    loaders(({ props }) => ({
        previewData: [
            [] as Record<string, any>[],
            {
                loadPreviewData: async () => {
                    if (!props.tableName) {
                        return []
                    }

                    const previewLimit = props.limit || DEFAULT_LIMIT
                    const trimmedWhereClause = props.whereClause?.trim()
                    const previewExpressionSelectClause =
                        props.expressionColumns && props.expressionColumns.length > 0
                            ? `, ${props.expressionColumns
                                  .map(({ expression, key }) =>
                                      String(hogql`${hogql.raw(expression)} AS ${hogql.identifier(key)}`)
                                  )
                                  .join(', ')}`
                            : ''

                    try {
                        const response = await hogqlQuery(
                            trimmedWhereClause
                                ? hogql`SELECT *${hogql.raw(previewExpressionSelectClause)} FROM ${hogql.identifier(props.tableName)} WHERE ${hogql.raw(trimmedWhereClause)} LIMIT ${previewLimit}`
                                : hogql`SELECT *${hogql.raw(previewExpressionSelectClause)} FROM ${hogql.identifier(props.tableName)} LIMIT ${previewLimit}`
                        )
                        return (response.results || []).map((row: any[]) =>
                            Object.fromEntries(
                                (response.columns || []).map((column: string, index: number) => [column, row[index]])
                            )
                        )
                    } catch (error) {
                        posthog.captureException(error)
                        return []
                    }
                },
            },
        ],
    })),
    afterMount(({ actions }) => {
        actions.loadPreviewData()
    }),
    propsChanged(({ actions, props }, oldProps) => {
        const previousWhereClause = oldProps.whereClause?.trim() || null
        const nextWhereClause = props.whereClause?.trim() || null
        const previousLimit = oldProps.limit || DEFAULT_LIMIT
        const nextLimit = props.limit || DEFAULT_LIMIT
        const previousExpressionColumns = JSON.stringify(oldProps.expressionColumns || [])
        const nextExpressionColumns = JSON.stringify(props.expressionColumns || [])

        if (
            props.tableName !== oldProps.tableName ||
            previousWhereClause !== nextWhereClause ||
            previousLimit !== nextLimit ||
            previousExpressionColumns !== nextExpressionColumns
        ) {
            actions.loadPreviewData()
        }
    }),
])