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.

2.7 KB
Raw Blame
import { richContentToText } from 'lib/components/RichContentEditor/richContentText'
import { JSONContent } from 'lib/components/RichContentEditor/types'
import { dayjs } from 'lib/dayjs'
import { urls } from 'scenes/urls'

import { ActivityScope } from '~/types'

export interface RecordingLinkInfo {
    recordingId: string
    unixTimestampMillis: number | undefined
    url: string
}

export type RecordingCommentTarget = {
    scope: ActivityScope | string
    item_id?: string | null
    item_context?: { time_in_recording?: string | null } | null
}

export function getRecordingLinkInfo(comment: RecordingCommentTarget): RecordingLinkInfo | null {
    const isRecordingComment = comment.scope === ActivityScope.REPLAY || comment.scope === ActivityScope.RECORDING
    if (!isRecordingComment || !comment.item_id) {
        return null
    }
    const timeInRecording = comment.item_context?.time_in_recording
    const unixTimestampMillis = timeInRecording ? dayjs(timeInRecording).valueOf() : undefined
    const url = urls.replaySingle(comment.item_id, unixTimestampMillis ? { unixTimestampMillis } : undefined)
    return {
        recordingId: comment.item_id,
        unixTimestampMillis,
        url,
    }
}

/** Scopes whose discussions never mirror to Slack, because a Slack channel cannot apply their access checks. */
const SCOPES_WITHOUT_SLACK_SYNC: ReadonlySet<string> = new Set([ActivityScope.REPLAY_OBSERVATION])

export function scopeSyncsToSlack(scope: string): boolean {
    return !SCOPES_WITHOUT_SLACK_SYNC.has(scope)
}

export function getCommentText(comment: { content?: string | null; rich_content?: JSONContent | null }): string {
    // This is only temporary until all comments are backfilled to rich content
    const content = comment.rich_content
        ? comment.rich_content
        : {
              type: 'doc',
              content: [
                  {
                      type: 'paragraph',
                      content: comment.content
                          ? [
                                {
                                    type: 'text',
                                    text: comment.content,
                                },
                            ]
                          : [],
                  },
              ],
          }

    return richContentToText(content, (id) => `@member:${id}`)
}

export function isViewingRecording(recordingId: string): boolean {
    const url = new URL(window.location.href)
    const pathMatch = url.pathname.match(/\/replay\/([^/?#]+)/)
    if (pathMatch && pathMatch[1] === recordingId) {
        return true
    }
    const sessionRecordingId =
        url.searchParams.get('sessionRecordingId') || url.hash.match(/sessionRecordingId=([^&]+)/)?.[1]
    return sessionRecordingId === recordingId
}