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.

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

import { closeTraceDialogUnderModal } from 'lib/components/ViewTraceButton/traceDialogOpenLogic'

import { SessionRecordingId, SessionRecordingType } from '~/types'

interface HashParams {
    sessionRecordingId?: SessionRecordingId
}

interface QueryParams {
    timestamp?: number
}

export interface HeatmapBackgroundSelectionContext {
    type: 'heatmap-background-selection'
    targetUrl: string
    matchingRecordingCount: number
}

export type SessionPlayerModalContext = HeatmapBackgroundSelectionContext | null

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface sessionPlayerModalLogicValues {
    activeSessionRecording: Pick<SessionRecordingType, 'id' | 'matching_events'> | null
    initialTimestamp: number | null
    modalContext: SessionPlayerModalContext
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface sessionPlayerModalLogicActions {
    closeSessionPlayer: () => {
        value: true
    }
    completeHeatmapBackgroundSelection: (storageKey: string) => {
        storageKey: string
    }
    openSessionPlayer: (
        sessionRecording: Pick<SessionRecordingType, 'id' | 'matching_events'>,
        initialTimestamp?: number | null,
        modalContext?: SessionPlayerModalContext
    ) => {
        initialTimestamp: number | null
        modalContext: SessionPlayerModalContext
        sessionRecording: Pick<SessionRecordingType, 'id' | 'matching_events'>
    }
}

export type sessionPlayerModalLogicType = MakeLogicType<sessionPlayerModalLogicValues, sessionPlayerModalLogicActions>

export const sessionPlayerModalLogic = kea<sessionPlayerModalLogicType>([
    path(['scenes', 'session-recordings', 'sessionPlayerModalLogic']),
    actions({
        openSessionPlayer: (
            sessionRecording: Pick<SessionRecordingType, 'id' | 'matching_events'>,
            initialTimestamp: number | null = null,
            modalContext: SessionPlayerModalContext = null
        ) => ({
            sessionRecording,
            initialTimestamp,
            modalContext,
        }),
        closeSessionPlayer: true,
        completeHeatmapBackgroundSelection: (storageKey: string) => ({ storageKey }),
    }),
    reducers({
        activeSessionRecording: [
            null as Pick<SessionRecordingType, 'id' | 'matching_events'> | null,
            {
                openSessionPlayer: (_, { sessionRecording }) => sessionRecording,
                closeSessionPlayer: () => null,
            },
        ],
        initialTimestamp: [
            null as number | null,
            {
                openSessionPlayer: (_, { initialTimestamp }) => initialTimestamp,
                closeSessionPlayer: () => null,
            },
        ],
        modalContext: [
            null as SessionPlayerModalContext,
            {
                openSessionPlayer: (_, { modalContext }) => modalContext,
                closeSessionPlayer: () => null,
            },
        ],
    }),
    listeners(() => ({
        openSessionPlayer: () => {
            closeTraceDialogUnderModal()
        },
    })),
    actionToUrl(({ values }) => {
        const buildURL = (): [
            string,
            Record<string, any>,
            Record<string, any>,
            {
                replace: boolean
            },
        ] => {
            const hashParams: HashParams = {
                ...router.values.hashParams,
            }
            const searchParams: QueryParams = {
                ...router.values.searchParams,
            }

            if (!values.activeSessionRecording?.id) {
                delete hashParams.sessionRecordingId
            } else {
                hashParams.sessionRecordingId = values.activeSessionRecording.id
            }

            if (!values.initialTimestamp) {
                delete searchParams.timestamp
            } else {
                searchParams.timestamp = values.initialTimestamp
            }

            return [router.values.location.pathname, searchParams, hashParams, { replace: true }]
        }

        return {
            openSessionPlayer: () => buildURL(),
            closeSessionPlayer: () => buildURL(),
        }
    }),
    urlToAction(({ actions, values }) => {
        const urlToAction = (_: any, searchParams: QueryParams, hashParams: HashParams): void => {
            // Check if the logic is still mounted. Because this is called on every URL change, the logic might have been unmounted already.
            if (sessionPlayerModalLogic.isMounted()) {
                const nulledSessionRecordingId = hashParams.sessionRecordingId ?? null
                const initialTimestamp = searchParams.timestamp ?? null
                if (nulledSessionRecordingId && nulledSessionRecordingId !== values.activeSessionRecording?.id) {
                    actions.openSessionPlayer({ id: nulledSessionRecordingId }, initialTimestamp)
                }
            }
        }
        return {
            '*': urlToAction,
        }
    }),
])