import { isEmpty, propEq, values } from 'ramda'; import { useState, useEffect, useMemo, FC } from 'react'; import { Button, Card, Nav, NavLink, Progress } from 'reactstrap'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { faCalendarAlt, faMapMarkedAlt, faList, faChartPie } from '@fortawesome/free-solid-svg-icons'; import { IconDefinition } from '@fortawesome/fontawesome-common-types'; import moment from 'moment'; import DateRangeRow from '../utils/DateRangeRow'; import Message from '../utils/Message'; import { formatDate } from '../utils/helpers/date'; import { ShlinkVisitsParams } from '../utils/services/types'; import SortableBarGraph from './helpers/SortableBarGraph'; import GraphCard from './helpers/GraphCard'; import LineChartCard from './helpers/LineChartCard'; import VisitsTable from './VisitsTable'; import { NormalizedVisit, Stats, VisitsInfo } from './types'; import OpenMapModalBtn from './helpers/OpenMapModalBtn'; import { normalizeVisits, processStatsFromVisits } from './services/VisitsParser'; import './VisitsStats.scss'; export interface VisitsStatsProps { getVisits: (params: Partial) => void; visitsInfo: VisitsInfo; cancelGetVisits: () => void; } type HighlightableProps = 'referer' | 'country' | 'city'; type Section = 'byTime' | 'byContext' | 'byLocation' | 'list'; const sections: Record = { byTime: { title: 'By time', icon: faCalendarAlt }, byContext: { title: 'By context', icon: faChartPie }, byLocation: { title: 'By location', icon: faMapMarkedAlt }, list: { title: 'List', icon: faList }, }; const highlightedVisitsToStats = ( highlightedVisits: NormalizedVisit[], prop: HighlightableProps, ): Stats => highlightedVisits.reduce((acc, highlightedVisit) => { if (!acc[highlightedVisit[prop]]) { acc[highlightedVisit[prop]] = 0; } acc[highlightedVisit[prop]] += 1; return acc; }, {}); const format = formatDate(); let selectedBar: string | undefined; const VisitsStats: FC = ({ children, visitsInfo, getVisits, cancelGetVisits }) => { const [ startDate, setStartDate ] = useState(null); const [ endDate, setEndDate ] = useState(null); const [ highlightedVisits, setHighlightedVisits ] = useState([]); const [ highlightedLabel, setHighlightedLabel ] = useState(); const [ activeSection, setActiveSection ] = useState
('byTime'); const onSectionChange = (section: Section) => () => setActiveSection(section); const { visits, loading, loadingLarge, error, progress } = visitsInfo; const normalizedVisits = useMemo(() => normalizeVisits(visits), [ visits ]); const { os, browsers, referrers, countries, cities, citiesForMap } = useMemo( () => processStatsFromVisits(normalizedVisits), [ normalizedVisits ], ); const mapLocations = values(citiesForMap); const setSelectedVisits = (selectedVisits: NormalizedVisit[]) => { selectedBar = undefined; setHighlightedVisits(selectedVisits); }; const highlightVisitsForProp = (prop: HighlightableProps) => (value: string) => { const newSelectedBar = `${prop}_${value}`; if (selectedBar === newSelectedBar) { setHighlightedVisits([]); setHighlightedLabel(undefined); selectedBar = undefined; } else { setHighlightedVisits(normalizedVisits.filter(propEq(prop, value))); setHighlightedLabel(value); selectedBar = newSelectedBar; } }; useEffect(() => () => cancelGetVisits(), []); useEffect(() => { getVisits({ startDate: format(startDate) ?? undefined, endDate: format(endDate) ?? undefined }); }, [ startDate, endDate ]); const renderVisitsContent = () => { if (loadingLarge) { return ( This is going to take a while... :S ); } if (loading) { return ; } if (error) { return ( An error occurred while loading visits :( ); } if (isEmpty(visits)) { return There are no visits matching current filter :(; } return ( <>
{activeSection === 'byTime' && (
)} {activeSection === 'byContext' && ( <>
)} {activeSection === 'byLocation' && ( <>
mapLocations.length > 0 && } sortingItems={{ name: 'City name', amount: 'Visits amount', }} onClick={highlightVisitsForProp('city')} />
)} {activeSection === 'list' && (
)}
); }; return ( <> {children}
{visits.length > 0 && (
)}
{renderVisitsContent()}
); }; export default VisitsStats;