import React from 'react'; import * as api from '../api' import { Link } from 'react-router-dom' import { countFilters } from '../query'; export default class CurrentVisitors extends React.Component { constructor(props) { super(props) this.state = {currentVisitors: null} } componentDidMount() { this.updateCount() this.props.timer.onTick(this.updateCount.bind(this)) } updateCount() { return api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/current-visitors`) .then((res) => this.setState({currentVisitors: res})) } render() { if (countFilters(this.props.query) !== 0) { return null } const query = new URLSearchParams(window.location.search) query.set('period', 'realtime') const { currentVisitors } = this.state; if (currentVisitors !== null) { return ( {currentVisitors} current visitor{currentVisitors === 1 ? '' : 's'} ) } else { return null } } }