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 (
{props.renderIcon && props.renderIcon(listItem)} {props.renderIcon && ' '} {listItem.name} {numberFormatter(listItem[valueKey])} { listItem.percentage >= 0 ? ({listItem.percentage}%) : null } {showConversionRate && {listItem.conversion_rate}%}
) } function renderList() { if (state.list && state.list.length > 0) { return ( <>
{ props.keyLabel } {label()} {showConversionRate && CR}
{ state.list && state.list.map(renderListItem) } ) } return
No data yet
} return ( { state.loading &&
} { renderList() } {props.detailsLink && !state.loading && }
) }