import { useState } from 'react'; import classNames from 'classnames'; import { Row, Column } from 'react-basics'; import { formatShortTime, formatNumber, formatLongNumber } from 'lib/format'; import MetricCard from 'components/metrics/MetricCard'; import RefreshButton from 'components/input/RefreshButton'; import WebsiteDateFilter from 'components/input/WebsiteDateFilter'; import MetricsBar from 'components/metrics/MetricsBar'; import { useApi, useDateRange, usePageQuery, useMessages, useSticky } from 'hooks'; import styles from './WebsiteMetricsBar.module.css'; export function WebsiteMetricsBar({ websiteId, sticky }) { const { formatMessage, labels } = useMessages(); const { get, useQuery } = useApi(); const [dateRange] = useDateRange(websiteId); const { startDate, endDate, modified } = dateRange; const [format, setFormat] = useState(true); const { ref, isSticky } = useSticky({ enabled: sticky }); const { query: { url, referrer, title, os, browser, device, country, region, city }, } = usePageQuery(); const { data, error, isLoading, isFetched } = useQuery( [ 'websites:stats', { websiteId, modified, url, referrer, title, os, browser, device, country, region, city }, ], () => get(`/websites/${websiteId}/stats`, { startAt: +startDate, endAt: +endDate, url, referrer, title, os, browser, device, country, region, city, }), ); const formatFunc = format ? n => (n >= 0 ? formatLongNumber(n) : `-${formatLongNumber(Math.abs(n))}`) : formatNumber; function handleSetFormat() { setFormat(state => !state); } const { pageviews, uniques, bounces, totaltime } = data || {}; const num = Math.min(data && uniques.value, data && bounces.value); const diffs = data && { pageviews: pageviews.value - pageviews.change, uniques: uniques.value - uniques.change, bounces: bounces.value - bounces.change, totaltime: totaltime.value - totaltime.change, }; return ( {!error && isFetched && ( <> Number(n).toFixed(0) + '%'} reverseColors /> `${n < 0 ? '-' : ''}${formatShortTime(Math.abs(~~n), ['m', 's'], ' ')}` } /> )}
); } export default WebsiteMetricsBar;