Skip to content

Commit a8b806e

Browse files
MelvinBotbrunovjk
andcommitted
Refactor: use key-based active tab matching in SearchTypeMenuNarrow
Return activeKey from useSearchTypeMenuSections hook and use key comparison instead of fragile index-based matching in SearchTypeMenuNarrow. This avoids potential offset issues when menu sections include items rendered differently. Co-authored-by: Bruno Rocha <brunovjk@users.noreply.github.com>
1 parent 83b97e9 commit a8b806e

2 files changed

Lines changed: 11 additions & 4 deletions

File tree

src/hooks/useSearchTypeMenuSections.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,18 @@ const useSearchTypeMenuSections = (queryParams?: UseSearchTypeMenuSectionsParams
172172
return -1;
173173
}, [typeMenuSections, savedSearches, hash, similarSearchHash, sortBy, sortOrder, type]);
174174

175+
const activeKey = useMemo(() => {
176+
if (activeItemIndex < 0) {
177+
return undefined;
178+
}
179+
const flatItems = typeMenuSections.flatMap((section) => section.menuItems);
180+
return flatItems.at(activeItemIndex)?.key;
181+
}, [typeMenuSections, activeItemIndex]);
182+
175183
return {
176184
typeMenuSections,
177185
activeItemIndex,
186+
activeKey,
178187
};
179188
};
180189

src/pages/Search/SearchTypeMenuNarrow.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ function SearchTypeMenuNarrow({queryJSON, onTabPress}: SearchTypeMenuNarrowProps
7474
const {translate} = useLocalize();
7575
const {isOffline} = useNetwork();
7676
const navigation = useNavigation();
77-
const {typeMenuSections, activeItemIndex} = useSearchTypeMenuSections({
77+
const {typeMenuSections, activeKey: activeTypeMenuKey} = useSearchTypeMenuSections({
7878
hash: queryJSON?.hash,
7979
similarSearchHash: queryJSON?.similarSearchHash,
8080
sortBy: queryJSON?.sortBy,
@@ -169,7 +169,6 @@ function SearchTypeMenuNarrow({queryJSON, onTabPress}: SearchTypeMenuNarrowProps
169169
.filter((item) => item !== null)
170170
: [];
171171

172-
let typeMenuItemIndex = 0;
173172
for (const section of typeMenuSections) {
174173
if (section.translationPath === 'search.savedSearchesMenuItemTitle') {
175174
tabItems.push(...savedSearchesTabItems);
@@ -185,10 +184,9 @@ function SearchTypeMenuNarrow({queryJSON, onTabPress}: SearchTypeMenuNarrowProps
185184
badgeText,
186185
});
187186
queryMap.set(item.key, {query: item.searchQuery});
188-
if (activeItemIndex === typeMenuItemIndex) {
187+
if (item.key === activeTypeMenuKey) {
189188
activeKey = item.key;
190189
}
191-
typeMenuItemIndex++;
192190
}
193191
}
194192
}

0 commit comments

Comments
 (0)