Skip to content

Commit b4390dd

Browse files
Merge pull request #54 from Azure-Samples/mirza/fe/fix-applied-bar
Refactor tag removal logic for clarity and maintainability
2 parents 6d6ab43 + f7ffd9b commit b4390dd

1 file changed

Lines changed: 30 additions & 7 deletions

File tree

  • src/components/gallery/FilterAppliedBar

src/components/gallery/FilterAppliedBar/index.tsx

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ import {
1414
ChevronRight20Filled,
1515
} from "@fluentui/react-icons";
1616
import { Tags, type TagType } from "../../../data/tags";
17-
import { toggleListItem, normalizeLabel } from "../../../utils/jsUtils";
17+
import { toggleListItem } from "../../../utils/jsUtils";
18+
import {
19+
getSubTagKey,
20+
isContextualSubTag,
21+
} from "../../../utils/filterTagUtils";
1822
import { prepareUserState } from "../../../pages/index";
1923
import styles from "../../../pages/styles.module.css";
2024

@@ -40,9 +44,7 @@ function removeTagWithSubFilters(
4044

4145
const tagObject = Tags[tag];
4246
if (tagObject?.subType?.length) {
43-
const subKeys = tagObject.subType.map(
44-
(s) => normalizeLabel(s.label) as TagType,
45-
);
47+
const subKeys = tagObject.subType.map((s) => getSubTagKey(tag, s.label));
4648
newTags = newTags.filter((t) => !subKeys.includes(t));
4749
}
4850

@@ -54,7 +56,7 @@ function removeTagWithSubFilters(
5456
const parentObj = Tags[parentKey as TagType];
5557
if (parentObj?.subType && Array.isArray(parentObj.subType)) {
5658
parentObj.subType.forEach((s) => {
57-
const childKey = normalizeLabel(s.label);
59+
const childKey = getSubTagKey(parentKey as TagType, s.label);
5860
parentMap[childKey] ??= [];
5961
parentMap[childKey].push(parentKey);
6062
});
@@ -69,8 +71,8 @@ function removeTagWithSubFilters(
6971
const parentObj = Tags[parentKey as TagType];
7072
if (!parentObj || !parentObj.subType) return;
7173

72-
const childKeys = parentObj.subType.map(
73-
(s) => s.label.toLowerCase().replace(/\s+/g, "-") as TagType,
74+
const childKeys = parentObj.subType.map((s) =>
75+
getSubTagKey(parentKey as TagType, s.label),
7476
);
7577

7678
const anyChildSelected = childKeys.some((ck) => newTags.includes(ck));
@@ -79,6 +81,27 @@ function removeTagWithSubFilters(
7981
}
8082
});
8183

84+
// Legacy plain "overview" can still arrive from older URLs. If it is removed,
85+
// drop any selected parent that no longer has a child represented in the tag state.
86+
if (tag === "overview" && !isContextualSubTag(tag)) {
87+
Object.keys(Tags).forEach((parentKey) => {
88+
const parentObj = Tags[parentKey as TagType];
89+
if (!parentObj?.subType?.length) return;
90+
91+
const childKeys = parentObj.subType.map((s) =>
92+
getSubTagKey(parentKey as TagType, s.label),
93+
);
94+
95+
const anyChildSelected = childKeys.some((childKey) =>
96+
newTags.includes(childKey),
97+
);
98+
99+
if (!anyChildSelected && newTags.includes(parentKey as TagType)) {
100+
newTags = newTags.filter((selectedTag) => selectedTag !== parentKey);
101+
}
102+
});
103+
}
104+
82105
return newTags;
83106
}
84107

0 commit comments

Comments
 (0)