1
0
mirror of https://github.com/kremalicious/umami.git synced 2024-12-24 02:06:19 +01:00
umami/components/pages/WebsiteDetails.js

183 lines
5.6 KiB
JavaScript
Raw Normal View History

import React, { useState } from 'react';
2020-09-06 02:27:01 +02:00
import { FormattedMessage } from 'react-intl';
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';
2020-10-10 05:37:24 +02:00
import GridLayout, { GridRow, GridColumn } from 'components/layout/GridLayout';
2020-08-12 07:24:41 +02:00
import MenuLayout from 'components/layout/MenuLayout';
2020-09-26 08:38:28 +02:00
import Link from 'components/common/Link';
import Loading from 'components/common/Loading';
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';
2020-09-27 09:51:29 +02:00
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';
import EventsTable from '../metrics/EventsTable';
import EventsChart from '../metrics/EventsChart';
import useFetch from 'hooks/useFetch';
2020-09-26 08:38:28 +02:00
import usePageQuery from 'hooks/usePageQuery';
2020-10-11 11:29:55 +02:00
import useShareToken from 'hooks/useShareToken';
import { DEFAULT_ANIMATION_DURATION, TOKEN_HEADER } from 'lib/constants';
2020-08-01 04:05:14 +02:00
const views = {
url: PagesTable,
referrer: ReferrersTable,
browser: BrowsersTable,
os: OSTable,
device: DevicesTable,
country: CountriesTable,
event: EventsTable,
};
2020-10-11 11:29:55 +02:00
export default function WebsiteDetails({ websiteId }) {
const shareToken = useShareToken();
const { data } = useFetch(`/api/website/${websiteId}`, {
headers: { [TOKEN_HEADER]: shareToken?.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 {
2020-09-26 07:31:18 +02:00
resolve,
query: { view },
} = usePageQuery();
const BackButton = () => (
2020-10-01 01:27:27 +02:00
<div key="back-button" className={styles.backButton}>
2020-11-10 06:01:53 +01:00
<Link key="back-button" href={resolve({ view: undefined })} icon={<Arrow />} size="small">
2020-10-13 07:53:59 +02:00
<FormattedMessage id="label.back" defaultMessage="Back" />
2020-09-29 07:12:48 +02:00
</Link>
</div>
);
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" />,
2020-09-26 07:31:18 +02:00
value: resolve({ 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" />,
2020-09-26 07:31:18 +02:00
value: resolve({ 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" />,
2020-09-26 07:31:18 +02:00
value: resolve({ 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" />,
2020-09-26 07:31:18 +02:00
value: resolve({ view: 'os' }),
2020-09-08 00:25:09 +02:00
},
{
label: <FormattedMessage id="metrics.devices" defaultMessage="Devices" />,
2020-09-26 07:31:18 +02:00
value: resolve({ view: 'device' }),
2020-09-08 00:25:09 +02:00
},
{
label: <FormattedMessage id="metrics.countries" defaultMessage="Countries" />,
2020-09-26 07:31:18 +02:00
value: resolve({ view: 'country' }),
2020-09-08 00:25:09 +02:00
},
{
label: <FormattedMessage id="metrics.events" defaultMessage="Events" />,
2020-09-26 07:31:18 +02:00
value: resolve({ 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,
websiteDomain: data?.domain,
2020-08-23 07:01:14 +02:00
limit: 10,
};
const DetailsComponent = views[view];
2020-08-27 12:42:24 +02:00
function handleDataLoad() {
2020-08-31 23:11:30 +02:00
if (!chartLoaded) {
2020-10-10 05:37:24 +02:00
setTimeout(() => setChartLoaded(true), DEFAULT_ANIMATION_DURATION);
2020-08-31 23:11:30 +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}
2020-08-31 23:11:30 +02:00
title={data.name}
2020-10-21 15:44:43 +02:00
domain={data.domain}
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 && (
2020-10-10 05:37:24 +02:00
<GridLayout>
<GridRow>
<GridColumn md={12} lg={6}>
<PagesTable {...tableProps} />
2020-10-10 05:37:24 +02:00
</GridColumn>
<GridColumn md={12} lg={6}>
<ReferrersTable {...tableProps} />
2020-10-10 05:37:24 +02:00
</GridColumn>
</GridRow>
<GridRow>
<GridColumn md={12} lg={4}>
<BrowsersTable {...tableProps} />
2020-10-10 05:37:24 +02:00
</GridColumn>
<GridColumn md={12} lg={4}>
<OSTable {...tableProps} />
2020-10-10 05:37:24 +02:00
</GridColumn>
<GridColumn md={12} lg={4}>
<DevicesTable {...tableProps} />
2020-10-10 05:37:24 +02:00
</GridColumn>
</GridRow>
<GridRow>
<GridColumn xs={12} md={12} lg={8}>
<WorldMap data={countryData} />
2020-10-10 05:37:24 +02:00
</GridColumn>
<GridColumn xs={12} md={12} lg={4}>
2020-08-28 03:44:20 +02:00
<CountriesTable {...tableProps} onDataLoad={setCountryData} />
2020-10-10 05:37:24 +02:00
</GridColumn>
</GridRow>
<GridRow className={classNames({ [styles.hidden]: !eventsData?.length > 0 })}>
<GridColumn xs={12} md={12} lg={4}>
2020-08-28 03:44:20 +02:00
<EventsTable {...tableProps} onDataLoad={setEventsData} />
2020-10-10 05:37:24 +02:00
</GridColumn>
<GridColumn xs={12} md={12} lg={8}>
2020-10-11 11:29:55 +02:00
<EventsChart className={styles.eventschart} websiteId={websiteId} />
2020-10-10 05:37:24 +02:00
</GridColumn>
</GridRow>
</GridLayout>
)}
2020-10-11 10:33:26 +02:00
{view && chartLoaded && (
<MenuLayout
className={styles.view}
menuClassName={styles.menu}
contentClassName={styles.content}
menu={menuOptions}
>
2020-10-13 01:31:51 +02:00
<DetailsComponent
{...tableProps}
height={500}
limit={false}
animte={false}
showFilters
virtualize
/>
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
);
}