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';
|
2020-08-23 04:05:07 +02:00
|
|
|
import MetricsTable from './MetricsTable';
|
|
|
|
import { refFilter } from 'lib/filters';
|
2020-08-31 23:11:30 +02:00
|
|
|
import ButtonGroup from 'components/common/ButtonGroup';
|
2020-09-06 02:27:01 +02:00
|
|
|
import { FILTER_DOMAIN_ONLY, FILTER_COMBINED, FILTER_RAW } from 'lib/constants';
|
2020-08-23 04:05:07 +02:00
|
|
|
|
2020-08-31 23:11:30 +02:00
|
|
|
export default function ReferrersTable({ websiteId, websiteDomain, limit, onExpand = () => {} }) {
|
2020-09-06 02:27:01 +02:00
|
|
|
const [filter, setFilter] = useState(FILTER_COMBINED);
|
|
|
|
|
|
|
|
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
|
|
|
|
2020-08-25 08:49:14 +02:00
|
|
|
const renderLink = ({ x: url }) => {
|
2020-08-23 09:23:47 +02:00
|
|
|
return url.startsWith('http') ? (
|
|
|
|
<a href={url} target="_blank" rel="noreferrer">
|
2020-08-25 08:49:14 +02:00
|
|
|
{decodeURI(url)}
|
2020-08-23 09:23:47 +02:00
|
|
|
</a>
|
|
|
|
) : (
|
2020-08-25 08:49:14 +02:00
|
|
|
decodeURI(url)
|
2020-08-23 09:23:47 +02:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-08-23 04:05:07 +02:00
|
|
|
return (
|
|
|
|
<MetricsTable
|
2020-09-06 02:27:01 +02:00
|
|
|
title={<FormattedMessage id="metrics.referrers" defaultMessage="Referrers" />}
|
2020-08-23 04:05:07 +02:00
|
|
|
type="referrer"
|
2020-09-06 02:27:01 +02:00
|
|
|
metric={<FormattedMessage id="metrics.views" defaultMessage="Views" />}
|
|
|
|
headerComponent={
|
|
|
|
limit ? null : <FilterButtons buttons={buttons} selected={filter} onClick={setFilter} />
|
|
|
|
}
|
2020-08-23 04:05:07 +02:00
|
|
|
websiteId={websiteId}
|
2020-08-29 06:34:20 +02:00
|
|
|
websiteDomain={websiteDomain}
|
2020-08-23 04:05:07 +02:00
|
|
|
limit={limit}
|
|
|
|
dataFilter={refFilter}
|
2020-08-23 07:01:14 +02:00
|
|
|
filterOptions={{
|
|
|
|
domain: websiteDomain,
|
2020-09-06 02:27:01 +02:00
|
|
|
domainOnly: filter === FILTER_DOMAIN_ONLY,
|
|
|
|
raw: filter === FILTER_RAW,
|
2020-08-23 07:01:14 +02:00
|
|
|
}}
|
2020-08-23 04:05:07 +02:00
|
|
|
onExpand={onExpand}
|
2020-08-25 08:49:14 +02:00
|
|
|
renderLabel={renderLink}
|
2020-08-23 04:05:07 +02:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2020-08-23 07:01:14 +02:00
|
|
|
|
2020-09-06 02:27:01 +02:00
|
|
|
const FilterButtons = ({ buttons, selected, onClick }) => {
|
|
|
|
return <ButtonGroup size="xsmall" items={buttons} selectedItem={selected} onClick={onClick} />;
|
2020-08-23 07:01:14 +02:00
|
|
|
};
|