umami/components/WebsiteDetails.js

155 lines
4.7 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-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-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-01 04:05:14 +02:00
const { startDate, endDate } = dateRange;
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-23 07:01:14 +02:00
const tableProps = {
websiteId,
startDate,
endDate,
limit: 10,
onExpand: handleExpand,
websiteDomain: data?.domain,
};
const DetailsComponent = expand?.component;
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) {
setExpand(menuOptions.find(e => e.value === value));
2020-08-09 12:04:48 +02:00
}
2020-08-10 00:13:38 +02:00
function handleMenuSelect(value) {
setExpand(menuOptions.find(e => e.value === value));
}
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">
<CountriesTable {...tableProps} onDataLoad={data => setCountryData(data)} />
</div>
</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
);
}