diff --git a/packages/discovery-react-components/src/components/SearchResults/components/Result/Result.tsx b/packages/discovery-react-components/src/components/SearchResults/components/Result/Result.tsx index 052cfd5ca..495af2cc8 100644 --- a/packages/discovery-react-components/src/components/SearchResults/components/Result/Result.tsx +++ b/packages/discovery-react-components/src/components/SearchResults/components/Result/Result.tsx @@ -129,10 +129,6 @@ export const Result: React.FunctionComponent = ({ 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); diff --git a/packages/discovery-react-components/src/utils/getDocumentTitle.ts b/packages/discovery-react-components/src/utils/getDocumentTitle.ts index bfae511d3..a05c641b0 100644 --- a/packages/discovery-react-components/src/utils/getDocumentTitle.ts +++ b/packages/discovery-react-components/src/utils/getDocumentTitle.ts @@ -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 ''; };