mirror of
https://github.com/umami-software/umami.git
synced 2026-02-04 04:37:11 +01:00
92 lines
3 KiB
TypeScript
92 lines
3 KiB
TypeScript
import { LoadingPanel } from '@/components/common/LoadingPanel';
|
|
import { useDateRange, useMessages } from '@/components/hooks';
|
|
import { useWebsiteStatsQuery } from '@/components/hooks/queries/useWebsiteStatsQuery';
|
|
import { MetricCard } from '@/components/metrics/MetricCard';
|
|
import { MetricsBar } from '@/components/metrics/MetricsBar';
|
|
import { formatLongNumber, formatShortTime } from '@/lib/format';
|
|
|
|
export function WebsiteMetricsBar({
|
|
websiteId,
|
|
compareMode,
|
|
}: {
|
|
websiteId: string;
|
|
showChange?: boolean;
|
|
compareMode?: boolean;
|
|
}) {
|
|
const { isAllTime, dateCompare } = useDateRange();
|
|
const { formatMessage, labels, getErrorMessage } = useMessages();
|
|
const { data, isLoading, isFetching, error } = useWebsiteStatsQuery({
|
|
websiteId,
|
|
compare: compareMode ? dateCompare?.compare : undefined,
|
|
});
|
|
|
|
const { pageviews, visitors, visits, bounces, totaltime, comparison } = data || {};
|
|
|
|
const metrics = data
|
|
? [
|
|
{
|
|
value: visitors,
|
|
label: formatMessage(labels.visitors),
|
|
change: visitors - comparison.visitors,
|
|
formatValue: formatLongNumber,
|
|
},
|
|
{
|
|
value: visits,
|
|
label: formatMessage(labels.visits),
|
|
change: visits - comparison.visits,
|
|
formatValue: formatLongNumber,
|
|
},
|
|
{
|
|
value: pageviews,
|
|
label: formatMessage(labels.views),
|
|
change: pageviews - comparison.pageviews,
|
|
formatValue: formatLongNumber,
|
|
},
|
|
{
|
|
label: formatMessage(labels.bounceRate),
|
|
value: (Math.min(visits, bounces) / visits) * 100,
|
|
prev: (Math.min(comparison.visits, comparison.bounces) / comparison.visits) * 100,
|
|
change:
|
|
(Math.min(visits, bounces) / visits) * 100 -
|
|
(Math.min(comparison.visits, comparison.bounces) / comparison.visits) * 100,
|
|
formatValue: n => `${Math.round(+n)}%`,
|
|
reverseColors: true,
|
|
},
|
|
{
|
|
label: formatMessage(labels.visitDuration),
|
|
value: totaltime / visits,
|
|
prev: comparison.totaltime / comparison.visits,
|
|
change: totaltime / visits - comparison.totaltime / comparison.visits,
|
|
formatValue: n =>
|
|
`${+n < 0 ? '-' : ''}${formatShortTime(Math.abs(~~n), ['m', 's'], ' ')}`,
|
|
},
|
|
]
|
|
: null;
|
|
|
|
return (
|
|
<LoadingPanel
|
|
data={metrics}
|
|
isLoading={isLoading}
|
|
isFetching={isFetching}
|
|
error={getErrorMessage(error)}
|
|
minHeight="136px"
|
|
>
|
|
<MetricsBar>
|
|
{metrics?.map(({ label, value, prev, change, formatValue, reverseColors }) => {
|
|
return (
|
|
<MetricCard
|
|
key={label}
|
|
value={value}
|
|
previousValue={prev}
|
|
label={label}
|
|
change={change}
|
|
formatValue={formatValue}
|
|
reverseColors={reverseColors}
|
|
showChange={!isAllTime}
|
|
/>
|
|
);
|
|
})}
|
|
</MetricsBar>
|
|
</LoadingPanel>
|
|
);
|
|
}
|