2021-03-28 21:56:16 +03:00
|
|
|
import { isEmpty, propEq, values } from 'ramda';
|
2020-11-14 00:44:26 +03:00
|
|
|
import { useState, useEffect, useMemo, FC } from 'react';
|
2020-12-30 21:48:02 +03:00
|
|
|
import { Button, Card, Nav, NavLink, Progress, Row } from 'reactstrap';
|
2020-09-05 09:49:18 +03:00
|
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
2021-03-14 20:12:10 +03:00
|
|
|
import { faCalendarAlt, faMapMarkedAlt, faList, faChartPie, faFileDownload } from '@fortawesome/free-solid-svg-icons';
|
2020-12-12 22:45:23 +03:00
|
|
|
import { IconDefinition } from '@fortawesome/fontawesome-common-types';
|
2020-12-20 21:28:09 +03:00
|
|
|
import { Route, Switch, NavLink as RouterNavLink, Redirect } from 'react-router-dom';
|
|
|
|
import { Location } from 'history';
|
2021-03-28 16:57:22 +03:00
|
|
|
import classNames from 'classnames';
|
2020-12-15 00:58:15 +03:00
|
|
|
import { DateRangeSelector } from '../utils/dates/DateRangeSelector';
|
2020-09-05 09:49:18 +03:00
|
|
|
import Message from '../utils/Message';
|
2020-12-15 20:40:36 +03:00
|
|
|
import { formatIsoDate } from '../utils/helpers/date';
|
2020-12-22 11:49:13 +03:00
|
|
|
import { ShlinkVisitsParams } from '../api/types';
|
2020-12-15 20:40:36 +03:00
|
|
|
import { DateInterval, DateRange, intervalToDateRange } from '../utils/dates/types';
|
2020-12-21 19:54:05 +03:00
|
|
|
import { Result } from '../utils/Result';
|
2020-12-22 01:41:50 +03:00
|
|
|
import { ShlinkApiError } from '../api/ShlinkApiError';
|
2021-03-06 12:56:49 +03:00
|
|
|
import { Settings } from '../settings/reducers/settings';
|
2020-09-05 09:49:18 +03:00
|
|
|
import SortableBarGraph from './helpers/SortableBarGraph';
|
|
|
|
import GraphCard from './helpers/GraphCard';
|
|
|
|
import LineChartCard from './helpers/LineChartCard';
|
|
|
|
import VisitsTable from './VisitsTable';
|
2021-03-28 21:56:16 +03:00
|
|
|
import { NormalizedOrphanVisit, NormalizedVisit, OrphanVisitType, VisitsInfo } from './types';
|
2020-09-05 09:49:18 +03:00
|
|
|
import OpenMapModalBtn from './helpers/OpenMapModalBtn';
|
2021-03-28 21:56:16 +03:00
|
|
|
import { processStatsFromVisits } from './services/VisitsParser';
|
2021-03-28 18:45:47 +03:00
|
|
|
import { OrphanVisitTypeDropdown } from './helpers/OrphanVisitTypeDropdown';
|
2020-12-12 22:45:23 +03:00
|
|
|
import './VisitsStats.scss';
|
2021-03-28 21:56:16 +03:00
|
|
|
import { HighlightableProps, highlightedVisitsToStats, normalizeAndFilterVisits } from './types/helpers';
|
2020-09-05 09:49:18 +03:00
|
|
|
|
|
|
|
export interface VisitsStatsProps {
|
|
|
|
getVisits: (params: Partial<ShlinkVisitsParams>) => void;
|
|
|
|
visitsInfo: VisitsInfo;
|
2021-03-06 12:56:49 +03:00
|
|
|
settings: Settings;
|
2020-09-05 09:49:18 +03:00
|
|
|
cancelGetVisits: () => void;
|
2020-12-20 21:28:09 +03:00
|
|
|
baseUrl: string;
|
|
|
|
domain?: string;
|
2021-03-14 14:49:12 +03:00
|
|
|
exportCsv: (visits: NormalizedVisit[]) => void;
|
2021-03-28 16:57:22 +03:00
|
|
|
isOrphanVisits?: boolean;
|
2020-09-05 09:49:18 +03:00
|
|
|
}
|
|
|
|
|
2020-12-21 11:22:13 +03:00
|
|
|
interface VisitsNavLinkProps {
|
|
|
|
title: string;
|
|
|
|
subPath: string;
|
|
|
|
icon: IconDefinition;
|
|
|
|
}
|
|
|
|
|
2020-12-12 22:45:23 +03:00
|
|
|
type Section = 'byTime' | 'byContext' | 'byLocation' | 'list';
|
|
|
|
|
2020-12-21 11:22:13 +03:00
|
|
|
const sections: Record<Section, VisitsNavLinkProps> = {
|
2020-12-20 21:42:37 +03:00
|
|
|
byTime: { title: 'By time', subPath: '', icon: faCalendarAlt },
|
|
|
|
byContext: { title: 'By context', subPath: '/by-context', icon: faChartPie },
|
|
|
|
byLocation: { title: 'By location', subPath: '/by-location', icon: faMapMarkedAlt },
|
|
|
|
list: { title: 'List', subPath: '/list', icon: faList },
|
2020-12-12 22:45:23 +03:00
|
|
|
};
|
2020-09-05 09:49:18 +03:00
|
|
|
|
|
|
|
let selectedBar: string | undefined;
|
|
|
|
|
2020-12-30 21:48:02 +03:00
|
|
|
const VisitsNavLink: FC<VisitsNavLinkProps & { to: string }> = ({ subPath, title, icon, to }) => (
|
2020-12-21 11:22:13 +03:00
|
|
|
<NavLink
|
|
|
|
tag={RouterNavLink}
|
|
|
|
className="visits-stats__nav-link"
|
2020-12-30 21:48:02 +03:00
|
|
|
to={to}
|
2021-02-27 22:03:51 +03:00
|
|
|
isActive={(_: null, { pathname }: Location) => pathname.endsWith(`visits${subPath}`)}
|
2020-12-21 11:22:13 +03:00
|
|
|
replace
|
|
|
|
>
|
|
|
|
<FontAwesomeIcon icon={icon} />
|
|
|
|
<span className="ml-2 d-none d-sm-inline">{title}</span>
|
|
|
|
</NavLink>
|
|
|
|
);
|
|
|
|
|
2021-03-06 12:56:49 +03:00
|
|
|
const VisitsStats: FC<VisitsStatsProps> = (
|
2021-03-28 16:57:22 +03:00
|
|
|
{ children, visitsInfo, getVisits, cancelGetVisits, baseUrl, domain, settings, exportCsv, isOrphanVisits = false },
|
2021-03-06 12:56:49 +03:00
|
|
|
) => {
|
|
|
|
const initialInterval: DateInterval = settings.visits?.defaultInterval ?? 'last30Days';
|
2020-12-15 20:40:36 +03:00
|
|
|
const [ dateRange, setDateRange ] = useState<DateRange>(intervalToDateRange(initialInterval));
|
2020-09-05 09:49:18 +03:00
|
|
|
const [ highlightedVisits, setHighlightedVisits ] = useState<NormalizedVisit[]>([]);
|
|
|
|
const [ highlightedLabel, setHighlightedLabel ] = useState<string | undefined>();
|
2021-03-28 18:45:47 +03:00
|
|
|
const [ orphanVisitType, setOrphanVisitType ] = useState<OrphanVisitType | undefined>();
|
2020-09-05 09:49:18 +03:00
|
|
|
|
2020-12-20 21:28:09 +03:00
|
|
|
const buildSectionUrl = (subPath?: string) => {
|
|
|
|
const query = domain ? `?domain=${domain}` : '';
|
|
|
|
|
2020-12-20 21:42:37 +03:00
|
|
|
return !subPath ? `${baseUrl}${query}` : `${baseUrl}${subPath}${query}`;
|
2020-12-20 21:28:09 +03:00
|
|
|
};
|
2020-12-22 01:41:50 +03:00
|
|
|
const { visits, loading, loadingLarge, error, errorData, progress } = visitsInfo;
|
2021-03-28 18:45:47 +03:00
|
|
|
const normalizedVisits = useMemo(
|
|
|
|
() => normalizeAndFilterVisits(visits, orphanVisitType),
|
|
|
|
[ visits, orphanVisitType ],
|
|
|
|
);
|
2021-03-28 21:56:16 +03:00
|
|
|
const { os, browsers, referrers, countries, cities, citiesForMap, visitedUrls } = useMemo(
|
2020-09-05 09:49:18 +03:00
|
|
|
() => processStatsFromVisits(normalizedVisits),
|
|
|
|
[ normalizedVisits ],
|
|
|
|
);
|
|
|
|
const mapLocations = values(citiesForMap);
|
|
|
|
|
|
|
|
const setSelectedVisits = (selectedVisits: NormalizedVisit[]) => {
|
|
|
|
selectedBar = undefined;
|
|
|
|
setHighlightedVisits(selectedVisits);
|
|
|
|
};
|
2021-03-28 21:56:16 +03:00
|
|
|
const highlightVisitsForProp = (prop: HighlightableProps<NormalizedOrphanVisit>) => (value: string) => {
|
2020-09-05 09:49:18 +03:00
|
|
|
const newSelectedBar = `${prop}_${value}`;
|
|
|
|
|
|
|
|
if (selectedBar === newSelectedBar) {
|
|
|
|
setHighlightedVisits([]);
|
|
|
|
setHighlightedLabel(undefined);
|
|
|
|
selectedBar = undefined;
|
|
|
|
} else {
|
2021-03-28 21:56:16 +03:00
|
|
|
setHighlightedVisits((normalizedVisits as NormalizedOrphanVisit[]).filter(propEq(prop, value)));
|
2020-09-05 09:49:18 +03:00
|
|
|
setHighlightedLabel(value);
|
|
|
|
selectedBar = newSelectedBar;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-12-15 20:40:36 +03:00
|
|
|
useEffect(() => cancelGetVisits, []);
|
2020-09-05 09:49:18 +03:00
|
|
|
useEffect(() => {
|
2020-12-15 20:40:36 +03:00
|
|
|
const { startDate, endDate } = dateRange;
|
|
|
|
|
|
|
|
getVisits({ startDate: formatIsoDate(startDate) ?? undefined, endDate: formatIsoDate(endDate) ?? undefined });
|
|
|
|
}, [ dateRange ]);
|
2020-09-05 09:49:18 +03:00
|
|
|
|
|
|
|
const renderVisitsContent = () => {
|
|
|
|
if (loadingLarge) {
|
|
|
|
return (
|
|
|
|
<Message loading>
|
|
|
|
This is going to take a while... :S
|
|
|
|
<Progress value={progress} striped={progress === 100} className="mt-3" />
|
|
|
|
</Message>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (loading) {
|
|
|
|
return <Message loading />;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error) {
|
2020-12-22 01:41:50 +03:00
|
|
|
return (
|
|
|
|
<Result type="error">
|
|
|
|
<ShlinkApiError errorData={errorData} fallbackMessage="An error occurred while loading visits :(" />
|
|
|
|
</Result>
|
|
|
|
);
|
2020-09-05 09:49:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isEmpty(visits)) {
|
|
|
|
return <Message>There are no visits matching current filter :(</Message>;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2020-12-12 22:45:23 +03:00
|
|
|
<>
|
2020-12-21 11:22:13 +03:00
|
|
|
<Card className="visits-stats__nav p-0 overflow-hidden" body>
|
2020-12-30 21:48:02 +03:00
|
|
|
<Nav pills fill>
|
2020-12-21 11:22:13 +03:00
|
|
|
{Object.entries(sections).map(([ section, props ]) =>
|
2020-12-30 21:48:02 +03:00
|
|
|
<VisitsNavLink key={section} {...props} to={buildSectionUrl(props.subPath)} />)}
|
2020-12-12 22:45:23 +03:00
|
|
|
</Nav>
|
|
|
|
</Card>
|
2020-12-30 21:48:02 +03:00
|
|
|
<Row>
|
2020-12-20 21:28:09 +03:00
|
|
|
<Switch>
|
|
|
|
<Route exact path={baseUrl}>
|
2021-03-29 22:08:48 +03:00
|
|
|
<div className="col-12 mt-3">
|
2020-12-20 21:28:09 +03:00
|
|
|
<LineChartCard
|
|
|
|
title="Visits during time"
|
|
|
|
visits={normalizedVisits}
|
|
|
|
highlightedVisits={highlightedVisits}
|
|
|
|
highlightedLabel={highlightedLabel}
|
|
|
|
setSelectedVisits={setSelectedVisits}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</Route>
|
|
|
|
|
2020-12-20 21:42:37 +03:00
|
|
|
<Route exact path={`${baseUrl}${sections.byContext.subPath}`}>
|
2021-03-29 22:08:48 +03:00
|
|
|
<div className={classNames('mt-3 col-lg-6', { 'col-xl-4': !isOrphanVisits })}>
|
2020-12-12 22:45:23 +03:00
|
|
|
<GraphCard title="Operating systems" stats={os} />
|
|
|
|
</div>
|
2021-03-29 22:08:48 +03:00
|
|
|
<div className={classNames('mt-3 col-lg-6', { 'col-xl-4': !isOrphanVisits })}>
|
2020-12-12 22:45:23 +03:00
|
|
|
<GraphCard title="Browsers" stats={browsers} />
|
|
|
|
</div>
|
2021-03-29 22:08:48 +03:00
|
|
|
<div className={classNames('mt-3', { 'col-xl-4': !isOrphanVisits, 'col-lg-6': isOrphanVisits })}>
|
2020-12-12 22:45:23 +03:00
|
|
|
<SortableBarGraph
|
|
|
|
title="Referrers"
|
|
|
|
stats={referrers}
|
|
|
|
withPagination={false}
|
|
|
|
highlightedStats={highlightedVisitsToStats(highlightedVisits, 'referer')}
|
|
|
|
highlightedLabel={highlightedLabel}
|
|
|
|
sortingItems={{
|
|
|
|
name: 'Referrer name',
|
|
|
|
amount: 'Visits amount',
|
|
|
|
}}
|
|
|
|
onClick={highlightVisitsForProp('referer')}
|
|
|
|
/>
|
|
|
|
</div>
|
2021-03-28 16:57:22 +03:00
|
|
|
{isOrphanVisits && (
|
2021-03-29 22:08:48 +03:00
|
|
|
<div className="mt-3 col-lg-6">
|
2021-03-28 16:57:22 +03:00
|
|
|
<SortableBarGraph
|
|
|
|
title="Visited URLs"
|
2021-03-28 21:56:16 +03:00
|
|
|
stats={visitedUrls}
|
|
|
|
highlightedLabel={highlightedLabel}
|
|
|
|
highlightedStats={highlightedVisitsToStats(highlightedVisits, 'visitedUrl')}
|
2021-03-28 16:57:22 +03:00
|
|
|
sortingItems={{
|
|
|
|
visitedUrl: 'Visited URL',
|
|
|
|
amount: 'Visits amount',
|
|
|
|
}}
|
2021-03-28 21:56:16 +03:00
|
|
|
onClick={highlightVisitsForProp('visitedUrl')}
|
2021-03-28 16:57:22 +03:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)}
|
2020-12-20 21:28:09 +03:00
|
|
|
</Route>
|
|
|
|
|
2020-12-20 21:42:37 +03:00
|
|
|
<Route exact path={`${baseUrl}${sections.byLocation.subPath}`}>
|
2021-03-29 22:08:48 +03:00
|
|
|
<div className="col-lg-6 mt-3">
|
2020-12-12 22:45:23 +03:00
|
|
|
<SortableBarGraph
|
|
|
|
title="Countries"
|
|
|
|
stats={countries}
|
|
|
|
highlightedStats={highlightedVisitsToStats(highlightedVisits, 'country')}
|
|
|
|
highlightedLabel={highlightedLabel}
|
|
|
|
sortingItems={{
|
|
|
|
name: 'Country name',
|
|
|
|
amount: 'Visits amount',
|
|
|
|
}}
|
|
|
|
onClick={highlightVisitsForProp('country')}
|
|
|
|
/>
|
|
|
|
</div>
|
2021-03-29 22:08:48 +03:00
|
|
|
<div className="col-lg-6 mt-3">
|
2020-12-12 22:45:23 +03:00
|
|
|
<SortableBarGraph
|
|
|
|
title="Cities"
|
|
|
|
stats={cities}
|
|
|
|
highlightedStats={highlightedVisitsToStats(highlightedVisits, 'city')}
|
|
|
|
highlightedLabel={highlightedLabel}
|
|
|
|
extraHeaderContent={(activeCities: string[]) =>
|
|
|
|
mapLocations.length > 0 &&
|
|
|
|
<OpenMapModalBtn modalTitle="Cities" locations={mapLocations} activeCities={activeCities} />
|
|
|
|
}
|
|
|
|
sortingItems={{
|
|
|
|
name: 'City name',
|
|
|
|
amount: 'Visits amount',
|
|
|
|
}}
|
|
|
|
onClick={highlightVisitsForProp('city')}
|
|
|
|
/>
|
|
|
|
</div>
|
2020-12-20 21:28:09 +03:00
|
|
|
</Route>
|
|
|
|
|
2020-12-20 21:42:37 +03:00
|
|
|
<Route exact path={`${baseUrl}${sections.list.subPath}`}>
|
2020-12-20 21:28:09 +03:00
|
|
|
<div className="col-12">
|
|
|
|
<VisitsTable
|
|
|
|
visits={normalizedVisits}
|
|
|
|
selectedVisits={highlightedVisits}
|
|
|
|
setSelectedVisits={setSelectedVisits}
|
2021-03-28 16:57:22 +03:00
|
|
|
isOrphanVisits={isOrphanVisits}
|
2020-12-20 21:28:09 +03:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</Route>
|
|
|
|
|
|
|
|
<Redirect to={baseUrl} />
|
|
|
|
</Switch>
|
2020-12-30 21:48:02 +03:00
|
|
|
</Row>
|
2020-12-12 22:45:23 +03:00
|
|
|
</>
|
2020-09-05 09:49:18 +03:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2020-11-14 00:44:26 +03:00
|
|
|
<>
|
2020-09-05 09:49:18 +03:00
|
|
|
{children}
|
|
|
|
|
2021-03-29 22:08:48 +03:00
|
|
|
<section className="mt-3">
|
2020-09-05 09:49:18 +03:00
|
|
|
<div className="row flex-md-row-reverse">
|
|
|
|
<div className="col-lg-7 col-xl-6">
|
2021-03-28 18:45:47 +03:00
|
|
|
<div className="d-md-flex">
|
|
|
|
<div className="flex-fill">
|
|
|
|
<DateRangeSelector
|
|
|
|
disabled={loading}
|
|
|
|
initialDateRange={initialInterval}
|
|
|
|
defaultText="All visits"
|
|
|
|
onDatesChange={setDateRange}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
{isOrphanVisits && (
|
|
|
|
<OrphanVisitTypeDropdown
|
|
|
|
text="Filter by type"
|
2021-03-29 22:08:48 +03:00
|
|
|
className="ml-0 ml-md-2 mt-3 mt-md-0"
|
2021-03-28 18:45:47 +03:00
|
|
|
selected={orphanVisitType}
|
|
|
|
onChange={setOrphanVisitType}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</div>
|
2020-09-05 09:49:18 +03:00
|
|
|
</div>
|
2020-12-12 22:45:23 +03:00
|
|
|
{visits.length > 0 && (
|
2021-03-29 22:08:48 +03:00
|
|
|
<div className="col-lg-5 col-xl-6 mt-3 mt-lg-0">
|
2021-03-14 15:30:50 +03:00
|
|
|
<div className="d-flex">
|
|
|
|
<Button
|
|
|
|
outline
|
|
|
|
disabled={highlightedVisits.length === 0}
|
|
|
|
className="btn-md-block mr-2"
|
|
|
|
onClick={() => setSelectedVisits([])}
|
|
|
|
>
|
|
|
|
Clear selection {highlightedVisits.length > 0 && <>({highlightedVisits.length})</>}
|
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
outline
|
|
|
|
color="primary"
|
|
|
|
className="btn-md-block"
|
|
|
|
onClick={() => exportCsv(normalizedVisits)}
|
|
|
|
>
|
2021-03-14 20:12:10 +03:00
|
|
|
<FontAwesomeIcon icon={faFileDownload} /> Export ({normalizedVisits.length})
|
2021-03-14 15:30:50 +03:00
|
|
|
</Button>
|
|
|
|
</div>
|
2020-12-12 22:45:23 +03:00
|
|
|
</div>
|
|
|
|
)}
|
2020-09-05 09:49:18 +03:00
|
|
|
</div>
|
|
|
|
</section>
|
|
|
|
|
2021-03-29 22:08:48 +03:00
|
|
|
<section className="mt-3">
|
2020-09-05 09:49:18 +03:00
|
|
|
{renderVisitsContent()}
|
|
|
|
</section>
|
2020-11-14 00:44:26 +03:00
|
|
|
</>
|
2020-09-05 09:49:18 +03:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default VisitsStats;
|