From deff6525528b047450eda9ed08047b6fc162a9b4 Mon Sep 17 00:00:00 2001 From: Bob MacCallum Date: Tue, 19 Nov 2024 12:04:45 +0000 Subject: [PATCH] fix multifilter when there is no filter applied to the variable --- .../core/components/filter/MultiFilter.tsx | 145 +++++++++--------- 1 file changed, 71 insertions(+), 74 deletions(-) diff --git a/packages/libs/eda/src/lib/core/components/filter/MultiFilter.tsx b/packages/libs/eda/src/lib/core/components/filter/MultiFilter.tsx index 64e2192ce3..71d42dde44 100644 --- a/packages/libs/eda/src/lib/core/components/filter/MultiFilter.tsx +++ b/packages/libs/eda/src/lib/core/components/filter/MultiFilter.tsx @@ -193,81 +193,78 @@ export function MultiFilter(props: Props) { }, [_thisFilter, thisFilter]); // Counts retrieved from the backend, used for the table display. - const leafSummariesPromise = useCachedPromise( - () => { - return Promise.all( - leaves.map((leaf) => { - const thisFilterWithoutLeaf = thisFilter && { - ...thisFilter, - subFilters: thisFilter.subFilters.filter( - (f) => f.variableId !== leaf.term - ), - }; - return getDistribution( - { - entityId: entity.id, - variableId: leaf.term, - filters: - thisFilterWithoutLeaf == null || - thisFilterWithoutLeaf.subFilters.length === 0 || - thisFilterWithoutLeaf.operation === 'union' - ? otherFilters - : [...(otherFilters || []), thisFilterWithoutLeaf], - }, - (filters) => - subsettingClient.getDistribution( - studyMetadata.id, - entity.id, - leaf.term, - { - filters, - valueSpec: 'count', - } - ) - ).then((distribution) => { - const fgValueByLabel = Object.fromEntries( - distribution.foreground.histogram.map(({ binLabel, value }) => [ - binLabel, - value ?? 0, - ]) - ); - const bgValueByLabel = Object.fromEntries( - distribution.background.histogram.map(({ binLabel, value }) => [ - binLabel, - value ?? 0, - ]) + const leafSummariesPromise = useCachedPromise(() => { + return Promise.all( + leaves.map((leaf) => { + const thisFilterWithoutLeaf = thisFilter && { + ...thisFilter, + subFilters: thisFilter.subFilters.filter( + (f) => f.variableId !== leaf.term + ), + }; + return getDistribution( + { + entityId: entity.id, + variableId: leaf.term, + filters: + thisFilterWithoutLeaf == null || + thisFilterWithoutLeaf.subFilters.length === 0 || + thisFilterWithoutLeaf.operation === 'union' + ? otherFilters + : [...(otherFilters || []), thisFilterWithoutLeaf], + }, + (filters) => + subsettingClient.getDistribution( + studyMetadata.id, + entity.id, + leaf.term, + { + filters, + valueSpec: 'count', + } + ) + ).then((distribution) => { + const fgValueByLabel = Object.fromEntries( + distribution.foreground.histogram.map(({ binLabel, value }) => [ + binLabel, + value ?? 0, + ]) + ); + const bgValueByLabel = Object.fromEntries( + distribution.background.histogram.map(({ binLabel, value }) => [ + binLabel, + value ?? 0, + ]) + ); + const variable = variablesById[leaf.term]; + if (variable == null || !isTableVariable(variable)) + throw new Error( + `Could not find a categorical EDA variable associated with the leaf field "${leaf.term}".` ); - const variable = variablesById[leaf.term]; - if (variable == null || !isTableVariable(variable)) - throw new Error( - `Could not find a categorical EDA variable associated with the leaf field "${leaf.term}".` - ); - return { - term: leaf.term, - display: leaf.display, - valueCounts: variable.vocabulary?.map((label) => ({ - value: label, - count: bgValueByLabel[label], - filteredCount: fgValueByLabel[label] ?? 0, - })), - internalsCount: - distribution.background.statistics.numDistinctEntityRecords, - internalsFilteredCount: - distribution.foreground.statistics.numDistinctEntityRecords, - }; - }); - }) - ); - }, - [ - thisFilter, - otherFilters, - leaves, - entity.id, - studyMetadata.id, - variablesById, - ] // used to have `subsettingClient` - ); + return { + term: leaf.term, + display: leaf.display, + valueCounts: variable.vocabulary?.map((label) => ({ + value: label, + count: bgValueByLabel[label], + filteredCount: fgValueByLabel[label] ?? 0, + })), + internalsCount: + distribution.background.statistics.numDistinctEntityRecords, + internalsFilteredCount: + distribution.foreground.statistics.numDistinctEntityRecords, + }; + }); + }) + ); + }, [ + thisFilter ? thisFilter : { type: 'NO_FILTER' }, + otherFilters, + leaves, + entity.id, + studyMetadata.id, + variablesById, + ]); // Sorted counts. This is done separately from retrieving the data so that // updates to sorting don't incur backend requests.