Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

Remove theme context from the react codebase

Uku Taht6633d724182c
diff --git a/assets/js/dashboard/index.js b/assets/js/dashboard/index.js
index 42fa42d197..54c1fc0a84 100644
--- a/assets/js/dashboard/index.js
+++ b/assets/js/dashboard/index.js
@@ -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)
diff --git a/assets/js/dashboard/stats/locations/map.js b/assets/js/dashboard/stats/locations/map.js
index 3a5c3545ee..8be7dba6c1 100644
--- a/assets/js/dashboard/stats/locations/map.js
+++ b/assets/js/dashboard/stats/locations/map.js
@@ -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)
diff --git a/assets/js/dashboard/stats/visitor-graph.js b/assets/js/dashboard/stats/visitor-graph.js
index 23c882e0f3..24090042cb 100644
--- a/assets/js/dashboard/stats/visitor-graph.js
+++ b/assets/js/dashboard/stats/visitor-graph.js
@@ -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}/>
)
}
}
diff --git a/assets/js/dashboard/theme-consumer-hoc.js b/assets/js/dashboard/theme-consumer-hoc.js
deleted file mode 100644
index a52755c514..0000000000
--- a/assets/js/dashboard/theme-consumer-hoc.js
+++ /dev/null
@@ -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>
- );
- }
- }
-}
diff --git a/assets/js/dashboard/theme-context.js b/assets/js/dashboard/theme-context.js
deleted file mode 100644
index e7f9319d43..0000000000
--- a/assets/js/dashboard/theme-context.js
+++ /dev/null
@@ -1,5 +0,0 @@
-import React from 'react';
-
-export const ThemeContext = React.createContext({
- dark: false
-});
diff --git a/assets/js/dashboard/theme-provider-hoc.js b/assets/js/dashboard/theme-provider-hoc.js
deleted file mode 100644
index 0360d0ff2a..0000000000
--- a/assets/js/dashboard/theme-provider-hoc.js
+++ /dev/null
@@ -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>
- );
- }
- }
-}