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): keep #panel hash across the / homepage redirect (#71018)

Michael Matlokaffae028e43d6
diff --git a/frontend/src/scenes/project-homepage/ai-first/aiFirstHomepageLogic.test.ts b/frontend/src/scenes/project-homepage/ai-first/aiFirstHomepageLogic.test.ts
new file mode 100644
index 000000000000..5b463004c4b2
--- /dev/null
+++ b/frontend/src/scenes/project-homepage/ai-first/aiFirstHomepageLogic.test.ts
@@ -0,0 +1,53 @@
+import { router } from 'kea-router'
+import { expectLogic } from 'kea-test-utils'
+
+import { featureFlagLogic } from 'lib/logic/featureFlagLogic'
+import { maxLogic } from 'scenes/max/maxLogic'
+import { urls } from 'scenes/urls'
+
+import { sidePanelStateLogic } from '~/layout/navigation-3000/sidepanel/sidePanelStateLogic'
+import { useMocks } from '~/mocks/jest'
+import { initKeaTests } from '~/test/init'
+
+import { aiFirstHomepageLogic } from './aiFirstHomepageLogic'
+import { HOMEPAGE_TAB_ID } from './constants'
+
+describe('aiFirstHomepageLogic', () => {
+ let logic: ReturnType<typeof aiFirstHomepageLogic.build>
+
+ beforeEach(() => {
+ localStorage.clear()
+ useMocks({
+ get: {
+ '/api/environments/:team_id/conversations/': { results: [] },
+ '/api/projects/:team_id/dashboards/': { results: [] },
+ '/api/projects/:team_id/file_system/': { results: [] },
+ '/api/projects/:team_id/file_system_shortcut/': { results: [] },
+ },
+ })
+ initKeaTests()
+ featureFlagLogic.mount()
+ sidePanelStateLogic.mount()
+ logic = aiFirstHomepageLogic()
+ logic.mount()
+ })
+
+ afterEach(() => {
+ logic.unmount()
+ sidePanelStateLogic.unmount()
+ })
+
+ // Regression guard: on the PostHog AI homepage, a `#panel=max:<prompt>` link should prefill the
+ // full-scene chat, not stack a redundant Max side panel on top of it. Without interception the
+ // side panel opens (via sidePanelStateLogic's `*` handler) and the prompt never reaches the
+ // homepage composer.
+ it('consumes #panel=max:<prompt> into the full-scene chat instead of the side panel', async () => {
+ // The one navigation drives both logics: sidePanelStateLogic opens the Max panel, then the
+ // homepage handler closes it again and enters AI mode with the prompt prefilled.
+ router.actions.push(urls.projectHomepage(), {}, { panel: 'max:what is my dau' })
+ await expectLogic(logic).delay(1).toMatchValues({ mode: 'ai' })
+
+ expect(maxLogic({ panelId: HOMEPAGE_TAB_ID }).values.question).toEqual('what is my dau')
+ expect(sidePanelStateLogic.values.sidePanelOpen).toBe(false)
+ })
+})
diff --git a/frontend/src/scenes/project-homepage/ai-first/aiFirstHomepageLogic.ts b/frontend/src/scenes/project-homepage/ai-first/aiFirstHomepageLogic.ts
index ba590ab2b379..8902f1908897 100644
--- a/frontend/src/scenes/project-homepage/ai-first/aiFirstHomepageLogic.ts
+++ b/frontend/src/scenes/project-homepage/ai-first/aiFirstHomepageLogic.ts
@@ -12,10 +12,11 @@ import {
capabilitiesForGrouping,
capabilityGroupingFromVariant,
} from 'scenes/max/maxCapabilities'
-import { maxLogic } from 'scenes/max/maxLogic'
+import { maxLogic, parseCommandString } from 'scenes/max/maxLogic'
import { teamLogic } from 'scenes/teamLogic'
import { urls } from 'scenes/urls'
+import { sidePanelStateLogic } from '~/layout/navigation-3000/sidepanel/sidePanelStateLogic'
import { projectTreeDataLogic } from '~/layout/panel-layout/ProjectTree/projectTreeDataLogic'
import { splitPath, unescapePath } from '~/layout/panel-layout/ProjectTree/utils'
import { dashboardsModel } from '~/models/dashboardsModel'
@@ -24,7 +25,7 @@ import { FileSystemEntry } from '~/queries/schema/schema-general'
import { sceneLogic } from '~/scenes/sceneLogic'
import { emptySceneParams } from '~/scenes/scenes'
import { Scene, SceneTab } from '~/scenes/sceneTypes'
-import { DashboardBasicType } from '~/types'
+import { DashboardBasicType, SidePanelTab } from '~/types'
import type { FeatureFlagsSet } from '../../../lib/logic/featureFlagLogic'
import type { Node } from '../../../queries/schema/schema-general'
@@ -473,7 +474,27 @@ export const aiFirstHomepageLogic = kea<aiFirstHomepageLogicType>([
})),
urlToAction(({ actions, values }) => ({
- [urls.projectHomepage()]: (_, searchParams) => {
+ [urls.projectHomepage()]: (_, searchParams, hashParams) => {
+ // A `#panel=max:<prompt>` link (e.g. app.posthog.com/#panel=max:...) means "open PostHog
+ // AI prefilled". Here the homepage already IS the full-scene PostHog AI, so consume the
+ // prompt into this scene instead of stacking a redundant Max side panel on top of it.
+ const panelHash = typeof hashParams.panel === 'string' ? hashParams.panel : undefined
+ if (panelHash) {
+ const [panel, ...rest] = panelHash.split(':')
+ if (panel === SidePanelTab.Max) {
+ const { question } = parseCommandString(rest.join(':'))
+ if (question) {
+ actions.setQuestion(question)
+ }
+ // enterAiMode rewrites the URL to ?mode=ai (dropping the hash), and closeSidePanel
+ // clears the panel hash too — so both undo the side panel that already opened and
+ // stop it replaying on refresh.
+ actions.enterAiMode('')
+ sidePanelStateLogic.actions.closeSidePanel(SidePanelTab.Max)
+ return
+ }
+ }
+
const urlMode = (searchParams.mode as HomepageMode) || 'idle'
const urlQuery = searchParams.q != null ? String(searchParams.q) : ''
const urlChat = (searchParams.chat as string) || ''
diff --git a/frontend/src/scenes/sceneLogic.test.tsx b/frontend/src/scenes/sceneLogic.test.tsx
index cb96f0fa369a..e2834d3ce567 100644
--- a/frontend/src/scenes/sceneLogic.test.tsx
+++ b/frontend/src/scenes/sceneLogic.test.tsx
@@ -183,5 +183,36 @@ describe('sceneLogic', () => {
expect(removeProjectIdIfPresent(router.values.location.pathname)).toEqual(urls.projectHomepage())
expect(router.values.searchParams).toEqual({ modal: 'feature' })
})
+
+ // Regression guard: `/#panel=max:<prompt>` pre-fills the Max side panel, but only if the
+ // hash survives the `/` → homepage redirect. It used to be dropped, so the prompt was lost
+ // on the Home scene (yet worked everywhere else, which have no such redirect).
+ it.each([
+ ['no homepage is configured', null, urls.projectHomepage()],
+ ['a dashboard homepage is configured', dashboardHomepage, urls.dashboard(42)],
+ ])('preserves the #panel hash across the / redirect when %s', async (_desc, homepage, expectedPathname) => {
+ logic.actions.setHomepage(homepage)
+ router.actions.push('/', {}, { panel: 'max:what is my dau' })
+ await expectLogic(logic).delay(1)
+ expect(removeProjectIdIfPresent(router.values.location.pathname)).toEqual(expectedPathname)
+ expect(router.values.hashParams).toEqual({ panel: 'max:what is my dau' })
+ })
+
+ // A configured homepage can carry its own hash (e.g. a dashboard tab). Forwarding the
+ // incoming hash must merge over that, not replace it — so an empty incoming hash keeps the
+ // configured one, and a `#panel` hash is added alongside it.
+ it.each([
+ ['no incoming hash', {}, { tab: 'configured' }],
+ ['an incoming #panel hash', { panel: 'max:hi' }, { tab: 'configured', panel: 'max:hi' }],
+ ])(
+ 'keeps a configured homepage hash across the / redirect with %s',
+ async (_desc, incomingHash, expectedHash) => {
+ logic.actions.setHomepage({ ...dashboardHomepage, hash: '#tab=configured' })
+ router.actions.push('/', {}, incomingHash)
+ await expectLogic(logic).delay(1)
+ expect(removeProjectIdIfPresent(router.values.location.pathname)).toEqual(urls.dashboard(42))
+ expect(router.values.hashParams).toEqual(expectedHash)
+ }
+ )
})
})
diff --git a/frontend/src/scenes/sceneLogic.tsx b/frontend/src/scenes/sceneLogic.tsx
index e8c3fe61fe2c..3ba7c6576730 100644
--- a/frontend/src/scenes/sceneLogic.tsx
+++ b/frontend/src/scenes/sceneLogic.tsx
@@ -12,7 +12,7 @@ import {
reducers,
selectors,
} from 'kea'
-import { router, urlToAction } from 'kea-router'
+import { combineUrl, router, urlToAction } from 'kea-router'
import type { LocationChangedPayload } from 'kea-router/lib/types'
import posthog from 'posthog-js'
import { useEffect, useState } from 'react'
@@ -198,6 +198,24 @@ export function withForwardedSearchParams(
return redirectUrlObj.pathname + redirectUrlObj.search + redirectUrlObj.hash
}
+/**
+ * Builds a redirect URL that carries the incoming hash (e.g. #panel=max:<prompt>, merged over any
+ * hash the target already has) plus the allow-listed query params, so URL-driven side panel prompts
+ * survive scene redirects like `/` → the homepage.
+ */
+function withForwardedHashAndSearchParams(
+ redirectUrl: string,
+ currentSearchParams: Params,
+ hashParams: Params,
+ forwardedQueryParams: string[]
+): string {
+ return withForwardedSearchParams(
+ combineUrl(redirectUrl, {}, hashParams).url,
+ currentSearchParams,
+ forwardedQueryParams
+ )
+}
+
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface sceneLogicValues {
billing: BillingType | null // billingLogic
@@ -970,7 +988,7 @@ export const sceneLogic = kea<sceneLogicType>([
// The Home button (via `/`) and a direct visit to /home should both land on the user's
// configured homepage (set in the Configure home modal). Redirect there unless we're
// already at it, which also guards against loops when the homepage is the launchpad itself.
- const redirectToConfiguredHomepage = (searchParams: Params): boolean => {
+ const redirectToConfiguredHomepage = (searchParams: Params, hashParams: Params): boolean => {
const homepage = values.homepage
if (!homepage) {
return false
@@ -979,11 +997,12 @@ export const sceneLogic = kea<sceneLogicType>([
if (removeProjectIdIfPresent(targetPathname) === '/') {
targetPathname = addProjectIdIfMissing(urls.projectHomepage())
}
- // Forward allow-listed params (e.g. modal) onto the homepage the same way the launchpad
- // redirect does, and compare against that final target so a forwarded param can't loop.
- const target = withForwardedSearchParams(
+ // Forward the incoming hash and allow-listed params (e.g. modal) onto the homepage, and
+ // compare against that final target so a forwarded param can't loop.
+ const target = withForwardedHashAndSearchParams(
targetPathname + (homepage.search || '') + (homepage.hash || ''),
searchParams,
+ hashParams,
forwardedRedirectQueryParams
)
const loc = router.values.currentLocation
@@ -994,12 +1013,17 @@ export const sceneLogic = kea<sceneLogicType>([
return true
}
- mapping['/'] = guardRoute((_params, searchParams) => {
- if (redirectToConfiguredHomepage(searchParams)) {
+ mapping['/'] = guardRoute((_params, searchParams, hashParams) => {
+ if (redirectToConfiguredHomepage(searchParams, hashParams)) {
return
}
router.actions.replace(
- withForwardedSearchParams(urls.projectHomepage(), searchParams, forwardedRedirectQueryParams)
+ withForwardedHashAndSearchParams(
+ urls.projectHomepage(),
+ searchParams,
+ hashParams,
+ forwardedRedirectQueryParams
+ )
)
})
@@ -1007,7 +1031,7 @@ export const sceneLogic = kea<sceneLogicType>([
for (const [path, [scene, sceneKey]] of Object.entries(routes)) {
mapping[path] = guardRoute((params, searchParams, hashParams, { method }) => {
// A direct visit to /home honors the configured homepage just like the Home button.
- if (path === projectHomepagePath && redirectToConfiguredHomepage(searchParams)) {
+ if (path === projectHomepagePath && redirectToConfiguredHomepage(searchParams, hashParams)) {
return
}
actions.openScene(