umami/components/pages/WebsiteDetails.js

185 lines
5.8 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';
2022-03-02 06:13:15 +01:00
import PagesTable from 'components/metrics/PagesTable';
import ReferrersTable from 'components/metrics/ReferrersTable';
import BrowsersTable from 'components/metrics/BrowsersTable';
import OSTable from 'components/metrics/OSTable';
import DevicesTable from 'components/metrics/DevicesTable';
import CountriesTable from 'components/metrics/CountriesTable';
import LanguagesTable from 'components/metrics/LanguagesTable';
import EventsTable from 'components/metrics/EventsTable';
import EventsChart from 'components/metrics/EventsChart';
import useFetch from 'hooks/useFetch';
2020-09-26 08:38:28 +02:00
import usePageQuery from 'hooks/usePageQuery';
import { DEFAULT_ANIMATION_DURATION } from 'lib/constants';
2022-03-02 06:13:15 +01:00
import styles from './WebsiteDetails.module.css';
2020-08-01 04:05:14 +02:00
const views = {
url: PagesTable,
referrer: ReferrersTable,
browser: BrowsersTable,
os: OSTable,
device: DevicesTable,
country: CountriesTable,
2021-12-02 04:32:45 +01:00
language: LanguagesTable,
event: EventsTable,
};
2020-10-11 11:29:55 +02:00
export default function WebsiteDetails({ websiteId }) {
const { data } = useFetch(`/website/${websiteId}`);
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 = () => (
2022-02-17 08:10:35 +01:00
<div key="back-button" className={classNames(styles.backButton, 'col-12')}>
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
},
2021-12-02 04:32:45 +01:00
{
label: <FormattedMessage id="metrics.languages" defaultMessage="Languages" />,
value: resolve({ view: 'language' }),
},
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
/>
2021-11-22 07:00:14 +01:00
{!chartLoaded && <Loading />}
2020-08-02 06:20:52 +02:00
</div>
2020-08-01 12:34:56 +02:00
</div>
{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}>
<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
);
}