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.

6.9 KB
Raw Blame
import { MakeLogicType, actions, connect, kea, listeners, path, reducers, selectors } from 'kea'
import { actionToUrl, router, urlToAction } from 'kea-router'

import { SessionRecordingSidebarTab } from '~/types'

import { OverviewTab, overviewTabForProperty } from '../player-meta/overviewTabs'
import { getPropertyDisplayInfo } from '../player-meta/playerMetaLogic'
import { sessionRecordingPinnedPropertiesLogic } from '../player-meta/sessionRecordingPinnedPropertiesLogic'

const asSidebarTab = (value: unknown): SessionRecordingSidebarTab | null =>
    Object.values(SessionRecordingSidebarTab).includes(value as SessionRecordingSidebarTab)
        ? (value as SessionRecordingSidebarTab)
        : null

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface playerSidebarLogicValues {
    pinnedProperties: string[] // sessionRecordingPinnedPropertiesLogic
    activeTab: SessionRecordingSidebarTab
    autoTab: SessionRecordingSidebarTab | null
    defaultTab: SessionRecordingSidebarTab
    overviewTab: OverviewTab
    selectedTab: SessionRecordingSidebarTab | null
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface playerSidebarLogicActions {
    setAutoTab: (tab: SessionRecordingSidebarTab | null) => {
        tab: SessionRecordingSidebarTab | null
    }
    setDefaultTab: (tab: SessionRecordingSidebarTab) => {
        tab: SessionRecordingSidebarTab
    }
    setOverviewTab: (tab: OverviewTab) => {
        tab: OverviewTab
    }
    setTab: (tab: SessionRecordingSidebarTab) => {
        tab: SessionRecordingSidebarTab
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface playerSidebarLogicMeta {
    __keaTypeGenInternalSelectorTypes: {
        activeTab: (
            selectedTab: SessionRecordingSidebarTab | null,
            autoTab: SessionRecordingSidebarTab | null,
            defaultTab: SessionRecordingSidebarTab
        ) => SessionRecordingSidebarTab
    }
}

export type playerSidebarLogicType = MakeLogicType<
    playerSidebarLogicValues,
    playerSidebarLogicActions,
    Record<string, any>,
    playerSidebarLogicMeta
>

export const playerSidebarLogic = kea<playerSidebarLogicType>([
    path(() => ['scenes', 'session-recordings', 'player', 'playerSidebarLogic']),
    connect(() => ({
        values: [sessionRecordingPinnedPropertiesLogic, ['pinnedProperties']],
    })),

    actions(() => ({
        setTab: (tab: SessionRecordingSidebarTab) => ({ tab }),
        setDefaultTab: (tab: SessionRecordingSidebarTab) => ({ tab }),
        setAutoTab: (tab: SessionRecordingSidebarTab | null) => ({ tab }),
        setOverviewTab: (tab: OverviewTab) => ({ tab }),
    })),

    reducers(() => ({
        // The tab the viewer picked, or null while they haven't picked one.
        selectedTab: [null as SessionRecordingSidebarTab | null, { setTab: (_, { tab }) => tab }],
        // Where the sidebar lands until the viewer picks a tab. Hosts embedding the player set this
        // to whichever tab their surface is about — the experiment recordings tab wants Overview,
        // since that's where the experiment context lives.
        defaultTab: [
            SessionRecordingSidebarTab.INSPECTOR as SessionRecordingSidebarTab,
            { setDefaultTab: (_, { tab }) => tab },
        ],
        autoTab: [null as SessionRecordingSidebarTab | null, { setAutoTab: (_, { tab }) => tab }],
        // Persisted so the chosen Overview group carries over between recordings.
        overviewTab: ['session' as OverviewTab, { persist: true }, { setOverviewTab: (_, { tab }) => tab }],
    })),

    selectors({
        activeTab: [
            (s) => [s.selectedTab, s.autoTab, s.defaultTab],
            (
                selectedTab: SessionRecordingSidebarTab | null,
                autoTab: SessionRecordingSidebarTab | null,
                defaultTab: SessionRecordingSidebarTab
            ): SessionRecordingSidebarTab => selectedTab ?? autoTab ?? defaultTab,
        ],
    }),

    listeners(({ actions }) => ({
        // A newly pinned property may belong to another tab, so follow it there.
        [sessionRecordingPinnedPropertiesLogic.actionTypes.togglePropertyPin]: (
            { propertyKey }: { propertyKey: string },
            _breakpoint,
            _action,
            previousState
        ) => {
            const wasPinned = sessionRecordingPinnedPropertiesLogic.selectors.pinnedProperties(previousState)
            if (!wasPinned.includes(propertyKey)) {
                actions.setOverviewTab(
                    overviewTabForProperty(propertyKey, getPropertyDisplayInfo(propertyKey).propertyFilterType)
                )
            }
        },
    })),

    actionToUrl(() => ({
        // Only the viewer's own pick belongs in the URL. A host's default describes where the player
        // is embedded, not what the viewer chose, so `setDefaultTab` deliberately has no entry here.
        setTab: ({ tab }) => {
            const { currentLocation } = router.values
            return [
                currentLocation.pathname,
                {
                    ...currentLocation.searchParams,
                    sidebarTab: tab,
                },
                currentLocation.hashParams,
            ]
        },
    })),

    urlToAction(({ actions, values }) => ({
        '*': (_, searchParams, hashParams) => {
            // This matcher is `'*'`, so the sidebar sees every route, including the scenes that
            // keep their own `tab` — the experiment view, a group page. Holding its tab under a
            // name no other scene claims is what makes it safe to read here and safe to write
            // there: a host that reads `tab` never sees a value the sidebar put in it.
            const urlTab = asSidebarTab(searchParams.sidebarTab) ?? asSidebarTab(hashParams.sidebarTab)

            // Older links carry the tab as a bare `tab`. Reading one has to prove a recording is on
            // screen first, since two of this enum's values ('overview', 'sessions') are also tab
            // values on scenes this matcher sees.
            const isShowingRecording =
                Object.keys(searchParams).includes('sessionRecordingId') ||
                Object.keys(hashParams).includes('sessionRecordingId')
            const legacyTab = isShowingRecording
                ? (asSidebarTab(searchParams.tab) ?? asSidebarTab(hashParams.tab))
                : null

            // Compared against the viewer's pick rather than the active tab: a tab in the URL is an
            // explicit choice, and checking the active tab would skip recording it whenever a host
            // default happened to match — the choice would then evaporate once that host went away.
            const nextTab = urlTab ?? legacyTab
            if (nextTab && nextTab !== values.selectedTab) {
                actions.setTab(nextTab)
            }
        },
    })),
])