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

import {
    TRACING_SCENE_VIEWER_ID,
    tracingFiltersLogic,
    TracingFiltersLogicProps,
} from 'products/tracing/frontend/tracingFiltersLogic'

import type { UniversalFiltersGroup } from '../../../../../frontend/src/types'
import { FacetSource, cycleFacetFilter } from './facets'

export interface FacetRailLogicProps {
    id: string
}

function toggleMembership(values: readonly string[] | undefined | null, value: string): string[] {
    const current = values ?? []
    return current.includes(value) ? current.filter((v) => v !== value) : [...current, value]
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface facetRailLogicValues {
    collapsedFacets: string[]
    facetNameSearch: string
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface facetRailLogicActions {
    setFilterGroup: (
        filterGroup: UniversalFiltersGroup,
        skipQuery?: boolean | undefined
    ) => {
        filterGroup: UniversalFiltersGroup
        skipQuery: boolean
    } // tracingFiltersLogic
    setServiceNames: (serviceNames: string[]) => {
        serviceNames: string[]
    } // tracingFiltersLogic
    setFacetNameSearch: (search: string) => {
        search: string
    }
    toggleFacetCollapsed: (facetKey: string) => {
        facetKey: string
    }
    toggleFacetValue: (
        source: FacetSource,
        value: string
    ) => {
        source: FacetSource
        value: string
    }
}

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

export type facetRailLogicType = MakeLogicType<
    facetRailLogicValues,
    facetRailLogicActions,
    FacetRailLogicProps,
    facetRailLogicMeta
>

// The rail is a lens over the existing filter model — facet toggles write straight back to
// tracingFiltersLogic's reducers, so URL sync, saved views, and the filter chips stay in step.
export const facetRailLogic = kea<facetRailLogicType>([
    path(['products', 'tracing', 'frontend', 'components', 'FacetRail', 'facetRailLogic']),
    props({ id: TRACING_SCENE_VIEWER_ID } as FacetRailLogicProps),
    key((props) => props.id),

    connect((props: FacetRailLogicProps) => ({
        actions: [
            tracingFiltersLogic({ id: props.id } as TracingFiltersLogicProps),
            ['setServiceNames', 'setFilterGroup'],
        ],
    })),

    actions({
        // Generic toggle: the rail is config-driven, so a single action writes a value into whichever
        // filter field/group the facet's source maps to (see FacetConfig.source).
        toggleFacetValue: (source: FacetSource, value: string) => ({ source, value }),
        toggleFacetCollapsed: (facetKey: string) => ({ facetKey }),
        // Free-text filter over the facet *fields* shown in the rail (not their values).
        setFacetNameSearch: (search: string) => ({ search }),
    }),

    reducers({
        facetNameSearch: [
            '',
            {
                setFacetNameSearch: (_, { search }) => search,
            },
        ],
        collapsedFacets: [
            [] as string[],
            { persist: true },
            {
                toggleFacetCollapsed: (state, { facetKey }) =>
                    state.includes(facetKey) ? state.filter((k) => k !== facetKey) : [...state, facetKey],
            },
        ],
    }),

    listeners(({ props, actions }) => ({
        toggleFacetValue: ({ source, value }) => {
            const { serviceNames, filterGroup } = tracingFiltersLogic({
                id: props.id,
            } as TracingFiltersLogicProps).values
            if (source.type === 'column') {
                if (source.column === 'service_name') {
                    // Service selection lives in the dedicated serviceNames field the span queries
                    // read, not in the filterGroup (see FilterGroupFacetSource).
                    actions.setServiceNames(toggleMembership(serviceNames, value))
                } else {
                    // Rebuilt (not passed through) so control flow narrows the column literal past
                    // 'service_name', matching cycleFacetFilter's FilterGroupFacetSource contract.
                    // A click cycles the value included → excluded → cleared.
                    actions.setFilterGroup(
                        cycleFacetFilter(filterGroup, { type: 'column', column: source.column }, value)
                    )
                }
            } else {
                actions.setFilterGroup(cycleFacetFilter(filterGroup, source, value))
            }
        },
    })),
])