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.

27.8 KB
Raw Blame
import { MakeLogicType, actions, connect, kea, listeners, path, reducers } from 'kea'
import { loaders } from 'kea-loaders'
import posthog from 'posthog-js'

import api from 'lib/api'
import { TriggerExportProps, downloadBlob, downloadExportedAsset } from 'lib/components/ExportButton/exporter'
import {
    ExportNudge,
    KickoffToast,
    foldNudgeIntoSettledToast,
    nudgeToastOptions,
    settleWithDownload,
    startExportNudge,
} from 'lib/components/ExportButton/exportNudge'
import { isLongRunningExportFormat } from 'lib/components/ExportButton/exportStatus'
import { lemonToast } from 'lib/lemon-ui/LemonToast'
import { ToastButton } from 'lib/lemon-ui/LemonToast/LemonToast'
import { delay } from 'lib/utils/async'
import { uuid } from 'lib/utils/dom'
import { retryImport } from 'lib/utils/retryImport'
import type { SessionRecordingPlayerMode } from 'scenes/session-recordings/player/sessionRecordingPlayerLogic'

import { sidePanelStateLogic } from '~/layout/navigation-3000/sidepanel/sidePanelStateLogic'
import { cohortsModel } from '~/models/cohortsModel'
import { AnyDataNode } from '~/queries/schema/schema-general'
import {
    APIErrorType,
    CohortType,
    ExportContext,
    ExportedAssetType,
    ExporterFormat,
    LocalExportContext,
    SidePanelTab,
} from '~/types'

const POLL_DELAY_MS = 10000
// Long-running formats (e.g. MP4 session-replay renders) can take 30+ minutes,
// so polling every 10s produces unhelpful timeout noise. Back off when the
// pending queue is dominated by long-running formats.
const LONG_RUNNING_POLL_DELAY_MS = 30000
const EXPORT_PENDING_MESSAGE = 'Preparing export…'
const EXPORT_COMPLETE_MESSAGE = 'Export complete!'

// An export is still rendering while it has neither produced content nor failed.
const isRendering = (asset: ExportedAssetType): boolean => !asset.has_content && !asset.exception

export type ExportStatusFetcher = () => Promise<ExportedAssetType | null>

const fetchExportOrNull = async (id: number): Promise<ExportedAssetType | null> => {
    try {
        return await api.exports.get(id)
    } catch {
        return null
    }
}

export const pickPollDelayMs = (pendingAssets: ExportedAssetType[]): number => {
    const pending = pendingAssets.filter(isRendering)
    if (pending.length === 0) {
        return POLL_DELAY_MS
    }
    return pending.every((asset) => isLongRunningExportFormat(asset.export_format))
        ? LONG_RUNNING_POLL_DELAY_MS
        : POLL_DELAY_MS
}

const isLocalExport = (context: ExportContext | undefined): context is LocalExportContext =>
    !!(context && 'localData' in context)

// The user gesture that started the export is still live, so an auto-download will fire. Falls back
// to true where navigator.userActivation is unavailable (older Firefox), which tolerates a delayed
// click anyway.
const isUserActivationLive = (): boolean => navigator.userActivation?.isActive ?? true

