import React, { useState, useEffect, useRef } from 'react'; import { Link } from 'react-router-dom' import FadeIn from '../../fade-in' import MoreLink from '../more-link' import numberFormatter from '../../util/number-formatter' import Bar from '../bar' import LazyLoader from '../../components/lazy-loader' function ExternalLink({item, externalLinkDest}) { if (externalLinkDest) { const dest = externalLinkDest(item) return ( ) } return null } export default function ListReport(props) { const [state, setState] = useState({loading: true, list: null}) const valueKey = props.valueKey || 'visitors' const showConversionRate = !!props.query.filters.goal const prevQuery = useRef(); function fetchData() { if (typeof(prevQuery.current) === 'undefined' || prevQuery.current !== props.query) { prevQuery.current = props.query; setState({loading: true, list: null}) props.fetchData() .then((res) => setState({loading: false, list: res})) } } function onVisible() { fetchData() if (props.timer) props.timer.onTick(fetchData) } function label() { if (props.query.period === 'realtime') { return 'Current visitors' } if (showConversionRate) { return 'Conversions' } return props.valueLabel || 'Visitors' } useEffect(fetchData, [props.query]); function renderListItem(listItem) { const query = new URLSearchParams(window.location.search) Object.entries(props.filter).forEach((([key, valueKey]) => { query.set(key, listItem[valueKey]) })) const maxWidthDeduction = showConversionRate ? "10rem" : "5rem" const lightBackground = props.color || 'bg-green-50' const noop = () => {} return (