import React, { useState, useEffect, useCallback } from 'react'; import { Link } from 'react-router-dom' import FlipMove from 'react-flip-move'; 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 [visible, setVisible] = useState(false) const valueKey = props.valueKey || 'visitors' const showConversionRate = !!props.query.filters.goal const fetchData = useCallback(() => { if (props.query.period !== 'realtime') { setState({loading: true, list: null}) } props.fetchData() .then((res) => setState({loading: false, list: res})) }, [props.query]) function onVisible() { setVisible(true) if (props.query.period == 'realtime') { document.addEventListener('tick', fetchData) } } useEffect(() => { if (visible) { fetchData() } }, [props.query, visible]); useEffect(() => { return () => { document.removeEventListener('tick', fetchData) } }, []); function label() { if (props.query.period === 'realtime') { return 'Current visitors' } if (showConversionRate) { return 'Conversions' } return props.valueLabel || 'Visitors' } 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 (