import React, { useEffect, useState } from 'react'; import classNames from 'classnames'; import WebsiteChart from './charts/WebsiteChart'; import RankingsChart from './charts/RankingsChart'; import WorldMap from './common/WorldMap'; import Page from './layout/Page'; import PageHeader from './layout/PageHeader'; import MenuLayout from './layout/MenuLayout'; import Button from './common/Button'; import { getDateRange } from 'lib/date'; import { get } from 'lib/web'; import { browserFilter, urlFilter, refFilter, deviceFilter, countryFilter } from 'lib/filters'; import Arrow from 'assets/arrow-right.svg'; import styles from './WebsiteDetails.module.css'; const pageviewClasses = 'col-md-12 col-lg-6'; const sessionClasses = 'col-md-12 col-lg-4'; export default function WebsiteDetails({ websiteId, defaultDateRange = '7day' }) { const [data, setData] = useState(); const [chartLoaded, setChartLoaded] = useState(false); const [countryData, setCountryData] = useState(); const [dateRange, setDateRange] = useState(getDateRange(defaultDateRange)); const [expand, setExpand] = useState(); const { startDate, endDate } = dateRange; const menuOptions = [ { render: () => ( ), }, { label: 'Pages', value: 'url', filter: urlFilter }, { label: 'Referrers', value: 'referrer', filter: refFilter(data?.domain) }, { label: 'Browsers', value: 'browser', filter: browserFilter }, { label: 'Operating system', value: 'os' }, { label: 'Devices', value: 'device', filter: deviceFilter }, { label: 'Countries', value: 'country', filter: countryFilter, onDataLoad: data => setCountryData(data), }, ]; 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(menuOptions.find(e => e.value === value)); } function handleMenuSelect(value) { setExpand(menuOptions.find(e => e.value === value)); } function getHeading(type) { return type === 'url' || type === 'referrer' ? 'Views' : 'Visitors'; } useEffect(() => { if (websiteId) { loadData(); } }, [websiteId]); if (!data) { return null; } return (
{data.name}
{chartLoaded && !expand && ( <>
setCountryData(data)} onExpand={handleExpand} />
)} {expand && ( )}
); }