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

import { trackedActionToUrl } from 'lib/logic/scenes/trackedActionToUrl'
import { capitalizeFirstLetter } from 'lib/utils/strings'
import { sceneConfigurations } from 'scenes/scenes'
import { Scene } from 'scenes/sceneTypes'
import {
    SessionRecordingPlaylistLogicProps,
    sessionRecordingsPlaylistLogic,
} from 'scenes/session-recordings/playlist/sessionRecordingsPlaylistLogic'
import { urls } from 'scenes/urls'

import { aiConsentLogic } from '~/layout/ai/aiConsentLogic'
import { SIDE_PANEL_CONTEXT_KEY, SidePanelSceneContext } from '~/layout/navigation-3000/sidepanel/types'
import { ActivityScope, Breadcrumb, ReplayTabs } from '~/types'

export const SCENE_PLAYLIST_LOGIC_PROPS: SessionRecordingPlaylistLogicProps = {
    logicKey: 'scene',
    updateSearchParams: true,
}

export function replayUrlPointsSomewhere(searchParams: Record<string, any>, hashParams: Record<string, any>): boolean {
    return Object.keys(searchParams).length > 0 || Boolean(hashParams.sessionRecordingId)
}

export const humanFriendlyTabName = (tab: ReplayTabs): string => {
    switch (tab) {
        case ReplayTabs.Home:
            return 'Recordings'
        case ReplayTabs.Playlists:
            return 'Collections'
        case ReplayTabs.Settings:
            return 'Settings'
        default:
            return capitalizeFirstLetter(tab)
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface sessionReplaySceneLogicValues {
    dataProcessingAccepted: boolean // aiConsentLogic
    breadcrumbs: Breadcrumb[]
    pickSeekSessionId: string | null
    picksInList: boolean
    sidePanelContext: SidePanelSceneContext
    tab: ReplayTabs
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface sessionReplaySceneLogicActions {
    setPickSeekSessionId: (sessionId: string | null) => {
        sessionId: string | null
    }
    setTab: (tab?: ReplayTabs) => {
        tab: ReplayTabs
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface sessionReplaySceneLogicMeta {
    __keaTypeGenInternalSelectorTypes: {
        picksInList: (dataProcessingAccepted: boolean) => boolean
        breadcrumbs: (tab: ReplayTabs) => Breadcrumb[]
        sidePanelContext: (searchParams: Record<string, any>) => SidePanelSceneContext
    }
}

export type sessionReplaySceneLogicType = MakeLogicType<
    sessionReplaySceneLogicValues,
    sessionReplaySceneLogicActions,
    Record<string, any>,
    sessionReplaySceneLogicMeta
>

export const sessionReplaySceneLogic = kea<sessionReplaySceneLogicType>([
    path(() => ['scenes', 'session-recordings', 'sessionReplaySceneLogic']),
    connect(() => ({
        values: [aiConsentLogic, ['dataProcessingAccepted']],
    })),
    actions({
        setTab: (tab: ReplayTabs = ReplayTabs.Home) => ({ tab }),
        setPickSeekSessionId: (sessionId: string | null) => ({ sessionId }),
    }),
    reducers(() => ({
        tab: [
            ReplayTabs.Home as ReplayTabs,
            {
                setTab: (_, { tab }) => tab,
            },
        ],
        pickSeekSessionId: [
            null as string | null,
            {
                setPickSeekSessionId: (_, { sessionId }) => sessionId,
            },
        ],
    })),

    listeners(({ actions, values }) => ({
        [sessionRecordingsPlaylistLogic(SCENE_PLAYLIST_LOGIC_PROPS).actionTypes.setSelectedRecordingId]: ({
            id,
        }: {
            id: string | null
        }) => {
            if (!values.pickSeekSessionId || id === values.pickSeekSessionId) {
                return
            }
            actions.setPickSeekSessionId(null)
            const { location, searchParams, hashParams } = router.values
            if (searchParams.t === undefined) {
                return
            }
            const nextParams = { ...searchParams }
            delete nextParams.t
            router.actions.replace(location.pathname, nextParams, hashParams)
        },
    })),

    trackedActionToUrl(({ values }) => {
        return {
            setTab: () => [urls.replay(values.tab), router.values.searchParams, router.values.hashParams],
        }
    }),

    selectors(() => ({
        picksInList: [
            (s) => [s.dataProcessingAccepted],
            (dataProcessingAccepted: boolean): boolean => dataProcessingAccepted,
        ],
        breadcrumbs: [
            (s) => [s.tab],
            (tab: ReplayTabs): Breadcrumb[] => {
                const breadcrumbs: Breadcrumb[] = []
                if (tab !== ReplayTabs.Home) {
                    breadcrumbs.push({
                        key: Scene.Replay,
                        name: 'Replay',
                        path: urls.replay(),
                        iconType: sceneConfigurations[Scene.Replay].iconType || 'default_icon_type',
                    })
                }
                breadcrumbs.push({
                    key: tab,
                    name: humanFriendlyTabName(tab),
                    iconType: sceneConfigurations[Scene.Replay].iconType || 'default_icon_type',
                })

                return breadcrumbs
            },
        ],
        [SIDE_PANEL_CONTEXT_KEY]: [
            () => [router.selectors.searchParams],
            (searchParams: Record<string, any>): SidePanelSceneContext => {
                return searchParams.sessionRecordingId
                    ? {
                          activity_scope: ActivityScope.REPLAY,
                          activity_item_id: searchParams.sessionRecordingId,
                      }
                    : {
                          activity_scope: ActivityScope.REPLAY,
                      }
            },
        ],
    })),

    urlToAction(({ actions, values }) => {
        return {
            '/replay/:tab': ({ tab }) => {
                // we saw a page get stuck in a redirect loop between recent and home
                // so, we're extra careful that the value being set is a valid tab
                const candidateTab = tab as ReplayTabs
                const validTab = Object.values(ReplayTabs).includes(candidateTab) ? candidateTab : ReplayTabs.Home
                if (validTab !== values.tab) {
                    actions.setTab(validTab)
                }
            },
        }
    }),
])