import React from 'react';
import Bar from './bar'
import MoreLink from './more-link'
import * as api from '../api'
function FadeIn({show, children}) {
const className = show ? "fade-enter-active" : "fade-enter"
return
{children}
}
const EXPLANATION = {
'Mobile': 'up to 576px',
'Tablet': '576px to 992px',
'Laptop': '992px to 1440px',
'Desktop': 'above 1440px',
}
function iconFor(screenSize) {
if (screenSize === 'Mobile') {
return (
)
} else if (screenSize === 'Tablet') {
return (
)
} else if (screenSize === 'Laptop') {
return (
)
} else if (screenSize === 'Desktop') {
return (
)
}
}
class ScreenSizes extends React.Component {
constructor(props) {
super(props)
this.state = {loading: true}
}
componentDidMount() {
this.fetchScreenSizes()
if (this.props.timer) this.props.timer.onTick(this.fetchScreenSizes.bind(this))
}
componentDidUpdate(prevProps) {
if (this.props.query !== prevProps.query) {
this.setState({loading: true, sizes: null})
this.fetchScreenSizes()
}
}
fetchScreenSizes() {
api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/screen-sizes`, this.props.query)
.then((res) => this.setState({loading: false, sizes: res}))
}
renderScreenSize(size) {
return (
{iconFor(size.name)} {size.name}
{size.count} ({size.percentage}%)
)
}
label() {
return this.props.query.period === 'realtime' ? 'Current visitors' : 'Visitors'
}
renderList() {
if (this.state.sizes && this.state.sizes.length > 0) {
return (
Screen size
{ this.label() }
{ this.state.sizes && this.state.sizes.map(this.renderScreenSize.bind(this)) }
)
} else {
return No data yet
}
}
render() {
return (
{ this.state.loading && }
{ this.renderList() }
)
}
}
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() {
api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/browsers`, this.props.query)
.then((res) => this.setState({loading: false, browsers: res}))
}
renderBrowser(browser) {
return (
{browser.name}
{browser.count} ({browser.percentage}%)
)
}
label() {
return this.props.query.period === 'realtime' ? 'Current visitors' : 'Visitors'
}
renderList() {
if (this.state.browsers && this.state.browsers.length > 0) {
return (
Browser
{ 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() }
)
}
}
class OperatingSystems extends React.Component {
constructor(props) {
super(props)
this.state = {loading: true}
}
componentDidMount() {
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() {
api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/operating-systems`, this.props.query)
.then((res) => this.setState({loading: false, operatingSystems: res}))
}
renderOperatingSystem(os) {
return (
{os.name}
{os.count} ({os.percentage}%)
)
}
label() {
return this.props.query.period === 'realtime' ? 'Current visitors' : 'Visitors'
}
renderList() {
if (this.state.operatingSystems && this.state.operatingSystems.length > 0) {
return (
Operating system
{ 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() }
)
}
}
export default class Devices extends React.Component {
constructor(props) {
super(props)
this.state = {mode: 'size'}
}
renderContent() {
if (this.state.mode === 'size') {
return
} else if (this.state.mode === 'browser') {
return
} else if (this.state.mode === 'os') {
return
}
}
setMode(mode) {
return () => {
this.setState({mode})
}
}
renderPill(name, mode) {
const isActive = this.state.mode === mode
const extraClass = name === 'OS' ? '' : ' border-r border-gray-300'
if (isActive) {
return {name}
} else {
return {name}
}
}
render() {
return (
Devices
{ this.renderPill('Size', 'size') }
{ this.renderPill('Browser', 'browser') }
{ this.renderPill('OS', 'os') }
{ this.renderContent() }
)
}
}