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(mcp-analytics): memoize session preview rows to cut re-render churn (#68378)

Paul D'Ambrab4262b04c4eb
diff --git a/products/mcp_analytics/frontend/sessions/MCPSessionsPlaylist.tsx b/products/mcp_analytics/frontend/sessions/MCPSessionsPlaylist.tsx
index 94b50d0d1fd6..2f57f09f1158 100644
--- a/products/mcp_analytics/frontend/sessions/MCPSessionsPlaylist.tsx
+++ b/products/mcp_analytics/frontend/sessions/MCPSessionsPlaylist.tsx
@@ -1,5 +1,5 @@
import { useActions, useValues } from 'kea'
-import { useRef } from 'react'
+import { memo, useRef } from 'react'
import { IconBolt, IconClock, IconRefresh, IconSearch, IconSparkles } from '@posthog/icons'
import {
@@ -134,8 +134,10 @@ function SessionDetailPanel({ className }: { className?: string }): JSX.Element
}
function SessionsListPanel(): JSX.Element {
- const { setFilters, setDateFilter, loadSessions, loadMoreSessions, setSorting } = useActions(mcpSessionsLogic)
- const { sessions, sessionsLoading, filters, dateFilter, sorting, hasNext } = useValues(mcpSessionsLogic)
+ const { setFilters, setDateFilter, loadSessions, loadMoreSessions, setSorting, selectSession } =
+ useActions(mcpSessionsLogic)
+ const { sessions, sessionsLoading, filters, dateFilter, sorting, hasNext, selectedSessionId } =
+ useValues(mcpSessionsLogic)
return (
<div className="flex flex-col h-full min-h-0 overflow-hidden rounded border border-primary bg-surface-primary">
@@ -209,7 +211,11 @@ function SessionsListPanel(): JSX.Element {
<ul className="flex flex-col list-none pl-0 m-0 divide-y divide-primary">
{sessions.map((session) => (
<li key={session.session_id}>
- <MCPSessionPreview session={session} />
+ <MCPSessionPreview
+ session={session}
+ isActive={session.session_id === selectedSessionId}
+ onSelect={selectSession}
+ />
</li>
))}
</ul>
@@ -233,11 +239,18 @@ function SessionsListPanel(): JSX.Element {
)
}
-function MCPSessionPreview({ session }: { session: MCPSessionApi }): JSX.Element {
- const { selectSession } = useActions(mcpSessionsLogic)
- const { selectedSessionId } = useValues(mcpSessionsLogic)
- const isActive = session.session_id === selectedSessionId
-
+// memo relies on stable identities: kea replaces the sessions array wholesale on load
+// (so unchanged rows keep their reference) and selectSession is a stable bound action.
+// A selector that maps or clones sessions would silently defeat this.
+const MCPSessionPreview = memo(function MCPSessionPreview({
+ session,
+ isActive,
+ onSelect,
+}: {
+ session: MCPSessionApi
+ isActive: boolean
+ onSelect: (sessionId: string) => void
+}): JSX.Element {
const personLabel = session.person_name || session.person_email
const durationMs = sessionDurationMs(session.session_start, session.session_end)
@@ -246,7 +259,7 @@ function MCPSessionPreview({ session }: { session: MCPSessionApi }): JSX.Element
type="button"
data-attr="mcp-session-preview"
aria-pressed={isActive}
- onClick={() => selectSession(session.session_id)}
+ onClick={() => onSelect(session.session_id)}
className={cn(
'w-full text-left cursor-pointer border-l-2 px-2 py-1.5 text-xs flex flex-col gap-1',
isActive
@@ -284,4 +297,4 @@ function MCPSessionPreview({ session }: { session: MCPSessionApi }): JSX.Element
</div>
</button>
)
-}
+})