import React from 'react'; import { Link } from 'react-router-dom' import FlipMove from 'react-flip-move'; import FadeIn from '../../fade-in' import Bar from '../bar' import MoreLink from '../more-link' import numberFormatter from '../../number-formatter' import { eventName } from '../../query' import * as api from '../../api' import LazyLoader from '../../lazy-loader' export default class Visits extends React.Component { constructor(props) { super(props) this.state = {loading: true} this.onVisible = this.onVisible.bind(this) } onVisible() { this.fetchPages() if (this.props.timer) this.props.timer.onTick(this.fetchPages.bind(this)) } componentDidUpdate(prevProps) { if (this.props.query !== prevProps.query) { this.setState({loading: true, pages: null}) this.fetchPages() } } fetchPages() { api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/pages`, this.props.query) .then((res) => this.setState({loading: false, pages: res})) } renderPage(page) { const query = new URLSearchParams(window.location.search) query.set('page', page.name) const domain = new URL('https://' + this.props.site.domain) const externalLink = 'https://' + domain.host + page.name return (
{page.name}
{numberFormatter(page.count)}
) } label() { const filters = this.props.query.filters if (this.props.query.period === 'realtime') { return 'Current visitors' } else { return 'Visitors' } } renderList() { if (this.state.pages && this.state.pages.length > 0) { return (
Page url { this.label() }
{ this.state.pages.map(this.renderPage.bind(this)) }
) } else { return
No data yet
} } render() { const { loading } = this.state; return ( { loading &&
} { this.renderList() } {!loading && }
) } }