Skip to content

πŸͺ’ Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

27.8 KB
Raw Blame
import { useDefaultLayout } from "@/src/components/ui/resizable";
import { useRouter } from "next/router";
import { StringParam, useQueryParam } from "use-query-params";
import {
  Group,
  Separator,
  Panel,
  usePanelRef,
  useGroupRef,
  type PanelImperativeHandle,
} from "react-resizable-panels";
import {
  useEffect,
  useLayoutEffect,
  useRef,
  useMemo,
  createContext,
  useContext,
  type ReactNode,
} from "react";
import { PanelRightOpen } from "@langfuse/icons";
import { Button } from "@/src/components/ui/button";
import { cn } from "@/src/utils/tailwind";
import { useViewPreferences } from "@/src/features/traces/contexts/ViewPreferencesProvider";
import { useSelection } from "@/src/features/traces/contexts/SelectionProvider";
import { TraceReviewLayout } from "./TraceReviewLayout";
import { resolveEffectiveWidthFraction } from "@/src/components/table/peek/store/peekPanelStore";
import { usePeekTableState } from "@/src/components/table/peek/contexts/PeekTableStateProvider";

const RESIZABLE_PANEL_HANDLE_ID = "trace-layout-handle";
const RESIZABLE_PANEL_NAVIGATION_ID = "trace-layout-panel-navigation";
const RESIZABLE_PANEL_PREVIEW_ID = "trace-layout-panel-preview";

// Default split (LFE-10601 peek, LFE-10729 full-page). We size the
// tree/timeline (the index) to a comfortable band and give the *rest* to the
// detail panel (the content), so on a wide container the extra width flows to
// info, not the tree β€” killing the old "very wide tree, cramped info" that the
// 60/40 share produced on big screens.
//
// We express this as an explicit *percentage* layout computed from the known
// container width, NOT as a px `defaultSize`. The library converts a px
// defaultSize to a percentage against whatever group width it happens to
// measure first, which on a peek is a transient mid-open value β€” so the
// resolved ratio (and thus what gets persisted) was non-deterministic. A
// percentage is width-independent, so the default is stable across opens.
const INFO_COMFORTABLE_TARGET_PX = 560; // info wants ~this to read JSON/scores
const NAV_COMFORTABLE_MIN_PX = 340; // tree's comfortable floor (> the 260 hard min)
const NAV_COMFORTABLE_MAX_PX = 460; // never default the tree wider than this

// Tree/info split (as a nav-panel percentage) for a container of
// `containerWidthPx`: give info its comfortable target, hand the remainder to
// the tree clamped to its comfortable band. Returns undefined when the width is
// unknown (SSR).
function computeNavPercent(containerWidthPx: number): number | undefined {
  if (!(containerWidthPx > 0)) return undefined;
  const navPx = Math.min(
    NAV_COMFORTABLE_MAX_PX,
    Math.max(
      NAV_COMFORTABLE_MIN_PX,
      containerWidthPx - INFO_COMFORTABLE_TARGET_PX,
    ),
  );
  // Keep it a sane share; the panels' own min/collapse constraints do the rest.
  return Math.min(90, Math.max(10, (navPx / containerWidthPx) * 100));
}

// Min widths of the two collapsible panels (kept here so the Panel props and the
// toggle/scroll logic below can't drift apart).
//
// react-resizable-panels is percentage-based and its constraint solver refuses
// to shrink a panel below its min. In a narrow peek the container can be too
// small to satisfy BOTH mins at once. The previous fix (LFE-10547) reacted by
// force-collapsing the opposite panel so only one was ever open when narrow β€”
// too radical: users want both panels open at once.
//
// Instead we let BOTH panels stay open at their mins and make the peek scroll
// horizontally: when both are open we pin the panel group to a min-width equal
// to the sum of the mins (plus the 1px handle) inside an `overflow-x-auto`
// wrapper. The library measures the group from its panels' own widths, so it
// sees enough room for both mins and never collapses one β€” the overflow simply
// scrolls. When either panel is collapsed the pin is dropped so the open panel
// fills the available width as usual.
const NAVIGATION_PANEL_MIN_PX = 260;
const DETAIL_PANEL_MIN_PX = 360;
const RESIZE_HANDLE_PX = 1;
const COLLAPSED_PANEL_PX = 40;
// Width below which the two mins can't both fit; at-or-above this the group
// fills the container normally, below it the group keeps this width and the
// wrapper scrolls horizontally.
const BOTH_PANELS_MIN_WIDTH_PX =
  NAVIGATION_PANEL_MIN_PX + DETAIL_PANEL_MIN_PX + RESIZE_HANDLE_PX;

