From 8220c8aaa15114d44f717ce4d8fe4c497afd1830 Mon Sep 17 00:00:00 2001 From: Catalin Hoha <20538711+catalinhoha@users.noreply.github.com> Date: Wed, 27 Oct 2021 18:13:38 +0300 Subject: [PATCH] feat: count all descendant visible section items --- .../SectionRenderer/SectionRenderer.tsx | 2 +- src/models/navigator.ts | 1 + src/navsections/sectionBlueprintMiddleware.ts | 34 +++++++++++++++---- 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/src/components/organisms/NavigatorPane/SectionRenderer/SectionRenderer.tsx b/src/components/organisms/NavigatorPane/SectionRenderer/SectionRenderer.tsx index 915b47601a..1e97a129d5 100644 --- a/src/components/organisms/NavigatorPane/SectionRenderer/SectionRenderer.tsx +++ b/src/components/organisms/NavigatorPane/SectionRenderer/SectionRenderer.tsx @@ -146,7 +146,7 @@ function SectionRenderer(props: SectionRendererProps diff --git a/src/models/navigator.ts b/src/models/navigator.ts index 239aa7b5f6..a99e48c541 100644 --- a/src/models/navigator.ts +++ b/src/models/navigator.ts @@ -94,6 +94,7 @@ export interface SectionInstance { visibleGroupIds: string[]; visibleChildSectionIds?: string[]; visibleDescendantSectionIds?: string[]; + visibleDescendantItemsCount?: number; isLoading: boolean; isVisible: boolean; isInitialized: boolean; diff --git a/src/navsections/sectionBlueprintMiddleware.ts b/src/navsections/sectionBlueprintMiddleware.ts index 05e67f201e..c98dbe0891 100644 --- a/src/navsections/sectionBlueprintMiddleware.ts +++ b/src/navsections/sectionBlueprintMiddleware.ts @@ -38,16 +38,21 @@ const hasNavigatorInstanceStateChanged = ( function computeSectionVisibility( sectionInstance: SectionInstance, sectionInstanceMap: Record -): [boolean, string[] | undefined] { +): [boolean, string[] | undefined, number | undefined] { const sectionBlueprint = sectionBlueprintMap.getById(sectionInstance.id); + let visibleDescendantItemsCount = 0; + if (sectionBlueprint.childSectionIds && sectionBlueprint.childSectionIds.length > 0) { const childSectionVisibilityMap: Record = {}; + sectionBlueprint.childSectionIds.forEach(childSectionId => { const childSectionInstance = sectionInstanceMap[childSectionId]; - const [isChildSectionVisible, visibleDescendantSectionIdsOfChildSection] = computeSectionVisibility( - childSectionInstance, - sectionInstanceMap - ); + const [ + isChildSectionVisible, + visibleDescendantSectionIdsOfChildSection, + visibleDescendantItemsCountOfChildSection, + ] = computeSectionVisibility(childSectionInstance, sectionInstanceMap); + if (visibleDescendantSectionIdsOfChildSection) { if (sectionInstance.visibleDescendantSectionIds) { sectionInstance.visibleDescendantSectionIds.push(...visibleDescendantSectionIdsOfChildSection); @@ -55,6 +60,11 @@ function computeSectionVisibility( sectionInstance.visibleDescendantSectionIds = [...visibleDescendantSectionIdsOfChildSection]; } } + + if (visibleDescendantItemsCountOfChildSection) { + visibleDescendantItemsCount += visibleDescendantItemsCountOfChildSection; + } + childSectionVisibilityMap[childSectionId] = isChildSectionVisible; if (isChildSectionVisible) { if (sectionInstance.visibleChildSectionIds) { @@ -64,6 +74,7 @@ function computeSectionVisibility( } } }); + if (sectionInstance.visibleChildSectionIds) { if (sectionInstance.visibleDescendantSectionIds) { sectionInstance.visibleDescendantSectionIds.push(...sectionInstance.visibleChildSectionIds); @@ -72,10 +83,21 @@ function computeSectionVisibility( sectionInstance.visibleDescendantSectionIds = [...sectionInstance.visibleChildSectionIds]; } } + sectionInstance.isVisible = sectionInstance.isVisible || Object.values(childSectionVisibilityMap).some(isVisible => isVisible === true); } - return [sectionInstance.isVisible, sectionInstance.visibleDescendantSectionIds]; + + if (sectionInstance.visibleItemIds) { + visibleDescendantItemsCount += sectionInstance.visibleItemIds.length; + } + + sectionInstance.visibleDescendantItemsCount = visibleDescendantItemsCount; + return [ + sectionInstance.isVisible, + sectionInstance.visibleDescendantSectionIds, + sectionInstance.visibleDescendantItemsCount, + ]; } /** * Build the section and item instances based on the registered section blueprints