/**
* @fileoverview PivotTable Chart Component
*
* A configurable pivot table widget component that displays data in a tabular format
* with support for multiple dimensions (currently up to 2), metrics as columns,
* subtotals, and grand totals.
*
* Features:
* - Dynamic dimension support (0-N dimensions, currently limited to 2)
* - Proper indentation for nested dimension levels
* - Subtotal and grand total calculations
* - Responsive design within dashboard grid
* - Consistent styling with Langfuse design system
* - Row limiting to prevent performance issues
* - Interactive sorting with hierarchical behavior
*
* Usage:
* Used as part of the dashboard widget system to display tabular data
* visualizations with grouping and aggregation capabilities.
*/
import React, { useMemo, useCallback, useState, useEffect } from "react";
import { cn } from "@/src/utils/tailwind";
import {
Table,
TableHeader,
TableBody,
TableHead,
TableRow,
TableCell,
} from "@/src/components/ui/table";
import {
transformToPivotTable,
extractDimensionValues,
extractMetricValues,
sortPivotTableRows,
getNextSortState,
type PivotTableRow,
type PivotTableConfig,
type DatabaseRow,
DEFAULT_ROW_LIMIT,
} from "@/src/features/widgets/utils/pivot-table-utils";
import { type ChartProps } from "@/src/features/widgets/chart-library/chart-props";
import { numberFormatter } from "@/src/utils/numbers";
import { formatMetricName } from "@/src/features/widgets/utils";
import { type OrderByState } from "@langfuse/shared";
import { Loader2 } from "lucide-react";
/**
* Props interface for the PivotTable component
* Uses standard chart data structure with pivot-specific configuration
*/
export interface PivotTableProps {
/** Array of data points from the chart query */
data: ChartProps["data"];
/** Pivot table specific configuration */
config?: PivotTableConfig;
/** Chart configuration from shadcn/ui (for consistency with other charts) */
chartConfig?: ChartProps["config"];
/** Accessibility layer flag */
accessibilityLayer?: boolean;
/** Current sort state */
sortState?: OrderByState;
/** Callback for sort state changes */
onSortChange?: (sortState: OrderByState | null) => void;
/** Loading state for when data is being refreshed */
isLoading?: boolean;
}
/**
* Non-sortable column header component
* Simple header without sorting functionality
*/
const StaticHeader: React.FC<{
label: string;
className?: string;
}> = ({ label, className }) => {
return (