// Detect whether a panel is sitting on its collapsed rail in a RESTORED layout.
// `useDefaultLayout` returns a `{ [panelId]: number }` map of flexGrow shares
// that sum to ~100 (percentages of the group). A collapsed panel is exactly its
// `collapsedSize` (40px) wide, while an open panel is always at least its
// `minSize` (260/360px) β€” or, for the peek's width-capped nav default, ~460px.
// The rail share and the open share never overlap, but WHERE the boundary sits
// depends on the group's actual pixel width: a 460px open nav is ~42% at a 621px
// peek but only ~15% at a 3000px one. So the threshold must be computed against
// the real width, not a fixed percent β€” otherwise a legitimately-open (but
// small-share) nav on a wide peek is mis-seeded as collapsed and its content
// unmounts for a frame until `onResize` corrects it. Boundary = midpoint between
// the 40px rail and the smallest open min (nav 260px), as a share of the width.
// The boundary is kept LOCAL (not a module const, the #14735 pattern): as a
// single-use top-level const it tripped the SWC prod minifier's dropped-binding
// bug β€” the declaration was deleted while this function's inlined body kept the
// reference, throwing `ReferenceError` on peek open (LFE-10640; the CI
// client-bundle scan now guards the class, LFE-10645). Delete this workaround
// once swc-project/swc#11983 is fixed in the Next-vendored swc_core.
function collapsedShareMaxForWidth(groupWidthPx: number): number {
  const boundaryPx = (COLLAPSED_PANEL_PX + NAVIGATION_PANEL_MIN_PX) / 2;
  return (boundaryPx / groupWidthPx) * 100;
}
// Fallback when the container width isn't known (SSR): the boundary as a share
// of the narrowest both-open width, matching the prior heuristic.
const COLLAPSED_LAYOUT_SHARE_MAX_PCT = collapsedShareMaxForWidth(
  BOTH_PANELS_MIN_WIDTH_PX,
);
// Used only to seed the collapse flags on mount so `bothPanelsOpen` (and thus
// the min-width pin) matches the layout the library is about to restore,
// avoiding a one-frame scrollbar flash when restoring a saved layout.
function isPanelCollapsedInLayout(
  layout: Record<string, number> | undefined,
  panelId: string,
  maxSharePct: number,
): boolean {
  const share = layout?.[panelId];
  return typeof share === "number" && share <= maxSharePct;
}

// Context for sharing panel state with compound components
interface TraceLayoutDesktopContext {
  reviewOpen: boolean;
  isNavigationPanelCollapsed: boolean;
  setIsNavigationPanelCollapsed: (collapsed: boolean) => void;
  panelRef: React.RefObject<PanelImperativeHandle | null>;
  handleTogglePanel: () => void;
  // Detail (info/preview) panel β€” collapsible like the navigation panel.
  detailPanelRef: React.RefObject<PanelImperativeHandle | null>;
  isDetailPanelCollapsed: boolean;
  setIsDetailPanelCollapsed: (collapsed: boolean) => void;
  expandDetailPanel: () => void;
}

const LayoutContext = createContext<TraceLayoutDesktopContext | null>(null);

function useLayoutContext() {
  const context = useContext(LayoutContext);
  if (!context) {
    throw new Error(
      "TraceLayoutDesktop compound components must be used within TraceLayoutDesktop",
    );
  }
  return context;
}

// Export hook for use in Trace.tsx
export function useDesktopLayoutContext() {
  return useLayoutContext();
}

// Safe variant for components rendered in both desktop and mobile layouts
// (mobile has no provider): returns null instead of throwing.
export function useDesktopLayoutContextOptional() {
  return useContext(LayoutContext);
}

