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(aio): step the trace drawer with n and p (#128306)

Carlos Marchal3e24efac441d
diff --git a/products/ai_observability/frontend/redesign/trace/TraceDrawerHost.test.tsx b/products/ai_observability/frontend/redesign/trace/TraceDrawerHost.test.tsx
index 28df6141414f..0db531c02458 100644
--- a/products/ai_observability/frontend/redesign/trace/TraceDrawerHost.test.tsx
+++ b/products/ai_observability/frontend/redesign/trace/TraceDrawerHost.test.tsx
@@ -1,6 +1,8 @@
import { RenderResult, act, cleanup, fireEvent, render, waitFor } from '@testing-library/react'
import { combineUrl, router } from 'kea-router'
+import { LemonButton, LemonModal } from '@posthog/lemon-ui'
+
import { FEATURE_FLAGS } from 'lib/constants'
import { featureFlagLogic } from 'lib/logic/featureFlagLogic'
import { urls } from 'scenes/urls'
@@ -178,6 +180,31 @@ describe('TraceDrawerHost', () => {
}
)
+ it('steps with n and p, but not while typing or with a modal open', async () => {
+ useTraceMocks(answerTrace('a'))
+ setUp(drawerUrl(ROW_A))
+ const view = renderHost([ROW_A, ROW_B])
+ await view.findAllByText('answer-a')
+
+ fireEvent.keyDown(attr(view.container, 'trace-view-search-input'), { key: 'n' })
+ expect(router.values.searchParams.trace).toBe('a')
+
+ const modal = render(
+ <LemonModal isOpen onClose={() => {}} title="Edit dataset item">
+ <LemonButton>Save</LemonButton>
+ </LemonModal>
+ )
+ fireEvent.keyDown(modal.getByText('Save'), { key: 'n' })
+ modal.unmount()
+ expect(router.values.searchParams.trace).toBe('a')
+
+ fireEvent.keyDown(document.body, { key: 'n' })
+ await waitFor(() => expect(router.values.searchParams.trace).toBe('b'))
+
+ fireEvent.keyDown(document.body, { key: 'p' })
+ await waitFor(() => expect(router.values.searchParams.trace).toBe('a'))
+ })
+
it('reads no node when it closes on a step other than the default', async () => {
useTraceMocks(answerTrace('a'))
setUp(combineUrl(LIST_PATH, { trace: 'a', ts: ROW_A.timestamp ?? undefined, step: 'span-1' }).url)
diff --git a/products/ai_observability/frontend/redesign/trace/TraceDrawerHost.tsx b/products/ai_observability/frontend/redesign/trace/TraceDrawerHost.tsx
index 9bc25b918abf..8f299a40b8f0 100644
--- a/products/ai_observability/frontend/redesign/trace/TraceDrawerHost.tsx
+++ b/products/ai_observability/frontend/redesign/trace/TraceDrawerHost.tsx
@@ -1,6 +1,7 @@
import { useActions, useValues } from 'kea'
import { useRef } from 'react'
+import { useShortcut } from 'lib/components/Shortcuts/useShortcut'
import { Resizer } from 'lib/lemon-ui/Resizer/Resizer'
import { ResizerLogicProps, resizerLogic } from 'lib/lemon-ui/Resizer/resizerLogic'
@@ -9,8 +10,9 @@ import { panelLayoutLogic } from '~/layout/panel-layout/panelLayoutLogic'
import { DetailPanel, isDetailPanelFullWidth } from '../shared/components/DetailPanel'
import { TraceViewContainer } from './adapter/TraceViewContainer'
import { TraceDrawerToolbar } from './components/TraceDrawerToolbar'
+import { isOverlayOpen } from './shouldCloseOnEscape'
import type { TraceDrawerListId } from './traceDrawerLists'
-import { traceDrawerLogic } from './traceDrawerLogic'
+import { TraceStepDirection, traceDrawerLogic } from './traceDrawerLogic'
export interface TraceDrawerHostProps {
listId: TraceDrawerListId
@@ -32,6 +34,29 @@ export function TraceDrawerHost({ listId }: TraceDrawerHostProps): JSX.Element |
}
const { desiredSize } = useValues(resizerLogic(resizerProps))
+ // Stepping unmounts the trace view, and with it any modal opened from it, so a key never steps under an open overlay.
+ const stepUnlessOverlayOpen = (direction: TraceStepDirection): void => {
+ if (!isOverlayOpen()) {
+ stepTo(direction)
+ }
+ }
+ useShortcut({
+ name: 'TraceDrawerPrevious',
+ keybind: [['p']],
+ intent: 'Previous trace',
+ interaction: 'function',
+ callback: () => stepUnlessOverlayOpen('previous'),
+ disabled: !previousTrace,
+ })
+ useShortcut({
+ name: 'TraceDrawerNext',
+ keybind: [['n']],
+ intent: 'Next trace',
+ interaction: 'function',
+ callback: () => stepUnlessOverlayOpen('next'),
+ disabled: !nextTrace,
+ })
+
if (!isEnabled || !openRef || !fullPageHref || !mainContentRect) {
return null
}
diff --git a/products/ai_observability/frontend/redesign/trace/components/TraceDrawerToolbar.tsx b/products/ai_observability/frontend/redesign/trace/components/TraceDrawerToolbar.tsx
index 5fab3fe93805..cd2ca69c5e07 100644
--- a/products/ai_observability/frontend/redesign/trace/components/TraceDrawerToolbar.tsx
+++ b/products/ai_observability/frontend/redesign/trace/components/TraceDrawerToolbar.tsx
@@ -28,7 +28,11 @@ export function TraceDrawerToolbar({
<LemonButton
size="small"
icon={<IconChevronDown className="rotate-180" />}
- tooltip="Previous trace"
+ tooltip={
+ <>
+ Previous trace <KeyboardShortcut p />
+ </>
+ }
aria-label="Previous trace"
data-attr="trace-drawer-previous"
onClick={onPrevious ?? undefined}
@@ -37,14 +41,18 @@ export function TraceDrawerToolbar({
<LemonButton
size="small"
icon={<IconChevronDown />}
- tooltip="Next trace"
+ tooltip={
+ <>
+ Next trace <KeyboardShortcut n />
+ </>
+ }
aria-label="Next trace"
data-attr="trace-drawer-next"
onClick={onNext ?? undefined}
disabledReason={onNext ? undefined : position ? 'This is the last loaded trace' : NOT_IN_LIST_REASON}
/>
{position ? (
- <span className="ml-1 whitespace-nowrap text-xs text-secondary">
+ <span className="ml-1 whitespace-nowrap text-xs text-secondary" aria-live="polite">
{`${position.index + 1} of ${position.total}`}
</span>
) : null}
diff --git a/products/ai_observability/frontend/redesign/trace/shouldCloseOnEscape.ts b/products/ai_observability/frontend/redesign/trace/shouldCloseOnEscape.ts
index c07d1e24a34a..d69355ba23e1 100644
--- a/products/ai_observability/frontend/redesign/trace/shouldCloseOnEscape.ts
+++ b/products/ai_observability/frontend/redesign/trace/shouldCloseOnEscape.ts
@@ -1,11 +1,16 @@
const EDITABLE_TARGET = 'input, textarea, [contenteditable]:not([contenteditable="false"])'
-// A modal or popover owns Escape while it is open.
-const ESCAPE_OWNERS = '.ReactModal__Overlay, .Popover'
+// A modal or popover owns the keyboard while it is open. react-modal appends a custom overlay class
+// after its own, so `.ReactModal__Overlay` also matches LemonModal, LemonDialog and LemonDrawer.
+const KEYBOARD_OWNERS = '.ReactModal__Overlay, .Popover'
+
+export function isOverlayOpen(): boolean {
+ return document.querySelector(KEYBOARD_OWNERS) !== null
+}
export function shouldCloseOnEscape(event: KeyboardEvent): boolean {
if (event.key !== 'Escape' || event.defaultPrevented) {
return false
}
const isEditing = event.target instanceof Element && event.target.closest(EDITABLE_TARGET) !== null
- return !isEditing && document.querySelector(ESCAPE_OWNERS) === null
+ return !isEditing && !isOverlayOpen()
}