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.

fix(max): clear the delayed homepage AI send after unmount (#98795)

posthog[bot]3c311ff0a987
diff --git a/frontend/src/scenes/project-homepage/ai-first/HomepageThread.test.tsx b/frontend/src/scenes/project-homepage/ai-first/HomepageThread.test.tsx
new file mode 100644
index 000000000000..ad6aca09cd13
--- /dev/null
+++ b/frontend/src/scenes/project-homepage/ai-first/HomepageThread.test.tsx
@@ -0,0 +1,76 @@
+import { act, cleanup, render } from '@testing-library/react'
+import { Provider } from 'kea'
+
+import { maxLogic } from 'scenes/max/maxLogic'
+import { maxMocks } from 'scenes/max/testUtils'
+
+import { useMocks } from '~/mocks/jest'
+import { initKeaTests } from '~/test/init'
+
+import { aiFirstHomepageLogic } from './aiFirstHomepageLogic'
+import { HOMEPAGE_TAB_ID } from './constants'
+import { HomepageThread } from './HomepageThread'
+
+jest.mock('scenes/max/Thread', () => ({
+ Thread: () => <div data-attr="mock-thread" />,
+}))
+
+describe('HomepageThread', () => {
+ const QUERY = 'why did signups drop'
+
+ let homepageLogic: ReturnType<typeof aiFirstHomepageLogic.build>
+ let maxLogicInstance: ReturnType<typeof maxLogic.build>
+
+ beforeEach(() => {
+ useMocks({
+ ...maxMocks,
+ get: {
+ ...maxMocks.get,
+ '/api/projects/:team_id/dashboards/': { results: [] },
+ '/api/projects/:team_id/file_system/': { results: [] },
+ '/api/projects/:team_id/file_system_shortcut/': { results: [] },
+ },
+ })
+ initKeaTests()
+ jest.useFakeTimers()
+
+ maxLogicInstance = maxLogic({ panelId: HOMEPAGE_TAB_ID })
+ maxLogicInstance.mount()
+ homepageLogic = aiFirstHomepageLogic()
+ homepageLogic.mount()
+ homepageLogic.actions.setQuery(QUERY)
+ })
+
+ afterEach(() => {
+ cleanup()
+ jest.useRealTimers()
+ jest.restoreAllMocks()
+ homepageLogic.unmount()
+ maxLogicInstance.unmount()
+ })
+
+ // The send waits 100 ms so the thread logic is mounted first. Without cleanup on the effect, a
+ // navigation away inside that window still starts a conversation the user has already left.
+ it.each([
+ ['sends the initial query once the delay passes', false, 1],
+ ['drops the send when the thread unmounts first', true, 0],
+ ])('%s', (_case, unmountBeforeSend, expectedCalls) => {
+ const askMax = jest
+ .spyOn(maxLogicInstance.actions, 'askMax')
+ .mockImplementation((prompt) => ({ prompt, addToThread: true, uiContext: undefined }))
+
+ const { unmount } = render(
+ <Provider>
+ <HomepageThread />
+ </Provider>
+ )
+ if (unmountBeforeSend) {
+ unmount()
+ }
+ act(() => {
+ jest.advanceTimersByTime(100)
+ })
+
+ expect(askMax).toHaveBeenCalledTimes(expectedCalls)
+ })
+})
diff --git a/frontend/src/scenes/project-homepage/ai-first/HomepageThread.tsx b/frontend/src/scenes/project-homepage/ai-first/HomepageThread.tsx
index add5ace32003..07def89730f3 100644
--- a/frontend/src/scenes/project-homepage/ai-first/HomepageThread.tsx
+++ b/frontend/src/scenes/project-homepage/ai-first/HomepageThread.tsx
@@ -33,9 +33,12 @@ export function HomepageThread(): JSX.Element {
if (query && !hasSentInitial.current) {
hasSentInitial.current = true
setQuestion(query)
- setTimeout(() => {
+ // Cleared on unmount, so a fast navigation away doesn't send a prompt into a thread
+ // the user has already left.
+ const sendTimer = setTimeout(() => {
askMax(query)
}, 100)
+ return () => clearTimeout(sendTimer)
}
}, [query]) // eslint-disable-line react-hooks/exhaustive-deps