: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
refactor(side-panel): move the tab registry and share the panel frame (#119034)
Adam Leith685946c09238
diff --git a/frontend/src/layout/navigation-3000/sidepanel/SidePanel.tsx b/frontend/src/layout/navigation-3000/sidepanel/SidePanel.tsxindex 6c9cf6c9b75f..820c0da35f00 100644--- a/frontend/src/layout/navigation-3000/sidepanel/SidePanel.tsx+++ b/frontend/src/layout/navigation-3000/sidepanel/SidePanel.tsx@@ -1,181 +1,30 @@import './SidePanel.scss'import { useActions, useValues } from 'kea'-import { memo, useEffect, useRef } from 'react'--import { IconApps, IconChat, IconLock, IconLogomark, IconNotebook, IconPulse } from '@posthog/icons'+import { memo } from 'react'import { Resizer } from 'lib/components/Resizer/Resizer'-import { ResizerLogicProps, resizerLogic } from 'lib/components/Resizer/resizerLogic'import { cn } from 'lib/utils/css-classes'-import { lazyWithRetry } from 'lib/utils/retryImport'--const NotebookPanel = lazyWithRetry(() =>- import('scenes/notebooks/NotebookPanel/NotebookPanel').then((m) => ({ default: m.NotebookPanel }))-)-// The support panel renders the ticket editor, which pulls in tiptap extensions and lowlight.-const SidePanelSupport = lazyWithRetry(() =>- import('./panels/support/SidePanelSupport').then((m) => ({ default: m.SidePanelSupport }))-)-// Discussion renders the comment composer, which pulls in the rich content editor and emoji picker.-const SidePanelDiscussion = lazyWithRetry(() =>- import('./panels/discussion/SidePanelDiscussion').then((m) => ({ default: m.SidePanelDiscussion }))-)-const SidePanelAccessControl = lazyWithRetry(() =>- import('./panels/access_control/SidePanelAccessControl').then((m) => ({ default: m.SidePanelAccessControl }))-)-const SidePanelAccessDetail = lazyWithRetry(() =>- import('./panels/access_control/SidePanelAccessDetail').then((m) => ({ default: m.SidePanelAccessDetail }))-)-const SidePanelExports = lazyWithRetry(() =>- import('./panels/exports/SidePanelExports').then((m) => ({ default: m.SidePanelExports }))-)-const CanvasSidePanel = lazyWithRetry(() =>- import('products/canvas/frontend/sidePanel/CanvasSidePanel').then((m) => ({ default: m.CanvasSidePanel }))-)-const SidePanelActivity = lazyWithRetry(() =>- import('./panels/activity/SidePanelActivity').then((m) => ({ default: m.SidePanelActivity }))-)--import { useWindowSize } from 'lib/hooks/useWindowSize'import { themeLogic } from '~/layout/navigation-3000/themeLogic'-import { panelLayoutLogic } from '~/layout/panel-layout/panelLayoutLogic'-import { SidePanelTab } from '~/types'-import { SidePanelSupportIcon } from 'products/conversations/frontend/components/SidePanel/SidePanelSupportIcon'--import { SidePanelActivityIcon } from './panels/activity/SidePanelActivityIcon'-import { SidePanelDiscussionIcon } from './panels/discussion/SidePanelDiscussionIcon'-import { SidePanelExportsIcon } from './panels/exports/SidePanelExportsIcon'-import { SidePanelInfo, SidePanelInfoIcon } from './panels/info/SidePanelInfo'-import { SidePanelMax } from './panels/max/SidePanelMax'import { SidePanelContent } from './SidePanelContent'-import { sidePanelLogic } from './sidePanelLogic'import { sidePanelStateLogic } from './sidePanelStateLogic'--export const SIDE_PANEL_TABS: Record<SidePanelTab, { label: string; Icon: any; Content: any }> = {- [SidePanelTab.Max]: {- label: 'PostHog AI',- Icon: IconLogomark,- Content: SidePanelMax,- },- [SidePanelTab.Notebooks]: {- label: 'Notebooks',- Icon: IconNotebook,- Content: NotebookPanel,- },- [SidePanelTab.Support]: {- label: 'Support',- Icon: SidePanelSupportIcon,- Content: SidePanelSupport,- },- [SidePanelTab.Exports]: {- label: 'Exports',- Icon: SidePanelExportsIcon,- Content: SidePanelExports,- },- [SidePanelTab.Activity]: {- label: 'Activity logs',- Icon: SidePanelActivityIcon,- Content: SidePanelActivity,- },- [SidePanelTab.Discussion]: {- label: 'Discuss',- Icon: SidePanelDiscussionIcon,- Content: SidePanelDiscussion,- },- [SidePanelTab.AccessControl]: {- label: 'Access',- Icon: IconLock,- Content: SidePanelAccessControl,- },- [SidePanelTab.AccessDetail]: {- label: 'Access',- Icon: IconLock,- Content: SidePanelAccessDetail,- },- [SidePanelTab.Info]: {- label: 'Actions',- Icon: SidePanelInfoIcon,- Content: SidePanelInfo,- },- [SidePanelTab.CanvasChat]: {- label: 'Chat',- Icon: IconChat,- Content: CanvasSidePanel,- },- [SidePanelTab.CanvasBlocks]: {- label: 'Blocks',- Icon: IconApps,- Content: CanvasSidePanel,- },- [SidePanelTab.CanvasTimeline]: {- label: 'Timeline',- Icon: IconPulse,- Content: CanvasSidePanel,- },-}+import { SIDE_PANEL_TABS } from './sidePanelTabs'+import { SIDE_PANEL_MIN_WIDTH_COMPACT, useSidePanelFrame } from './useSidePanelFrame'const DEFAULT_WIDTH = 512-const SIDE_PANEL_MIN_WIDTH_COMPACT = 330export const SidePanel = memo(function SidePanel({ className }: { className?: string }): JSX.Element | null {const { theme } = useValues(themeLogic)- const { enabledTabs, visibleTabs } = useValues(sidePanelLogic)- const { selectedTab, sidePanelOpen } = useValues(sidePanelStateLogic)- const { openSidePanel, closeSidePanel, setSidePanelAvailable } = useActions(sidePanelStateLogic)-- const activeTab = sidePanelOpen && selectedTab-- // Use enabledTabs (not visibleTabs) so programmatically-opened tabs like Support render- const PanelContent = activeTab && enabledTabs.includes(activeTab) ? SIDE_PANEL_TABS[activeTab]?.Content : null-- const ref = useRef<HTMLDivElement>(null)-- const resizerLogicProps: ResizerLogicProps = {- containerRef: ref,- logicKey: 'side-panel',- persistent: true,- closeThreshold: 200,- placement: 'left',- onToggleClosed: (shouldBeClosed) => {- shouldBeClosed ? closeSidePanel() : selectedTab ? openSidePanel(selectedTab) : undefined- },- }-- const { desiredSize, isResizeInProgress } = useValues(resizerLogic(resizerLogicProps))- const { setSidePanelWidth } = useActions(panelLayoutLogic)-- useEffect(() => {- setSidePanelAvailable(true)- return () => {- setSidePanelAvailable(false)- }- }, [setSidePanelAvailable])-- // 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)-- const { windowSize } = useWindowSize()-- const rawSidePanelWidth = !visibleTabs.length- ? 0- : sidePanelOpenAndAvailable- ? Math.max(desiredSize ?? DEFAULT_WIDTH, SIDE_PANEL_MIN_WIDTH_COMPACT)- : 0-- const sidePanelWidth = windowSize.width != null ? Math.min(rawSidePanelWidth, windowSize.width) : rawSidePanelWidth-- // Update sidepanel width in panelLayoutLogic- useEffect(() => {- setSidePanelWidth(sidePanelWidth)- }, [sidePanelWidth, setSidePanelWidth])-- if (!visibleTabs.length) {+ const { sidePanelOpen } = useValues(sidePanelStateLogic)+ const { closeSidePanel } = useActions(sidePanelStateLogic)+ const { ref, resizerLogicProps, isResizeInProgress, activeTab, width, hasTabs } = useSidePanelFrame({+ defaultWidth: DEFAULT_WIDTH,+ })+ const PanelContent = activeTab ? SIDE_PANEL_TABS[activeTab]?.Content : null++ if (!hasTabs) {return null}@@ -183,7 +32,7 @@ export const SidePanel = memo(function SidePanel({ className }: { className?: st<divclassName={cn('SidePanel3000',- sidePanelOpenAndAvailable && 'SidePanel3000--open justify-end',+ activeTab && 'SidePanel3000--open justify-end',isResizeInProgress && 'SidePanel3000--resizing','@container/side-panel bg-surface-secondary absolute top-0 right-0 bottom-0 h-full flex flex-col border-t-none','[--side-panel-width:initial] [--scene-layout-rect-width:initial] [--scene-layout-rect-right:initial]',@@ -194,20 +43,20 @@ export const SidePanel = memo(function SidePanel({ className }: { className?: st// eslint-disable-next-line react/forbid-dom-propsstyle={{- width: sidePanelOpenAndAvailable ? sidePanelWidth : '0px',+ width: activeTab ? width : '0px','--side-panel-min-width': `${SIDE_PANEL_MIN_WIDTH_COMPACT}px`,...theme?.sidebarStyle,} as React.CSSProperties}id="side-panel">- {sidePanelOpenAndAvailable && (+ {activeTab && (<><Resizer{...resizerLogicProps}className={cn('top-[calc(var(--scene-layout-header-height)+8px)] left-[-1px] bottom-4', {- 'left-0': sidePanelOpenAndAvailable,- 'top-0 h-full': sidePanelOpenAndAvailable,+ 'left-0': !!activeTab,+ 'top-0 h-full': !!activeTab,})}/>{/* Overlay for mobile to click outside to close the side panel */}@@ -215,7 +64,7 @@ export const SidePanel = memo(function SidePanel({ className }: { className?: st</>)}- {PanelContent && <SidePanelContent activeTab={activeTab as SidePanelTab} Content={PanelContent} />}+ {activeTab && PanelContent && <SidePanelContent activeTab={activeTab} Content={PanelContent} />}</div>)})diff --git a/frontend/src/layout/navigation-3000/sidepanel/SidePanelNavigation.tsx b/frontend/src/layout/navigation-3000/sidepanel/SidePanelNavigation.tsxindex ddf7d731ade5..2283921f4a80 100644--- a/frontend/src/layout/navigation-3000/sidepanel/SidePanelNavigation.tsx+++ b/frontend/src/layout/navigation-3000/sidepanel/SidePanelNavigation.tsx@@ -11,9 +11,9 @@ import { cn } from 'lib/utils/css-classes'import { SidePanelTab } from '~/types'-import { SIDE_PANEL_TABS } from './SidePanel'import { sidePanelLogic } from './sidePanelLogic'import { sidePanelStateLogic } from './sidePanelStateLogic'+import { SIDE_PANEL_TABS } from './sidePanelTabs'interface SidePanelNavigationProps {activeTab: SidePanelTabdiff --git a/frontend/src/layout/navigation-3000/sidepanel/sidePanelTabs.tsx b/frontend/src/layout/navigation-3000/sidepanel/sidePanelTabs.tsxnew file mode 100644index 000000000000..c995f5e8c774--- /dev/null+++ b/frontend/src/layout/navigation-3000/sidepanel/sidePanelTabs.tsx@@ -0,0 +1,103 @@+import { IconApps, IconChat, IconLock, IconLogomark, IconNotebook, IconPulse } from '@posthog/icons'++import { lazyWithRetry } from 'lib/utils/retryImport'++import { SidePanelTab } from '~/types'++import { SidePanelSupportIcon } from 'products/conversations/frontend/components/SidePanel/SidePanelSupportIcon'++import { SidePanelActivityIcon } from './panels/activity/SidePanelActivityIcon'+import { SidePanelDiscussionIcon } from './panels/discussion/SidePanelDiscussionIcon'+import { SidePanelExportsIcon } from './panels/exports/SidePanelExportsIcon'+import { SidePanelInfo, SidePanelInfoIcon } from './panels/info/SidePanelInfo'+import { SidePanelMax } from './panels/max/SidePanelMax'++const NotebookPanel = lazyWithRetry(() =>+ import('scenes/notebooks/NotebookPanel/NotebookPanel').then((m) => ({ default: m.NotebookPanel }))+)+// The support panel renders the ticket editor, which pulls in tiptap extensions and lowlight.+const SidePanelSupport = lazyWithRetry(() =>+ import('./panels/support/SidePanelSupport').then((m) => ({ default: m.SidePanelSupport }))+)+// Discussion renders the comment composer, which pulls in the rich content editor and emoji picker.+const SidePanelDiscussion = lazyWithRetry(() =>+ import('./panels/discussion/SidePanelDiscussion').then((m) => ({ default: m.SidePanelDiscussion }))+)+const SidePanelAccessControl = lazyWithRetry(() =>+ import('./panels/access_control/SidePanelAccessControl').then((m) => ({ default: m.SidePanelAccessControl }))+)+const SidePanelAccessDetail = lazyWithRetry(() =>+ import('./panels/access_control/SidePanelAccessDetail').then((m) => ({ default: m.SidePanelAccessDetail }))+)+const SidePanelExports = lazyWithRetry(() =>+ import('./panels/exports/SidePanelExports').then((m) => ({ default: m.SidePanelExports }))+)+const CanvasSidePanel = lazyWithRetry(() =>+ import('products/canvas/frontend/sidePanel/CanvasSidePanel').then((m) => ({ default: m.CanvasSidePanel }))+)+const SidePanelActivity = lazyWithRetry(() =>+ import('./panels/activity/SidePanelActivity').then((m) => ({ default: m.SidePanelActivity }))+)++export const SIDE_PANEL_TABS: Record<SidePanelTab, { label: string; Icon: any; Content: any }> = {+ [SidePanelTab.Max]: {+ label: 'PostHog AI',+ Icon: IconLogomark,+ Content: SidePanelMax,+ },+ [SidePanelTab.Notebooks]: {+ label: 'Notebooks',+ Icon: IconNotebook,+ Content: NotebookPanel,+ },+ [SidePanelTab.Support]: {+ label: 'Support',+ Icon: SidePanelSupportIcon,+ Content: SidePanelSupport,+ },+ [SidePanelTab.Exports]: {+ label: 'Exports',+ Icon: SidePanelExportsIcon,+ Content: SidePanelExports,+ },+ [SidePanelTab.Activity]: {+ label: 'Activity logs',+ Icon: SidePanelActivityIcon,+ Content: SidePanelActivity,+ },+ [SidePanelTab.Discussion]: {+ label: 'Discuss',+ Icon: SidePanelDiscussionIcon,+ Content: SidePanelDiscussion,+ },+ [SidePanelTab.AccessControl]: {+ label: 'Access',+ Icon: IconLock,+ Content: SidePanelAccessControl,+ },+ [SidePanelTab.AccessDetail]: {+ label: 'Access',+ Icon: IconLock,+ Content: SidePanelAccessDetail,+ },+ [SidePanelTab.Info]: {+ label: 'Actions',+ Icon: SidePanelInfoIcon,+ Content: SidePanelInfo,+ },+ [SidePanelTab.CanvasChat]: {+ label: 'Chat',+ Icon: IconChat,+ Content: CanvasSidePanel,+ },+ [SidePanelTab.CanvasBlocks]: {+ label: 'Blocks',+ Icon: IconApps,+ Content: CanvasSidePanel,+ },+ [SidePanelTab.CanvasTimeline]: {+ label: 'Timeline',+ Icon: IconPulse,+ Content: CanvasSidePanel,+ },+}diff --git a/frontend/src/layout/navigation-3000/sidepanel/useSidePanelFrame.ts b/frontend/src/layout/navigation-3000/sidepanel/useSidePanelFrame.tsnew file mode 100644index 000000000000..3fde0fc5fc2c--- /dev/null+++ b/frontend/src/layout/navigation-3000/sidepanel/useSidePanelFrame.ts@@ -0,0 +1,74 @@+import { useActions, useValues } from 'kea'+import { RefObject, useEffect, useRef } from 'react'++import { ResizerLogicProps, resizerLogic } from 'lib/components/Resizer/resizerLogic'+import { useWindowSize } from 'lib/hooks/useWindowSize'++import { panelLayoutLogic } from '~/layout/panel-layout/panelLayoutLogic'+import { SidePanelTab } from '~/types'++import { sidePanelLogic } from './sidePanelLogic'+import { sidePanelStateLogic } from './sidePanelStateLogic'++export const SIDE_PANEL_MIN_WIDTH_COMPACT = 330++export interface SidePanelFrameOptions {+ defaultWidth: number+ maxWidth?: number+}++export interface SidePanelFrame {+ ref: RefObject<HTMLDivElement>+ resizerLogicProps: ResizerLogicProps+ isResizeInProgress: boolean+ activeTab: SidePanelTab | null+ width: number+ hasTabs: boolean+}++/** The side panel's resizable frame: its open tab, width and resizer, shared by every layout that hosts it. */+export function useSidePanelFrame({ defaultWidth, maxWidth = Infinity }: SidePanelFrameOptions): SidePanelFrame {+ const { enabledTabs, visibleTabs } = useValues(sidePanelLogic)+ const { selectedTab, sidePanelOpen } = useValues(sidePanelStateLogic)+ const { openSidePanel, closeSidePanel, setSidePanelAvailable } = useActions(sidePanelStateLogic)+ const { setSidePanelWidth } = useActions(panelLayoutLogic)+ const { windowSize } = useWindowSize()+ const ref = useRef<HTMLDivElement>(null)++ const resizerLogicProps: ResizerLogicProps = {+ containerRef: ref,+ logicKey: 'side-panel',+ persistent: true,+ closeThreshold: 200,+ placement: 'left',+ onToggleClosed: (shouldBeClosed) => {+ shouldBeClosed ? closeSidePanel() : selectedTab ? openSidePanel(selectedTab) : undefined+ },+ }+ const { desiredSize, isResizeInProgress } = useValues(resizerLogic(resizerLogicProps))++ useEffect(() => {+ setSidePanelAvailable(true)+ return () => {+ setSidePanelAvailable(false)+ }+ }, [setSidePanelAvailable])++ // 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 activeTab = selectedTab && sidePanelOpen && enabledTabs.includes(selectedTab) ? selectedTab : null++ const rawWidth =+ visibleTabs.length && activeTab+ ? Math.min(Math.max(desiredSize ?? defaultWidth, SIDE_PANEL_MIN_WIDTH_COMPACT), maxWidth)+ : 0+ const width = windowSize.width != null ? Math.min(rawWidth, windowSize.width) : rawWidth++ useEffect(() => {+ setSidePanelWidth(width)+ }, [width, setSidePanelWidth])++ return { ref, resizerLogicProps, isResizeInProgress, activeTab, width, hasTabs: visibleTabs.length > 0 }+}