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.5 KB
Raw Blame
import { MakeLogicType, actions, connect, kea, key, listeners, path, props } from 'kea'
import { loaders } from 'kea-loaders'
import posthog from 'posthog-js'

import api from 'lib/api'
import { preloadRichContent } from 'lib/components/RichContentEditor/LazyRichContent'
import { lemonToast } from 'lib/lemon-ui/LemonToast'
import { playerCommentModel } from 'scenes/session-recordings/player/commenting/playerCommentModel'
import { RecordingComment } from 'scenes/session-recordings/player/inspector/playerInspectorLogic'

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

import type { SessionRecordingType } from '../../../types'
import { SessionRecordingMetaLogicProps, sessionRecordingMetaLogic } from './sessionRecordingMetaLogic'

export interface SessionRecordingCommentsLogicProps {
    sessionRecordingId: SessionRecordingId
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface sessionRecordingCommentsLogicValues {
    sessionComments: CommentType[]
    sessionCommentsLoading: boolean
    sessionNotebookComments: any
    sessionNotebookCommentsLoading: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface sessionRecordingCommentsLogicActions {
    loadRecordingMetaSuccess: (
        sessionPlayerMetaData: SessionRecordingType | null,
        payload?:
            | {
                  value: true
              }
            | undefined
    ) => {
        payload?: {
            value: true
        }
        sessionPlayerMetaData: SessionRecordingType | null
    } // metaLogic
    deleteComment: (id: any) => any
    deleteCommentFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    deleteCommentSuccess: (
        sessionComments: CommentType[],
        payload?: any
    ) => {
        sessionComments: CommentType[]
        payload?: any
    }
    loadRecordingComments: () => {
        value: true
    }
    loadRecordingCommentsFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadRecordingCommentsSuccess: (
        sessionComments: CommentType[],
        payload?: {
            value: true
        }
    ) => {
        sessionComments: CommentType[]
        payload?: {
            value: true
        }
    }
    loadRecordingNotebookComments: () => {
        value: true
    }
    loadRecordingNotebookCommentsFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadRecordingNotebookCommentsSuccess: (
        sessionNotebookComments: RecordingComment[],
        payload?: {
            value: true
        }
    ) => {
        sessionNotebookComments: RecordingComment[]
        payload?: {
            value: true
        }
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface sessionRecordingCommentsLogicMeta {
    key: string
}

export type sessionRecordingCommentsLogicType = MakeLogicType<
    sessionRecordingCommentsLogicValues,
    sessionRecordingCommentsLogicActions,
    SessionRecordingCommentsLogicProps,
    sessionRecordingCommentsLogicMeta
>

export const sessionRecordingCommentsLogic = kea<sessionRecordingCommentsLogicType>([
    path((key) => ['scenes', 'session-recordings', 'sessionRecordingCommentsLogic', key]),
    props({} as SessionRecordingCommentsLogicProps),
    key(({ sessionRecordingId }) => sessionRecordingId || 'no-session-recording-id'),
    connect((props: SessionRecordingCommentsLogicProps) => {
        const metaLogic = sessionRecordingMetaLogic(props as SessionRecordingMetaLogicProps)
        return {
            actions: [metaLogic, ['loadRecordingMetaSuccess']],
        }
    }),
    actions({
        loadRecordingComments: true,
        loadRecordingNotebookComments: true,
    }),
    loaders(({ values, props }) => ({
        sessionComments: [
            [] as CommentType[],
            {
                loadRecordingComments: async (_, breakpoint): Promise<CommentType[]> => {
                    const empty: CommentType[] = []
                    if (!props.sessionRecordingId) {
                        return empty
                    }

                    // Pass scope so Postgres can use the (team_id, scope, item_id, ...) composite index.
                    // All historical 'recording' comments were migrated to 'Replay' in migration 0870.
                    const response = await api.comments.list({
                        scope: 'Replay',
                        item_id: props.sessionRecordingId,
                    })
                    breakpoint()

                    return response.results || empty
                },
                deleteComment: async (id, breakpoint): Promise<CommentType[]> => {
                    await breakpoint(25)
                    await api.comments.delete(id)
                    return values.sessionComments.filter((sc) => sc.id !== id)
                },
            },
        ],
        sessionNotebookComments: {
            loadRecordingNotebookComments: async (_, breakpoint) => {
                const empty: RecordingComment[] = []
                if (!props.sessionRecordingId) {
                    return empty
                }

                const response = await api.notebooks.recordingComments(props.sessionRecordingId)
                breakpoint()

                return response.results || empty
            },
        },
    })),
    listeners(({ actions, props }) => ({
        loadRecordingMetaSuccess: () => {
            actions.loadRecordingComments()
            actions.loadRecordingNotebookComments()
        },

        loadRecordingCommentsSuccess: ({ sessionComments }) => {
            // Comments render as rich text on the seekbar, in the inspector and in the comments list.
            if (sessionComments.length > 0) {
                preloadRichContent()
            }
        },

        deleteCommentSuccess: () => {
            lemonToast.success('Comment deleted')
        },

        deleteCommentFailure: (e) => {
            posthog.captureException(e, { action: 'session recording comments logic delete comment' })
            lemonToast.error('Could not delete comment, refresh and try again')
        },

        [playerCommentModel.actionTypes.commentEdited]: ({ recordingId }) => {
            if (props.sessionRecordingId === recordingId) {
                actions.loadRecordingComments()
            }
        },
    })),
])