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.8 KB
Raw Blame
import { MakeLogicType, actions, afterMount, connect, kea, listeners, path, selectors } from 'kea'

import { productSetupStatusLogic } from 'lib/components/ProductEmptyState/productSetupStatusLogic'
import { ProductSetupStatus } from 'lib/components/ProductEmptyState/types'
import { teamLogic } from 'scenes/teamLogic'

import { isSharedView } from '~/exporter/exporterViewLogic'
import { ProductKey } from '~/queries/schema/schema-general'

import { dataWarehouseSetupLogic } from 'products/data_warehouse/frontend/emptyState/dataWarehouseSetupLogic'

import type { TeamPublicType, TeamType } from '../../../types'

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface sampleDataStateLogicValues {
    warehouseSetupStatus: ProductSetupStatus // productSetupStatusLogic
    currentTeam: TeamPublicType | TeamType | null // teamLogic
    needsWarehouseDetection: boolean
    sampleDataPending: boolean
    shouldShowSampleData: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface sampleDataStateLogicActions {
    syncWarehouseDetection: () => {
        value: true
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface sampleDataStateLogicMeta {
    __keaTypeGenInternalSelectorTypes: {
        needsWarehouseDetection: (currentTeam: TeamPublicType | TeamType | null) => boolean
        sampleDataPending: (needsWarehouseDetection: boolean, warehouseSetupStatus: ProductSetupStatus) => boolean
        shouldShowSampleData: (needsWarehouseDetection: boolean, warehouseSetupStatus: ProductSetupStatus) => boolean
    }
}

export type sampleDataStateLogicType = MakeLogicType<
    sampleDataStateLogicValues,
    sampleDataStateLogicActions,
    Record<string, any>,
    sampleDataStateLogicMeta
>

/**
 * Gates the sample-data placeholder on confirmed absence of events and warehouse setup.
 * The wizard setup status shown on the placeholder comes from `setupWizardStatusLogic`.
 */
export const sampleDataStateLogic = kea<sampleDataStateLogicType>([
    path(['scenes', 'insights', 'EmptyStates', 'sampleDataStateLogic']),
    connect(() => ({
        values: [
            teamLogic,
            ['currentTeam'],
            productSetupStatusLogic({ productKey: ProductKey.DATA_WAREHOUSE }),
            ['status as warehouseSetupStatus'],
        ],
    })),
    actions({ syncWarehouseDetection: true }),
    selectors({
        needsWarehouseDetection: [
            (s) => [s.currentTeam],
            (currentTeam: TeamPublicType | TeamType | null): boolean =>
                !isSharedView() && !!currentTeam && !currentTeam.ingested_event && !currentTeam.is_demo,
        ],
        sampleDataPending: [
            (s) => [s.needsWarehouseDetection, s.warehouseSetupStatus],
            (needsWarehouseDetection: boolean, warehouseSetupStatus: ProductSetupStatus): boolean =>
                needsWarehouseDetection && warehouseSetupStatus === 'loading',
        ],
        shouldShowSampleData: [
            (s) => [s.needsWarehouseDetection, s.warehouseSetupStatus],
            // Never show the placeholder in shared/embedded/exported views: `TeamPublicSerializer`
            // omits `ingested_event`, so it reads as `undefined` and would otherwise falsely flag
            // every empty tile on a real, data-carrying project as "never ingested". External
            // viewers should just see the normal "No data" state.
            (needsWarehouseDetection: boolean, warehouseSetupStatus: ProductSetupStatus): boolean =>
                needsWarehouseDetection && warehouseSetupStatus === 'needs-setup',
        ],
    }),
    listeners(({ actions, values, cache }) => ({
        syncWarehouseDetection: () => {
            if (values.needsWarehouseDetection) {
                if (cache.warehouseDetectionTeamId !== values.currentTeam?.id) {
                    cache.disposables.add(
                        () => {
                            const unmount = dataWarehouseSetupLogic.mount()
                            cache.warehouseDetectionTeamId = values.currentTeam?.id
                            return () => {
                                unmount()
                                cache.warehouseDetectionTeamId = null
                            }
                        },
                        'warehouseDetection',
                        { pauseOnPageHidden: false }
                    )
                }
            } else {
                cache.disposables.dispose('warehouseDetection')
            }
        },
        [teamLogic.actionTypes.loadCurrentTeamSuccess]: () => actions.syncWarehouseDetection(),
        [teamLogic.actionTypes.updateCurrentTeamSuccess]: () => actions.syncWarehouseDetection(),
    })),
    afterMount(({ actions }) => actions.syncWarehouseDetection()),
])