From 4b034cb0ce488e6b69d5d5a16e0c448679f131e8 Mon Sep 17 00:00:00 2001 From: Joe Smallwood Date: Thu, 21 Sep 2023 16:51:11 +0300 Subject: [PATCH] Truncate long category names in breadcrumbs --- components/actions/ActionHero.tsx | 138 +++++++++++++++++++----------- 1 file changed, 86 insertions(+), 52 deletions(-) diff --git a/components/actions/ActionHero.tsx b/components/actions/ActionHero.tsx index e78c60ec7..f6f1242cc 100644 --- a/components/actions/ActionHero.tsx +++ b/components/actions/ActionHero.tsx @@ -1,5 +1,5 @@ import React from 'react'; -import { Container, Row, Col } from 'reactstrap'; +import { Container, Row, Col, UncontrolledTooltip } from 'reactstrap'; import styled from 'styled-components'; import { useTheme } from 'common/theme'; import { getActionTermContext, useTranslation } from 'common/i18n'; @@ -12,6 +12,7 @@ import { import { usePlan } from 'context/plan'; import Icon from 'components/common/Icon'; +import { Category } from 'common/__generated__/graphql'; const Hero = styled.header<{ bgColor: string }>` position: relative; @@ -150,65 +151,98 @@ const ActionNumber = styled.span` } `; -function ActionCategories(props) { - const { categories } = props; - const theme = useTheme(); +const MAX_CRUMB_LENGTH = 90; + +const isIdentifierVisible = (category: Category, showIdentifiers: boolean) => + category.categoryPage && category.identifier && showIdentifiers; + +const getCategoryName = (category: Category, showIdentifiers: boolean) => + isIdentifierVisible(category, showIdentifiers) + ? `${category.identifier}. ${category.name}` + : category.name; + +const getCategoryUrl = (category: Category, primaryCategory) => { + if (category.categoryPage) { + return category.categoryPage.urlPath; + } + + if (primaryCategory) { + return `/actions?cat-${primaryCategory.identifier}=${category.id}`; + } + + return undefined; +}; + +type PartialCategory = Pick & { + url?: string; + parent?: PartialCategory; +}; + +function Crumb({ category }: { category: PartialCategory }) { + const id = `crumb-${category.id}`; + const ariaId = `tt-content-${category.id}`; + const isTruncated = category.name.length > MAX_CRUMB_LENGTH; + const name = isTruncated + ? `${category.name.slice(0, MAX_CRUMB_LENGTH).trim()}...` + : category.name; + + return ( + <> + + {category.url ? ( + + {name} + + ) : ( + name + )} + {` / `} + + + {isTruncated && ( + + {category.name} + + )} + + ); +} + +function ActionCategories({ categories }: { categories: Category[] }) { const plan = usePlan(); const showIdentifiers = !plan.primaryActionClassification?.hideCategoryIdentifiers; - const displayCategories = []; const primaryCT = plan.primaryActionClassification; const primaryCatId = primaryCT?.id; - categories.forEach((cat, indx) => { - if (cat.type.id !== primaryCatId) return; - displayCategories[indx] = {}; - let categoryTitle = cat.name; - if (cat.categoryPage) { - displayCategories[indx].url = cat.categoryPage.urlPath; - if (cat.identifier && showIdentifiers) - categoryTitle = `${cat.identifier}. ${cat.name}`; - } else if (primaryCT) { - displayCategories[ - indx - ].url = `/actions?cat-${primaryCT.identifier}=${cat.id}`; - } - displayCategories[indx].name = categoryTitle; - displayCategories[indx].id = cat.id; - if (cat.parent) { - displayCategories[indx].parent = {}; - let categoryParentTitle = cat.parent.name; - if (cat.parent.categoryPage) { - displayCategories[indx].parent.url = cat.parent.categoryPage.urlPath; - if (cat.parent.identifier && showIdentifiers) { - categoryParentTitle = `${cat.parent.identifier}. ${cat.parent.name}`; - } - } else { - displayCategories[ - indx - ].parent.url = `/actions?cat-${primaryCT.identifier}=${cat.parent.id}`; - } - displayCategories[indx].parent.name = categoryParentTitle; - displayCategories[indx].parent.id = cat.parent.id; - } - return true; - }); + const displayCategories: PartialCategory[] = categories + .filter((cat) => cat.type.id === primaryCatId) + .map((cat) => ({ + id: cat.id, + name: getCategoryName(cat, showIdentifiers), + url: getCategoryUrl(cat, primaryCT), + parent: cat.parent + ? { + id: cat.parent.id, + name: getCategoryName(cat.parent, showIdentifiers), + url: getCategoryUrl(cat.parent, primaryCT), + } + : undefined, + })); + return ( - {displayCategories.map((item) => ( -
- {item.parent && ( - - - {item.parent.name} - {' '} - /{' '} - - )} - - {item.name} - {' '} - /{' '} + {displayCategories.map((category) => ( +
+ {category.parent && } + +
))}