Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

1.5 KB
Raw Blame
import { useCallback } from "react";
import { useSelection } from "@/src/features/traces/contexts/SelectionProvider";
import { usePostHogClientCapture } from "@/src/features/posthog-analytics/usePostHogClientCapture";
import { useTraceAnalyticsDimensions } from "@/src/features/traces/hooks/useTraceAnalyticsDimensions";

/** Which view is reporting the selection — the only thing that differs. */
export type TraceNodeSelectionSource = "tree" | "timeline" | "timeline_compact";

/**
 * Selecting an observation is four things at once: record it, point the app at
 * it, open the detail panel, and on mobile switch to the tab that shows it. Every
 * view of a trace owes all four, and each one that grew its own copy was a chance
 * for them to drift — so they share this.
 *
 * The panel reopens on EVERY select, including re-selecting the row that is
 * already selected: the URL parameter does not change then, so nothing else would
 * bring the detail back after you collapsed it.
 */
export function useSelectTraceNode(source: TraceNodeSelectionSource) {
  const { selectNode } = useSelection();
  const capture = usePostHogClientCapture();
  const analyticsDimensions = useTraceAnalyticsDimensions();

  return useCallback(
    (nodeId: string | null) => {
      if (nodeId) {
        capture("trace_detail:node_selected", {
          source,
          ...analyticsDimensions,
        });
      }
      selectNode(nodeId);
    },
    [source, capture, analyticsDimensions, selectNode],
  );
}