From ce5fe91dcc720533f0b4fbdbf66bd1ef95b3638d Mon Sep 17 00:00:00 2001 From: Robert Joonas Date: Sun, 24 Mar 2024 11:34:42 +0000 Subject: [PATCH] Extract a StatsExport function component Again, instead of relying on `graphData?.interval` we can read it from localStorage, or default to the largest interval available. The export should not be dependant on the graph. --- .../js/dashboard/stats/graph/stats-export.js | 53 +++++++++++++++++++ .../js/dashboard/stats/graph/visitor-graph.js | 47 ++-------------- 2 files changed, 57 insertions(+), 43 deletions(-) create mode 100644 assets/js/dashboard/stats/graph/stats-export.js diff --git a/assets/js/dashboard/stats/graph/stats-export.js b/assets/js/dashboard/stats/graph/stats-export.js new file mode 100644 index 000000000..6716b2eb1 --- /dev/null +++ b/assets/js/dashboard/stats/graph/stats-export.js @@ -0,0 +1,53 @@ +import React, { useState } from "react" +import * as api from '../../api' +import { getCurrentInterval } from "./interval-picker" + +export default function StatsExport({site, query}) { + const [exporting, setExporting] = useState(false) + + function startExport() { + setExporting(true) + document.cookie = "exporting=" + pollExportReady() + } + + function pollExportReady() { + if (document.cookie.includes('exporting')) { + setTimeout(pollExportReady, 1000) + } else { + setExporting(false) + } + } + + function renderLoading() { + return ( + + + + + ) + } + + function renderExportLink() { + const interval = getCurrentInterval(site, query) + const queryParams = api.serializeQuery(query, [{ interval }]) + const endpoint = `/${encodeURIComponent(site.domain)}/export${queryParams}` + + return ( + + + + + + + + ) + } + + return ( +
+ {exporting && renderLoading()} + {!exporting && renderExportLink()} +
+ ) +} \ No newline at end of file diff --git a/assets/js/dashboard/stats/graph/visitor-graph.js b/assets/js/dashboard/stats/graph/visitor-graph.js index f707efcbe..b4dab7317 100644 --- a/assets/js/dashboard/stats/graph/visitor-graph.js +++ b/assets/js/dashboard/stats/graph/visitor-graph.js @@ -5,7 +5,8 @@ import * as storage from '../../util/storage' import LazyLoader from '../../components/lazy-loader' import { LoadingState } from './graph-util' import TopStats from './top-stats'; -import { IntervalPicker, getCurrentInterval } from './interval-picker'; +import { IntervalPicker, getCurrentInterval } from './interval-picker' +import StatsExport from './stats-export' import FadeIn from '../../fade-in'; import * as url from '../../util/url' import { parseNaiveDate, isBefore } from '../../util/date' @@ -34,7 +35,6 @@ export default class VisitorGraph extends React.Component { loading: LoadingState.loading, topStatData: null, graphData: null, - exported: false } this.onVisible = this.onVisible.bind(this) this.fetchTopStatsAndGraphData = this.fetchTopStatsAndGraphData.bind(this) @@ -116,46 +116,6 @@ export default class VisitorGraph extends React.Component { storeTopStatsContainerHeight() { storage.setItem(`topStatsHeight__${this.props.site.domain}`, document.getElementById('top-stats-container').clientHeight) } - - 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 @@ -224,6 +184,7 @@ export default class VisitorGraph extends React.Component { const { graphData, topStatData, loading } = this.state; const isDarkTheme = document.querySelector('html').classList.contains('dark') || false + const isRealtime = query.period === 'realtime' return ( @@ -236,7 +197,7 @@ export default class VisitorGraph extends React.Component {
{loading === LoadingState.updatingGraph && renderLoader()}
- {this.downloadLink()} + {!isRealtime && } {this.samplingNotice()} {this.importedNotice()}