From 6ee9dc2ea3fe7a29b356a406375df262fff06518 Mon Sep 17 00:00:00 2001 From: Austen Money Date: Wed, 12 Feb 2025 10:00:19 -0500 Subject: [PATCH] memoize entity counts --- .../savedLists/SavedListPanel/hooks.ts | 30 +++++++++++++++---- 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/context/app/static/js/components/savedLists/SavedListPanel/hooks.ts b/context/app/static/js/components/savedLists/SavedListPanel/hooks.ts index b6c492f6d4..0bc1037ea4 100644 --- a/context/app/static/js/components/savedLists/SavedListPanel/hooks.ts +++ b/context/app/static/js/components/savedLists/SavedListPanel/hooks.ts @@ -29,13 +29,31 @@ function useSavedEntityCountsData(savedEntities: Record) { function useSavedEntityTypeCounts(listSavedEntities: Record) { const { searchData } = useSavedEntityCountsData(listSavedEntities); - const counts = { donors: 0, samples: 0, datasets: 0 }; + const counts = useMemo(() => { + if (!searchData?.aggregations?.entity_counts?.buckets) { + return { donors: 0, samples: 0, datasets: 0 }; + } - searchData?.aggregations?.entity_counts?.buckets?.forEach((bucket) => { - if (bucket.key === 'Donor') counts.donors = bucket.doc_count; - else if (bucket.key === 'Sample') counts.samples = bucket.doc_count; - else if (bucket.key === 'Dataset') counts.datasets = bucket.doc_count; - }); + return searchData.aggregations.entity_counts.buckets.reduce( + (acc, { key, doc_count }) => { + switch (key) { + case 'Donor': + acc.donors = doc_count; + break; + case 'Sample': + acc.samples = doc_count; + break; + case 'Dataset': + acc.datasets = doc_count; + break; + default: + break; + } + return acc; + }, + { donors: 0, samples: 0, datasets: 0 }, + ); + }, [searchData]); return counts; }