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 no-nested-ternary */
/* eslint-disable @repo/no-style-props */
import React, { useCallback, useState } from "react";
import {
  AlertCircle,
  ChevronLeft,
  ChevronRight,
  Loader2,
} from "@langfuse/icons";
import { type DashboardWidgetChartType } from "@langfuse/shared/src/db";
import type { DataPoint } from "@/src/features/widgets/chart-library/chart-props";
import { Button } from "@/src/components/ui/button";
import { cn } from "@/src/utils/tailwind";
import {
  type AggregationFn,
  type ChartViewConfig,
  type DimensionKey,
  type MetricKey,
} from "../types";
import { describeConfig, getMetric } from "../vocab";
import { ChartCanvas } from "./ChartCanvas";
import {
  AggregationSelect,
  BreakdownSelect,
  ChartTypePicker,
  MetricSelect,
} from "./ConfigControls";

/**
 * The "Take B" chart-view layout (the direction Nikita picked): a maximized
 * chart canvas with the visualization config docked in a collapsible right-hand
 * panel. View-only — `data` and the loading/error state are supplied by the
 * caller (the server query in production, the mock aggregator in Storybook).
 * Optional `granularitySlot` lets a caller add a granularity control without
 * forking the layout.
 */
export const ChartViewPanel = React.memo(function ChartViewPanel({
  config,
  onConfigChange,
  data,
  isLoading = false,
  error = null,
  emptyMessage,
  granularitySlot,
  chartActions,
  className,
}: {
  config: ChartViewConfig;
  onConfigChange: (patch: Partial<ChartViewConfig>) => void;
  data: DataPoint[];
  isLoading?: boolean;
  error?: string | null;
  emptyMessage?: string;
  granularitySlot?: React.ReactNode;
  /** Right-aligned actions next to the chart subtitle (e.g. "Add to dashboard"). */
  chartActions?: React.ReactNode;
  /** Root class passthrough. The panel fills a bounded-height flex ancestor via
   *  `flex-1`; a standalone caller (e.g. a story) can give it an explicit height
   *  here instead of relying on a host. */
  className?: string;
}) {
  const [open, setOpen] = useState(true);
  const metric = getMetric(config.metric);

  const onMetric = useCallback(
    (metric: MetricKey) => onConfigChange({ metric }),
    [onConfigChange],
  );
  const onAggregation = useCallback(
    (aggregation: AggregationFn) => onConfigChange({ aggregation }),
    [onConfigChange],
  );
  const onBreakdown = useCallback(
    (breakdown: DimensionKey) => onConfigChange({ breakdown }),
    [onConfigChange],
  );
  const onChartType = useCallback(
    (chartType: DashboardWidgetChartType) => onConfigChange({ chartType }),
    [onConfigChange],
  );

  return (
    <div className={cn("flex min-h-0 flex-1 flex-col md:flex-row", className)}>
      {/* Canvas — floored on mobile so a tall stacked config panel can't shrink
          the chart to 0 on short viewports (flex-basis:0 gets none of a negative
          deficit); desktop keeps min-h-0 for the side-by-side row. */}
      <div className="flex min-h-64 min-w-0 flex-1 flex-col gap-1 p-3 md:min-h-0">
        <div className="flex items-center justify-between gap-2">
          <div
            className="text-foreground min-w-0 truncate text-sm font-bold"
            title={describeConfig(config)}
          >
            {describeConfig(config)}
          </div>
          {chartActions}
        </div>
        <div className="min-h-0 flex-1">
          {error ? (
            <div className="text-muted-foreground flex h-full flex-col items-center justify-center gap-2 p-8 text-center">
              <AlertCircle __rawClassName="h-10 w-10 opacity-40" />
              <p className="max-w-md text-sm">{error}</p>
            </div>
          ) : isLoading ? (
            <div className="text-muted-foreground flex h-full items-center justify-center">
              <Loader2 size="lg" __rawClassName="animate-spin" />
            </div>
          ) : (
            <ChartCanvas
              data={data}
              chartType={config.chartType}
              breakdown={config.breakdown}
              aggregation={config.aggregation}
              metricLabel={metric.label}
              metricUnit={metric.unit}
              emptyMessage={
                emptyMessage ?? "No events match the current filters."
              }
            />
          )}
        </div>
      </div>

      {/* Config panel */}
      {open ? (
        <div className="flex w-full flex-col gap-3 overflow-y-auto border-t p-3 md:w-72 md:shrink-0 md:border-t-0 md:border-l">
          <div className="flex items-center justify-between">
            <span className="text-sm font-bold">Visualize</span>
            <Button
              variant="ghost"
              size="icon-xs"
              aria-label="Collapse panel"
              onClick={() => setOpen(false)}
            >
              <ChevronRight size="sm" color="default" />
            </Button>
          </div>
          <PanelField label="Chart type">
            <ChartTypePicker
              value={config.chartType}
              onChange={onChartType}
              layout="full"
            />
          </PanelField>
          <PanelField label="Metric">
            <MetricSelect value={config.metric} onChange={onMetric} />
          </PanelField>
          <PanelField label="Aggregation">
            <AggregationSelect
              metric={config.metric}
              value={config.aggregation}
              onChange={onAggregation}
            />
          </PanelField>
          <PanelField label="Breakdown">
            <BreakdownSelect value={config.breakdown} onChange={onBreakdown} />
          </PanelField>
          {granularitySlot ? (
            <PanelField label="Granularity">{granularitySlot}</PanelField>
          ) : null}
        </div>
      ) : (
        <div className="flex flex-col items-center border-t p-1.5 md:shrink-0 md:border-t-0 md:border-l">
          <Button
            variant="ghost"
            size="icon-xs"
            aria-label="Expand panel"
            onClick={() => setOpen(true)}
          >
            <ChevronLeft size="sm" color="default" />
          </Button>
        </div>
      )}
    </div>
  );
});

function PanelField({
  label,
  children,
}: {
  label: string;
  children: React.ReactNode;
}) {
  return (
    <div className="flex flex-col gap-1.5">
      <span className="text-muted-foreground text-xs font-bold">{label}</span>
      {children}
    </div>
  );
}