2022-12-03 14:15:36 +03:00
|
|
|
import { isEmpty, pipe, propEq, values } from 'ramda';
|
2022-04-24 11:39:11 +03:00
|
|
|
import { useState, useEffect, useMemo, FC, useRef, PropsWithChildren } from 'react';
|
2022-02-13 22:20:20 +03:00
|
|
|
import { Button, Progress, Row } from 'reactstrap';
|
2020-09-05 09:49:18 +03:00
|
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
2022-03-12 22:51:30 +03:00
|
|
|
import { faCalendarAlt, faMapMarkedAlt, faList, faChartPie } from '@fortawesome/free-solid-svg-icons';
|
2020-12-12 22:45:23 +03:00
|
|
|
import { IconDefinition } from '@fortawesome/fontawesome-common-types';
|
2022-02-13 22:20:20 +03:00
|
|
|
import { Route, Routes, Navigate } from 'react-router-dom';
|
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';
|
2022-05-28 12:16:59 +03:00
|
|
|
import { Message } from '../utils/Message';
|
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';
|
2021-06-13 12:07:32 +03:00
|
|
|
import { SelectedServer } from '../servers/data';
|
2021-06-22 22:01:20 +03:00
|
|
|
import { supportsBotVisits } from '../utils/helpers/features';
|
2021-11-01 13:18:09 +03:00
|
|
|
import { prettify } from '../utils/helpers/numbers';
|
2022-02-14 21:58:20 +03:00
|
|
|
import { NavPillItem, NavPills } from '../utils/NavPills';
|
2022-03-12 22:51:30 +03:00
|
|
|
import { ExportBtn } from '../utils/ExportBtn';
|
2022-05-28 11:34:12 +03:00
|
|
|
import { LineChartCard } from './charts/LineChartCard';
|
2022-05-28 12:16:59 +03:00
|
|
|
import { VisitsTable } from './VisitsTable';
|
2022-12-03 14:15:36 +03:00
|
|
|
import { NormalizedOrphanVisit, NormalizedVisit, VisitsParams } from './types';
|
2022-05-28 12:16:59 +03:00
|
|
|
import { OpenMapModalBtn } from './helpers/OpenMapModalBtn';
|
2021-06-30 04:23:45 +03:00
|
|
|
import { normalizeVisits, processStatsFromVisits } from './services/VisitsParser';
|
2021-06-30 03:36:13 +03:00
|
|
|
import { VisitsFilterDropdown } from './helpers/VisitsFilterDropdown';
|
2021-06-30 04:23:45 +03:00
|
|
|
import { HighlightableProps, highlightedVisitsToStats } from './types/helpers';
|
2021-09-18 20:05:28 +03:00
|
|
|
import { DoughnutChartCard } from './charts/DoughnutChartCard';
|
|
|
|
import { SortableBarChartCard } from './charts/SortableBarChartCard';
|
2022-11-12 11:18:37 +03:00
|
|
|
import { VisitsInfo } from './reducers/types';
|
2022-12-03 14:15:36 +03:00
|
|
|
import { useVisitsQuery } from './helpers/hooks';
|
|
|
|
import { DateInterval, DateRange, toDateRange } from '../utils/dates/types';
|
2020-09-05 09:49:18 +03:00
|
|
|
|
2022-04-24 11:39:11 +03:00
|
|
|
export type VisitsStatsProps = PropsWithChildren<{
|
2021-12-23 12:51:13 +03:00
|
|
|
getVisits: (params: VisitsParams, doIntervalFallback?: boolean) => void;
|
2020-09-05 09:49:18 +03:00
|
|
|
visitsInfo: VisitsInfo;
|
2021-03-06 12:56:49 +03:00
|
|
|
settings: Settings;
|
2021-06-13 12:07:32 +03:00
|
|
|
selectedServer: SelectedServer;
|
2020-09-05 09:49:18 +03:00
|
|
|
cancelGetVisits: () => void;
|
2020-12-20 21:28:09 +03:00
|
|
|
domain?: string;
|
2021-03-14 14:49:12 +03:00
|
|
|
exportCsv: (visits: NormalizedVisit[]) => void;
|
2021-03-28 16:57:22 +03:00
|
|
|
isOrphanVisits?: boolean;
|
2022-04-24 11:39:11 +03:00
|
|
|
}>;
|
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> = {
|
2022-02-06 22:07:18 +03:00
|
|
|
byTime: { title: 'By time', subPath: 'by-time', 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;
|
|
|
|
|
2022-05-28 11:34:12 +03:00
|
|
|
export const VisitsStats: FC<VisitsStatsProps> = ({
|
2021-06-13 12:07:32 +03:00
|
|
|
children,
|
|
|
|
visitsInfo,
|
|
|
|
getVisits,
|
|
|
|
cancelGetVisits,
|
|
|
|
domain,
|
|
|
|
settings,
|
|
|
|
exportCsv,
|
|
|
|
selectedServer,
|
|
|
|
isOrphanVisits = false,
|
|
|
|
}) => {
|
2021-12-22 22:23:26 +03:00
|
|
|
const { visits, loading, loadingLarge, error, errorData, progress, fallbackInterval } = visitsInfo;
|
2022-12-03 14:15:36 +03:00
|
|
|
const [{ dateRange, visitsFilter }, updateFiltering] = useVisitsQuery();
|
|
|
|
const setDates = pipe(
|
|
|
|
({ startDate: theStartDate, endDate: theEndDate }: DateRange) => ({
|
|
|
|
dateRange: {
|
|
|
|
startDate: theStartDate ?? undefined,
|
|
|
|
endDate: theEndDate ?? undefined,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
updateFiltering,
|
|
|
|
);
|
|
|
|
const initialInterval = useRef<DateRange | DateInterval>(
|
|
|
|
dateRange ?? fallbackInterval ?? settings.visits?.defaultInterval ?? 'last30Days',
|
2021-12-22 22:23:26 +03:00
|
|
|
);
|
2022-03-26 14:17:42 +03:00
|
|
|
const [highlightedVisits, setHighlightedVisits] = useState<NormalizedVisit[]>([]);
|
|
|
|
const [highlightedLabel, setHighlightedLabel] = useState<string | undefined>();
|
2021-06-22 22:01:20 +03:00
|
|
|
const botsSupported = supportsBotVisits(selectedServer);
|
2021-12-22 22:23:26 +03:00
|
|
|
const isFirstLoad = useRef(true);
|
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}` : '';
|
2022-02-06 22:07:18 +03:00
|
|
|
return !subPath ? `${query}` : `${subPath}${query}`;
|
2020-12-20 21:28:09 +03:00
|
|
|
};
|
2022-03-26 14:17:42 +03:00
|
|
|
const normalizedVisits = useMemo(() => normalizeVisits(visits), [visits]);
|
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),
|
2022-03-26 14:17:42 +03:00
|
|
|
[normalizedVisits],
|
2020-09-05 09:49:18 +03:00
|
|
|
);
|
|
|
|
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(() => {
|
2022-12-03 14:15:36 +03:00
|
|
|
const resolvedDateRange = !isFirstLoad.current ? dateRange : (dateRange ?? toDateRange(initialInterval.current));
|
|
|
|
getVisits({ dateRange: resolvedDateRange, filter: visitsFilter }, isFirstLoad.current);
|
2021-12-22 22:23:26 +03:00
|
|
|
isFirstLoad.current = false;
|
2022-03-26 14:17:42 +03:00
|
|
|
}, [dateRange, visitsFilter]);
|
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)) {
|
2022-07-17 11:24:34 +03:00
|
|
|
return <Message>There are no visits matching current filter</Message>;
|
2020-09-05 09:49:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2020-12-12 22:45:23 +03:00
|
|
|
<>
|
2022-02-14 22:28:28 +03:00
|
|
|
<NavPills fill>
|
2022-02-14 21:58:20 +03:00
|
|
|
{Object.values(sections).map(({ title, icon, subPath }, index) => (
|
|
|
|
<NavPillItem key={index} to={buildSectionUrl(subPath)} replace>
|
|
|
|
<FontAwesomeIcon icon={icon} />
|
2022-03-05 15:26:28 +03:00
|
|
|
<span className="ms-2 d-none d-sm-inline">{title}</span>
|
2022-02-14 21:58:20 +03:00
|
|
|
</NavPillItem>
|
|
|
|
))}
|
|
|
|
</NavPills>
|
2020-12-30 21:48:02 +03:00
|
|
|
<Row>
|
2022-02-06 22:07:18 +03:00
|
|
|
<Routes>
|
|
|
|
<Route
|
|
|
|
path={sections.byTime.subPath}
|
|
|
|
element={(
|
|
|
|
<div className="col-12 mt-3">
|
|
|
|
<LineChartCard
|
|
|
|
title="Visits during time"
|
|
|
|
visits={normalizedVisits}
|
|
|
|
highlightedVisits={highlightedVisits}
|
2021-03-28 21:56:16 +03:00
|
|
|
highlightedLabel={highlightedLabel}
|
2022-02-06 22:07:18 +03:00
|
|
|
setSelectedVisits={setSelectedVisits}
|
2021-03-28 16:57:22 +03:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)}
|
2022-02-06 22:07:18 +03:00
|
|
|
/>
|
2020-12-20 21:28:09 +03:00
|
|
|
|
2022-02-06 22:07:18 +03:00
|
|
|
<Route
|
|
|
|
path={sections.byContext.subPath}
|
|
|
|
element={(
|
|
|
|
<>
|
|
|
|
<div className={classNames('mt-3 col-lg-6', { 'col-xl-4': !isOrphanVisits })}>
|
|
|
|
<DoughnutChartCard title="Operating systems" stats={os} />
|
|
|
|
</div>
|
|
|
|
<div className={classNames('mt-3 col-lg-6', { 'col-xl-4': !isOrphanVisits })}>
|
|
|
|
<DoughnutChartCard title="Browsers" stats={browsers} />
|
|
|
|
</div>
|
|
|
|
<div className={classNames('mt-3', { 'col-xl-4': !isOrphanVisits, 'col-lg-6': isOrphanVisits })}>
|
|
|
|
<SortableBarChartCard
|
|
|
|
title="Referrers"
|
|
|
|
stats={referrers}
|
|
|
|
withPagination={false}
|
|
|
|
highlightedStats={highlightedVisitsToStats(highlightedVisits, 'referer')}
|
|
|
|
highlightedLabel={highlightedLabel}
|
|
|
|
sortingItems={{
|
|
|
|
name: 'Referrer name',
|
|
|
|
amount: 'Visits amount',
|
|
|
|
}}
|
|
|
|
onClick={highlightVisitsForProp('referer')}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
{isOrphanVisits && (
|
|
|
|
<div className="mt-3 col-lg-6">
|
|
|
|
<SortableBarChartCard
|
|
|
|
title="Visited URLs"
|
|
|
|
stats={visitedUrls}
|
|
|
|
highlightedLabel={highlightedLabel}
|
|
|
|
highlightedStats={highlightedVisitsToStats(highlightedVisits, 'visitedUrl')}
|
|
|
|
sortingItems={{
|
|
|
|
visitedUrl: 'Visited URL',
|
|
|
|
amount: 'Visits amount',
|
|
|
|
}}
|
|
|
|
onClick={highlightVisitsForProp('visitedUrl')}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
/>
|
2020-12-20 21:28:09 +03:00
|
|
|
|
2022-02-06 22:07:18 +03:00
|
|
|
<Route
|
|
|
|
path={sections.byLocation.subPath}
|
|
|
|
element={(
|
|
|
|
<>
|
|
|
|
<div className="col-lg-6 mt-3">
|
|
|
|
<SortableBarChartCard
|
|
|
|
title="Countries"
|
|
|
|
stats={countries}
|
|
|
|
highlightedStats={highlightedVisitsToStats(highlightedVisits, 'country')}
|
|
|
|
highlightedLabel={highlightedLabel}
|
|
|
|
sortingItems={{
|
|
|
|
name: 'Country name',
|
|
|
|
amount: 'Visits amount',
|
|
|
|
}}
|
|
|
|
onClick={highlightVisitsForProp('country')}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className="col-lg-6 mt-3">
|
|
|
|
<SortableBarChartCard
|
|
|
|
title="Cities"
|
|
|
|
stats={cities}
|
|
|
|
highlightedStats={highlightedVisitsToStats(highlightedVisits, 'city')}
|
|
|
|
highlightedLabel={highlightedLabel}
|
2022-06-11 19:10:08 +03:00
|
|
|
extraHeaderContent={(activeCities) => mapLocations.length > 0 && (
|
2022-02-06 22:07:18 +03:00
|
|
|
<OpenMapModalBtn modalTitle="Cities" locations={mapLocations} activeCities={activeCities} />
|
2022-03-26 14:17:42 +03:00
|
|
|
)}
|
2022-02-06 22:07:18 +03:00
|
|
|
sortingItems={{
|
|
|
|
name: 'City name',
|
|
|
|
amount: 'Visits amount',
|
|
|
|
}}
|
|
|
|
onClick={highlightVisitsForProp('city')}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
|
|
|
|
<Route
|
|
|
|
path={sections.list.subPath}
|
|
|
|
element={(
|
|
|
|
<div className="col-12">
|
|
|
|
<VisitsTable
|
|
|
|
visits={normalizedVisits}
|
|
|
|
selectedVisits={highlightedVisits}
|
|
|
|
setSelectedVisits={setSelectedVisits}
|
|
|
|
isOrphanVisits={isOrphanVisits}
|
|
|
|
selectedServer={selectedServer}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
/>
|
2020-12-20 21:28:09 +03:00
|
|
|
|
2022-02-06 22:07:18 +03:00
|
|
|
<Route path="*" element={<Navigate replace to={buildSectionUrl(sections.byTime.subPath)} />} />
|
|
|
|
</Routes>
|
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
|
2021-12-22 22:23:26 +03:00
|
|
|
updatable
|
2021-03-28 18:45:47 +03:00
|
|
|
disabled={loading}
|
2022-12-03 14:15:36 +03:00
|
|
|
initialDateRange={initialInterval.current}
|
2021-03-28 18:45:47 +03:00
|
|
|
defaultText="All visits"
|
2022-12-03 14:15:36 +03:00
|
|
|
onDatesChange={setDates}
|
2021-03-28 18:45:47 +03:00
|
|
|
/>
|
|
|
|
</div>
|
2021-06-22 21:34:28 +03:00
|
|
|
<VisitsFilterDropdown
|
2022-03-05 15:26:28 +03:00
|
|
|
className="ms-0 ms-md-2 mt-3 mt-md-0"
|
2021-06-22 21:34:28 +03:00
|
|
|
isOrphanVisits={isOrphanVisits}
|
2021-06-22 22:01:20 +03:00
|
|
|
botsSupported={botsSupported}
|
2021-06-22 21:34:28 +03:00
|
|
|
selected={visitsFilter}
|
2022-12-03 14:15:36 +03:00
|
|
|
onChange={(newVisitsFilter) => updateFiltering({ visitsFilter: newVisitsFilter })}
|
2021-06-22 21:34:28 +03:00
|
|
|
/>
|
2021-03-28 18:45:47 +03:00
|
|
|
</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">
|
2022-05-25 19:26:34 +03:00
|
|
|
<ExportBtn
|
|
|
|
className="btn-md-block"
|
|
|
|
amount={normalizedVisits.length}
|
|
|
|
onClick={() => exportCsv(normalizedVisits)}
|
|
|
|
/>
|
2021-03-14 15:30:50 +03:00
|
|
|
<Button
|
|
|
|
outline
|
|
|
|
disabled={highlightedVisits.length === 0}
|
2022-05-25 19:26:34 +03:00
|
|
|
className="btn-md-block ms-2"
|
2021-03-14 15:30:50 +03:00
|
|
|
onClick={() => setSelectedVisits([])}
|
|
|
|
>
|
2021-11-01 13:18:09 +03:00
|
|
|
Clear selection {highlightedVisits.length > 0 && <>({prettify(highlightedVisits.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
|
|
|
);
|
|
|
|
};
|