umami/components/WebsiteDetails.js

188 lines
5.5 KiB
JavaScript
Raw Normal View History

import React, { useState } from 'react';
2020-09-06 02:27:01 +02:00
import { FormattedMessage } from 'react-intl';
import { useRouter } from 'next/router';
2020-08-01 04:05:14 +02:00
import classNames from 'classnames';
import WebsiteChart from 'components/metrics/WebsiteChart';
2020-08-12 07:24:41 +02:00
import WorldMap from 'components/common/WorldMap';
import Page from 'components/layout/Page';
import MenuLayout from 'components/layout/MenuLayout';
import Button from 'components/common/Button';
2020-08-10 00:13:38 +02:00
import Arrow from 'assets/arrow-right.svg';
2020-08-01 04:05:14 +02:00
import styles from './WebsiteDetails.module.css';
import PagesTable from './metrics/PagesTable';
import ReferrersTable from './metrics/ReferrersTable';
import BrowsersTable from './metrics/BrowsersTable';
import OSTable from './metrics/OSTable';
import DevicesTable from './metrics/DevicesTable';
import CountriesTable from './metrics/CountriesTable';
2020-08-25 08:49:14 +02:00
import EventsTable from './metrics/EventsTable';
2020-08-27 12:42:24 +02:00
import EventsChart from './metrics/EventsChart';
import useFetch from 'hooks/useFetch';
import Loading from 'components/common/Loading';
2020-08-01 04:05:14 +02:00
const views = {
url: PagesTable,
referrer: ReferrersTable,
browser: BrowsersTable,
os: OSTable,
device: DevicesTable,
country: CountriesTable,
event: EventsTable,
};
export default function WebsiteDetails({ websiteId, token }) {
const router = useRouter();
const { data } = useFetch(`/api/website/${websiteId}`, { token });
const [chartLoaded, setChartLoaded] = useState(false);
2020-08-01 12:34:56 +02:00
const [countryData, setCountryData] = useState();
2020-08-28 03:44:20 +02:00
const [eventsData, setEventsData] = useState();
const {
query: { id, view },
basePath,
asPath,
} = router;
const path = `${basePath}/${asPath.split('/')[1]}/${id.join('/')}`;
const BackButton = () => (
<Button
2020-09-11 08:55:29 +02:00
key="back-button"
className={styles.backButton}
icon={<Arrow />}
size="xsmall"
onClick={() => router.push(path)}
>
2020-09-06 02:27:01 +02:00
<div>
<FormattedMessage id="button.back" defaultMessage="Back" />
</div>
</Button>
);
2020-08-10 00:13:38 +02:00
const menuOptions = [
{
render: BackButton,
2020-08-10 00:13:38 +02:00
},
2020-09-08 00:25:09 +02:00
{
label: <FormattedMessage id="metrics.pages" defaultMessage="Pages" />,
value: `${path}?view=url`,
2020-09-08 00:25:09 +02:00
},
{
2020-09-08 21:13:30 +02:00
label: <FormattedMessage id="metrics.referrers" defaultMessage="Referrers" />,
value: `${path}?view=referrer`,
2020-09-08 00:25:09 +02:00
},
{
2020-09-08 21:13:30 +02:00
label: <FormattedMessage id="metrics.browsers" defaultMessage="Browsers" />,
value: `${path}?view=browser`,
2020-09-08 00:25:09 +02:00
},
{
2020-09-12 12:22:14 +02:00
label: <FormattedMessage id="metrics.operating-systems" defaultMessage="Operating system" />,
value: `${path}?view=os`,
2020-09-08 00:25:09 +02:00
},
{
label: <FormattedMessage id="metrics.devices" defaultMessage="Devices" />,
value: `${path}?view=device`,
2020-09-08 00:25:09 +02:00
},
{
label: <FormattedMessage id="metrics.countries" defaultMessage="Countries" />,
value: `${path}?view=country`,
2020-09-08 00:25:09 +02:00
},
{
label: <FormattedMessage id="metrics.events" defaultMessage="Events" />,
value: `${path}?view=event`,
2020-09-08 00:25:09 +02:00
},
2020-08-10 00:13:38 +02:00
];
2020-08-27 12:42:24 +02:00
const tableProps = {
2020-08-31 23:11:30 +02:00
websiteId,
token,
websiteDomain: data?.domain,
2020-08-23 07:01:14 +02:00
limit: 10,
onExpand: handleExpand,
};
const DetailsComponent = views[view];
2020-08-27 12:42:24 +02:00
function handleDataLoad() {
2020-08-31 23:11:30 +02:00
if (!chartLoaded) {
setTimeout(() => setChartLoaded(true), 300);
}
2020-08-01 04:05:14 +02:00
}
2020-08-10 00:13:38 +02:00
function handleExpand(value) {
router.push(`${path}?view=${value}`);
2020-08-10 00:13:38 +02:00
}
2020-08-01 04:05:14 +02:00
if (!data) {
return null;
2020-08-01 04:05:14 +02:00
}
return (
2020-08-06 04:04:02 +02:00
<Page>
2020-08-01 04:05:14 +02:00
<div className="row">
2020-08-01 06:56:25 +02:00
<div className={classNames(styles.chart, 'col')}>
<WebsiteChart
2020-08-04 03:12:28 +02:00
websiteId={websiteId}
token={token}
2020-08-31 23:11:30 +02:00
title={data.name}
onDataLoad={handleDataLoad}
2020-08-31 23:11:30 +02:00
showLink={false}
stickyHeader
2020-08-04 03:12:28 +02:00
/>
2020-08-02 06:20:52 +02:00
</div>
2020-08-01 12:34:56 +02:00
</div>
{!chartLoaded && <Loading />}
{chartLoaded && !view && (
<>
<div className={classNames(styles.row, 'row')}>
<div className="col-md-12 col-lg-6">
<PagesTable {...tableProps} />
</div>
<div className="col-md-12 col-lg-6">
<ReferrersTable {...tableProps} />
</div>
</div>
<div className={classNames(styles.row, 'row')}>
<div className="col-md-12 col-lg-4">
<BrowsersTable {...tableProps} />
</div>
<div className="col-md-12 col-lg-4">
<OSTable {...tableProps} />
</div>
<div className="col-md-12 col-lg-4">
<DevicesTable {...tableProps} />
</div>
</div>
<div className={classNames(styles.row, 'row')}>
<div className="col-12 col-md-12 col-lg-8">
<WorldMap data={countryData} />
</div>
<div className="col-12 col-md-12 col-lg-4">
2020-08-28 03:44:20 +02:00
<CountriesTable {...tableProps} onDataLoad={setCountryData} />
</div>
</div>
2020-08-28 03:44:20 +02:00
<div
className={classNames(styles.row, 'row', { [styles.hidden]: !eventsData?.length > 0 })}
>
2020-08-25 08:49:14 +02:00
<div className="col-12 col-md-12 col-lg-4">
2020-08-28 03:44:20 +02:00
<EventsTable {...tableProps} onDataLoad={setEventsData} />
2020-08-25 08:49:14 +02:00
</div>
2020-08-28 03:44:20 +02:00
<div className="col-12 col-md-12 col-lg-8 pt-5 pb-5">
<EventsChart websiteId={websiteId} token={token} />
2020-08-27 12:42:24 +02:00
</div>
2020-08-25 08:49:14 +02:00
</div>
</>
)}
{view && (
<MenuLayout
className={styles.view}
menuClassName={styles.menu}
contentClassName={styles.content}
menu={menuOptions}
>
2020-08-23 07:01:14 +02:00
<DetailsComponent {...tableProps} limit={false} />
2020-08-10 00:13:38 +02:00
</MenuLayout>
2020-08-09 12:04:48 +02:00
)}
2020-08-06 04:04:02 +02:00
</Page>
2020-08-01 04:05:14 +02:00
);
}