2019-11-19 07:30:42 +03:00
|
|
|
import React from 'react';
|
2020-07-14 16:52:26 +03:00
|
|
|
import { Link } from 'react-router-dom'
|
2021-07-22 08:53:13 +03:00
|
|
|
import * as api from '../api'
|
2021-12-03 14:59:32 +03:00
|
|
|
import * as url from '../util/url'
|
2021-07-21 16:50:26 +03:00
|
|
|
import { appliedFilters } from '../query';
|
2019-11-19 07:30:42 +03:00
|
|
|
|
|
|
|
export default class CurrentVisitors extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props)
|
|
|
|
this.state = {currentVisitors: null}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2020-07-14 16:52:26 +03:00
|
|
|
this.updateCount()
|
|
|
|
this.props.timer.onTick(this.updateCount.bind(this))
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
updateCount() {
|
2021-04-07 11:06:33 +03:00
|
|
|
return api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/current-visitors`)
|
2019-11-19 07:30:42 +03:00
|
|
|
.then((res) => this.setState({currentVisitors: res}))
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2021-07-22 08:53:13 +03:00
|
|
|
if (appliedFilters(this.props.query).length >= 1) { return null }
|
2020-07-29 10:07:18 +03:00
|
|
|
const { currentVisitors } = this.state;
|
2021-09-21 11:44:51 +03:00
|
|
|
|
2020-07-29 10:07:18 +03:00
|
|
|
if (currentVisitors !== null) {
|
2019-11-19 07:30:42 +03:00
|
|
|
return (
|
2021-09-21 11:44:51 +03:00
|
|
|
<Link to={url.setQuery('period', 'realtime')} className="block ml-1 md:ml-2 mr-auto text-xs md:text-sm font-bold text-gray-500 dark:text-gray-300">
|
2021-07-21 16:50:26 +03:00
|
|
|
<svg className="inline w-2 mr-1 md:mr-2 text-green-500 fill-current" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg">
|
2021-07-22 08:53:13 +03:00
|
|
|
<circle cx="8" cy="8" r="8" />
|
2019-11-19 07:30:42 +03:00
|
|
|
</svg>
|
2021-07-22 10:37:37 +03:00
|
|
|
{currentVisitors} <span className="hidden sm:inline-block">current visitor{currentVisitors === 1 ? '' : 's'}</span>
|
2020-07-14 16:52:26 +03:00
|
|
|
</Link>
|
2019-11-19 07:30:42 +03:00
|
|
|
)
|
|
|
|
}
|
2021-07-22 08:53:13 +03:00
|
|
|
|
|
|
|
return null
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
|
|
|
}
|