import React from 'react'; import { Link } from 'react-router-dom' import FadeIn from '../../fade-in' import numberFormatter from '../../number-formatter' import Bar from '../bar' import * as api from '../../api' export default class Browsers extends React.Component { constructor(props) { super(props) this.state = {loading: true} } componentDidMount() { this.fetchBrowsers() if (this.props.timer) this.props.timer.onTick(this.fetchBrowsers.bind(this)) } componentDidUpdate(prevProps) { if (this.props.query !== prevProps.query) { this.setState({loading: true, browsers: null}) this.fetchBrowsers() } } fetchBrowsers() { if (this.props.query.filters.browser) { api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/browser-versions`, this.props.query) .then((res) => this.setState({loading: false, browsers: res})) } else { api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/browsers`, this.props.query) .then((res) => this.setState({loading: false, browsers: res})) } } renderBrowser(browser) { const query = new URLSearchParams(window.location.search) if (this.props.query.filters.browser) { query.set('browser_version', browser.name) } else { query.set('browser', browser.name) } return (
{browser.name}
{numberFormatter(browser.count)} ({browser.percentage}%)
) } label() { return this.props.query.period === 'realtime' ? 'Current visitors' : 'Visitors' } renderList() { const key = this.props.query.filters.browser ? this.props.query.filters.browser + ' version' : 'Browser' if (this.state.browsers && this.state.browsers.length > 0) { return (
{ key } { this.label() }
{ this.state.browsers && this.state.browsers.map(this.renderBrowser.bind(this)) }
) } else { return
No data yet
} } render() { return ( { this.state.loading &&
} { this.renderList() }
) } }