'use client'; import { ChevronDown, ChevronUp, X } from 'lucide-react'; import { type ReactNode, useEffect, useRef } from 'react'; import { IconButton } from '../../ui/icon-button'; import { Kbd } from '../../ui/kbd'; import { Tooltip, TooltipContent, TooltipTrigger } from '../../ui/tooltip'; import { PANEL_DEFAULT_WIDTH, PANEL_MIN_WIDTH } from './detail-panel-width'; import { DraggableBorder } from './draggable-border'; import { isTypingTarget } from './use-alt-held'; import type { DetailPanelWidth } from './use-detail-panel-width'; /** * The right-hand details aside shared by the Trace and Events tabs: resizable * left border, a header with the title, prev/next and close, and a scrolling * body. While mounted it handles the shortcuts its header advertises (K or * ArrowUp, J or ArrowDown, Escape) anywhere on the page, so mount it only * while an item is selected. The width lives with the owner * (`useDetailPanelWidth`), which stays mounted while the panel comes and goes. */ export function DetailPanelShell({ id, width, title, itemLabel, actions, onPrev, onNext, onClose, children, }: { id: string; width: DetailPanelWidth; title: ReactNode; /** What one item is called in labels, e.g. "span" or "event". */ itemLabel: string; actions?: ReactNode; onPrev?: () => void; onNext?: () => void; onClose: () => void; children: ReactNode; }): ReactNode { const asideRef = useRef(null); // Read through a ref so the listener is registered once, whatever the // identity of the callbacks the owner passes on each render. const handlersRef = useRef({ onPrev, onNext, onClose }); handlersRef.current = { onPrev, onNext, onClose }; useEffect(() => { const onKeyDown = (e: KeyboardEvent): void => { // Leave keys typed into a field, and presses another handler took (a // tree row moving focus, a search box clearing itself). if (e.defaultPrevented || isTypingTarget(e.target)) return; const handlers = handlersRef.current; let handler: (() => void) | undefined; if (e.key === 'k' || e.key === 'ArrowUp') handler = handlers.onPrev; else if (e.key === 'j' || e.key === 'ArrowDown') handler = handlers.onNext; else if (e.key === 'Escape') handler = handlers.onClose; if (!handler) return; e.preventDefault(); handler(); }; window.addEventListener('keydown', onKeyDown); return () => window.removeEventListener('keydown', onKeyDown); }, []); return ( ); }