From 93f9fe048170ad45a4af1807d7f2b5f67f83c029 Mon Sep 17 00:00:00 2001 From: Robert Joonas Date: Fri, 22 Mar 2024 12:13:26 +0000 Subject: [PATCH] Make the LineGraph component thinner --- .../js/dashboard/stats/graph/visitor-graph.js | 250 +++++++++--------- 1 file changed, 124 insertions(+), 126 deletions(-) diff --git a/assets/js/dashboard/stats/graph/visitor-graph.js b/assets/js/dashboard/stats/graph/visitor-graph.js index fa02ce647..bb61096be 100644 --- a/assets/js/dashboard/stats/graph/visitor-graph.js +++ b/assets/js/dashboard/stats/graph/visitor-graph.js @@ -31,12 +31,8 @@ const calculateMaximumY = function(dataset) { class LineGraph extends React.Component { constructor(props) { super(props); - this.boundary = React.createRef() this.regenerateChart = this.regenerateChart.bind(this); this.updateWindowDimensions = this.updateWindowDimensions.bind(this); - this.state = { - exported: false - }; } regenerateChart() { @@ -231,130 +227,16 @@ class LineGraph extends React.Component { } } - pollExportReady() { - if (document.cookie.includes('exporting')) { - setTimeout(this.pollExportReady.bind(this), 1000); - } else { - this.setState({ exported: false }) - } - } - - downloadSpinner() { - this.setState({ exported: true }); - document.cookie = "exporting="; - setTimeout(this.pollExportReady.bind(this), 1000); - } - - downloadLink() { - if (this.props.query.period !== 'realtime') { - - if (this.state.exported) { - return ( -
- - - - -
- ) - } else { - const interval = this.props.graphData?.interval - const queryParams = api.serializeQuery(this.props.query, [{ interval }]) - const endpoint = `/${encodeURIComponent(this.props.site.domain)}/export${queryParams}` - - return ( - - - - ) - } - } - } - - samplingNotice() { - const samplePercent = this.props.topStatData && this.props.topStatData.sample_percent - - if (samplePercent < 100) { - return ( -
- - - -
- ) - } - } - - importedNotice() { - if (!this.props.topStatData?.imported_source) return - - const isBeforeNativeStats = (date) => { - if (!date) return false - - const nativeStatsBegin = parseNaiveDate(this.props.site.nativeStatsBegin) - const parsedDate = parseNaiveDate(date) - - return isBefore(parsedDate, nativeStatsBegin, "day") - } - - const isQueryingImportedPeriod = isBeforeNativeStats(this.props.topStatData.from) - const isComparingImportedPeriod = isBeforeNativeStats(this.props.topStatData.comparing_from) - - if (isQueryingImportedPeriod || isComparingImportedPeriod) { - const source = this.props.topStatData.imported_source - const withImported = this.props.topStatData.with_imported; - const strike = withImported ? "" : " line-through" - const target = url.setQuery('with_imported', !withImported) - const tip = withImported ? "" : "do not "; - - return ( - -
- - {source[0].toUpperCase()} - -
- - ) - } - } - - // This function is used for maintaining the main-graph/top-stats container height in the - // loading process. The container height depends on how many top stat metrics are returned - // from the API, but in the loading state, we don't know that yet. We can use localStorage - // to keep track of the Top Stats container height. - getTopStatsHeight() { - if (this.props.topStatData) { - return 'auto' - } else { - return `${storage.getItem(`topStatsHeight__${this.props.site.domain}`) || 89}px` - } - } - render() { - const { mainGraphRefreshing, updateMetric, updateInterval, metric, topStatData, query, site, graphData, lastLoadTimestamp } = this.props + const { graphData } = this.props const canvasClass = classNames('mt-4 select-none', { 'cursor-pointer': !['minute', 'hour'].includes(graphData?.interval) }) return ( -
-
- + +
+
-
- {mainGraphRefreshing && renderLoader()} -
- {this.downloadLink()} - {this.samplingNotice()} - {this.importedNotice()} - -
- -
- -
-
-
-
+ ) } } @@ -367,13 +249,15 @@ export default class VisitorGraph extends React.Component { this.state = { topStatsLoadingState: LoadingState.loading, mainGraphLoadingState: LoadingState.loading, - metric: storage.getItem(`metric__${this.props.site.domain}`) || 'visitors' + metric: storage.getItem(`metric__${this.props.site.domain}`) || 'visitors', + exported: false } this.onVisible = this.onVisible.bind(this) this.updateMetric = this.updateMetric.bind(this) this.fetchTopStatData = this.fetchTopStatData.bind(this) this.fetchGraphData = this.fetchGraphData.bind(this) this.updateInterval = this.updateInterval.bind(this) + this.boundary = React.createRef() } isIntervalValid(interval) { @@ -491,12 +375,114 @@ export default class VisitorGraph extends React.Component { return res }) } + + pollExportReady() { + if (document.cookie.includes('exporting')) { + setTimeout(this.pollExportReady.bind(this), 1000); + } else { + this.setState({ exported: false }) + } + } + + downloadSpinner() { + this.setState({ exported: true }); + document.cookie = "exporting="; + setTimeout(this.pollExportReady.bind(this), 1000); + } + + downloadLink() { + if (this.props.query.period !== 'realtime') { + + if (this.state.exported) { + return ( +
+ + + + +
+ ) + } else { + const interval = this.state.graphData?.interval + const queryParams = api.serializeQuery(this.props.query, [{ interval }]) + const endpoint = `/${encodeURIComponent(this.props.site.domain)}/export${queryParams}` + + return ( + + + + ) + } + } + } + + samplingNotice() { + const samplePercent = this.state.topStatData?.sample_percent + + if (samplePercent && samplePercent < 100) { + return ( +
+ + + +
+ ) + } + } + + importedNotice() { + const { topStatData } = this.state + + if (!topStatData?.imported_source) return + + const isBeforeNativeStats = (date) => { + if (!date) return false + + const nativeStatsBegin = parseNaiveDate(this.props.site.nativeStatsBegin) + const parsedDate = parseNaiveDate(date) + + return isBefore(parsedDate, nativeStatsBegin, "day") + } + + const isQueryingImportedPeriod = isBeforeNativeStats(topStatData.from) + const isComparingImportedPeriod = isBeforeNativeStats(topStatData.comparing_from) + + if (isQueryingImportedPeriod || isComparingImportedPeriod) { + const source = topStatData.imported_source + const withImported = topStatData.with_imported; + const strike = withImported ? "" : " line-through" + const target = url.setQuery('with_imported', !withImported) + const tip = withImported ? "" : "do not "; + + return ( + +
+ + {source[0].toUpperCase()} + +
+ + ) + } + } + + // This function is used for maintaining the main-graph/top-stats container height in the + // loading process. The container height depends on how many top stat metrics are returned + // from the API, but in the loading state, we don't know that yet. We can use localStorage + // to keep track of the Top Stats container height. + getTopStatsHeight() { + if (this.state.topStatData) { + return 'auto' + } else { + return `${storage.getItem(`topStatsHeight__${this.props.site.domain}`) || 89}px` + } + } renderInner() { const { query, site } = this.props; const { graphData, metric, topStatData, topStatsLoadingState, mainGraphLoadingState } = this.state; - const theme = document.querySelector('html').classList.contains('dark') || false + const isDarkTheme = document.querySelector('html').classList.contains('dark') || false const mainGraphRefreshing = (mainGraphLoadingState === LoadingState.refreshing) const topStatAndGraphLoaded = !!(topStatData && graphData) @@ -508,7 +494,19 @@ export default class VisitorGraph extends React.Component { return ( - +
+ +
+
+ {mainGraphRefreshing && renderLoader()} +
+ {this.downloadLink()} + {this.samplingNotice()} + {this.importedNotice()} + +
+ +
) }