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.

3.3 KB
Raw Blame
import { MakeLogicType, connect, kea, path, selectors } from 'kea'
import { router } from 'kea-router'

import { removeProjectIdIfPresent } from 'lib/utils/kea-router'
import { objectsEqual } from 'lib/utils/objects'
import { sceneLogic } from 'scenes/sceneLogic'
import { SceneConfig } from 'scenes/sceneTypes'

import { SidePanelSceneContext } from './types'
import { SIDE_PANEL_CONTEXT_KEY } from './types'

export const activityFiltersForScene = (sceneConfig: SceneConfig | null): SidePanelSceneContext | null => {
    if (sceneConfig?.activityScope) {
        // NOTE: - HACKY, we are just parsing the item_id from the url optimistically...
        const pathParts = removeProjectIdIfPresent(router.values.currentLocation.pathname).split('/')
        const item_id = pathParts[2]

        // Loose check for the item_id being a number, a short_id (8 chars) or a uuid
        if (item_id && (item_id.length === 8 || item_id.length === 36 || !isNaN(parseInt(item_id)))) {
            return { activity_scope: sceneConfig.activityScope, activity_item_id: item_id }
        }

        return { activity_scope: sceneConfig.activityScope }
    }
    return null
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface sidePanelContextLogicValues {
    sceneConfig: SceneConfig | null // sceneLogic
    sceneSidePanelContext: SidePanelSceneContext
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface sidePanelContextLogicMeta {
    __keaTypeGenInternalSelectorTypes: {
        sceneSidePanelContext: (sceneConfig: SceneConfig | null, arg: any) => SidePanelSceneContext
    }
}

export type sidePanelContextLogicType = MakeLogicType<
    sidePanelContextLogicValues,
    {},
    Record<string, any>,
    sidePanelContextLogicMeta
>

export const sidePanelContextLogic = kea<sidePanelContextLogicType>([
    path(['scenes', 'navigation', 'sidepanel', 'sidePanelContextLogic']),
    connect(() => ({
        values: [sceneLogic, ['sceneConfig']],
    })),

    selectors({
        sceneSidePanelContext: [
            (s) => [
                s.sceneConfig,
                // Similar to "breadcrumbs"
                (state, props) => {
                    const activeSceneLogic = sceneLogic.selectors.activeSceneLogic(state, props)
                    if (activeSceneLogic && SIDE_PANEL_CONTEXT_KEY in activeSceneLogic.selectors) {
                        const activeLoadedScene = sceneLogic.selectors.activeLoadedScene(state, props)
                        try {
                            return activeSceneLogic.selectors[SIDE_PANEL_CONTEXT_KEY](
                                state,
                                activeLoadedScene?.paramsToProps?.(activeLoadedScene?.sceneParams) || props
                            )
                        } catch {
                            // Scene logic may not be mounted yet, return null
                            return null
                        }
                    }
                    return null
                },
            ],
            (sceneConfig: SceneConfig | null, context): SidePanelSceneContext => {
                return {
                    ...context,
                    ...(!context?.activity_scope ? activityFiltersForScene(sceneConfig) : {}),
                }
            },
            { resultEqualityCheck: objectsEqual },
        ],
    }),
])