umami/components/WebsiteDetails.js

178 lines
5.4 KiB
JavaScript
Raw Normal View History

2020-08-01 04:05:14 +02:00
import React, { useEffect, useState } from 'react';
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 WebsiteHeader from 'components/metrics/WebsiteHeader';
2020-08-12 07:24:41 +02:00
import MenuLayout from 'components/layout/MenuLayout';
import Button from 'components/common/Button';
2020-08-01 12:34:56 +02:00
import { getDateRange } from 'lib/date';
import { get } from 'lib/web';
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';
2020-08-01 04:05:14 +02:00
export default function WebsiteDetails({ websiteId, defaultDateRange = '7day' }) {
const [data, setData] = useState();
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();
2020-08-01 04:05:14 +02:00
const [dateRange, setDateRange] = useState(getDateRange(defaultDateRange));
2020-08-09 12:04:48 +02:00
const [expand, setExpand] = useState();
2020-08-27 12:42:24 +02:00
const { startDate, endDate, unit } = dateRange;
2020-08-01 04:05:14 +02:00
const BackButton = () => (
<Button
className={styles.backButton}
icon={<Arrow />}
size="xsmall"
onClick={() => setExpand(null)}
>
<div>Back</div>
</Button>
);
2020-08-10 00:13:38 +02:00
const menuOptions = [
{
render: BackButton,
2020-08-10 00:13:38 +02:00
},
{ label: 'Pages', value: 'url', component: PagesTable },
{ label: 'Referrers', value: 'referrer', component: ReferrersTable },
{ label: 'Browsers', value: 'browser', component: BrowsersTable },
{ label: 'Operating system', value: 'os', component: OSTable },
{ label: 'Devices', value: 'device', component: DevicesTable },
2020-08-10 00:13:38 +02:00
{
label: 'Countries',
value: 'country',
component: props => <CountriesTable {...props} onDataLoad={data => setCountryData(data)} />,
2020-08-10 00:13:38 +02:00
},
2020-08-25 08:49:14 +02:00
{ label: 'Events', value: 'event', component: EventsTable },
2020-08-10 00:13:38 +02:00
];
2020-08-27 12:42:24 +02:00
const dataProps = {
2020-08-23 07:01:14 +02:00
websiteId,
startDate,
endDate,
2020-08-27 12:42:24 +02:00
unit,
};
const tableProps = {
...dataProps,
websiteDomain: data?.domain,
2020-08-23 07:01:14 +02:00
limit: 10,
onExpand: handleExpand,
};
const DetailsComponent = expand?.component;
2020-08-27 12:42:24 +02:00
function getSelectedMenuOption(value) {
return menuOptions.find(e => e.value === value);
}
2020-08-01 04:05:14 +02:00
async function loadData() {
setData(await get(`/api/website/${websiteId}`));
}
function handleDataLoad() {
if (!chartLoaded) setTimeout(() => setChartLoaded(true), 300);
}
2020-08-01 04:05:14 +02:00
function handleDateChange(values) {
2020-08-02 09:51:26 +02:00
setTimeout(() => setDateRange(values), 300);
2020-08-01 04:05:14 +02:00
}
2020-08-10 00:13:38 +02:00
function handleExpand(value) {
2020-08-27 12:42:24 +02:00
setExpand(getSelectedMenuOption(value));
2020-08-09 12:04:48 +02:00
}
2020-08-10 00:13:38 +02:00
function handleMenuSelect(value) {
2020-08-27 12:42:24 +02:00
setExpand(getSelectedMenuOption(value));
2020-08-10 00:13:38 +02:00
}
2020-08-01 04:05:14 +02:00
useEffect(() => {
if (websiteId) {
loadData();
}
}, [websiteId]);
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')}>
2020-08-18 09:51:32 +02:00
<WebsiteHeader websiteId={websiteId} name={data.name} showLink={false} />
<WebsiteChart
2020-08-04 03:12:28 +02:00
websiteId={websiteId}
onDataLoad={handleDataLoad}
onDateChange={handleDateChange}
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>
2020-08-09 12:04:48 +02:00
{chartLoaded && !expand && (
<>
<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">
2020-08-27 12:42:24 +02:00
<EventsChart {...dataProps} />
</div>
2020-08-25 08:49:14 +02:00
</div>
</>
)}
2020-08-09 12:04:48 +02:00
{expand && (
<MenuLayout
className={styles.expand}
menuClassName={styles.menu}
2020-08-10 00:13:38 +02:00
optionClassName={styles.option}
2020-08-09 12:04:48 +02:00
menu={menuOptions}
2020-08-10 00:13:38 +02:00
selectedOption={expand.value}
2020-08-09 12:04:48 +02:00
onMenuSelect={handleMenuSelect}
2020-08-10 00:13:38 +02:00
>
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
);
}