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.2 KB
Raw Blame
import { MakeLogicType, connect, events, kea, key, path, props, selectors } from 'kea'
import { loaders } from 'kea-loaders'

import { customFacetsLogic } from 'lib/components/FacetRail/customFacetsLogic'
import { teamLogic } from 'scenes/teamLogic'

import { UserFacetSettingsEntryApi } from 'products/platform_features/frontend/generated/api.schemas'
import { TRACING_SCENE_VIEWER_ID } from 'products/tracing/frontend/tracingFiltersLogic'

import { tracingSpansAttributesRetrieve } from '../../generated/api'
import { TracingSpansAttributesRetrieveAttributeType } from '../../generated/api.schemas'
import { FACETS, FacetConfig, buildCustomFacet, resolveFacets } from './facets'

export interface FacetPresenceLogicProps {
    id: string
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface facetPresenceLogicValues {
    customFacetEntries: UserFacetSettingsEntryApi[] // customFacetsLogic
    currentTeamId: number | null // teamLogic
    customFacets: FacetConfig[]
    presentResourceKeys: string[]
    presentResourceKeysLoading: boolean
    resolvedFacets: FacetConfig[]
    visibleFacets: FacetConfig[]
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface facetPresenceLogicActions {
    loadPresentResourceKeys: () => any
    loadPresentResourceKeysFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadPresentResourceKeysSuccess: (
        presentResourceKeys: string[],
        payload?: any
    ) => {
        presentResourceKeys: string[]
        payload?: any
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface facetPresenceLogicMeta {
    key: string
    __keaTypeGenInternalSelectorTypes: {
        resolvedFacets: (presentResourceKeys: string[]) => FacetConfig[]
        customFacets: (customFacetEntries: UserFacetSettingsEntryApi[]) => FacetConfig[]
        visibleFacets: (resolvedFacets: FacetConfig[], customFacets: FacetConfig[]) => FacetConfig[]
    }
}

export type facetPresenceLogicType = MakeLogicType<
    facetPresenceLogicValues,
    facetPresenceLogicActions,
    FacetPresenceLogicProps,
    facetPresenceLogicMeta
>

/**
 * Which facets the rail renders. The curated list in FACETS is config, not data: a resource-attribute
 * facet only earns a slot if the tenant emits its key (or an older spelling of it), so this probes the
 * emitted keys once on mount and resolves the list against them. Values and counts are fetched per
 * facet, by facetValuesLogic.
 */
export const facetPresenceLogic = kea<facetPresenceLogicType>([
    props({ id: TRACING_SCENE_VIEWER_ID } as FacetPresenceLogicProps),
    key((props) => props.id),
    path((key) => ['products', 'tracing', 'frontend', 'components', 'FacetRail', 'facetPresenceLogic', key]),

    connect(() => ({
        values: [teamLogic, ['currentTeamId'], customFacetsLogic({ product: 'tracing' }), ['customFacetEntries']],
    })),

    loaders(({ values }) => ({
        presentResourceKeys: [
            [] as string[],
            {
                // Which resource attribute keys the tenant emits — gates which curated facets render.
                // Reads the trace_attributes rollup keys-only; no date range parameter (server default window).
                loadPresentResourceKeys: async () => {
                    if (!values.currentTeamId) {
                        return []
                    }
                    const response = await tracingSpansAttributesRetrieve(String(values.currentTeamId), {
                        attribute_type: TracingSpansAttributesRetrieveAttributeType.SpanResourceAttribute,
                        limit: 100,
                    })
                    return response.results.map((r) => r.name)
                },
            },
        ],
    })),

    selectors({
        // Column facets always render; resource-attribute facets only when the tenant emits the key (or one
        // of its aliases, which resolution rewrites the facet onto). Custom facets skip that presence gate
        // entirely — the user picked them from a live taxonomic list, so they're known to exist already —
        // and render last, after every curated facet.
        // Intermediate so curated facet identities stay stable across custom-facet changes — a fresh
        // alias-resolved FacetConfig would re-render every virtualized value row (see RailFacet).
        resolvedFacets: [
            (s) => [s.presentResourceKeys],
            (presentResourceKeys: string[]): FacetConfig[] => resolveFacets(FACETS, presentResourceKeys),
        ],
        customFacets: [
            (s) => [s.customFacetEntries],
            (customFacetEntries: UserFacetSettingsEntryApi[]): FacetConfig[] =>
                customFacetEntries.map((entry) => buildCustomFacet(entry.key, entry.source_type)),
        ],
        visibleFacets: [
            (s) => [s.resolvedFacets, s.customFacets],
            (resolvedFacets: FacetConfig[], customFacets: FacetConfig[]): FacetConfig[] => [
                ...resolvedFacets,
                ...customFacets,
            ],
        ],
    }),

    events(({ actions }) => ({
        afterMount: () => actions.loadPresentResourceKeys(),
    })),
])