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.

perf(frontend): load event details when a table row expands (#110356)

Paul D'Ambra852465922347
diff --git a/frontend/src/queries/nodes/DataTable/DataTable.tsx b/frontend/src/queries/nodes/DataTable/DataTable.tsx
index eea26390526b..bf2b4b6fe109 100644
--- a/frontend/src/queries/nodes/DataTable/DataTable.tsx
+++ b/frontend/src/queries/nodes/DataTable/DataTable.tsx
@@ -13,7 +13,6 @@ import { LemonButton } from 'lib/lemon-ui/LemonButton'
import { LemonDivider } from 'lib/lemon-ui/LemonDivider'
import { LemonTable, LemonTableColumn } from 'lib/lemon-ui/LemonTable'
import { useAttachedLogic } from 'lib/logic/scenes/useAttachedLogic'
-import { EventDetails } from 'scenes/activity/explore/EventDetails'
import { InsightEmptyState, InsightErrorState } from 'scenes/insights/EmptyStates'
import { createMarketingAnalyticsOrderBy } from 'scenes/web-analytics/tabs/marketing-analytics/frontend/logic/utils'
@@ -98,6 +97,7 @@ import { GroupPropertyFilters } from '../GroupsQuery/GroupPropertyFilters'
import { GroupsSearch } from '../GroupsQuery/GroupsSearch'
import { DataTableOpenEditor } from './DataTableOpenEditor'
import { DataTableViewReplays } from './DataTableViewReplays'
+import { ExpandedEventRow } from './ExpandedEventRow'
import { TableViewSupportedQueryType } from './TableView/tableViewLogic'
export enum ColumnFeature {
@@ -757,12 +757,16 @@ export function DataTable({
onRowExpand: (_: DataTableRow, rowIndex: number) => toggleRowExpanded(rowIndex),
onRowCollapse: (_: DataTableRow, rowIndex: number) => toggleRowExpanded(rowIndex),
expandedRowRender: function renderExpand({ result }: DataTableRow) {
- if (isEventsQuery(query.source) && Array.isArray(result)) {
- return <EventDetails event={result[columnsInResponse.indexOf('*')] ?? {}} />
- }
- if (result && !Array.isArray(result)) {
- return <EventDetails event={result as EventType} />
+ const event =
+ isEventsQuery(query.source) && Array.isArray(result)
+ ? (result[columnsInResponse.indexOf('*')] ?? {})
+ : result && !Array.isArray(result)
+ ? (result as EventType)
+ : null
+ if (!event) {
+ return undefined
}
+ return <ExpandedEventRow event={event} />
},
rowExpandable: ({ result }: DataTableRow) => !!result,
noIndent: true,
diff --git a/frontend/src/queries/nodes/DataTable/ExpandedEventRow.test.tsx b/frontend/src/queries/nodes/DataTable/ExpandedEventRow.test.tsx
new file mode 100644
index 000000000000..cb2db680de1c
--- /dev/null
+++ b/frontend/src/queries/nodes/DataTable/ExpandedEventRow.test.tsx
@@ -0,0 +1,56 @@
+import '@testing-library/jest-dom'
+
+import { cleanup, render, screen } from '@testing-library/react'
+import { Component, type ReactNode } from 'react'
+
+import { EventType } from '~/types'
+
+import { ExpandedEventRow } from './ExpandedEventRow'
+
+jest.mock('scenes/activity/explore/EventDetails', () => {
+ throw new TypeError('Failed to fetch dynamically imported module: /static/EventDetails.js')
+})
+
+class TableErrorBoundary extends Component<{ children: ReactNode }, { failed: boolean }> {
+ override state = { failed: false }
+
+ static getDerivedStateFromError(): { failed: boolean } {
+ return { failed: true }
+ }
+
+ override render(): ReactNode {
+ return this.state.failed ? <div>table replaced by an error</div> : this.props.children
+ }
+}
+
+describe('ExpandedEventRow', () => {
+ let consoleErrorSpy: jest.SpyInstance
+ let consoleWarnSpy: jest.SpyInstance
+
+ beforeEach(() => {
+ // The query's ErrorBoundary is nearer than the scene's, so a stale chunk that escaped this
+ // boundary would replace the whole table. Pretend a reload already ran, so the guard makes
+ // the boundary surface the error instead of reloading again.
+ window.localStorage.setItem('posthog-chunk-reload-at', String(Date.now()))
+ consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {})
+ consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {})
+ })
+
+ afterEach(() => {
+ window.localStorage.clear()
+ consoleErrorSpy.mockRestore()
+ consoleWarnSpy.mockRestore()
+ cleanup()
+ })
+
+ it('keeps the table on screen when the event details chunk fails after a reload', async () => {
+ render(
+ <TableErrorBoundary>
+ <ExpandedEventRow event={{ id: 'event-1' } as EventType} />
+ </TableErrorBoundary>
+ )
+
+ expect(await screen.findByText(/Couldn't load the event details/, {}, { timeout: 3000 })).toBeInTheDocument()
+ expect(screen.queryByText('table replaced by an error')).not.toBeInTheDocument()
+ })
+})
diff --git a/frontend/src/queries/nodes/DataTable/ExpandedEventRow.tsx b/frontend/src/queries/nodes/DataTable/ExpandedEventRow.tsx
new file mode 100644
index 000000000000..3f191c3d5554
--- /dev/null
+++ b/frontend/src/queries/nodes/DataTable/ExpandedEventRow.tsx
@@ -0,0 +1,34 @@
+import { Suspense } from 'react'
+
+import { LemonBanner } from 'lib/lemon-ui/LemonBanner'
+import { Spinner } from 'lib/lemon-ui/Spinner'
+import { lazyWithRetry } from 'lib/utils/retryImport'
+import { ChunkLoadErrorBoundary } from 'scenes/ChunkLoadErrorBoundary'
+
+import { EventType } from '~/types'
+
+// Event details render AI conversations and survey responses, which a table needs only once a row expands.
+const EventDetails = lazyWithRetry(() =>
+ import('scenes/activity/explore/EventDetails').then((m) => ({ default: m.EventDetails }))
+)
+
+export function ExpandedEventRow({ event }: { event: EventType }): JSX.Element {
+ return (
+ // The query's ErrorBoundary is nearer than the scene's, so without this a stale chunk never
+ // reloads. The fallback keeps the table if the reload already ran.
+ <ChunkLoadErrorBoundary
+ fallback={() => (
+ <LemonBanner
+ type="warning"
+ action={{ children: 'Reload page', onClick: () => window.location.reload() }}
+ >
+ Couldn't load the event details. Reload the page to try again.
+ </LemonBanner>
+ )}
+ >
+ <Suspense fallback={<Spinner />}>
+ <EventDetails event={event} />
+ </Suspense>
+ </ChunkLoadErrorBoundary>
+ )
+}