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.

feat(side-panel): close context-bound tabs when leaving the scene (#75919)

Alex Liderf823a89c9ec3
diff --git a/frontend/snapshots.yml b/frontend/snapshots.yml
index 41751b360dd4..fb4ed44be54b 100644
--- a/frontend/snapshots.yml
+++ b/frontend/snapshots.yml
@@ -7131,13 +7131,13 @@ snapshots:
scenes-app-settings-user--settings-user-reminders-modal--light:
hash: v1.k794b7964.fbcf3f0f69a5b43c28ea7537ce6f1aab3c21e70f9c2cbb6fdfef4546dadba86c.yYIPurSofuNP1kZXYzNBOa3YU5Zy7VLaSegvGrNZ8Mw
scenes-app-sidepanels--side-panel-access-control--dark:
- hash: v1.k794b7964.f17439abc9b2f4bda8f86d2549ea2f2f315d57df346e0e32f04f251377828b62.ZrvqHM9LEHcN6tSqfybToBxgFC4pLc6Tm1ZCjWj2D4A
+ hash: v1.k794b7964.e60d6e783c0b64700d190f4da0038804e03d065fd1be369709593f6fc285de7a.XlBW5ltTpM8EtAheQN55i19lzsSLDXYkbfGgycuB7Ro
scenes-app-sidepanels--side-panel-access-control--light:
- hash: v1.k794b7964.df9522e07333e83daf56ade57a3f49f93b22336aa35b4446b42def56984ee682.Nqhj4Nmor1i3-j1wsLqJ2PwcHwGj_LeRNcm-A39jphs
+ hash: v1.k794b7964.7b80e3ea9692d71b7edde6bc47d922e05742352bf80d71718c0f7fbec8450c3f._gaUVh_XlJYeC9peXYzWNMwn5U0ki37G-_1arIGeIls
scenes-app-sidepanels--side-panel-activity--dark:
- hash: v1.k794b7964.f0d28c71ea60e37ca2ba69297689e3d37c4296119999d829196073c5a8e869de.QXT5jt5M1GDwiOPkk66KNUd2iRLWtEbWv9Of6GczHec
+ hash: v1.k794b7964.ebfccf7dea5f456bab7bcf09adfd596e027c67bcfd591ec5439bf95a720c0bff.rQrpt2FWuM0a_Mf9s9h5amOFZxOhlBKl9JaeYBu-Ct8
scenes-app-sidepanels--side-panel-activity--light:
- hash: v1.k794b7964.9a7a85ff60769daaeaa660ec6de1883aaed3aa986a4813eb5a870eb472d74ff2.SNW2Fq1YE1AGvfl7M8Zumrij9hFaLxvimTRkajRSoRY
+ hash: v1.k794b7964.3240283bc754a6a8d44094a513b971223351d32d79def8f42e7abe7b7671bc3b.8MfJItPbvyodK_q9OjV25G81usif0qKAnSUsqM5CEUY
scenes-app-sidepanels--side-panel-discussion--dark:
hash: v1.k794b7964.0fd39b510066088b538009beccfec961f59c794f84d30632df6ede5ba52601f6.sH1JnKc2SKRgefMkPpkifYRvOPdc9ZPrsC0QqPkWVZc
scenes-app-sidepanels--side-panel-discussion--light:
@@ -7147,9 +7147,9 @@ snapshots:
scenes-app-sidepanels--side-panel-exports--light:
hash: v1.k794b7964.7df2f87888b4546a00efe016a6a4e8b0d44b4b78fcfe0e9ccf0f8be1705922c6.f01kqsTsCFl1uO8DPcXdk--LzZd3tWQuwg_oFwcDsWg
scenes-app-sidepanels--side-panel-info--dark:
- hash: v1.k794b7964.f17439abc9b2f4bda8f86d2549ea2f2f315d57df346e0e32f04f251377828b62.ggqS_aXxe8myZbwo71ICZkm712fHjE1ENAMDmW1imx8
+ hash: v1.k794b7964.d8f17e51bf9c3397de48fb337c37cc429dad662e8fc68a61456453772ec0c4bc.Rudohv1e06RYmzlA39EC1r6iav0vT8KW_75lQTLQBgI
scenes-app-sidepanels--side-panel-info--light:
- hash: v1.k794b7964.df9522e07333e83daf56ade57a3f49f93b22336aa35b4446b42def56984ee682.UWJbR-5MPUWR0Ebzw0aDwFIrM9iFR6I7ha7v2O1PTYQ
+ hash: v1.k794b7964.3455622154d4d5ff0514d6dc3672863cfabcc7f1d1051964c3e5e9681829b80a.Xsk9QO4o-WbRZX8LSOBr-GUmA0RuSwjcZOZPMt3aUGI
scenes-app-sidepanels--side-panel-max--dark:
hash: v1.k794b7964.f17439abc9b2f4bda8f86d2549ea2f2f315d57df346e0e32f04f251377828b62.MJvCiu8-npFjoN7gEIJ-Y7ntOc5_cpuHJcTzCLyyxqo
scenes-app-sidepanels--side-panel-max--light:
diff --git a/frontend/src/layout/navigation-3000/sidepanel/SidePanel.stories.tsx b/frontend/src/layout/navigation-3000/sidepanel/SidePanel.stories.tsx
index 8fe169997f41..003284882353 100644
--- a/frontend/src/layout/navigation-3000/sidepanel/SidePanel.stories.tsx
+++ b/frontend/src/layout/navigation-3000/sidepanel/SidePanel.stories.tsx
@@ -4,14 +4,24 @@ import { useActions } from 'kea'
import { FEATURE_FLAGS } from 'lib/constants'
import { useOnMountEffect } from 'lib/hooks/useOnMountEffect'
import { App } from 'scenes/App'
+import featureFlagsFixture from 'scenes/feature-flags/__mocks__/feature_flags.json'
import { urls } from 'scenes/urls'
import { mswDecorator } from '~/mocks/browser'
-import { SidePanelTab } from '~/types'
+import { useAvailableFeatures } from '~/mocks/features'
+import { AccessControlLevel, AvailableFeature, SidePanelTab } from '~/types'
import { sidePanelStateLogic } from './sidePanelStateLogic'
-type StoryArgs = { panel: SidePanelTab }
+type StoryArgs = { panel: SidePanelTab; availableFeatures?: AvailableFeature[] }
+
+/**
+ * Activity, Access and Info only appear once a scene exposes an object behind them, so those stories
+ * open a feature flag rather than the dashboard list. Without it they render whatever tab the panel
+ * falls back to instead of the one they are named after.
+ */
+const OBJECT_SCENE_FLAG_ID = 1779
+const objectScenePageUrl = urls.featureFlag(OBJECT_SCENE_FLAG_ID)
const meta: Meta<StoryArgs> = {
component: App,
@@ -25,7 +35,9 @@ const meta: Meta<StoryArgs> = {
includeNavigationInSnapshot: true,
},
},
- render: ({ panel }) => {
+ render: ({ panel, availableFeatures }) => {
+ // Entitlements are module-level state shared between stories, so every story sets them
+ useAvailableFeatures(availableFeatures ?? [])
const { openSidePanel } = useActions(sidePanelStateLogic)
useOnMountEffect(() => openSidePanel(panel))
return <App />
@@ -42,9 +54,36 @@ const meta: Meta<StoryArgs> = {
'/api/projects/:id/surveys/responses_count/': { results: [] },
'/api/environments/:team_id/exports/': { results: [] },
'/api/environments/:team_id/events': { results: [] },
+ '/api/projects/:team_id/feature_flags/:flagId/': ({ params }) => [
+ 200,
+ featureFlagsFixture.results.find((flag) => flag.id === Number(params['flagId'])),
+ ],
+ '/api/projects/:team_id/feature_flags/:flagId/status': {
+ status: 'active',
+ reason: 'Feature flag is active',
+ },
+ '/api/projects/:team_id/feature_flags/:flagId/activity': { results: [], count: 0 },
+ '/api/projects/:team_id/feature_flags/:flagId/access_controls': {
+ access_controls: [],
+ available_access_levels: [
+ AccessControlLevel.None,
+ AccessControlLevel.Viewer,
+ AccessControlLevel.Editor,
+ ],
+ user_access_level: AccessControlLevel.Editor,
+ default_access_level: AccessControlLevel.Viewer,
+ user_can_edit_access_levels: true,
+ },
+ '/api/environments/:team_id/default_evaluation_contexts/': {
+ default_evaluation_contexts: [],
+ available_contexts: [],
+ hidden_contexts: [],
+ enabled: false,
+ },
},
post: {
'/api/environments/:team_id/query/:kind': {},
+ '/api/projects/:team_id/feature_flags/user_blast_radius/': { affected: 120, total: 2000 },
},
}),
],
@@ -62,9 +101,10 @@ export const SidePanelMax: Story = {
}
export const SidePanelActivity: Story = {
- args: { panel: SidePanelTab.Activity },
+ // The tab needs the audit logs entitlement, which the feature flags below don't grant
+ args: { panel: SidePanelTab.Activity, availableFeatures: [AvailableFeature.AUDIT_LOGS] },
parameters: {
- pageUrl: urls.dashboard('1'),
+ pageUrl: objectScenePageUrl,
featureFlags: [FEATURE_FLAGS.CDP_ACTIVITY_LOG_NOTIFICATIONS, FEATURE_FLAGS.AUDIT_LOGS_ACCESS],
},
}
@@ -74,11 +114,17 @@ export const SidePanelDiscussion: Story = {
}
export const SidePanelAccessControl: Story = {
- args: { panel: SidePanelTab.AccessControl },
+ // Without the entitlements the panel renders the PayGateMini upsell instead of the permissions UI
+ args: {
+ panel: SidePanelTab.AccessControl,
+ availableFeatures: [AvailableFeature.ACCESS_CONTROL, AvailableFeature.ROLE_BASED_ACCESS],
+ },
+ parameters: { pageUrl: objectScenePageUrl },
}
export const SidePanelInfo: Story = {
args: { panel: SidePanelTab.Info },
+ parameters: { pageUrl: objectScenePageUrl },
}
export const SidePanelExports: Story = {
diff --git a/frontend/src/layout/navigation-3000/sidepanel/SidePanel.tsx b/frontend/src/layout/navigation-3000/sidepanel/SidePanel.tsx
index 712bd058f1a8..ed2742fe1817 100644
--- a/frontend/src/layout/navigation-3000/sidepanel/SidePanel.tsx
+++ b/frontend/src/layout/navigation-3000/sidepanel/SidePanel.tsx
@@ -123,17 +123,12 @@ export function SidePanel({ className }: { className?: string }): JSX.Element |
}
}, [desiredSize, sidePanelOpen, setMainContentRect, mainContentRef])
+ // A tab is unavailable both when the scene will never offer it and while the scene is still
+ // loading the object it describes. Staying open renders nothing until the tab resolves, which
+ // keeps deep links like #panel=access-control working on a cold load. Leaving a scene is what
+ // closes the panel, and sidePanelLogic handles that.
const sidePanelOpenAndAvailable = selectedTab && sidePanelOpen && enabledTabs.includes(selectedTab)
- // If the selected tab is no longer available (e.g. navigating away from a scene
- // with Settings or Info), fall back to Info or Max instead of closing
- useEffect(() => {
- if (sidePanelOpen && selectedTab && !sidePanelOpenAndAvailable) {
- const fallbackTab = enabledTabs.includes(SidePanelTab.Info) ? SidePanelTab.Info : SidePanelTab.Max
- openSidePanel(fallbackTab)
- }
- }, [sidePanelOpen, selectedTab, sidePanelOpenAndAvailable, enabledTabs, openSidePanel])
-
const { windowSize } = useWindowSize()
const rawSidePanelWidth = !visibleTabs.length
diff --git a/frontend/src/layout/navigation-3000/sidepanel/sidePanelLogic.test.ts b/frontend/src/layout/navigation-3000/sidepanel/sidePanelLogic.test.ts
new file mode 100644
index 000000000000..a25245c42250
--- /dev/null
+++ b/frontend/src/layout/navigation-3000/sidepanel/sidePanelLogic.test.ts
@@ -0,0 +1,85 @@
+import { router } from 'kea-router'
+import { expectLogic } from 'kea-test-utils'
+
+import api from 'lib/api'
+import { featureFlagLogic } from 'lib/logic/featureFlagLogic'
+import { sceneLogic } from 'scenes/sceneLogic'
+import { Scene } from 'scenes/sceneTypes'
+import { teamLogic } from 'scenes/teamLogic'
+import { urls } from 'scenes/urls'
+
+import { initKeaTests } from '~/test/init'
+import { SidePanelTab } from '~/types'
+
+import { sidePanelLogic } from './sidePanelLogic'
+import { sidePanelStateLogic } from './sidePanelStateLogic'
+
+jest.mock('lib/api', () => ({
+ __esModule: true,
+ default: {
+ get: jest.fn(),
+ update: jest.fn(),
+ create: jest.fn(),
+ },
+}))
+
+const sceneImport = (): any => ({ scene: { component: () => null } })
+
+const testScenes: Record<string, () => any> = {
+ [Scene.DataManagement]: sceneImport,
+ [Scene.Settings]: sceneImport,
+}
+
+describe('sidePanelLogic', () => {
+ let logic: ReturnType<typeof sidePanelLogic.build>
+
+ const navigate = async (url: string, hashParams?: Record<string, any>): Promise<void> => {
+ router.actions.push(url, {}, hashParams)
+ await expectLogic(sceneLogic).toDispatchActions(['setScene'])
+ }
+
+ beforeEach(async () => {
+ initKeaTests()
+ ;(api.get as jest.Mock).mockResolvedValue({ tabs: [], homepage: null })
+ ;(api.update as jest.Mock).mockResolvedValue({ tabs: [], homepage: null })
+ await expectLogic(teamLogic).toDispatchActions(['loadCurrentTeamSuccess'])
+ featureFlagLogic.mount()
+ sceneLogic.build({ scenes: testScenes }).mount()
+ logic = sidePanelLogic.build()
+ logic.mount()
+ await navigate(urls.eventDefinitions())
+ })
+
+ it('closes a context-bound tab when navigating to a different scene', async () => {
+ sidePanelStateLogic.actions.openSidePanel(SidePanelTab.Activity)
+ await navigate(urls.settings('user'))
+ await expectLogic(sidePanelStateLogic).toMatchValues({ sidePanelOpen: false })
+ })
+
+ it.each([SidePanelTab.Max, SidePanelTab.Support, SidePanelTab.Notebooks])(
+ 'keeps the %s tab open when navigating to a different scene',
+ async (tab) => {
+ sidePanelStateLogic.actions.openSidePanel(tab)
+ await navigate(urls.settings('user'))
+ await expectLogic(sidePanelStateLogic).toMatchValues({ sidePanelOpen: true, selectedTab: tab })
+ }
+ )
+
+ it('stays open when navigating within the same scene', async () => {
+ await navigate(urls.settings('user'))
+ sidePanelStateLogic.actions.openSidePanel(SidePanelTab.Discussion)
+ await navigate(urls.settings('project'))
+ await expectLogic(sidePanelStateLogic).toMatchValues({
+ sidePanelOpen: true,
+ selectedTab: SidePanelTab.Discussion,
+ })
+ })
+
+ it('does not close a panel deep-linked via #panel= on the destination URL', async () => {
+ await navigate(urls.settings('user'), { panel: 'discussion' })
+ await expectLogic(sidePanelStateLogic).toMatchValues({
+ sidePanelOpen: true,
+ selectedTab: SidePanelTab.Discussion,
+ })
+ })
+})
diff --git a/frontend/src/layout/navigation-3000/sidepanel/sidePanelLogic.tsx b/frontend/src/layout/navigation-3000/sidepanel/sidePanelLogic.tsx
index f14da97ae7e8..5810da0a1068 100644
--- a/frontend/src/layout/navigation-3000/sidepanel/sidePanelLogic.tsx
+++ b/frontend/src/layout/navigation-3000/sidepanel/sidePanelLogic.tsx
@@ -1,7 +1,8 @@
-import { MakeLogicType, connect, kea, path, selectors } from 'kea'
+import { MakeLogicType, connect, kea, listeners, path, selectors } from 'kea'
import { combineUrl, router, urlToAction } from 'kea-router'
import { preflightLogic } from 'scenes/PreflightCheck/preflightLogic'
+import { sceneLogic } from 'scenes/sceneLogic'
import { teamLogic } from 'scenes/teamLogic'
import { urls } from 'scenes/urls'
import { userLogic } from 'scenes/userLogic'
@@ -23,6 +24,13 @@ const TABS_REQUIRING_A_TEAM = [
SidePanelTab.Exports,
]
+/**
+ * Tabs whose content is not about the current page (an AI chat, a support ticket draft, a notebook),
+ * so they stay open across navigation. Every other tab describes the object on screen and closes
+ * when the user moves to a different scene.
+ */
+export const TABS_PERSISTED_ACROSS_NAVIGATION = [SidePanelTab.Max, SidePanelTab.Support, SidePanelTab.Notebooks]
+
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface sidePanelLogicValues {
isCloudOrDev: boolean | undefined // preflightLogic
@@ -150,6 +158,30 @@ export const sidePanelLogic = kea<sidePanelLogicType>([
},
],
}),
+ listeners(({ actions, values }) => ({
+ // Close context-bound tabs when the user moves to a different scene: their content describes
+ // the page being left. Comparing scenes (not URLs) keeps the panel open across in-page tab
+ // switches, filter changes, and search params. Tabs in TABS_PERSISTED_ACROSS_NAVIGATION hold
+ // cross-page state and survive.
+ [sceneLogic.actionTypes.setScene]: ({ sceneId }: { sceneId: string }, _: any, __: any, previousState: any) => {
+ if (!values.sidePanelOpen) {
+ return
+ }
+ // Not the sceneId selector: setExportedScene already wrote the new sceneId before setScene fires
+ const previousSceneId = sceneLogic.selectors.lastSetScenePayload(previousState)?.sceneId
+ if (!previousSceneId || previousSceneId === sceneId) {
+ return
+ }
+ // An explicit #panel= hash on the destination URL (deep link) wins over closing
+ if (router.values.hashParams['panel']) {
+ return
+ }
+ if (values.selectedTab && TABS_PERSISTED_ACROSS_NAVIGATION.includes(values.selectedTab)) {
+ return
+ }
+ actions.closeSidePanel()
+ },
+ })),
urlToAction(() => {
return {
'/': (_, _searchParams, hashParams): void => {