From b6ab9a1bdd74a645e69437f192ad4170e1fccd7a Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 31 May 2020 08:55:52 +0200 Subject: [PATCH] Improved memoization of grouped visits for line chart --- src/visits/helpers/LineChartCard.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/visits/helpers/LineChartCard.js b/src/visits/helpers/LineChartCard.js index 2b6664d5..1b78feb1 100644 --- a/src/visits/helpers/LineChartCard.js +++ b/src/visits/helpers/LineChartCard.js @@ -72,16 +72,14 @@ const generateLabels = (step, visits) => { ]; }; -const generateLabelsAndGroupedVisits = (visits, step, skipNoElements) => { - const groupedVisits = groupVisitsByStep(step, reverse(visits)); - +const generateLabelsAndGroupedVisits = (visits, groupedVisitsWithGaps, step, skipNoElements) => { if (skipNoElements) { - return [ Object.keys(groupedVisits), groupedVisits ]; + return [ Object.keys(groupedVisitsWithGaps), groupedVisitsWithGaps ]; } const labels = generateLabels(step, visits); - return [ labels, fillTheGaps(groupedVisits, labels) ]; + return [ labels, fillTheGaps(groupedVisitsWithGaps, labels) ]; }; const generateDataset = (stats, label, color) => ({ @@ -97,8 +95,9 @@ const LineChartCard = ({ title, visits, highlightedVisits }) => { const [ step, setStep ] = useState('monthly'); const [ skipNoVisits, toggleSkipNoVisits ] = useToggle(true); + const groupedVisitsWithGaps = useMemo(() => groupVisitsByStep(step, reverse(visits)), [ step, visits ]); const [ labels, groupedVisits ] = useMemo( - () => generateLabelsAndGroupedVisits(visits, step, skipNoVisits), + () => generateLabelsAndGroupedVisits(visits, groupedVisitsWithGaps, step, skipNoVisits), [ visits, step, skipNoVisits ] ); const groupedHighlighted = useMemo(