type TraceLayoutDesktopProps = {
  children: ReactNode;
  groupId: string;
  defaultNavigationCollapsed: boolean;
  reviewOpen: boolean;
  reviewPanel: ReactNode;
};

export function TraceLayoutDesktop(props: TraceLayoutDesktopProps) {
  const router = useRouter();
  const { reviewPanel, ...layoutProps } = props;
  return (
    <TraceReviewLayout
      open={props.reviewOpen}
      review={reviewPanel}
      collapseNavigationOnEntry={
        router.query.annotation === "open" ? true : undefined
      }
    >
      {({ collapsed, toggle }) => (
        <TraceNavigationDetailLayout
          {...layoutProps}
          reviewNavigationCollapsed={collapsed}
          toggleReviewNavigation={toggle}
        />
      )}
    </TraceReviewLayout>
  );
}

function TraceNavigationDetailLayout({
  children,
  groupId,
  defaultNavigationCollapsed,
  reviewOpen,
  reviewNavigationCollapsed,
  toggleReviewNavigation,
}: Pick<
  TraceLayoutDesktopProps,
  "children" | "groupId" | "defaultNavigationCollapsed" | "reviewOpen"
> & {
  reviewNavigationCollapsed: boolean;
  toggleReviewNavigation: () => void;
}) {
  // Peek sizing depends on the drawer width; persistence scope is caller-owned.
  const { isPeekMode } = useViewPreferences();

  /** Mount-time defaults use the host width; saved inner splits take precedence. */
  const getPanelWidthPx = usePeekTableState()?.getPanelWidthPx;
  const [peekView] = useQueryParam("peekView", StringParam);
  const isPeekExpanded = isPeekMode && peekView === "expanded";
  const containerWidthPx = useMemo(() => {
    if (typeof window === "undefined") return 0;
    if (!isPeekMode) return window.innerWidth;
    if (getPanelWidthPx) return getPanelWidthPx();
    if (isPeekExpanded) return window.innerWidth;
    return resolveEffectiveWidthFraction() * window.innerWidth;
  }, [isPeekMode, isPeekExpanded, getPanelWidthPx]);

  // Deterministic default split, computed from `containerWidthPx`. Only used on
  // first open (no saved layout); memoized so the Group sees a stable prop.
  // Undefined during SSR.
  const computedDefaultLayout = useMemo(() => {
    const navPercent = computeNavPercent(containerWidthPx);
    if (navPercent === undefined) return undefined;
    return {
      [RESIZABLE_PANEL_NAVIGATION_ID]: navPercent,
      [RESIZABLE_PANEL_PREVIEW_ID]: 100 - navPercent,
    };
  }, [containerWidthPx]);

  // Restored layout for this group. Read before the collapse flags so we can
  // seed them from what the library is about to restore on mount β€” otherwise the
  // first render assumes both panels are open and pins the group to 621px
  // even when a persisted layout has a panel on
  // its 40px rail, and a narrow peek flashes a useless horizontal scrollbar
  // until the Panel's onResize corrects the flag a frame later.
  const { defaultLayout, onLayoutChanged } = useDefaultLayout({
    id: groupId,
    panelIds: [RESIZABLE_PANEL_NAVIGATION_ID, RESIZABLE_PANEL_PREVIEW_ID],
    storage: "local",
  });

  // Collapse-seed threshold: width-aware, so a small-share-but-open nav on a
  // wide container isn't mis-seeded as collapsed; fixed fallback during SSR.
  const collapseSeedMaxSharePct =
    containerWidthPx > 0
      ? collapsedShareMaxForWidth(containerWidthPx)
      : COLLAPSED_LAYOUT_SHARE_MAX_PCT;

  const {
    navigationPanelCollapsed,
    detailPanelCollapsed,
    setNavigationPanelCollapsed: setIsNavigationPanelCollapsed,
    setDetailPanelCollapsed: setIsDetailPanelCollapsed,
    pendingPanelExpand: pendingExpand,
    setPendingPanelExpand: setPendingExpand,
    openDetails,
  } = useSelection();
  const isNavigationPanelCollapsed =
    navigationPanelCollapsed ??
    ((defaultNavigationCollapsed && !defaultLayout) ||
      isPanelCollapsedInLayout(
        defaultLayout,
        RESIZABLE_PANEL_NAVIGATION_ID,
        collapseSeedMaxSharePct,
      ));

  // Ref to programmatically control the panel
  const panelRef = usePanelRef();

  // The parent owns collapse state; until sizes are restored, derive visibility
  // from the same saved layout the panel library will apply.
  const detailPanelRef = usePanelRef();
  const isDetailPanelCollapsed =
    detailPanelCollapsed ??
    isPanelCollapsedInLayout(
      defaultLayout,
      RESIZABLE_PANEL_PREVIEW_ID,
      collapseSeedMaxSharePct,
    );

  // Group ref β€” drives the whole layout atomically (setLayout) when reopening a
  // collapsed panel, which is more robust than chaining the per-panel
  // expand()+resize() imperatives (those validate against a momentarily stale
  // store and silently no-op on a narrow peek).
  const groupRef = useGroupRef();
  const normalLayout = useRef(defaultLayout ?? computedDefaultLayout);
  const appliedReviewMode = useRef(false);
  // Synchronize the panel library after its constraints register. Normal
  // navigation sizes remain separate from the temporary review layout.
  useLayoutEffect(() => {
    let cancelled = false;
    let outerFrame = 0;
    let innerFrame = 0;
    const element = document.getElementById(groupId);
    const applyLayout = () => {
      const group = groupRef.current;
      if (cancelled || !group) return;
      if (reviewOpen) {
        const width = element?.clientWidth ?? 0;
        if (!width) return;
        const navigationWidth = reviewNavigationCollapsed
          ? COLLAPSED_PANEL_PX
          : Math.max(
              NAVIGATION_PANEL_MIN_PX,
              Math.min(340, width - DETAIL_PANEL_MIN_PX),
            );
        const navigationPercent = (navigationWidth / width) * 100;
        group.setLayout({
          [RESIZABLE_PANEL_NAVIGATION_ID]: navigationPercent,
          [RESIZABLE_PANEL_PREVIEW_ID]: 100 - navigationPercent,
        });
      } else if (appliedReviewMode.current && normalLayout.current) {
        group.setLayout(normalLayout.current);
      }
      appliedReviewMode.current = reviewOpen;
    };
    if (reviewOpen) queueMicrotask(applyLayout);
    // The outer split must finish expanding before the library validates the
    // restored percentages against the inner panel's minimum widths.
    else if (appliedReviewMode.current) {
      outerFrame = requestAnimationFrame(() => {
        innerFrame = requestAnimationFrame(applyLayout);
      });
    }
    const observer = reviewOpen
      ? new ResizeObserver(() => queueMicrotask(applyLayout))
      : null;
    if (element) observer?.observe(element);
    return () => {
      cancelled = true;
      cancelAnimationFrame(outerFrame);
      cancelAnimationFrame(innerFrame);
      observer?.disconnect();
    };
  }, [reviewOpen, reviewNavigationCollapsed, groupRef, groupId]);

  // Which collapsed panel a click asked to open, awaiting room. We pin the group
  // (below) for it, let React commit the wider min-width to the DOM, then run
  // the actual expand from the effect β€” so the solver has room for both mins.
  // Without this, expanding while the peek is too narrow for both mins is a
  // silent no-op: the group stays at its current width and the solver refuses to
  // shrink the open panel below its min (the original LFE-10547 symptom).

  // Both panels open (neither collapsed) β€” drives the min-width pin on the group
  // so a too-narrow peek scrolls horizontally instead of force-collapsing one.
  // When either is collapsed the pin is dropped so the open panel fills the
  // width. Mirrors the two collapse flags (kept in sync by each Panel's
  // onResize) rather than reading the refs, so it re-renders the group; a
  // `pendingExpand` keeps the pin on while a click's expand is in flight.
  const bothPanelsOpen =
    pendingExpand !== null ||
    (!isNavigationPanelCollapsed && !isDetailPanelCollapsed);

  // Reopen the queued panel once the pin's wider min-width has taken effect.
  //
  // Subtlety: setting `pendingExpand` commits `min-width: <sum of mins>` to the
  // GROUP element, growing it so there's room for both mins (the wrapper
  // scrolls). But react-resizable-panels derives its internal group size from
  // the summed widths of its panels, and only recomputes that when its own
  // ResizeObserver notices the group element grew β€” which is delivered a couple
  // of frames later. Until then the library still thinks the group is the old
  // (too-narrow) width, so expanding clamps the panel straight back to its
  // collapsed rail (the silent no-op behind LFE-10547). We therefore defer the
  // reopen across two animation frames, by which point the observer has fired
  // and the library sees the full width.
  //
  // expand() alone can also land on a degenerate size (LFE-10550): it restores
  // the panel's pre-collapse size, which may swallow the whole peek or sit below
  // its min. So after expanding we set the whole layout explicitly to a balanced
  // split that still leaves the sibling its min β€” groupWidth/2 clamped to
  // [own min, groupWidth βˆ’ sibling min]. On a too-narrow peek the upper clamp
  // wins: the panel lands on its own min and the sibling keeps its min (both at
  // mins + horizontal scroll); on a wide peek it's a true 50/50.
  useEffect(() => {
    if (!pendingExpand) return;
    const requestedPanel =
      pendingExpand === "navigation"
        ? panelRef.current
        : detailPanelRef.current;
    if (requestedPanel && !requestedPanel.isCollapsed()) {
      setPendingExpand(null);
      return;
    }
    if (reviewOpen) {
      setPendingExpand(null);
      return;
    }
    const target = pendingExpand;
    let innerRaf = 0;
    const outerRaf = requestAnimationFrame(() => {
      innerRaf = requestAnimationFrame(() => {
        const group = groupRef.current;
        const panel =
          target === "navigation" ? panelRef.current : detailPanelRef.current;
        const siblingPanel =
          target === "navigation" ? detailPanelRef.current : panelRef.current;
        const groupWidthPx = document.getElementById(groupId)?.offsetWidth ?? 0;
        if (group && panel && groupWidthPx > 0) {
          // Whether the sibling was deliberately collapsed (header toggle, rail
          // button, or dragged onto its 40px rail). If so we keep it there:
          // collapsing stays a manual action, so reopening one panel must not
          // silently uncollapse the other (LFE-10550). Captured before expand().
          const siblingCollapsed = siblingPanel?.isCollapsed() ?? false;
          panel.expand();
          const ownMinPx =
            target === "navigation"
              ? NAVIGATION_PANEL_MIN_PX
              : DETAIL_PANEL_MIN_PX;
          const siblingMinPx =
            target === "navigation"
              ? DETAIL_PANEL_MIN_PX
              : NAVIGATION_PANEL_MIN_PX;
          if (siblingCollapsed) {
            // Sibling stays on its rail: give the target everything except the
            // sibling's 40px rail. resize() (not the two-panel setLayout) leaves
            // the collapsed sibling untouched so its onResize never fires.
            const ownPx = Math.max(ownMinPx, groupWidthPx - COLLAPSED_PANEL_PX);
            panel.resize(`${(ownPx / groupWidthPx) * 100}%`);
          } else {
            // Both open: balanced split that still leaves the sibling its min β€”
            // groupWidth/2 clamped to [own min, groupWidth βˆ’ sibling min]. On a
            // too-narrow peek the upper clamp wins: target lands on its own min,
            // sibling keeps its min (both at mins + horizontal scroll); on a wide
            // peek it's a true 50/50.
            const ownPx = Math.max(
              ownMinPx,
              Math.min(groupWidthPx / 2, groupWidthPx - siblingMinPx),
            );
            const ownPercent = (ownPx / groupWidthPx) * 100;
            group.setLayout({
              [RESIZABLE_PANEL_NAVIGATION_ID]:
                target === "navigation" ? ownPercent : 100 - ownPercent,
              [RESIZABLE_PANEL_PREVIEW_ID]:
                target === "detail" ? ownPercent : 100 - ownPercent,
            });
          }
          // Only update state when the expand actually applied (inside the
          // guard) β€” otherwise the flags would flip to "open" while the library
          // still has the panel collapsed, desyncing the min-width pin until the
          // next onResize. Flip the target's flag before clearing pendingExpand
          // so the group stays pinned across the render that clears the intent
          // (otherwise it would briefly unpin and the solver could re-collapse
          // the panel we just opened). The sibling's flag is left to its own
          // onResize: when it stays collapsed nothing fires and the flag holds.
          if (target === "navigation") setIsNavigationPanelCollapsed(false);
          else setIsDetailPanelCollapsed(false);
        }
        // Always clear the transient intent β€” even on a failed expand β€” so a
        // later resize/onResize isn't blocked by a stale pendingExpand.
        setPendingExpand(null);
      });
    });
    return () => {
      cancelAnimationFrame(outerRaf);
      cancelAnimationFrame(innerRaf);
    };
  }, [
    pendingExpand,
    reviewOpen,
    groupRef,
    panelRef,
    detailPanelRef,
    groupId,
    setIsDetailPanelCollapsed,
    setIsNavigationPanelCollapsed,
    setPendingExpand,
  ]);

  // Guarded so it's a no-op (not a resize) when already open β€” safe to call on
  // every row click, which is how re-selecting the same node reopens it.
  const expandDetailPanel = () => {
    if (reviewOpen) return;
    if (!detailPanelRef.current?.isCollapsed()) return;
    openDetails();
  };

  // Apply the caller's first-use default. A restored layout always wins.
  useEffect(() => {
    if (
      defaultNavigationCollapsed &&
      !defaultLayout &&
      panelRef.current &&
      !panelRef.current.isCollapsed()
    ) {
      panelRef.current.collapse();
      setIsNavigationPanelCollapsed(true);
    }
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, []);

  // Manual toggle for the navigation panel (header button + handle double-click).
  // Collapse stays fully manual; expanding restores a balanced size so the nav
  // panel never swallows the whole peek, and both panels can be open at once β€”
  // a too-narrow peek scrolls horizontally rather than collapsing the detail
  // panel (LFE-10550).
  const handleTogglePanel = () => {
    if (reviewOpen) {
      toggleReviewNavigation();
      return;
    }
    if (!panelRef.current) return;

    if (panelRef.current.isCollapsed()) {
      setPendingExpand("navigation");
    } else {
      panelRef.current.collapse();
    }
  };

  const contextValue: TraceLayoutDesktopContext = {
    reviewOpen,
    isNavigationPanelCollapsed: reviewOpen
      ? reviewNavigationCollapsed
      : isNavigationPanelCollapsed,
    setIsNavigationPanelCollapsed,
    panelRef,
    handleTogglePanel,
    detailPanelRef,
    isDetailPanelCollapsed: reviewOpen ? false : isDetailPanelCollapsed,
    setIsDetailPanelCollapsed,
    expandDetailPanel,
  };
  const pinNavigation = reviewOpen
    ? !reviewNavigationCollapsed
    : bothPanelsOpen;
  const minimumGroupWidth = BOTH_PANELS_MIN_WIDTH_PX;

  return (
    <LayoutContext.Provider value={contextValue}>
      {/* Horizontal-scroll wrapper: when both panels are open we pin the panel
          group to the sum of the two mins so a peek too narrow for both keeps
          them at their mins and scrolls horizontally instead of force-collapsing
          one (LFE-10550). The library can't override `width`/`min-width` on the
          group's own style, so the pin lives there; the wrapper owns the scroll.
          When either panel is collapsed there's no pin and the group fills the
          width (`min-w-0`) as before. */}
      <div className="relative h-full w-full overflow-x-auto overflow-y-hidden">
        <Group
          orientation="horizontal"
          id={groupId}
          groupRef={groupRef}
          defaultLayout={defaultLayout ?? computedDefaultLayout}
          onLayoutChanged={(layout) => {
            if (reviewOpen || appliedReviewMode.current !== reviewOpen) return;
            normalLayout.current = layout;
            onLayoutChanged({
              [RESIZABLE_PANEL_NAVIGATION_ID]:
                layout[RESIZABLE_PANEL_NAVIGATION_ID]!,
              [RESIZABLE_PANEL_PREVIEW_ID]: layout[RESIZABLE_PANEL_PREVIEW_ID]!,
            });
          }}
          className={pinNavigation ? undefined : "min-w-0"}
          style={
            pinNavigation ? { minWidth: `${minimumGroupWidth}px` } : undefined
          }
        >
          {children}
        </Group>
        {/* When the detail panel is collapsed it renders its own collapsed rail
            with a "Show detail panel" button (see DetailPanel below) β€” the
            navigation header carries no re-open button, and there's no floating
            edge tab. */}
      </div>
    </LayoutContext.Provider>
  );
}

// Compound component: Navigation panel
TraceLayoutDesktop.NavigationPanel = function Navigation({
  children,
}: {
  children: ReactNode;
}) {
  const {
    setIsNavigationPanelCollapsed,
    panelRef,
    reviewOpen,
    isNavigationPanelCollapsed,
  } = useLayoutContext();
  const collapsedForReview = reviewOpen && isNavigationPanelCollapsed;

  return (
    <Panel
      id={RESIZABLE_PANEL_NAVIGATION_ID}
      panelRef={panelRef}
      collapsible={!reviewOpen}
      collapsedSize="40px"
      minSize={collapsedForReview ? "40px" : `${NAVIGATION_PANEL_MIN_PX}px`}
      maxSize={collapsedForReview ? "40px" : "100%"}
      // SSR/no-width fallback only β€” the computed percentage `defaultLayout`
      // on the Group drives the real default split. Detail carries no default
      // so it fills the remainder.
      defaultSize="40%"
      onResize={() => {
        if (reviewOpen) return;
        setIsNavigationPanelCollapsed(panelRef.current?.isCollapsed() ?? false);
      }}
    >
      <div className="h-full">{children}</div>
    </Panel>
  );
};

// Compound component: Resize handle
TraceLayoutDesktop.ResizeHandle = function ResizeHandle() {
  const { handleTogglePanel, reviewOpen, isNavigationPanelCollapsed } =
    useLayoutContext();

  return (
    <Separator
      id={RESIZABLE_PANEL_HANDLE_ID}
      disabled={reviewOpen && isNavigationPanelCollapsed}
      className="bg-border relative w-px transition-colors duration-200 after:absolute after:inset-y-0 after:left-0 after:w-1 after:bg-blue-200 after:opacity-0 after:transition-opacity after:duration-200 hover:after:opacity-100 active:after:opacity-100"
      onDoubleClick={handleTogglePanel}
    />
  );
};

// Compound component: Detail panel
TraceLayoutDesktop.DetailPanel = function Detail({
  children,
}: {
  children: ReactNode;
}) {
  const {
    detailPanelRef,
    setIsDetailPanelCollapsed,
    isDetailPanelCollapsed,
    expandDetailPanel,
    reviewOpen,
  } = useLayoutContext();

  return (
    // Collapsible like the navigation panel: dragging it below the 360px floor
    // snaps it to a 40px rail (collapsedSize) so the timeline/tree takes the
    // rest of the width while a "show detail panel" button on the rail brings it
    // back β€” mirroring the navigation panel's collapsed strip. 360px is the
    // readable minimum the narrow peek already renders at.
    <Panel
      id={RESIZABLE_PANEL_PREVIEW_ID}
      panelRef={detailPanelRef}
      collapsible={!reviewOpen}
      collapsedSize="40px"
      minSize={`${DETAIL_PANEL_MIN_PX}px`}
      onResize={() => {
        if (reviewOpen) return;
        setIsDetailPanelCollapsed(
          detailPanelRef.current?.isCollapsed() ?? false,
        );
      }}
    >
      {/* Keep the detail content MOUNTED while collapsed (just hidden), so its
          scroll position, local state, and in-progress comment/annotation
          drafts survive a collapse β†’ expand round-trip. */}
      <div
        className={cn(
          "h-full w-full",
          !reviewOpen && isDetailPanelCollapsed && "hidden",
        )}
      >
        {children}
      </div>
      {!reviewOpen && isDetailPanelCollapsed && (
        <div className="flex h-full w-full flex-col items-center p-2">
          <Button
            variant="ghost"
            size="icon"
            title="Show detail panel"
            aria-label="Show detail panel"
            onClick={expandDetailPanel}
            className="h-7 w-7 shrink-0"
          >
            <PanelRightOpen size="base" color="default" />
          </Button>
        </div>
      )}
    </Panel>
  );
};