From 325bbe7c41578edb5a3a19f6dd272cc54ea59be3 Mon Sep 17 00:00:00 2001 From: Robert Joonas Date: Sun, 24 Mar 2024 12:26:53 +0000 Subject: [PATCH] Extract WithImportedSwitch function component --- .../js/dashboard/stats/graph/visitor-graph.js | 41 +----------------- .../stats/graph/with-imported-switch.js | 42 +++++++++++++++++++ 2 files changed, 44 insertions(+), 39 deletions(-) create mode 100644 assets/js/dashboard/stats/graph/with-imported-switch.js diff --git a/assets/js/dashboard/stats/graph/visitor-graph.js b/assets/js/dashboard/stats/graph/visitor-graph.js index 211219b91..5c66be9a6 100644 --- a/assets/js/dashboard/stats/graph/visitor-graph.js +++ b/assets/js/dashboard/stats/graph/visitor-graph.js @@ -1,5 +1,4 @@ import React from 'react'; -import { Link } from 'react-router-dom' import * as api from '../../api' import * as storage from '../../util/storage' import LazyLoader from '../../components/lazy-loader' @@ -7,10 +6,10 @@ import { LoadingState } from './graph-util' import TopStats from './top-stats'; import { IntervalPicker, getCurrentInterval } from './interval-picker' import StatsExport from './stats-export' +import WithImportedSwitch from './with-imported-switch'; import SamplingNotice from './sampling-notice'; import FadeIn from '../../fade-in'; import * as url from '../../util/url' -import { parseNaiveDate, isBefore } from '../../util/date' import { isComparisonEnabled } from '../../comparison-input' import LineGraphWithRouter from './line-graph' @@ -118,42 +117,6 @@ export default class VisitorGraph extends React.Component { storage.setItem(`topStatsHeight__${this.props.site.domain}`, document.getElementById('top-stats-container').clientHeight) } - 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 @@ -186,7 +149,7 @@ export default class VisitorGraph extends React.Component {
{!isRealtime && } - {this.importedNotice()} +
diff --git a/assets/js/dashboard/stats/graph/with-imported-switch.js b/assets/js/dashboard/stats/graph/with-imported-switch.js new file mode 100644 index 000000000..a66a26d40 --- /dev/null +++ b/assets/js/dashboard/stats/graph/with-imported-switch.js @@ -0,0 +1,42 @@ +import React from "react" +import { parseNaiveDate, isBefore } from '../../util/date' +import { Link } from 'react-router-dom' +import * as url from '../../util/url' + +export default function WithImportedSwitch({site, topStatData}) { + if (!topStatData?.imported_source) { + return null + } + + function isBeforeNativeStats(date) { + if (!date) return false + + const nativeStatsBegin = parseNaiveDate(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()} + +
+ + ) + } else { + return null + } +} \ No newline at end of file