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 { Route, Switch, NavLink as RouterNavLink, Redirect } from 'react-router-dom'; import { Location } from 'history'; import { DateRangeSelector } from '../utils/dates/DateRangeSelector'; import Message from '../utils/Message'; import { formatIsoDate } from '../utils/helpers/date'; import { ShlinkVisitsParams } from '../utils/services/types'; import { DateInterval, DateRange, intervalToDateRange } from '../utils/dates/types'; import { Result } from '../utils/Result'; import { ShlinkApiError } from '../api/ShlinkApiError'; 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; baseUrl: string; domain?: string; } interface VisitsNavLinkProps { title: string; subPath: string; icon: IconDefinition; } type HighlightableProps = 'referer' | 'country' | 'city'; type Section = 'byTime' | 'byContext' | 'byLocation' | 'list'; const sections: Record = { 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 }, }; 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; }, {}); let selectedBar: string | undefined; const initialInterval: DateInterval = 'last30Days'; const VisitsNavLink: FC = ({ subPath, title, icon, children }) => ( pathname.endsWith(`/visits${subPath}`)} replace > {title} ); const VisitsStats: FC = ({ children, visitsInfo, getVisits, cancelGetVisits, baseUrl, domain }) => { const [ dateRange, setDateRange ] = useState(intervalToDateRange(initialInterval)); const [ highlightedVisits, setHighlightedVisits ] = useState([]); const [ highlightedLabel, setHighlightedLabel ] = useState(); const buildSectionUrl = (subPath?: string) => { const query = domain ? `?domain=${domain}` : ''; return !subPath ? `${baseUrl}${query}` : `${baseUrl}${subPath}${query}`; }; const { visits, loading, loadingLarge, error, errorData, 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(() => { const { startDate, endDate } = dateRange; getVisits({ startDate: formatIsoDate(startDate) ?? undefined, endDate: formatIsoDate(endDate) ?? undefined }); }, [ dateRange ]); const renderVisitsContent = () => { if (loadingLarge) { return ( This is going to take a while... :S ); } if (loading) { return ; } if (error) { return ( ); } if (isEmpty(visits)) { return There are no visits matching current filter :(; } return ( <>
mapLocations.length > 0 && } sortingItems={{ name: 'City name', amount: 'Visits amount', }} onClick={highlightVisitsForProp('city')} />
); }; return ( <> {children}
{visits.length > 0 && (
)}
{renderVisitsContent()}
); }; export default VisitsStats;