import React, { useState } from 'react'; import { FormattedMessage } from 'react-intl'; import classNames from 'classnames'; import Loading from 'components/common/Loading'; import ErrorMessage from 'components/common/ErrorMessage'; import useFetch from 'hooks/useFetch'; import useDateRange from 'hooks/useDateRange'; import usePageQuery from 'hooks/usePageQuery'; import useShareToken from 'hooks/useShareToken'; import { formatShortTime, formatNumber, formatLongNumber } from 'lib/format'; import { TOKEN_HEADER } from 'lib/constants'; import MetricCard from './MetricCard'; import styles from './MetricsBar.module.css'; export default function MetricsBar({ websiteId, className }) { const shareToken = useShareToken(); const [dateRange] = useDateRange(websiteId); const { startDate, endDate, modified } = dateRange; const [format, setFormat] = useState(true); const { query: { url }, } = usePageQuery(); const { data, error, loading } = useFetch( `/api/website/${websiteId}/stats`, { params: { start_at: +startDate, end_at: +endDate, url, }, headers: { [TOKEN_HEADER]: shareToken?.token }, }, [url, modified], ); const formatFunc = format ? formatLongNumber : formatNumber; function handleSetFormat() { setFormat(state => !state); } const { pageviews, uniques, bounces, totaltime } = data || {}; return (
{!data && loading && } {error && } {data && !error && ( <> } value={pageviews} format={formatFunc} /> } value={uniques} format={formatFunc} /> } value={uniques ? (bounces / uniques) * 100 : 0} format={n => Number(n).toFixed(0) + '%'} /> } value={totaltime && pageviews ? totaltime / (pageviews - bounces) : 0} format={n => formatShortTime(n, ['m', 's'], ' ')} /> )}
); }