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-09-26 08:38:28 +02:00
|
|
|
import classNames from 'classnames';
|
2020-09-26 07:31:18 +02:00
|
|
|
import Link from 'next/link';
|
2020-09-06 02:27:01 +02:00
|
|
|
import ButtonGroup from 'components/common/ButtonGroup';
|
2020-09-26 07:31:18 +02:00
|
|
|
import ButtonLayout from 'components/layout/ButtonLayout';
|
2020-08-23 04:05:07 +02:00
|
|
|
import { urlFilter } from 'lib/filters';
|
2020-09-06 02:27:01 +02:00
|
|
|
import { FILTER_COMBINED, FILTER_RAW } from 'lib/constants';
|
2020-09-26 07:31:18 +02:00
|
|
|
import usePageQuery from 'hooks/usePageQuery';
|
2020-09-06 02:27:01 +02:00
|
|
|
import MetricsTable from './MetricsTable';
|
2020-09-26 08:38:28 +02:00
|
|
|
import styles from './PagesTable.module.css';
|
2020-08-23 04:05:07 +02:00
|
|
|
|
2020-09-26 08:38:28 +02:00
|
|
|
export default function PagesTable({ websiteId, token, websiteDomain, limit, showFilters }) {
|
2020-09-06 02:27:01 +02:00
|
|
|
const [filter, setFilter] = useState(FILTER_COMBINED);
|
2020-09-26 08:38:28 +02:00
|
|
|
const {
|
|
|
|
resolve,
|
|
|
|
query: { url },
|
|
|
|
} = usePageQuery();
|
2020-09-06 02:27:01 +02:00
|
|
|
|
|
|
|
const buttons = [
|
|
|
|
{
|
|
|
|
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-09-26 07:31:18 +02:00
|
|
|
const renderLink = ({ x }) => {
|
|
|
|
return (
|
|
|
|
<Link href={resolve({ url: x })} replace={true}>
|
2020-09-26 08:38:28 +02:00
|
|
|
<a
|
|
|
|
className={classNames({
|
|
|
|
[styles.inactive]: url && x !== url,
|
|
|
|
[styles.active]: x === url,
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
{decodeURI(x)}
|
|
|
|
</a>
|
2020-09-26 07:31:18 +02:00
|
|
|
</Link>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-08-23 04:05:07 +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
|
|
|
|
title={<FormattedMessage id="metrics.pages" defaultMessage="Pages" />}
|
|
|
|
type="url"
|
|
|
|
metric={<FormattedMessage id="metrics.views" defaultMessage="Views" />}
|
|
|
|
websiteId={websiteId}
|
|
|
|
token={token}
|
|
|
|
limit={limit}
|
|
|
|
dataFilter={urlFilter}
|
|
|
|
filterOptions={{ domain: websiteDomain, raw: filter === FILTER_RAW }}
|
2020-09-26 07:31:18 +02:00
|
|
|
renderLabel={renderLink}
|
2020-09-22 06:34:55 +02:00
|
|
|
/>
|
|
|
|
</>
|
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 }) => {
|
2020-09-22 06:34:55 +02:00
|
|
|
return (
|
|
|
|
<ButtonLayout>
|
|
|
|
<ButtonGroup size="xsmall" items={buttons} selectedItem={selected} onClick={onClick} />
|
|
|
|
</ButtonLayout>
|
|
|
|
);
|
2020-08-23 07:01:14 +02:00
|
|
|
};
|