From 736fb6440d6661c754112864dcd9581c83cc3e67 Mon Sep 17 00:00:00 2001 From: Arya Apurvakumar Shah Date: Tue, 13 Feb 2024 01:26:16 -0800 Subject: [PATCH] fix: fixed error in search results due to document title --- .../SearchResults/components/Result/Result.tsx | 4 ---- .../src/utils/getDocumentTitle.ts | 17 ++++++++++++++--- 2 files changed, 14 insertions(+), 7 deletions(-) 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 ''; };