import React, { useEffect, useState } from 'react'; import classNames from 'classnames'; import WebsiteChart from 'components/metrics/WebsiteChart'; import WorldMap from 'components/common/WorldMap'; import Page from 'components/layout/Page'; import WebsiteHeader from 'components/metrics/WebsiteHeader'; import MenuLayout from 'components/layout/MenuLayout'; import Button from 'components/common/Button'; import { getDateRange } from 'lib/date'; import { get } from 'lib/web'; import Arrow from 'assets/arrow-right.svg'; 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'; import EventsTable from './metrics/EventsTable'; import EventsChart from './metrics/EventsChart'; export default function WebsiteDetails({ websiteId, defaultDateRange = '7day' }) { const [data, setData] = useState(); const [chartLoaded, setChartLoaded] = useState(false); const [countryData, setCountryData] = useState(); const [eventsData, setEventsData] = useState(); const [dateRange, setDateRange] = useState(getDateRange(defaultDateRange)); const [expand, setExpand] = useState(); const { startDate, endDate, unit } = dateRange; const BackButton = () => ( ); const menuOptions = [ { render: BackButton, }, { 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 }, { label: 'Countries', value: 'country', component: props => setCountryData(data)} />, }, { label: 'Events', value: 'event', component: EventsTable }, ]; const dataProps = { websiteId, startDate, endDate, unit, }; const tableProps = { ...dataProps, websiteDomain: data?.domain, limit: 10, onExpand: handleExpand, }; const DetailsComponent = expand?.component; function getSelectedMenuOption(value) { return menuOptions.find(e => e.value === value); } async function loadData() { setData(await get(`/api/website/${websiteId}`)); } function handleDataLoad() { if (!chartLoaded) setTimeout(() => setChartLoaded(true), 300); } function handleDateChange(values) { setTimeout(() => setDateRange(values), 300); } function handleExpand(value) { setExpand(getSelectedMenuOption(value)); } function handleMenuSelect(value) { setExpand(getSelectedMenuOption(value)); } useEffect(() => { if (websiteId) { loadData(); } }, [websiteId]); if (!data) { return null; } return (
{chartLoaded && !expand && ( <>
0 })} >
)} {expand && ( )}
); }