Skip to content

Commit

Permalink
fix: fixed error in search results due to document title
Browse files Browse the repository at this point in the history
  • Loading branch information
Arya Apurvakumar Shah authored and Arya Apurvakumar Shah committed Feb 13, 2024
1 parent 006f15e commit 736fb64
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,6 @@ export const Result: React.FunctionComponent<ResultProps> = ({

let title = getDocumentTitle(result, resultTitleField);

if (Array.isArray(title)) {
title = title[0]; // only first element will be shown if title is array
}

const searchResultClasses = [searchResultClass];
if (isEqual(result, selectedResult.document)) {
searchResultClasses.push(searchResultSelectedClass);
Expand Down
17 changes: 14 additions & 3 deletions packages/discovery-react-components/src/utils/getDocumentTitle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,23 @@ export const getDocumentTitle = (
titleField: string
): string => {
if (document) {
return (
let title =
get(document, titleField) ||
get(document, 'extracted_metadata.title') ||
get(document, 'extracted_metadata.filename') ||
document.document_id
);
document.document_id;
if (Array.isArray(title)) {
title = title[0]; // only first element will be shown if title is array
} else if (typeof title === 'object') {
if (title.hasOwnProperty('text') && typeof get(title, 'text') === 'string') {
// if title is an object return 'text' field if it exists
title = get(title, 'text');
} else {
// else return toString to prevent the component from crashing
title = title.toString();
}
}
return title;
}
return '';
};

0 comments on commit 736fb64

Please sign in to comment.