From d3829e6224e398d924b3a37a5840b6378a064334 Mon Sep 17 00:00:00 2001 From: Chris Chudzicki Date: Wed, 3 Jul 2024 12:04:43 -0400 Subject: [PATCH] hide tab when no results --- .../SearchDisplay/ResourceCategoryTabs.tsx | 48 +++++++++++-------- frontends/ol-components/src/index.ts | 2 +- 2 files changed, 30 insertions(+), 20 deletions(-) diff --git a/frontends/mit-open/src/page-components/SearchDisplay/ResourceCategoryTabs.tsx b/frontends/mit-open/src/page-components/SearchDisplay/ResourceCategoryTabs.tsx index 95e74ff481..ffc0ebcaa5 100644 --- a/frontends/mit-open/src/page-components/SearchDisplay/ResourceCategoryTabs.tsx +++ b/frontends/mit-open/src/page-components/SearchDisplay/ResourceCategoryTabs.tsx @@ -5,6 +5,7 @@ import { TabButtonList, TabPanel, styled, + useTabContext, } from "ol-components" import { ResourceCategoryEnum, LearningResourcesSearchResponse } from "api" @@ -73,13 +74,22 @@ const ResourceCategoryTabList: React.FC = ({ onTabChange, className, }) => { - const counts = resourceCategoryCounts(aggregations) + const tabContext = useTabContext() + const activeTab = tabContext?.value + const categoryCounts = resourceCategoryCounts(aggregations) const allCount = aggregations?.resource_category ? (aggregations.resource_category || []).reduce((count, bucket) => { count = count + bucket.doc_count return count }, 0) : undefined + const counts = categoryCounts + ? { + ...categoryCounts, + all: allCount ?? 0, + } + : null + const countsLoaded = !!counts return ( = ({ onTabChange?.() }} > - {tabs.map((t) => { - let count: number | undefined - if (t.name === "all") { - count = allCount - } else { - count = - counts && t.resource_category - ? counts[t.resource_category] ?? 0 - : undefined - } - return ( - - ) - })} + {tabs + .map((tab) => ({ + tab, + count: counts?.[tab.resource_category ?? "all"] ?? 0, + })) + .filter(({ count, tab }) => { + if (!countsLoaded || activeTab === tab.name) return true + return count + }) + .map(({ tab, count }) => { + return ( + + ) + })} ) } diff --git a/frontends/ol-components/src/index.ts b/frontends/ol-components/src/index.ts index 9913ff4a29..281f6fe22c 100644 --- a/frontends/ol-components/src/index.ts +++ b/frontends/ol-components/src/index.ts @@ -109,7 +109,7 @@ export { TabButtonList, } from "./components/TabButtons/TabButtonList" -export { default as TabContext } from "@mui/lab/TabContext" +export { default as TabContext, useTabContext } from "@mui/lab/TabContext" export type { TabContextProps } from "@mui/lab/TabContext" export { default as TabPanel } from "@mui/lab/TabPanel" export type { TabPanelProps } from "@mui/lab/TabPanel"