: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(replay): resolve the "Export starting" toast for async exports (#64115)
Tue Haulund78ed4e752bd4
diff --git a/frontend/src/lib/components/ExportButton/exportsLogic.test.ts b/frontend/src/lib/components/ExportButton/exportsLogic.test.tsindex 488f1a09f443..b836f1fa7944 100644--- a/frontend/src/lib/components/ExportButton/exportsLogic.test.ts+++ b/frontend/src/lib/components/ExportButton/exportsLogic.test.ts@@ -1,8 +1,21 @@+import { expectLogic } from 'kea-test-utils'++import { lemonToast } from 'lib/lemon-ui/LemonToast'+import { initKeaTests } from '~/test/init'import { ExportedAssetType, ExporterFormat } from '~/types'+import { downloadExportedAsset } from './exporter'import { exportsLogic, pickPollDelayMs } from './exportsLogic'+jest.mock('lib/lemon-ui/LemonToast', () => ({+ lemonToast: { info: jest.fn(), success: jest.fn(), error: jest.fn(), dismiss: jest.fn() },+}))+jest.mock('./exporter', () => ({+ ...jest.requireActual('./exporter'),+ downloadExportedAsset: jest.fn(),+}))+const asset = (overrides: Partial<ExportedAssetType> = {}): ExportedAssetType => ({id: 1,export_format: ExporterFormat.CSV,@@ -72,4 +85,48 @@ describe('exportsLogic', () => {expect(startExportSpy.mock.calls[0][0].export_context?.skip_inactivity).toBe(expected)})})++ describe('async export toast resolution', () => {+ let logic: ReturnType<typeof exportsLogic.build>++ beforeEach(() => {+ jest.clearAllMocks()+ initKeaTests()+ logic = exportsLogic()+ logic.mount()+ })++ it.each([+ {+ label: 'success',+ finishedAsset: asset({ id: 5, export_format: ExporterFormat.MP4, has_content: true }),+ expectedToast: { fn: 'success' as const, message: 'Export complete!' },+ expectsDownload: true,+ },+ {+ label: 'error',+ finishedAsset: asset({ id: 7, export_format: ExporterFormat.MP4, exception: 'boom' }),+ expectedToast: { fn: 'error' as const, message: 'Export failed: boom' },+ expectsDownload: false,+ },+ ])(+ 'resolves the per-export toast to $label once a tracked async export finishes',+ async ({ finishedAsset, expectedToast, expectsDownload }) => {+ logic.actions.addFresh(asset({ id: finishedAsset.id, export_format: ExporterFormat.MP4 }))++ await expectLogic(logic, () => {+ logic.actions.loadExportsSuccess([finishedAsset])+ }).toFinishAllListeners()++ if (expectsDownload) {+ expect(downloadExportedAsset).toHaveBeenCalledWith(finishedAsset)+ } else {+ expect(downloadExportedAsset).not.toHaveBeenCalled()+ }+ expect(lemonToast.dismiss).toHaveBeenCalledWith(`export-${finishedAsset.id}`)+ expect(lemonToast[expectedToast.fn]).toHaveBeenCalledWith(expectedToast.message)+ expect(logic.values.freshUndownloadedExports).toEqual([])+ }+ )+ })})diff --git a/frontend/src/lib/components/ExportButton/exportsLogic.ts b/frontend/src/lib/components/ExportButton/exportsLogic.tsindex d559c82d80c4..cd57aa0182cd 100644--- a/frontend/src/lib/components/ExportButton/exportsLogic.ts+++ b/frontend/src/lib/components/ExportButton/exportsLogic.ts@@ -37,6 +37,9 @@ export const pickPollDelayMs = (pendingAssets: ExportedAssetType[]): number => {const isLocalExport = (context: ExportContext | undefined): context is LocalExportContext =>!!(context && 'localData' in context)+// Single source for the per-export toast id so the show and dismiss sites can't drift.+const exportToastId = (id: number): string => `export-${id}`+export const exportsLogic = kea<exportsLogicType>([path(['scenes', 'navigation', 'sidepanel', 'exportsLogic']),@@ -114,21 +117,16 @@ export const exportsLogic = kea<exportsLogicType>([actions.createExport({ exportData })},createExportSuccess: () => {- lemonToast.info('Export starting...', {- button: {- label: 'View exports',- action: () => newInternalTab(urls.exports()),- },- autoClose: false,- })actions.loadExports()},loadExportsSuccess: async (_, breakpoint) => {// Auto-download (or surface errors for) async exports we kicked off that have now finished.- const { exports, freshUndownloadedExports } = exportsLogic.values+ // NB: avoid naming a local `exports` — it shadows the CommonJS module object under+ // transpilation (jest) and throws a TDZ error.+ const { exports: exportsList, freshUndownloadedExports } = exportsLogic.valuesfor (const fresh of freshUndownloadedExports) {// The list can be format-filtered (assetFormat), so fetch directly if the tracked export isn't in it.- let latest = exports.find((asset) => asset.id === fresh.id)+ let latest = exportsList.find((asset) => asset.id === fresh.id)if (!latest) {try {latest = await api.exports.get(fresh.id)@@ -140,18 +138,20 @@ export const exportsLogic = kea<exportsLogicType>([continue}actions.removeFresh(fresh)+ lemonToast.dismiss(exportToastId(fresh.id))if (latest.has_content) {await downloadExportedAsset(latest)+ lemonToast.success('Export complete!')} else {lemonToast.error('Export failed: ' + latest.exception)}}// Keep polling while the (possibly filtered) list or any tracked async export is still unfinished.- const pendingInList = exports.some((asset) => !asset.has_content && !asset.exception)+ const pendingInList = exportsList.some((asset) => !asset.has_content && !asset.exception)const pendingFresh = exportsLogic.values.freshUndownloadedExports.length > 0if (pendingInList || pendingFresh) {- await breakpoint(pickPollDelayMs([...exports, ...exportsLogic.values.freshUndownloadedExports]))+ await breakpoint(pickPollDelayMs([...exportsList, ...exportsLogic.values.freshUndownloadedExports]))actions.loadExports()return}@@ -254,13 +254,23 @@ export const exportsLogic = kea<exportsLogicType>([const updatedExports = [response, ...currentExports.filter((e) => e.id !== response.id)]actions.loadExportsSuccess(updatedExports)- // If this was a blocking export, we should download it nowif (response && response.has_content) {+ // Blocking export already finished — download and confirm.await downloadExportedAsset(response)+ lemonToast.success('Export complete!')} else if (response && response.exception) {lemonToast.error('Export failed: ' + response.exception)} else if (response) {- // Async export (e.g. video render): track it to auto-download once polling sees it finish.+ // Async export (e.g. video render): show a per-export toast that the+ // polling loop resolves to success (or error) once the render finishes.+ lemonToast.info('Export starting...', {+ toastId: exportToastId(response.id),+ autoClose: false,+ button: {+ label: 'View exports',+ action: () => newInternalTab(urls.exports()),+ },+ })actions.addFresh(response)}} catch (error) {