Remove theme context from the react codebase

pull/1498/head
Uku Taht 5 years ago
parent b695732521
commit 6633d72418

@ -5,7 +5,6 @@ import Historical from './historical'
import Realtime from './realtime'
import {parseQuery} from './query'
import * as api from './api'
import { withThemeProvider } from './theme-provider-hoc';
const THIRTY_SECONDS = 30000
@ -51,4 +50,4 @@ class Dashboard extends React.Component {
}
}
export default withRouter(withThemeProvider(Dashboard))
export default withRouter(Dashboard)

@ -9,7 +9,6 @@ import LazyLoader from '../../lazy-loader'
import MoreLink from '../more-link'
import * as api from '../../api'
import { navigateToQuery } from '../../query'
import { withThemeConsumer } from '../../theme-consumer-hoc';
class Countries extends React.Component {
constructor(props) {
@ -17,7 +16,10 @@ class Countries extends React.Component {
this.resizeMap = this.resizeMap.bind(this)
this.drawMap = this.drawMap.bind(this)
this.getDataset = this.getDataset.bind(this)
this.state = {loading: true}
this.state = {
loading: true,
darkTheme: document.querySelector('html').classList.contains('dark') || false
}
this.onVisible = this.onVisible.bind(this)
}
@ -27,14 +29,6 @@ class Countries extends React.Component {
this.setState({loading: true, countries: null})
this.fetchCountries().then(this.drawMap)
}
if (this.props.darkTheme !== prevProps.darkTheme) {
if (document.getElementById('map-container')) {
document.getElementById('map-container').removeChild(document.querySelector('.datamaps-hoverover'));
document.getElementById('map-container').removeChild(document.querySelector('.datamap'));
}
this.fetchCountries().then(this.drawMap)
}
}
componentWillUnmount() {
@ -57,8 +51,8 @@ class Countries extends React.Component {
const paletteScale = d3.scale.linear()
.domain([0,maxValue])
.range([
this.props.darkTheme ? "#2e3954" : "#f3ebff",
this.props.darkTheme ? "#6366f1" : "#a779e9"
this.state.darkTheme ? "#2e3954" : "#f3ebff",
this.state.darkTheme ? "#6366f1" : "#a779e9"
])
this.state.countries.forEach(function(item){
@ -86,10 +80,10 @@ class Countries extends React.Component {
drawMap() {
const dataset = this.getDataset();
const label = this.props.query.period === 'realtime' ? 'Current visitors' : 'Visitors'
const defaultFill = this.props.darkTheme ? '#2d3747' : '#f8fafc'
const highlightFill = this.props.darkTheme ? '#374151' : '#F5F5F5'
const borderColor = this.props.darkTheme ? '#1f2937' : '#dae1e7'
const highlightBorderColor = this.props.darkTheme ? '#4f46e5' : '#a779e9'
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'
this.map = new Datamap({
element: document.getElementById('map-container'),
@ -161,4 +155,4 @@ class Countries extends React.Component {
}
}
export default withRouter(withThemeConsumer(Countries))
export default withRouter(Countries)

@ -4,7 +4,6 @@ import Chart from 'chart.js/auto';
import { navigateToQuery } from '../query'
import numberFormatter, {durationFormatter} from '../number-formatter'
import * as api from '../api'
import {ThemeContext} from '../theme-context'
import LazyLoader from '../lazy-loader'
function buildDataSet(plot, present_index, ctx, label) {
@ -205,12 +204,6 @@ class LineGraph extends React.Component {
this.chart.update()
}
if (prevProps.darkTheme !== this.props.darkTheme) {
this.chart.destroy();
this.chart = this.regenerateChart();
this.chart.update();
}
}
/**
@ -403,12 +396,10 @@ export default class VisitorGraph extends React.Component {
renderInner() {
if (this.state.graphData) {
const theme = document.querySelector('html').classList.contains('dark') || false
return (
<ThemeContext.Consumer>
{theme => (
<LineGraphWithRouter graphData={this.state.graphData} site={this.props.site} query={this.props.query} darkTheme={theme}/>
)}
</ThemeContext.Consumer>
<LineGraphWithRouter graphData={this.state.graphData} site={this.props.site} query={this.props.query} darkTheme={theme}/>
)
}
}

@ -1,16 +0,0 @@
import React from 'react';
import { ThemeContext } from './theme-context'
export const withThemeConsumer = (WrappedComponent) => {
return class extends React.Component {
render() {
return (
<ThemeContext.Consumer>
{theme => (
<WrappedComponent darkTheme={theme} {...this.props} />
)}
</ThemeContext.Consumer>
);
}
}
}

@ -1,5 +0,0 @@
import React from 'react';
export const ThemeContext = React.createContext({
dark: false
});

@ -1,37 +0,0 @@
import React from 'react';
import { ThemeContext } from './theme-context'
export const withThemeProvider = (WrappedComponent) => {
return class extends React.Component {
constructor(props) {
super(props)
this.state = {
dark: document.querySelector('html').classList.contains('dark') || false
};
this.mutationObserver = new MutationObserver((mutationsList, _observer) => {
mutationsList.forEach(mutation => {
if (mutation.attributeName === 'class') {
this.setState({ dark: mutation.target.classList.contains('dark') });
}
});
});
}
componentDidMount() {
this.mutationObserver.observe(document.querySelector('html'), { attributes: true });
}
componentWillUnmount() {
this.mutationObserver.disconnect();
}
render() {
return (
<ThemeContext.Provider value={this.state.dark}>
<WrappedComponent {...this.props}/>
</ThemeContext.Provider>
);
}
}
}
Loading…
Cancel
Save