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-10-13 01:31:51 +02:00
|
|
|
import FilterButtons from 'components/common/FilterButtons';
|
2020-08-23 04:05:07 +02:00
|
|
|
import { urlFilter } from 'lib/filters';
|
2021-11-22 02:47:18 +01:00
|
|
|
import { safeDecodeURI } from 'lib/url';
|
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-10-13 01:31:51 +02:00
|
|
|
export const FILTER_COMBINED = 0;
|
|
|
|
export const FILTER_RAW = 1;
|
|
|
|
|
2020-10-11 11:29:55 +02:00
|
|
|
export default function PagesTable({ websiteId, websiteDomain, showFilters, ...props }) {
|
2020-09-06 02:27:01 +02:00
|
|
|
const [filter, setFilter] = useState(FILTER_COMBINED);
|
2020-09-26 08:38:28 +02:00
|
|
|
const {
|
|
|
|
resolve,
|
2021-11-22 07:00:14 +01:00
|
|
|
query: { url: currentUrl },
|
2020-09-26 08:38:28 +02:00
|
|
|
} = 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
|
|
|
|
2021-11-22 07:00:14 +01:00
|
|
|
const renderLink = ({ x: url }) => {
|
2020-09-26 07:31:18 +02:00
|
|
|
return (
|
2021-11-22 07:00:14 +01:00
|
|
|
<Link href={resolve({ url })} replace={true}>
|
2020-09-26 08:38:28 +02:00
|
|
|
<a
|
|
|
|
className={classNames({
|
2021-11-22 07:00:14 +01:00
|
|
|
[styles.inactive]: currentUrl && url !== currentUrl,
|
|
|
|
[styles.active]: url === currentUrl,
|
2020-09-26 08:38:28 +02:00
|
|
|
})}
|
|
|
|
>
|
2021-11-22 07:00:14 +01:00
|
|
|
{safeDecodeURI(url)}
|
2020-09-26 08:38:28 +02:00
|
|
|
</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}
|
|
|
|
dataFilter={urlFilter}
|
|
|
|
filterOptions={{ domain: websiteDomain, raw: filter === FILTER_RAW }}
|
2020-09-26 07:31:18 +02:00
|
|
|
renderLabel={renderLink}
|
2020-10-11 10:33:26 +02:00
|
|
|
{...props}
|
2020-09-22 06:34:55 +02:00
|
|
|
/>
|
|
|
|
</>
|
2020-08-23 04:05:07 +02:00
|
|
|
);
|
|
|
|
}
|