// For a polled export this toast is the only completion signal there is. An export that
// acknowledged its kickoff carries its offer over rather than claiming a second one.
const showExportCompleteToast = async (
    asset: ExportedAssetType,
    onDownload: () => void,
    kickoff?: KickoffToast
): Promise<void> => {
    await settleWithDownload(kickoff?.nudge ?? startExportNudge(asset), kickoff?.toastId ?? null, onDownload)
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface exportsLogicValues {
    assetFormat: ExporterFormat | null
    exports: ExportedAssetType[]
    exportsLoading: boolean
    freshUndownloadedExports: ExportedAssetType[]
    hasReachedExportFullVideoLimit: boolean
    pollingExports: ExportedAssetType[]
    pollingExportsLoading: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface exportsLogicActions {
    openSidePanel: (
        tab: SidePanelTab,
        options?: string | undefined
    ) => {
        options: string | undefined
        tab: SidePanelTab
    } // sidePanelStateLogic
    addFresh: (exportedAsset: ExportedAssetType) => {
        exportedAsset: ExportedAssetType
    }
    createExport: ({ exportData }: any) => any
    createExportFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    createExportSuccess: (
        pollingExports: any[],
        payload?: any
    ) => {
        pollingExports: any[]
        payload?: any
    }
    createStaticCohort: (
        name: string,
        query: AnyDataNode
    ) => {
        name: string
        query: AnyDataNode
    }
    downloadExport: (exportedAsset: ExportedAssetType) => {
        exportedAsset: ExportedAssetType
    }
    loadExports: () => {
        value: true
    }
    loadExportsFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadExportsSuccess: (
        exports: ExportedAssetType[],
        payload?: {
            value: true
        }
    ) => {
        exports: ExportedAssetType[]
        payload?: {
            value: true
        }
    }
    removeFresh: (exportedAsset: ExportedAssetType) => {
        exportedAsset: ExportedAssetType
    }
    setAssetFormat: (format: ExporterFormat | null) => {
        format: ExporterFormat | null
    }
    setHasReachedExportFullVideoLimit: (hasReached: boolean) => {
        hasReached: boolean
    }
    startExport: (exportData: TriggerExportProps) => {
        exportData: TriggerExportProps
    }
    startHeatmapExport: (export_context: ExportContext) => {
        export_context: ExportContext
    }
    startReplayExport: (
        sessionRecordingId: string,
        format?: ExporterFormat,
        timestamp?: number,
        duration?: number,
        mode?: SessionRecordingPlayerMode,
        options?: {
            css_selector?: string
            filename?: string
            height?: number
            skip_inactivity?: boolean
            width?: number
        }
    ) => {
        duration: number | undefined
        format: ExporterFormat | undefined
        mode: SessionRecordingPlayerMode | undefined
        options:
            | {
                  css_selector?: string | undefined
                  filename?: string | undefined
                  height?: number | undefined
                  skip_inactivity?: boolean | undefined
                  width?: number | undefined
              }
            | undefined
        sessionRecordingId: string
        timestamp: number | undefined
    }
    trackExport: (
        exportedAsset: ExportedAssetType,
        fetchLatest?: ExportStatusFetcher
    ) => {
        exportedAsset: ExportedAssetType
        fetchLatest: ExportStatusFetcher | undefined
    }
}

export type exportsLogicType = MakeLogicType<exportsLogicValues, exportsLogicActions>

export const exportsLogic = kea<exportsLogicType>([
    path(['scenes', 'navigation', 'sidepanel', 'exportsLogic']),
    connect(() => ({
        actions: [sidePanelStateLogic, ['openSidePanel']],
    })),

    actions({
        loadExports: true,
        startExport: (exportData: TriggerExportProps) => ({ exportData }),
        addFresh: (exportedAsset: ExportedAssetType) => ({ exportedAsset }),
        trackExport: (exportedAsset: ExportedAssetType, fetchLatest?: ExportStatusFetcher) => ({
            exportedAsset,
            fetchLatest,
        }),
        removeFresh: (exportedAsset: ExportedAssetType) => ({ exportedAsset }),
        downloadExport: (exportedAsset: ExportedAssetType) => ({ exportedAsset }),
        createStaticCohort: (name: string, query: AnyDataNode) => ({ query, name }),
        setAssetFormat: (format: ExporterFormat | null) => ({ format }),
        setHasReachedExportFullVideoLimit: (hasReached: boolean) => ({ hasReached }),
        startReplayExport: (
            sessionRecordingId: string,
            format?: ExporterFormat,
            timestamp?: number,
            duration?: number,
            mode?: SessionRecordingPlayerMode,
            options?: {
                width?: number
                height?: number
                css_selector?: string
                filename?: string
                skip_inactivity?: boolean
            }
        ) => ({ sessionRecordingId, format, timestamp, duration, mode, options }),
        startHeatmapExport: (export_context: ExportContext) => ({ export_context }),
    }),

    reducers({
        exports: [
            [] as ExportedAssetType[],
            {
                loadExportsSuccess: (_, { exports }) => exports,
            },
        ],
        assetFormat: [
            null as ExporterFormat | null,
            {
                setAssetFormat: (_, { format }) => format,
            },
        ],
        freshUndownloadedExports: [
            [] as ExportedAssetType[],
            {
                addFresh: (state, { exportedAsset }) =>
                    state.some((asset) => asset.id === exportedAsset.id) ? state : [...state, exportedAsset],
                removeFresh: (state, { exportedAsset }) => state.filter((asset) => asset.id !== exportedAsset.id),
            },
        ],
        hasReachedExportFullVideoLimit: [
            false,
            {
                setHasReachedExportFullVideoLimit: (_, { hasReached }) => hasReached,
            },
        ],
    }),

    listeners(({ actions, values, cache }) => ({
        startExport: async ({ exportData }) => {
            // Fires for every dashboard export entry point (menu bar, dropdown, export button)
            // regardless of edit permission. Format is a property so PNG is filterable.
            if (exportData.dashboard && exportData.export_format) {
                posthog.capture('dashboard exported', {
                    dashboard_id: exportData.dashboard,
                    export_format: exportData.export_format,
                })
            }

            if (isLocalExport(exportData.export_context)) {
                try {
                    const blob = new Blob([exportData.export_context.localData], {
                        type: exportData.export_context.mediaType,
                    })
                    downloadBlob(blob, exportData.export_context.filename)
                    lemonToast.success(EXPORT_COMPLETE_MESSAGE)
                } catch (e: any) {
                    lemonToast.error(`Export failed with error: ${e.message}`)
                }
                return
            }

            actions.createExport({ exportData })
        },
        trackExport: ({ exportedAsset, fetchLatest }) => {
            cache.exportStatusFetchers ??= new Map<number, ExportStatusFetcher>()
            if (fetchLatest) {
                cache.exportStatusFetchers.set(exportedAsset.id, fetchLatest)
            }
            actions.setAssetFormat(null)
            actions.addFresh(exportedAsset)
            actions.openSidePanel(SidePanelTab.Exports)
        },
        removeFresh: ({ exportedAsset }) => {
            cache.exportStatusFetchers?.delete(exportedAsset.id)
            cache.exportKickoffToasts?.delete(exportedAsset.id)
        },
        createExportSuccess: () => {
            actions.loadExports()
        },
        downloadExport: ({ exportedAsset }) => {
            // Content already exists for this asset, so trigger the download synchronously — the click
            // has to run inside the user gesture or Safari silently drops it. Drop the "not downloaded"
            // highlight now that the download has been triggered.
            downloadExportedAsset(exportedAsset)
            actions.removeFresh(exportedAsset)
            lemonToast.success('Download started')
        },
        loadExportsSuccess: async ({ exports: exportsList }, breakpoint) => {
            // An async render finishes long after its kickoff toast, so this poll is the only
            // completion signal the user gets.
            cache.notifiedExportIds ??= new Set<number>()
            const pending = exportsList.filter(isRendering)
            for (const fresh of values.freshUndownloadedExports) {
                if (cache.notifiedExportIds.has(fresh.id)) {
                    continue
                }
                // Claimed before the fetch, not after. This listener runs on the poll and on a
                // finished create call, and two runs could both pass a check placed after the await
                // and each raise a completion toast for the same export.
                cache.notifiedExportIds.add(fresh.id)
                // The list can be format-filtered (assetFormat), so fetch directly if it's not in it.
                const listed = exportsList.find((asset) => asset.id === fresh.id)
                const customFetcher = cache.exportStatusFetchers?.get(fresh.id) as ExportStatusFetcher | undefined
                const latest = customFetcher
                    ? await customFetcher().catch(() => null)
                    : (listed ?? (await fetchExportOrNull(fresh.id)))
                if (!latest || isRendering(latest)) {
                    // Still rendering, or a fetch miss. Release the claim so a later poll can
                    // notify, and keep polling an export the list no longer shows as rendering, such as
                    // one whose owner retries after a failure the generic row still records.
                    cache.notifiedExportIds.delete(fresh.id)
                    if (!listed || !isRendering(listed)) {
                        pending.push(latest ?? fresh)
                    }
                    continue
                }
                if (latest.has_content) {
                    cache.exportStatusFetchers?.delete(fresh.id)
                    const finished = latest
                    const kickoff = cache.exportKickoffToasts?.get(fresh.id)
                    cache.exportKickoffToasts?.delete(fresh.id)
                    if (
                        finished.export_format === ExporterFormat.MP4 &&
                        finished.export_context &&
                        'session_recording_id' in finished.export_context
                    ) {
                        posthog.capture('recording exported to file', {
                            format: 'mp4',
                            is_clip: String(kickoff?.replayMode) !== 'video',
                        })
                    }
                    void showExportCompleteToast(finished, () => actions.downloadExport(finished), kickoff).catch(
                        (error) => posthog.captureException(error)
                    )
                } else {
                    actions.removeFresh(fresh)
                    lemonToast.error('Export failed: ' + latest.exception)
                }
            }

            if (pending.length) {
                await breakpoint(pickPollDelayMs(pending))
                actions.loadExports()
            }
        },
        createStaticCohort: async ({ query, name }) => {
            const toastId = 'toast-' + Math.random()
            try {
                lemonToast.info('Saving cohort...', { toastId, autoClose: false })
                // The toast pulls in the workflows audience code, so it loads on demand. A failed load
                // must not stop the save or report a saved cohort as a failure.
                const toastModule = retryImport(() => import('scenes/cohorts/cohortSavedToast')).catch(
                    (error: unknown) => {
                        posthog.captureException(error)
                        return null
                    }
                )
                // nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. Use cohortsCreate() from 'products/cohorts/frontend/generated/api' instead.
                const cohort: CohortType = await api.create('api/cohort', {
                    is_static: true,
                    name: name || 'Query cohort',
                    query: query,
                })
                cohortsModel.findMounted()?.actions.cohortCreated(cohort)
                await delay(500) // just in case the toast is too fast
                lemonToast.dismiss(toastId)
                const savedToast = await toastModule
                if (savedToast) {
                    savedToast.cohortSavedToast(cohort, 'query_results', `${toastId}-success`)
                } else {
                    lemonToast.success('Cohort saved', { toastId: `${toastId}-success` })
                }
            } catch {
                lemonToast.dismiss(toastId)
                lemonToast.error('Cohort save failed')
            }
        },
        setAssetFormat: () => {
            actions.loadExports()
        },
        startReplayExport: async ({
            sessionRecordingId,
            format = ExporterFormat.PNG,
            timestamp,
            duration = 5,
            mode,
            options,
        }) => {
            cache.replayExportMode = mode
            const exportData: TriggerExportProps = {
                export_format: format,
                export_context: {
                    session_recording_id: sessionRecordingId,
                    timestamp: timestamp,
                    width: options?.width || 1400,
                    height: options?.height || 600,
                    filename: options?.filename || `replay-${sessionRecordingId}${timestamp ? `-t${timestamp}` : ''}`,
                    duration: duration,
                    skip_inactivity: options?.skip_inactivity ?? true,
                },
            }

            actions.startExport(exportData)
        },
        startHeatmapExport: async ({ export_context }) => {
            const exportData: TriggerExportProps = {
                export_format: ExporterFormat.PNG,
                export_context: export_context,
            }

            actions.startExport(exportData)
        },
    })),

    loaders(({ values, actions, cache }) => ({
        exports: [
            [] as ExportedAssetType[],
            {
                loadExports: async (_, breakpoint) => {
                    await breakpoint(100)
                    const params: Record<string, any> = {}

                    // Add format filter if set
                    const format = values.assetFormat
                    if (format) {
                        params.export_format = format
                    }

                    const response = await api.exports.list(undefined, params)
                    breakpoint()

                    return response.results
                },
            },
        ],
        pollingExports: [
            [] as ExportedAssetType[],
            {
                createExport: ({ exportData }) => {
                    const exportToastId = 'export-' + uuid()
                    // A video render lands in the exports panel minutes later, so its kickoff toast
                    // links there. A synchronous export has no row to link to while it runs.
                    const viewExportsButton: ToastButton | undefined = isLongRunningExportFormat(
                        exportData.export_format
                    )
                        ? { label: 'View exports', action: () => actions.openSidePanel(SidePanelTab.Exports) }
                        : undefined
                    const nudge: ExportNudge = startExportNudge(exportData)
                    const kickoffFrame = nudgeToastOptions(
                        nudge,
                        EXPORT_PENDING_MESSAGE,
                        exportToastId,
                        viewExportsButton
                    )
                    // Set when the export finished but the request outlived the click's user
                    // activation, so the file waits behind a Download button. Safari drops a
                    // programmatic download once activation expires. Held on an object because
                    // runExport assigns it, and the checker does not see that from this scope.
                    const awaiting: { download: ExportedAssetType | null } = { download: null }

                    // Non-video exports run synchronously on the backend, so this request can block
                    // for a while. lemonToast.promise shows a spinner right away and swaps to the
                    // result, so the menu never looks like it did nothing.
                    const runExport = async (): Promise<string> => {
                        const replayMode = cache.replayExportMode as SessionRecordingPlayerMode | undefined
                        cache.replayExportMode = undefined
                        let response: ExportedAssetType
                        try {
                            response = await api.exports.create({
                                export_format: exportData.export_format,
                                dashboard: exportData.dashboard,
                                insight: exportData.insight,
                                export_context: exportData.export_context,
                            })
                        } catch (error) {
                            // Preserve the errors the caller renders itself — the limit upsell and the
                            // reason a recording is too long — and give everything else a friendly
                            // message for the failure toast.
                            const attr = (error as { data?: APIErrorType })?.data?.attr
                            if (attr === 'export_limit_exceeded' || attr === 'export_duration_unsupported') {
                                throw error
                            }
                            const message = error instanceof Error ? error.message : String(error)
                            throw new Error('Export failed: ' + message)
                        }

                        const currentExports = values.exports
                        const updatedExports = [response, ...currentExports.filter((e) => e.id !== response.id)]
                        actions.loadExportsSuccess(updatedExports)

                        if (response.has_content) {
                            // Blocking export already finished in the request. Auto-download while the
                            // click's activation is still live; if the create call outlived it (a slow
                            // synchronous render), Safari would drop the download, so hand off to a
                            // Download button whose click is a fresh gesture.
                            if (isUserActivationLive()) {
                                downloadExportedAsset(response)
                                return EXPORT_COMPLETE_MESSAGE
                            }
                            // Claimed before addFresh, because that is the moment the poll can see
                            // this asset. A poll running before the toast settles would otherwise
                            // announce the same export again.
                            cache.notifiedExportIds ??= new Set<number>()
                            cache.notifiedExportIds.add(response.id)
                            actions.addFresh(response)
                            // Not a failure, so not a rejection: the export succeeded and the file
                            // is ready, it just needs a fresh click. Rejecting here would settle the
                            // toast into its error frame on the way past.
                            awaiting.download = response
                            return EXPORT_COMPLETE_MESSAGE
                        }
                        if (response.exception) {
                            throw new Error('Export failed: ' + response.exception)
                        }
                        // Async export (e.g. video render) is a background job: acknowledge the
                        // kickoff and open the exports panel, where it lands once the render finishes.
                        actions.addFresh(response)
                        actions.openSidePanel(SidePanelTab.Exports)
                        cache.exportKickoffToasts ??= new Map<number, KickoffToast>()
                        cache.exportKickoffToasts.set(response.id, { toastId: exportToastId, nudge, replayMode })
                        return 'Export started'
                    }

                    void (async () => {
                        try {
                            // The success frame is read when the toast settles, so an offer followed
                            // while the export was running is already gone by then, rather than
                            // being asked a second time.
                            const settledMessage: string = await lemonToast.promise(
                                runExport(),
                                {
                                    pending: kickoffFrame.message,
                                    success: (data) =>
                                        nudgeToastOptions(
                                            nudge,
                                            data || EXPORT_COMPLETE_MESSAGE,
                                            exportToastId,
                                            viewExportsButton
                                        ).message,
                                    error: 'Export failed',
                                },
                                { toastId: exportToastId, button: kickoffFrame.button }
                            )
                            const readyForDownload = awaiting.download
                            if (readyForDownload) {
                                await settleWithDownload(nudge, exportToastId, () =>
                                    actions.downloadExport(readyForDownload)
                                )
                                return
                            }
                            await foldNudgeIntoSettledToast(nudge, exportToastId, settledMessage, viewExportsButton)
                        } catch (error) {
                            const apiError = error as { data?: APIErrorType }
                            if (apiError?.data?.attr !== 'export_limit_exceeded') {
                                posthog.captureException(error)
                            }
                            // Show a survey when the user reaches the export limit, replacing the
                            // generic failure toast with the upsell.
                            if (apiError?.data?.attr === 'export_limit_exceeded') {
                                lemonToast.dismiss(exportToastId)
                                actions.setHasReachedExportFullVideoLimit(true)
                                lemonToast.error(apiError?.data?.detail || 'You reached your export limit.', {
                                    autoClose: false,
                                    button: {
                                        label: 'I want more',
                                        className: 'replay-export-limit-reached-button',
                                        action: () => {}, //we trigger the survey by clicking the button, but we need to keep the action for the toast to show
                                        dataAttr: 'export-limit-reached-button',
                                    },
                                })
                            }
                            // The recording is longer than the renderer can turn into one video. The
                            // API is what decides that, so its reason is shown rather than a generic
                            // failure: it names the limit and what to do instead.
                            if (apiError?.data?.attr === 'export_duration_unsupported') {
                                lemonToast.dismiss(exportToastId)
                                lemonToast.error(
                                    apiError?.data?.detail ||
                                        'This recording is too long to export as one video. Export part of it instead.'
                                )
                            }
                        }
                    })()

                    return [exportData]
                },
            },
        ],
    })),
])