Skip to content

Commit e8ba175

Browse files
fix: ensure content items are saved to recent searches (#2738)
1 parent d26e13d commit e8ba175

1 file changed

Lines changed: 19 additions & 2 deletions

File tree

packages/docsearch-react/src/DocSearchModal.tsx

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,21 +384,38 @@ export function DocSearchModal({
384384
prevStatus.current = status;
385385
}, [status, messages, conversations, disableUserPersonalization]);
386386

387+
const createSyntheticParent = React.useCallback(function createSyntheticParent(
388+
item: InternalDocSearchHit,
389+
): InternalDocSearchHit {
390+
// Find the deepest non-null hierarchy level
391+
const hierarchy = item.hierarchy;
392+
const levels = ['lvl6', 'lvl5', 'lvl4', 'lvl3', 'lvl2', 'lvl1', 'lvl0'] as const;
393+
394+
const deepestLevel = levels.find((level) => hierarchy[level]);
395+
396+
return {
397+
...item,
398+
type: deepestLevel || 'lvl0', // Use the deepest available level as type
399+
content: null, // Clear content since this represents a section, not specific content
400+
};
401+
}, []);
402+
387403
const saveRecentSearch = React.useCallback(
388404
function saveRecentSearch(item: InternalDocSearchHit) {
389405
if (disableUserPersonalization) {
390406
return;
391407
}
392408

393409
// We don't store `content` record, but their parent if available.
394-
const search = item.type === 'content' ? item.__docsearch_parent : item;
410+
// If no parent exists, create a synthetic parent from the hierarchy.
411+
const search = item.type === 'content' ? item.__docsearch_parent || createSyntheticParent(item) : item;
395412

396413
// We save the recent search only if it's not favorited.
397414
if (search && favoriteSearches.getAll().findIndex((x) => x.objectID === search.objectID) === -1) {
398415
recentSearches.add(search);
399416
}
400417
},
401-
[favoriteSearches, recentSearches, disableUserPersonalization],
418+
[favoriteSearches, recentSearches, disableUserPersonalization, createSyntheticParent],
402419
);
403420

404421
const sendItemClickEvent = React.useCallback(

0 commit comments

Comments
 (0)