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, kea, listeners, path, reducers, selectors } from 'kea'

import { TaxonomicFilterGroupType } from 'lib/components/TaxonomicFilter/types'

import { CORE_FILTER_DEFINITIONS_BY_GROUP } from '~/taxonomy/taxonomy'

/** Which namespace a pin is read from. The three the observed session can answer. */
export type PinnedPropertyType = 'session' | 'event' | 'person'

export interface PinnedProperty {
    key: string
    type: PinnedPropertyType
}

export const PINNED_PROPERTY_GROUPS: Record<PinnedPropertyType, TaxonomicFilterGroupType> = {
    session: TaxonomicFilterGroupType.SessionProperties,
    event: TaxonomicFilterGroupType.EventProperties,
    person: TaxonomicFilterGroupType.PersonProperties,
}

export function pinnedPropertyTypeForGroup(group: TaxonomicFilterGroupType): PinnedPropertyType | null {
    const entry = Object.entries(PINNED_PROPERTY_GROUPS).find(([, groupType]) => groupType === group)
    return entry ? (entry[0] as PinnedPropertyType) : null
}

/** Where the session came from leads, then who it was. That is the order these get compared in. */
export const DEFAULT_PINNED_PROPERTIES: PinnedProperty[] = [
    { key: '$entry_referring_domain', type: 'session' },
    { key: '$channel_type', type: 'session' },
    { key: '$entry_utm_source', type: 'session' },
    { key: '$geoip_country_code', type: 'event' },
]

/** Past this the strip stops being scannable, which is the only thing it is for. */
export const MAX_PINNED_PROPERTIES = 8

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface observationPinnedPropertiesLogicValues {
    pinnedProperties: PinnedProperty[]
    queryablePinnedProperties: PinnedProperty[]
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface observationPinnedPropertiesLogicActions {
    resetPinnedProperties: () => {}
    setPinnedProperties: (properties: PinnedProperty[]) => {
        properties: PinnedProperty[]
    }
    togglePropertyPin: (
        key: string,
        type: PinnedPropertyType
    ) => {
        key: string
        type: PinnedPropertyType
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface observationPinnedPropertiesLogicMeta {
    __keaTypeGenInternalSelectorTypes: {
        queryablePinnedProperties: (pinnedProperties: PinnedProperty[]) => PinnedProperty[]
    }
}

export type observationPinnedPropertiesLogicType = MakeLogicType<
    observationPinnedPropertiesLogicValues,
    observationPinnedPropertiesLogicActions,
    Record<string, any>,
    observationPinnedPropertiesLogicMeta
>

/**
 * Which properties the observation page shows in its pinned strip. Browser-local, so the picks
 * follow one person across scanners rather than being scoped to a scanner or shared with the team.
 */
export const observationPinnedPropertiesLogic = kea<observationPinnedPropertiesLogicType>([
    path(['products', 'replay_vision', 'frontend', 'observations', 'observationPinnedPropertiesLogic']),

    actions({
        setPinnedProperties: (properties: PinnedProperty[]) => ({ properties }),
        togglePropertyPin: (key: string, type: PinnedPropertyType) => ({ key, type }),
        resetPinnedProperties: () => ({}),
    }),

    reducers({
        pinnedProperties: [
            DEFAULT_PINNED_PROPERTIES,
            { persist: true },
            {
                setPinnedProperties: (_, { properties }) => properties.slice(0, MAX_PINNED_PROPERTIES),
                resetPinnedProperties: () => DEFAULT_PINNED_PROPERTIES,
            },
        ],
    }),

    selectors({
        queryablePinnedProperties: [
            (s) => [s.pinnedProperties],
            (pinnedProperties: PinnedProperty[]): PinnedProperty[] =>
                // The sessions table has a fixed schema, so an unknown column fails the whole query and
                // blanks every other pin with it. Event and person bags just read back null.
                pinnedProperties.filter(
                    (property) =>
                        property.type !== 'session' ||
                        Object.hasOwn(CORE_FILTER_DEFINITIONS_BY_GROUP.session_properties, property.key)
                ),
        ],
    }),

    listeners(({ actions, values }) => ({
        togglePropertyPin: ({ key, type }) => {
            const pinned = values.pinnedProperties
            const isPinned = pinned.some((property) => property.key === key && property.type === type)
            actions.setPinnedProperties(
                isPinned
                    ? pinned.filter((property) => !(property.key === key && property.type === type))
                    : [...pinned, { key, type }]
            )
        },
    })),
])