From 6f91ee4a74eb5bbda567c009d88343f538b4a57e Mon Sep 17 00:00:00 2001 From: Matthieu Date: Fri, 24 Jan 2025 11:08:18 +0000 Subject: [PATCH] fix: recursively descend into areas --- site/DataCatalog/DataCatalogUtils.ts | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/site/DataCatalog/DataCatalogUtils.ts b/site/DataCatalog/DataCatalogUtils.ts index 7840b420882..d43d35e1c4a 100644 --- a/site/DataCatalog/DataCatalogUtils.ts +++ b/site/DataCatalog/DataCatalogUtils.ts @@ -5,7 +5,7 @@ import { } from "instantsearch.js" import { getIndexName } from "../search/searchClient.js" import { ChartRecordType, SearchIndexName } from "../search/searchTypes.js" -import { TagGraphRoot } from "@ourworldindata/types" +import { TagGraphNode, TagGraphRoot } from "@ourworldindata/types" import { DataCatalogState } from "./DataCatalogState.js" import { countriesByName, Region } from "@ourworldindata/utils" import { SearchClient } from "algoliasearch" @@ -109,6 +109,17 @@ export function setToFacetFilters( return Array.from(facetSet).map((facet) => `${attribute}:${facet}`) } +function getAllTagsInArea(area: TagGraphNode): string[] { + const topics = area.children.reduce((tags, child) => { + tags.push(child.name) + if (child.children.length > 0) { + tags.push(...getAllTagsInArea(child)) + } + return tags + }, [] as string[]) + return Array.from(new Set(topics)) +} + export function getTopicsForRibbons( topics: Set, tagGraph: TagGraphRoot @@ -116,7 +127,7 @@ export function getTopicsForRibbons( if (topics.size === 0) return tagGraph.children.map((child) => child.name) if (topics.size === 1) { const area = tagGraph.children.find((child) => topics.has(child.name)) - if (area) return area.children.map((child) => child.name) + if (area) return getAllTagsInArea(area) } return [] }