From 9e8124b347c7603a8a0f1d1aea5ec9d143dd9692 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Adel=C3=B6w?= Date: Sat, 27 Jun 2020 21:23:29 +0200 Subject: [PATCH] Supply tabs from the catalog page again --- .../components/CatalogPage/CatalogPage.tsx | 30 ++++++- .../components/CatalogPage/CatalogTabs.tsx | 86 ++++++++----------- 2 files changed, 62 insertions(+), 54 deletions(-) diff --git a/plugins/catalog/src/components/CatalogPage/CatalogPage.tsx b/plugins/catalog/src/components/CatalogPage/CatalogPage.tsx index 9626b695a2..4f85ff3fb4 100644 --- a/plugins/catalog/src/components/CatalogPage/CatalogPage.tsx +++ b/plugins/catalog/src/components/CatalogPage/CatalogPage.tsx @@ -24,7 +24,7 @@ import { EntityFilterGroupsProvider, useFilteredEntities } from '../../filter'; import { CatalogFilter } from '../CatalogFilter/CatalogFilter'; import { CatalogTable } from '../CatalogTable/CatalogTable'; import CatalogLayout from './CatalogLayout'; -import { CatalogTabs } from './CatalogTabs'; +import { CatalogTabs, LabeledComponentType } from './CatalogTabs'; import { WelcomeBanner } from './WelcomeBanner'; const useStyles = makeStyles(theme => ({ @@ -36,6 +36,29 @@ const useStyles = makeStyles(theme => ({ }, })); +const tabs: LabeledComponentType[] = [ + { + id: 'service', + label: 'Services', + }, + { + id: 'website', + label: 'Websites', + }, + { + id: 'library', + label: 'Libraries', + }, + { + id: 'documentation', + label: 'Documentation', + }, + { + id: 'other', + label: 'Other', + }, +]; + const CatalogPageContents = () => { const styles = useStyles(); const { loading, error, matchingEntities } = useFilteredEntities(); @@ -44,7 +67,10 @@ const CatalogPageContents = () => { return ( - setSelectedTab(label)} /> + setSelectedTab(label)} + /> diff --git a/plugins/catalog/src/components/CatalogPage/CatalogTabs.tsx b/plugins/catalog/src/components/CatalogPage/CatalogTabs.tsx index 931874d2fc..9d07eae62e 100644 --- a/plugins/catalog/src/components/CatalogPage/CatalogTabs.tsx +++ b/plugins/catalog/src/components/CatalogPage/CatalogTabs.tsx @@ -16,66 +16,49 @@ import { Entity } from '@backstage/catalog-model'; import { HeaderTabs } from '@backstage/core'; -import React, { useCallback, useEffect, useRef, useState } from 'react'; +import React, { + useCallback, + useEffect, + useMemo, + useRef, + useState, +} from 'react'; import { FilterGroup, useEntityFilterGroup } from '../../filter'; -type ComponentType = - | 'service' - | 'website' - | 'library' - | 'documentation' - | 'other'; - +/** + * A component type, and a human readable label for it. + */ export type LabeledComponentType = { - id: ComponentType; + id: string; label: string; }; -const labeledEntityTypes: LabeledComponentType[] = [ - { - id: 'service', - label: 'Services', - }, - { - id: 'website', - label: 'Websites', - }, - { - id: 'library', - label: 'Libraries', - }, - { - id: 'documentation', - label: 'Documentation', - }, - { - id: 'other', - label: 'Other', - }, -]; - -const filterGroup: FilterGroup = { - filters: Object.fromEntries( - labeledEntityTypes.map(t => [ - t.id, - (entity: Entity) => entity.spec?.type === t.id, - ]), - ), -}; - -type OnChangeCallback = (item: { type: string; label: string }) => void; +/** + * Called on mount, and when the selected tab changes. + */ +export type OnChangeCallback = (tab: LabeledComponentType) => void; type Props = { + tabs: LabeledComponentType[]; onChange?: OnChangeCallback; }; /** * The tabs at the top of the catalog list page, for component type filtering. */ -export const CatalogTabs = ({ onChange }: Props) => { +export const CatalogTabs = ({ tabs, onChange }: Props) => { + const filterGroup = useMemo(() => { + return { + filters: Object.fromEntries( + tabs.map(t => [t.id, (entity: Entity) => entity.spec?.type === t.id]), + ), + }; + }, [tabs]); + const { setSelectedFilters } = useEntityFilterGroup('type', filterGroup, [ - labeledEntityTypes[0].id, + tabs[0].id, ]); + const [currentTabIndex, setCurrentTabIndex] = useState(0); const onChangeRef = useRef(); @@ -83,19 +66,18 @@ export const CatalogTabs = ({ onChange }: Props) => { onChangeRef.current = onChange; }, [onChange]); useEffect(() => { - const type = labeledEntityTypes[currentTabIndex]; - onChangeRef.current?.({ type: type.id, label: type.label }); - }, [currentTabIndex]); + onChangeRef.current?.(tabs[currentTabIndex]); + }, [tabs, currentTabIndex]); const switchTab = useCallback( (index: Number) => { - const type = labeledEntityTypes[index as number]; - setSelectedFilters([type.id]); + const tab = tabs[index as number]; + setSelectedFilters([tab.id]); setCurrentTabIndex(index as number); - onChangeRef.current?.({ type: type.id, label: type.label }); + onChangeRef.current?.(tab); }, - [setSelectedFilters], + [tabs, setSelectedFilters], ); - return ; + return ; };