Skip to content

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

6.3 KB
Raw Blame
/* eslint-disable @repo/no-abstracted-overlay-trigger, @repo/component-file-structure */
/**
 * TraceSettingsDropdown - View preferences dropdown component
 *
 * Provides toggles for:
 * - Show Comments
 * - Show Scores
 * - Show Duration
 * - Show Cost/Tokens
 * - Collapse System Prompts
 * - Minimum Observation Level filter
 *
 * All preferences are managed via ViewPreferencesContext and persisted to localStorage.
 */

import { ObservationLevel } from "@langfuse/shared";
import { Settings2 } from "@langfuse/icons";
import { Button } from "@/src/components/ui/button";
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuTrigger,
  DropdownMenuSub,
  DropdownMenuSubTrigger,
  DropdownMenuSubContent,
  DropdownMenuLabel,
} from "@/src/components/ui/dropdown-menu";
import { Switch } from "@/src/components/design-system/Switch/Switch";
import { usePostHogClientCapture } from "@/src/features/posthog-analytics/usePostHogClientCapture";
import { useViewPreferences } from "@/src/features/traces/contexts/ViewPreferencesProvider";
import { useTraceAnalyticsDimensions } from "@/src/features/traces/hooks/useTraceAnalyticsDimensions";

export function TraceSettingsDropdown() {
  return (
    <DropdownMenu>
      <DropdownMenuTrigger asChild>
        <Button
          variant="ghost"
          size="icon"
          title="View Options"
          className="h-7 w-7"
        >
          <Settings2 size="base" color="default" />
        </Button>
      </DropdownMenuTrigger>
      <DropdownMenuContent
        align="center"
        className="w-64 space-y-0 space-x-0 p-0 px-0"
      >
        <TraceViewOptionsMenuItems />
      </DropdownMenuContent>
    </DropdownMenu>
  );
}

/**
 * The view-option menu items (toggles + min-level submenu) without the
 * dropdown shell, so they can be reused inside the navigation header's overflow
 * "⋯" menu when the panel is too narrow for inline toolbar icons.
 */
export function TraceViewOptionsMenuItems() {
  const capture = usePostHogClientCapture();
  const analyticsDimensions = useTraceAnalyticsDimensions();

  // Get all preferences directly from context
  const {
    showComments,
    setShowComments,
    showScores,
    setShowScores,
    showDuration,
    setShowDuration,
    showCostTokens,
    setShowCostTokens,
    minObservationLevel,
    setMinObservationLevel,
    collapseSystemPrompt,
    setCollapseSystemPrompt,
  } = useViewPreferences();

  return (
    <>
      <div className="space-y-0 p-0 py-1">
        {/* Show Comments Toggle */}
        <DropdownMenuItem
          asChild
          onSelect={(e) => e.preventDefault()}
          className="px-2 py-1"
        >
          <div className="flex w-full items-center justify-between">
            <span className="mr-2">Show Comments</span>
            <Switch
              size="sm"
              checked={showComments}
              onCheckedChange={setShowComments}
            />
          </div>
        </DropdownMenuItem>

        {/* Show Scores Toggle */}
        <DropdownMenuItem
          asChild
          onSelect={(e) => e.preventDefault()}
          className="px-2 py-1"
        >
          <div className="flex w-full items-center justify-between">
            <span className="mr-2">Show Scores</span>
            <Switch
              size="sm"
              checked={showScores}
              onCheckedChange={(checked) => {
                capture("trace_detail:observation_tree_toggle_scores", {
                  show: checked,
                  ...analyticsDimensions,
                });
                setShowScores(checked);
              }}
            />
          </div>
        </DropdownMenuItem>

        {/* Show Duration Toggle */}
        <DropdownMenuItem
          asChild
          onSelect={(e) => e.preventDefault()}
          className="px-2 py-1"
        >
          <div className="flex w-full items-center justify-between">
            <span className="mr-2">Show Duration</span>
            <Switch
              size="sm"
              checked={showDuration}
              onCheckedChange={setShowDuration}
            />
          </div>
        </DropdownMenuItem>

        {/* Show Cost/Tokens Toggle */}
        <DropdownMenuItem
          asChild
          onSelect={(e) => e.preventDefault()}
          className="px-2 py-1"
        >
          <div className="flex w-full items-center justify-between">
            <span className="mr-2">Show Cost</span>
            <Switch
              size="sm"
              checked={showCostTokens}
              onCheckedChange={setShowCostTokens}
            />
          </div>
        </DropdownMenuItem>

        {/* Collapse System Prompts Toggle */}
        <DropdownMenuItem
          asChild
          onSelect={(e) => e.preventDefault()}
          className="px-2 py-1"
        >
          <div className="flex w-full items-center justify-between">
            <span className="mr-2">Collapse System Prompts</span>
            <Switch
              size="sm"
              checked={collapseSystemPrompt}
              onCheckedChange={(checked) => {
                // No analyticsDimensions: the inline toggle fires this event
                // from shared components without trace context, and the event
                // must keep one shape across sources.
                capture("trace_detail:system_prompt_collapse_toggle", {
                  collapsed: checked,
                  source: "settings",
                });
                setCollapseSystemPrompt(checked);
              }}
            />
          </div>
        </DropdownMenuItem>
      </div>

      {/* Minimum Observation Level Submenu */}
      <DropdownMenuSub>
        <DropdownMenuSubTrigger>
          <span className="flex items-center">
            Min Level: {minObservationLevel}
          </span>
        </DropdownMenuSubTrigger>
        <DropdownMenuSubContent>
          <DropdownMenuLabel className="font-bold">
            Minimum Level
          </DropdownMenuLabel>
          {Object.values(ObservationLevel).map((level) => (
            <DropdownMenuItem
              key={level}
              onSelect={(e) => {
                e.preventDefault();
                setMinObservationLevel(level);
              }}
            >
              {level}
            </DropdownMenuItem>
          ))}
        </DropdownMenuSubContent>
      </DropdownMenuSub>
    </>
  );
}