import React, { useState, useEffect, useMemo } from 'react'; import { FixedSizeList } from 'react-window'; import { useSpring, animated, config } from 'react-spring'; import classNames from 'classnames'; import Button from 'components/common/Button'; import Arrow from 'assets/arrow-right.svg'; import { get } from 'lib/web'; import { percentFilter } from 'lib/filters'; import { formatNumber, formatLongNumber } from 'lib/format'; import styles from './MetricsTable.module.css'; import Loading from '../common/Loading'; export default function MetricsTable({ title, metric, websiteId, startDate, endDate, type, className, dataFilter, filterOptions, limit, headerComponent, renderLabel, onDataLoad = () => {}, onExpand = () => {}, }) { const [data, setData] = useState(); const [format, setFormat] = useState(true); const formatFunc = format ? formatLongNumber : formatNumber; const shouldAnimate = limit > 0; const rankings = useMemo(() => { if (data) { const items = percentFilter(dataFilter ? dataFilter(data, filterOptions) : data); if (limit) { return items.filter((e, i) => i < limit); } return items; } return []; }, [data, dataFilter, filterOptions]); async function loadData() { const data = await get(`/api/website/${websiteId}/rankings`, { type, start_at: +startDate, end_at: +endDate, }); setData(data); onDataLoad(data); } const handleSetFormat = () => setFormat(state => !state); const getRow = row => { const { x: label, y: value, z: percent } = row; return ( ); }; const Row = ({ index, style }) => { return
{getRow(rankings[index])}
; }; useEffect(() => { if (websiteId) { loadData(); } }, [websiteId, startDate, endDate, type]); return (
{data ? ( <>
{title}
{headerComponent}
{metric}
{limit ? rankings.map(row => getRow(row)) : data?.length > 0 && ( {Row} )}
{limit && data.length > limit && ( )}
) : ( )}
); } const AnimatedRow = ({ label, value = 0, percent, animate, format, onClick }) => { const props = useSpring({ width: percent, y: value, from: { width: 0, y: 0 }, config: animate ? config.default : { duration: 0 }, }); return (
{label}
{props.y?.interpolate(format)}
`${n}%`) }} /> {props.width.interpolate(n => `${n.toFixed(0)}%`)}
); };