import React from 'react'; import { Link } from 'react-router-dom' import * as api from '../api' import { appliedFilters } 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 (appliedFilters(this.props.query).length >= 1) { 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'} ) } return null } }