umami/components/metrics/ReferrersTable.js

77 lines
2.5 KiB
JavaScript
Raw Normal View History

2020-08-23 07:01:14 +02:00
import React, { useState } from 'react';
2020-09-06 02:27:01 +02:00
import { FormattedMessage } from 'react-intl';
import MetricsTable from './MetricsTable';
2020-10-13 01:31:51 +02:00
import FilterButtons from 'components/common/FilterButtons';
2020-10-11 11:29:55 +02:00
import { refFilter } from 'lib/filters';
import { safeDecodeURI } from 'lib/url';
2021-11-22 07:00:14 +01:00
import Link from 'next/link';
import classNames from 'classnames';
import usePageQuery from 'hooks/usePageQuery';
import External from 'assets/arrow-up-right-from-square.svg';
import Icon from '../common/Icon';
import styles from './ReferrersTable.module.css';
2020-10-13 01:31:51 +02:00
export const FILTER_DOMAIN_ONLY = 0;
export const FILTER_COMBINED = 1;
export const FILTER_RAW = 2;
2020-10-11 11:29:55 +02:00
export default function ReferrersTable({ websiteId, websiteDomain, showFilters, ...props }) {
2020-09-06 02:27:01 +02:00
const [filter, setFilter] = useState(FILTER_COMBINED);
2021-11-22 07:00:14 +01:00
const {
resolve,
query: { referrer: currentRef },
2021-11-22 07:00:14 +01:00
} = usePageQuery();
2020-09-06 02:27:01 +02:00
const buttons = [
{
label: <FormattedMessage id="metrics.filter.domain-only" defaultMessage="Domain only" />,
value: FILTER_DOMAIN_ONLY,
},
{
label: <FormattedMessage id="metrics.filter.combined" defaultMessage="Combined" />,
value: FILTER_COMBINED,
},
{ label: <FormattedMessage id="metrics.filter.raw" defaultMessage="Raw" />, value: FILTER_RAW },
];
2020-08-23 07:01:14 +02:00
2021-11-22 07:00:14 +01:00
const renderLink = ({ w: link, x: label }) => {
return (
<div className={styles.row}>
<Link href={resolve({ referrer: label })} replace={true}>
2021-11-22 07:00:14 +01:00
<a
className={classNames(styles.label, {
[styles.inactive]: currentRef && label !== currentRef,
[styles.active]: label === currentRef,
})}
>
{safeDecodeURI(label)}
</a>
</Link>
<a href={link || label} target="_blank" rel="noreferrer noopener" className={styles.link}>
<Icon icon={<External />} className={styles.icon} />
</a>
</div>
2020-08-23 09:23:47 +02:00
);
};
return (
2020-09-22 06:34:55 +02:00
<>
2020-09-26 07:31:18 +02:00
{showFilters && <FilterButtons buttons={buttons} selected={filter} onClick={setFilter} />}
2020-09-22 06:34:55 +02:00
<MetricsTable
2020-10-11 10:33:26 +02:00
{...props}
2020-09-22 06:34:55 +02:00
title={<FormattedMessage id="metrics.referrers" defaultMessage="Referrers" />}
type="referrer"
metric={<FormattedMessage id="metrics.views" defaultMessage="Views" />}
websiteId={websiteId}
dataFilter={refFilter}
filterOptions={{
domain: websiteDomain,
domainOnly: filter === FILTER_DOMAIN_ONLY,
raw: filter === FILTER_RAW,
}}
renderLabel={renderLink}
/>
</>
);
}