/* Copyright 2026 Marimo. All rights reserved. */ import type { Table } from "@tanstack/react-table"; import { useAtomValue } from "jotai"; import { useLocale } from "react-aria"; import { Tooltip } from "@/components/ui/tooltip"; import { cn } from "@/utils/cn"; import { selectedCellsAtom } from "./atoms"; import { countDataCellsInSelection, getNumericValuesFromSelectedCells, } from "./utils"; // Offers a good default for most use cases. const MAX_FRACTION_DIGITS = 3; /** * Displays summary stats (Count, Sum, Average) for the current cell selection. * Renders only when 2+ data cells are selected (checkbox column excluded). * Count excludes the checkbox column; Sum and Average are shown only when * the selection contains at least one numeric value. */ export const CellSelectionStats = ({ table, className, }: { table: Table; className?: string; }) => { const { locale } = useLocale(); const selectedCells = useAtomValue(selectedCellsAtom); const dataCellCount = countDataCellsInSelection(selectedCells); if (dataCellCount < 2) { return ( No selection ); } const numericValues = getNumericValuesFromSelectedCells(table, selectedCells); return (
); }; const formatNumber = (value: number, locale: string): string => { return value.toLocaleString(locale, { maximumFractionDigits: MAX_FRACTION_DIGITS, }); }; const StatSpan = ({ name, value, locale, }: { name: string; value: number; locale: string; }) => { return ( {name}: {formatNumber(value, locale)} ); }; const CountStat = ({ count, locale }: { count: number; locale: string }) => { return ; }; const SumStat = ({ numericValues, locale, }: { numericValues: number[]; locale: string; }) => { if (numericValues.length === 0) { return null; } const sum = numericValues.reduce((acc, n) => acc + n, 0); const sumRounded = Number(sum.toFixed(MAX_FRACTION_DIGITS)); return ; }; const AverageStat = ({ numericValues, locale, }: { numericValues: number[]; locale: string; }) => { if (numericValues.length === 0) { return null; } const average = numericValues.reduce((acc, n) => acc + n, 0) / numericValues.length; const averageRounded = Number(average.toFixed(MAX_FRACTION_DIGITS)); return ; };