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' import LazyLoader from '../../lazy-loader' export default class OperatingSystems extends React.Component { constructor(props) { super(props) this.state = {loading: true} this.onVisible = this.onVisible.bind(this) } onVisible() { this.fetchOperatingSystems() if (this.props.timer) this.props.timer.onTick(this.fetchOperatingSystems.bind(this)) } componentDidUpdate(prevProps) { if (this.props.query !== prevProps.query) { this.setState({loading: true, operatingSystems: null}) this.fetchOperatingSystems() } } fetchOperatingSystems() { if (this.props.query.filters.os) { api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/operating-system-versions`, this.props.query) .then((res) => this.setState({loading: false, operatingSystems: res})) } else { api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/operating-systems`, this.props.query) .then((res) => this.setState({loading: false, operatingSystems: res})) } } renderOperatingSystem(os) { const query = new URLSearchParams(window.location.search) if (this.props.query.filters.os) { query.set('os_version', os.name) } else { query.set('os', os.name) } return (
{os.name}
{numberFormatter(os.count)} ({os.percentage}%)
) } label() { return this.props.query.period === 'realtime' ? 'Current visitors' : 'Visitors' } renderList() { const key = this.props.query.filters.os ? this.props.query.filters.os + ' version' : 'Operating system' if (this.state.operatingSystems && this.state.operatingSystems.length > 0) { return (
{ key } { this.label() }
{ this.state.operatingSystems && this.state.operatingSystems.map(this.renderOperatingSystem.bind(this)) }
) } else { return
No data yet
} } render() { return ( { this.state.loading &&
} { this.renderList() }
) } }