: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(conversations): avoid redundant page-load requests (#103093)
Matt Pua676d3b12d919
diff --git a/docs/internal/conversations-reliability.md b/docs/internal/conversations-reliability.mdindex 9abc7bed4440..c92f63a9b3bb 100644--- a/docs/internal/conversations-reliability.md+++ b/docs/internal/conversations-reliability.md@@ -30,6 +30,14 @@ Until then, scale indexed sweepers.If Kafka is added later, it is a wake-up relay from the Postgres outbox, not a second source of truth.Widget writes stay synchronous in Postgres because the API must return ticket and message ids.+## Unread ticket count++The app fetches the unread ticket count when support is enabled and polls every 5 seconds while the browser tab is visible.+Ticket actions can trigger an immediate refresh.+Unrelated updates to the current team do not reset the count or trigger another request.+Changing teams or toggling support resets the count and restarts polling when support is enabled.+Failed requests retain the previous count and increase the polling interval.+## Inbound receipts (`ConversationInboundEvent`)One row per accepted provider callback, keyed by `(team, source, source_id)`.diff --git a/frontend/src/scenes/max/maxLogic.test.ts b/frontend/src/scenes/max/maxLogic.test.tsindex 0d73077a0618..f911c62aff46 100644--- a/frontend/src/scenes/max/maxLogic.test.ts+++ b/frontend/src/scenes/max/maxLogic.test.ts@@ -31,15 +31,21 @@ describe('maxLogic', () => {let logic: ReturnType<typeof maxLogic.build>let threadLogic: ReturnType<typeof maxThreadLogic.build> | null = nulllet actionsRequestCount: number+ let conversationRequestCount: numberbeforeEach(() => {localStorage.clear()sessionStorage.clear()actionsRequestCount = 0+ conversationRequestCount = 0useMocks({...maxMocks,get: {...maxMocks.get,+ '/api/environments/:team_id/conversations/': () => {+ conversationRequestCount+++ return [200, { results: [] }]+ },'/api/projects/:team/actions/': () => {actionsRequestCount++return [200, { results: [], count: 0 }]@@ -70,13 +76,14 @@ describe('maxLogic', () => {})})- it('does not load actions when Max mounts', async () => {+ it('shares the initial history request and does not load actions when Max mounts', async () => {logic = maxLogic({ panelId: 'test' })logic.mount()await expectLogic(logic).toDispatchActions(['loadConversationHistorySuccess'])expect(actionsRequestCount).toBe(0)+ expect(conversationRequestCount).toBe(1)})it.each([diff --git a/frontend/src/scenes/max/maxLogic.tsx b/frontend/src/scenes/max/maxLogic.tsxindex 9d8c4b4e8813..ee89f9e1ba09 100644--- a/frontend/src/scenes/max/maxLogic.tsx+++ b/frontend/src/scenes/max/maxLogic.tsx@@ -949,8 +949,10 @@ export const maxLogic = kea<maxLogicType>([handleCommandString(sidePanelStateLogic.values.selectedTabOptions, actions, values.effectivePhaiView)}- // Load conversation history on mount- actions.loadConversationHistory()+ // The global logic may already be loading history for the navigation.+ if (!values.conversationHistoryLoading) {+ actions.loadConversationHistory()+ }}),urlToAction(({ actions, values }) => ({diff --git a/products/conversations/frontend/supportTicketCounterLogic.test.ts b/products/conversations/frontend/supportTicketCounterLogic.test.tsnew file mode 100644index 000000000000..b344f1aab997--- /dev/null+++ b/products/conversations/frontend/supportTicketCounterLogic.test.ts@@ -0,0 +1,46 @@+import { MOCK_DEFAULT_TEAM } from 'lib/api.mock'++import api from 'lib/api'+import { teamLogic } from 'scenes/teamLogic'++import { initKeaTests } from '~/test/init'++import { supportTicketCounterLogic } from './supportTicketCounterLogic'++describe('supportTicketCounterLogic', () => {+ let logic: ReturnType<typeof supportTicketCounterLogic.build>+ let unreadCount: jest.SpyInstance+ const team = { ...MOCK_DEFAULT_TEAM, conversations_enabled: true }++ beforeEach(() => {+ jest.useFakeTimers()+ initKeaTests(true, team)+ unreadCount = jest.spyOn(api.conversationsTickets, 'unreadCount').mockResolvedValue({ count: 3 })+ logic = supportTicketCounterLogic()+ logic.mount()+ })++ afterEach(() => {+ logic.unmount()+ jest.useRealTimers()+ jest.restoreAllMocks()+ })++ it('ignores unrelated team updates but refreshes when support is enabled again', async () => {+ await jest.advanceTimersByTimeAsync(1)+ teamLogic.actions.loadCurrentTeamSuccess({ ...team, name: 'Updated name' })+ await jest.advanceTimersByTimeAsync(1)+ expect(unreadCount).toHaveBeenCalledTimes(1)+ expect(logic.values.unreadCount).toBe(3)++ teamLogic.actions.loadCurrentTeamSuccess({ ...team, conversations_enabled: false })+ await jest.advanceTimersByTimeAsync(1)+ expect(logic.values.unreadCount).toBe(0)+ await jest.advanceTimersByTimeAsync(60_000)+ expect(unreadCount).toHaveBeenCalledTimes(1)++ teamLogic.actions.loadCurrentTeamSuccess(team)+ await jest.advanceTimersByTimeAsync(1)+ expect(unreadCount).toHaveBeenCalledTimes(2)+ })+})diff --git a/products/conversations/frontend/supportTicketCounterLogic.ts b/products/conversations/frontend/supportTicketCounterLogic.tsindex d3d042c822e8..14868f149839 100644--- a/products/conversations/frontend/supportTicketCounterLogic.ts+++ b/products/conversations/frontend/supportTicketCounterLogic.ts@@ -188,7 +188,11 @@ export const supportTicketCounterLogic = kea<supportTicketCounterLogicType>([// React to team changes - reset and re-fetch for new teamcurrentTeam: (currentTeam, oldTeam) => {// Skip initial mount (oldTeam is undefined)- if (oldTeam === undefined) {+ if (+ oldTeam === undefined ||+ (currentTeam?.id === oldTeam?.id &&+ currentTeam?.conversations_enabled === oldTeam?.conversations_enabled)+ ) {return}