From 6f4d4798527b294481a5eb71e0337e1db77ed334 Mon Sep 17 00:00:00 2001 From: RobertJoonas <56999674+RobertJoonas@users.noreply.github.com> Date: Tue, 14 Feb 2023 12:59:25 +0000 Subject: [PATCH] Refactor top stats and graph positioning (#2662) * Position the graph component relatively * lose graph container height flicker (resized canvas) * rename variable * prevent flicker in the loading process * remove unused import * disable cursor-pointer class for minute interval * change var names and loading state conditions * refactor showLoader condition * fix UI bug with interval change set `graphData` to `null` so that the component would fade out when loader becomes visible. Before, the spinner was just displayed on top of the graph. --- assets/css/app.css | 58 --------------- .../js/dashboard/stats/graph/visitor-graph.js | 73 +++++++++++++------ 2 files changed, 51 insertions(+), 80 deletions(-) diff --git a/assets/css/app.css b/assets/css/app.css index 939d8e531..298eaf64a 100644 --- a/assets/css/app.css +++ b/assets/css/app.css @@ -67,64 +67,6 @@ blockquote { @tailwind utilities; -.main-graph { - position: relative; - height: 0; - - /* Formula is: (top row height + (graph height / graph width * 100%) - adjustment for stat selectors) */ - padding-top: calc(168px + (451 / 1088 * 100%) - 20px); -} - -@screen sm { - .main-graph { - padding-top: calc(148px + (451 / 1088 * 100%)); - } -} - -@screen md { - .main-graph { - padding-top: calc(128px + (451 / 1088 * 100%)); - } -} - -@screen lg { - .main-graph { - padding-top: calc(28px + (451 / 1088 * 100%) - 10px); - } -} - -.top-stats-only { - position: relative; - height: 0; - padding-top: calc(158px + 15px); -} - -@screen sm { - .top-stats-only { - padding-top: calc(155px + 15px); - } -} - -@screen md { - .top-stats-only { - padding-top: calc(168px + 15px); - } -} - -@screen lg { - .top-stats-only { - padding-top: calc(75px + 15px); - } -} - -.graph-inner { - position: absolute; - top: 0; - left: 0; - width: 100%; - height: auto; -} - .light-text { color: #F0F4F8; } diff --git a/assets/js/dashboard/stats/graph/visitor-graph.js b/assets/js/dashboard/stats/graph/visitor-graph.js index c445b8852..4c84c9675 100644 --- a/assets/js/dashboard/stats/graph/visitor-graph.js +++ b/assets/js/dashboard/stats/graph/visitor-graph.js @@ -11,7 +11,7 @@ import TopStats from './top-stats'; import { IntervalPicker, getStoredInterval, storeInterval } from './interval-picker'; import FadeIn from '../../fade-in'; import * as url from '../../util/url' -import classNames from "classnames"; +import classNames from 'classnames'; const calculateMaximumY = function(dataset) { const yAxisValues = dataset @@ -61,6 +61,7 @@ class LineGraph extends React.Component { }, }, responsive: true, + maintainAspectRatio: false, onResize: this.updateWindowDimensions, elements: { line: { tension: 0 }, point: { radius: 0 } }, onClick: this.onClick.bind(this), @@ -316,24 +317,39 @@ class LineGraph extends React.Component { } } + // 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 { updateMetric, metric, topStatData, query, site, graphData } = this.props - const extraClass = this.props.graphData && this.props.graphData.interval === 'hour' ? '' : 'cursor-pointer' + const { mainGraphRefreshing, updateMetric, updateInterval, metric, topStatData, query, site, graphData, lastLoadTimestamp } = 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() } - +
- +
+ +
@@ -379,7 +395,7 @@ export default class VisitorGraph extends React.Component { updateInterval(interval) { if (this.isIntervalValid(interval)) { storeInterval(this.props.query.period, this.props.site.domain, interval) - this.setState({ mainGraphLoadingState: LoadingState.refreshing }, this.fetchGraphData) + this.setState({ mainGraphLoadingState: LoadingState.refreshing, graphData: null }, this.fetchGraphData) } } @@ -428,6 +444,10 @@ export default class VisitorGraph extends React.Component { document.removeEventListener('tick', this.fetchTopStatData) } + storeTopStatsContainerHeight() { + storage.setItem(`topStatsHeight__${this.props.site.domain}`, document.getElementById('top-stats-container').clientHeight) + } + updateMetric(clickedMetric) { if (this.state.metric == clickedMetric) return @@ -455,7 +475,10 @@ export default class VisitorGraph extends React.Component { fetchTopStatData() { api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/top-stats`, this.props.query) .then((res) => { - this.setState({ topStatsLoadingState: LoadingState.loaded, topStatData: res }, this.resetMetric) + this.setState({ topStatsLoadingState: LoadingState.loaded, topStatData: res }, () => { + this.storeTopStatsContainerHeight() + this.resetMetric() + }) return res }) } @@ -469,36 +492,42 @@ export default class VisitorGraph extends React.Component { const mainGraphRefreshing = (mainGraphLoadingState === LoadingState.refreshing) const topStatAndGraphLoaded = !!(topStatData && graphData) - const showGraph = - LoadingState.isLoadedOrRefreshing(topStatsLoadingState) && + const shouldShow = + topStatsLoadingState === LoadingState.loaded && LoadingState.isLoadedOrRefreshing(mainGraphLoadingState) && (topStatData && mainGraphRefreshing || topStatAndGraphLoaded) return ( - - + + ) } render() { const {mainGraphLoadingState, topStatsLoadingState} = this.state - const loaderClassName = classNames('mx-auto loading', { - 'pt-52 sm:pt-56 md:pt-60': mainGraphLoadingState == LoadingState.refreshing, - 'pt-32 sm:pt-36 md:pt-48': mainGraphLoadingState !== LoadingState.refreshing, - }) const showLoader = - LoadingState.isLoadingOrRefreshing(mainGraphLoadingState) || - LoadingState.isLoadingOrRefreshing(topStatsLoadingState) + [mainGraphLoadingState, topStatsLoadingState].includes(LoadingState.loading) && + mainGraphLoadingState !== LoadingState.refreshing return ( -
- {showLoader &&
} +
+ {showLoader && renderLoader()} {this.renderInner()}
) } } + +function renderLoader() { + return ( +
+
+
+
+
+ ) +}