import { Column, Grid, Row, Text } from '@umami/react-zen'; import { useSpring, useTransform } from 'motion/react'; import { type ReactNode, useEffect } from 'react'; import { List, type RowComponentProps } from 'react-window'; import { AnimatedDiv } from '@/components/common/AnimatedDiv'; import { Empty } from '@/components/common/Empty'; import { useMessages, useMobile } from '@/components/hooks'; import { formatLongNumber } from '@/lib/format'; const ITEM_SIZE = 30; interface ListData { label: string; count: number; percent: number; } export interface ListTableProps { data?: ListData[]; title?: string; metric?: string; className?: string; renderLabel?: (data: ListData, index: number) => ReactNode; renderChange?: (data: ListData, index: number) => ReactNode; animate?: boolean; virtualize?: boolean; showPercentage?: boolean; itemCount?: number; formatCount?: (n: number) => string; } export function ListTable({ data = [], title, metric, renderLabel, renderChange, animate = true, virtualize = false, showPercentage = true, itemCount = 10, formatCount, }: ListTableProps) { const { t, labels } = useMessages(); const { isPhone } = useMobile(); const getRow = (row: ListData, index: number) => { const { label, count, percent } = row; return ( ); }; const ListTableRow = ({ index, style }: RowComponentProps) => { return
{getRow(data[index], index)}
; }; return ( {title} {metric} {data?.length === 0 && } {virtualize && data.length > 0 ? ( ) : ( data.map(getRow) )} ); } const AnimatedRow = ({ label, value = 0, percent, change, animate, showPercentage = true, formatCount, isPhone, }) => { const y = !Number.isNaN(value) ? value : 0; const ySpring = useSpring(0, { stiffness: 170, damping: 26 }); const widthSpring = useSpring(0, { stiffness: 170, damping: 26 }); const yText = useTransform(ySpring, n => (formatCount ? formatCount(n) : formatLongNumber(n))); const widthText = useTransform(widthSpring, n => `${n?.toFixed?.(0)}%`); useEffect(() => { if (animate) { ySpring.set(y); widthSpring.set(percent); } else { ySpring.jump(y); widthSpring.jump(percent); } }, [y, percent, animate, ySpring, widthSpring]); return ( {label} {change} {yText} {showPercentage && ( {widthText} )} ); };