2019-11-19 07:30:42 +03:00
|
|
|
import React from 'react';
|
2020-02-10 16:17:00 +03:00
|
|
|
import Datamap from 'datamaps'
|
2020-10-13 12:03:42 +03:00
|
|
|
import { withRouter } from 'react-router-dom'
|
2021-10-11 15:48:19 +03:00
|
|
|
import * as d3 from "d3"
|
2019-11-19 07:30:42 +03:00
|
|
|
|
2021-12-03 14:59:32 +03:00
|
|
|
import numberFormatter from '../../util/number-formatter'
|
2021-10-14 11:55:43 +03:00
|
|
|
import FadeIn from '../../fade-in'
|
2021-12-03 14:59:32 +03:00
|
|
|
import LazyLoader from '../../components/lazy-loader'
|
2021-10-14 11:55:43 +03:00
|
|
|
import MoreLink from '../more-link'
|
|
|
|
import * as api from '../../api'
|
|
|
|
import { navigateToQuery } from '../../query'
|
|
|
|
|
2021-11-23 12:39:09 +03:00
|
|
|
class Countries extends React.Component {
|
2019-11-19 07:30:42 +03:00
|
|
|
constructor(props) {
|
|
|
|
super(props)
|
2020-02-10 16:17:00 +03:00
|
|
|
this.resizeMap = this.resizeMap.bind(this)
|
2020-12-16 12:57:28 +03:00
|
|
|
this.drawMap = this.drawMap.bind(this)
|
|
|
|
this.getDataset = this.getDataset.bind(this)
|
2021-11-26 17:27:34 +03:00
|
|
|
this.state = {
|
|
|
|
loading: true,
|
|
|
|
darkTheme: document.querySelector('html').classList.contains('dark') || false
|
|
|
|
}
|
2021-03-25 12:55:15 +03:00
|
|
|
this.onVisible = this.onVisible.bind(this)
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate(prevProps) {
|
|
|
|
if (this.props.query !== prevProps.query) {
|
2021-11-23 12:39:09 +03:00
|
|
|
// eslint-disable-next-line react/no-did-update-set-state
|
2019-11-19 07:30:42 +03:00
|
|
|
this.setState({loading: true, countries: null})
|
2021-06-14 16:44:46 +03:00
|
|
|
this.fetchCountries().then(this.drawMap)
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-23 12:39:09 +03:00
|
|
|
componentWillUnmount() {
|
|
|
|
window.removeEventListener('resize', this.resizeMap);
|
|
|
|
}
|
|
|
|
|
|
|
|
onVisible() {
|
|
|
|
this.fetchCountries().then(this.drawMap.bind(this))
|
|
|
|
window.addEventListener('resize', this.resizeMap);
|
|
|
|
if (this.props.timer) this.props.timer.onTick(this.updateCountries.bind(this))
|
|
|
|
}
|
|
|
|
|
2020-07-14 16:52:26 +03:00
|
|
|
getDataset() {
|
2021-11-23 12:39:09 +03:00
|
|
|
const dataset = {};
|
2020-02-10 16:17:00 +03:00
|
|
|
|
2021-11-04 15:20:39 +03:00
|
|
|
var onlyValues = this.state.countries.map(function(obj){ return obj.visitors });
|
2020-10-19 13:46:08 +03:00
|
|
|
var maxValue = Math.max.apply(null, onlyValues);
|
2020-02-10 16:17:00 +03:00
|
|
|
|
2021-11-23 12:39:09 +03:00
|
|
|
// eslint-disable-next-line no-undef
|
|
|
|
const paletteScale = d3.scale.linear()
|
2020-10-19 13:46:08 +03:00
|
|
|
.domain([0,maxValue])
|
2020-12-16 12:57:28 +03:00
|
|
|
.range([
|
2021-11-26 17:27:34 +03:00
|
|
|
this.state.darkTheme ? "#2e3954" : "#f3ebff",
|
|
|
|
this.state.darkTheme ? "#6366f1" : "#a779e9"
|
2021-11-23 12:39:09 +03:00
|
|
|
])
|
2020-02-10 16:17:00 +03:00
|
|
|
|
|
|
|
this.state.countries.forEach(function(item){
|
2021-12-13 13:03:27 +03:00
|
|
|
dataset[item.alpha_3] = {numberOfThings: item.visitors, fillColor: paletteScale(item.visitors)};
|
2020-02-10 16:17:00 +03:00
|
|
|
});
|
|
|
|
|
2020-07-14 16:52:26 +03:00
|
|
|
return dataset
|
|
|
|
}
|
|
|
|
|
|
|
|
updateCountries() {
|
|
|
|
this.fetchCountries().then(() => {
|
|
|
|
this.map.updateChoropleth(this.getDataset(), {reset: true})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fetchCountries() {
|
2021-11-30 13:51:58 +03:00
|
|
|
return api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/countries`, this.props.query, {limit: 300})
|
2020-07-14 16:52:26 +03:00
|
|
|
.then((res) => this.setState({loading: false, countries: res}))
|
|
|
|
}
|
|
|
|
|
|
|
|
resizeMap() {
|
|
|
|
this.map && this.map.resize()
|
|
|
|
}
|
|
|
|
|
|
|
|
drawMap() {
|
2021-11-23 12:39:09 +03:00
|
|
|
const dataset = this.getDataset();
|
2020-08-18 14:14:56 +03:00
|
|
|
const label = this.props.query.period === 'realtime' ? 'Current visitors' : 'Visitors'
|
2021-11-26 17:27:34 +03:00
|
|
|
const defaultFill = this.state.darkTheme ? '#2d3747' : '#f8fafc'
|
|
|
|
const highlightFill = this.state.darkTheme ? '#374151' : '#F5F5F5'
|
|
|
|
const borderColor = this.state.darkTheme ? '#1f2937' : '#dae1e7'
|
|
|
|
const highlightBorderColor = this.state.darkTheme ? '#4f46e5' : '#a779e9'
|
2020-07-14 16:52:26 +03:00
|
|
|
|
2020-02-10 16:17:00 +03:00
|
|
|
this.map = new Datamap({
|
|
|
|
element: document.getElementById('map-container'),
|
|
|
|
responsive: true,
|
|
|
|
projection: 'mercator',
|
2020-12-16 12:57:28 +03:00
|
|
|
fills: { defaultFill },
|
2020-02-10 16:17:00 +03:00
|
|
|
data: dataset,
|
|
|
|
geographyConfig: {
|
2020-12-16 12:57:28 +03:00
|
|
|
borderColor,
|
2020-02-10 16:17:00 +03:00
|
|
|
highlightBorderWidth: 2,
|
2021-11-23 12:39:09 +03:00
|
|
|
highlightFillColor: (geo) => geo.fillColor || highlightFill,
|
2020-12-16 12:57:28 +03:00
|
|
|
highlightBorderColor,
|
2021-11-23 12:39:09 +03:00
|
|
|
popupTemplate: (geo, data) => {
|
|
|
|
if (!data) { return null; }
|
2020-07-30 13:44:43 +03:00
|
|
|
const pluralizedLabel = data.numberOfThings === 1 ? label.slice(0, -1) : label
|
2020-12-16 12:57:28 +03:00
|
|
|
return ['<div class="hoverinfo dark:bg-gray-800 dark:shadow-gray-850 dark:border-gray-850 dark:text-gray-200">',
|
2021-11-30 13:51:58 +03:00
|
|
|
'<strong>', geo.properties.name, ' </strong>',
|
|
|
|
'<br><strong class="dark:text-indigo-400">', numberFormatter(data.numberOfThings), '</strong> ', pluralizedLabel,
|
2020-02-10 16:17:00 +03:00
|
|
|
'</div>'].join('');
|
|
|
|
}
|
2020-10-13 12:03:42 +03:00
|
|
|
},
|
|
|
|
done: (datamap) => {
|
|
|
|
datamap.svg.selectAll('.datamaps-subunit').on('click', (geography) => {
|
2021-12-13 13:03:27 +03:00
|
|
|
const country = this.state.countries.find(c => c.alpha_3 === geography.id)
|
|
|
|
|
2021-12-14 12:54:36 +03:00
|
|
|
if (country) {
|
|
|
|
this.props.onClick()
|
|
|
|
|
|
|
|
navigateToQuery(
|
|
|
|
this.props.history,
|
|
|
|
this.props.query,
|
|
|
|
{
|
|
|
|
country: country.code,
|
|
|
|
country_name: country.name
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-10-13 12:03:42 +03:00
|
|
|
})
|
2020-02-10 16:17:00 +03:00
|
|
|
}
|
|
|
|
});
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
|
|
|
|
2021-04-12 12:21:07 +03:00
|
|
|
geolocationDbNotice() {
|
|
|
|
if (this.props.site.selfhosted) {
|
|
|
|
return (
|
2021-10-11 15:48:19 +03:00
|
|
|
<span className="text-xs text-gray-500 absolute bottom-4 right-3">IP Geolocation by <a target="_blank" href="https://db-ip.com" rel="noreferrer" className="text-indigo-600">DB-IP</a></span>
|
2021-04-12 12:21:07 +03:00
|
|
|
)
|
|
|
|
}
|
2021-11-23 12:39:09 +03:00
|
|
|
|
|
|
|
return null
|
2021-04-12 12:21:07 +03:00
|
|
|
}
|
|
|
|
|
2020-03-03 12:13:08 +03:00
|
|
|
renderBody() {
|
|
|
|
if (this.state.countries) {
|
2019-11-19 07:30:42 +03:00
|
|
|
return (
|
2021-11-23 12:39:09 +03:00
|
|
|
<>
|
2021-10-14 11:55:43 +03:00
|
|
|
<div className="mx-auto mt-4" style={{width: '100%', maxWidth: '475px', height: '335px'}} id="map-container"></div>
|
2020-02-10 16:17:00 +03:00
|
|
|
<MoreLink site={this.props.site} list={this.state.countries} endpoint="countries" />
|
2021-04-12 12:21:07 +03:00
|
|
|
{ this.geolocationDbNotice() }
|
2021-11-23 12:39:09 +03:00
|
|
|
</>
|
2019-11-19 07:30:42 +03:00
|
|
|
)
|
|
|
|
}
|
2021-11-23 12:39:09 +03:00
|
|
|
|
|
|
|
return null
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
2020-03-03 12:13:08 +03:00
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
2021-10-14 11:55:43 +03:00
|
|
|
<LazyLoader onVisible={this.onVisible}>
|
|
|
|
{ this.state.loading && <div className="mx-auto my-32 loading"><div></div></div> }
|
|
|
|
<FadeIn show={!this.state.loading}>
|
|
|
|
{ this.renderBody() }
|
|
|
|
</FadeIn>
|
|
|
|
</LazyLoader>
|
2020-03-03 12:13:08 +03:00
|
|
|
)
|
|
|
|
}
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
2020-10-13 12:03:42 +03:00
|
|
|
|
2021-11-26 17:27:34 +03:00
|
|
|
export default withRouter(Countries)
|