From aa9cddeaa7a429c5af1ca7e6389b23c8dddb2fe5 Mon Sep 17 00:00:00 2001 From: Camila Belo Date: Fri, 1 Jul 2022 10:37:42 +0200 Subject: [PATCH] refactor: checks if search results are relatives Signed-off-by: Camila Belo --- .../CatalogSearchResultListItem.tsx | 6 +++++- .../DefaultResultListItem/DefaultResultListItem.tsx | 6 +++++- .../components/TechDocsSearchResultListItem.tsx | 12 ++++++++++-- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/plugins/catalog/src/components/CatalogSearchResultListItem/CatalogSearchResultListItem.tsx b/plugins/catalog/src/components/CatalogSearchResultListItem/CatalogSearchResultListItem.tsx index fbb8f7b132..807ba611d9 100644 --- a/plugins/catalog/src/components/CatalogSearchResultListItem/CatalogSearchResultListItem.tsx +++ b/plugins/catalog/src/components/CatalogSearchResultListItem/CatalogSearchResultListItem.tsx @@ -65,7 +65,11 @@ export function CatalogSearchResultListItem( const analytics = useAnalytics(); const configApi = useApi(configApiRef); - const to = configApi.getString('app.baseUrl').concat(result.location); + let to = result.location; + // Is relative url + if (!to.match(/^([a-z]*:)?\/\//i)) { + to = configApi.getString('app.baseUrl').concat(to); + } const handleClick = () => { analytics.captureEvent('discover', result.title, { diff --git a/plugins/search-react/src/components/DefaultResultListItem/DefaultResultListItem.tsx b/plugins/search-react/src/components/DefaultResultListItem/DefaultResultListItem.tsx index f410bb24f4..fabf61c813 100644 --- a/plugins/search-react/src/components/DefaultResultListItem/DefaultResultListItem.tsx +++ b/plugins/search-react/src/components/DefaultResultListItem/DefaultResultListItem.tsx @@ -65,7 +65,11 @@ export const DefaultResultListItemComponent = ({ const analytics = useAnalytics(); const configApi = useApi(configApiRef); - const to = configApi.getString('app.baseUrl').concat(result.location); + let to = result.location; + // Is relative url + if (!to.match(/^([a-z]*:)?\/\//i)) { + to = configApi.getString('app.baseUrl').concat(to); + } const handleClick = () => { analytics.captureEvent('discover', result.title, { diff --git a/plugins/techdocs/src/search/components/TechDocsSearchResultListItem.tsx b/plugins/techdocs/src/search/components/TechDocsSearchResultListItem.tsx index 55731e6f2a..d0969ee075 100644 --- a/plugins/techdocs/src/search/components/TechDocsSearchResultListItem.tsx +++ b/plugins/techdocs/src/search/components/TechDocsSearchResultListItem.tsx @@ -77,7 +77,11 @@ export const TechDocsSearchResultListItem = ( const configApi = useApi(configApiRef); const handleClick = () => { - const to = configApi.getString('app.baseUrl').concat(result.location); + let to = result.location; + // Is relative url + if (!to.match(/^([a-z]*:)?\/\//i)) { + to = configApi.getString('app.baseUrl').concat(to); + } analytics.captureEvent('discover', result.title, { attributes: { to }, value: rank, @@ -153,7 +157,11 @@ export const TechDocsSearchResultListItem = ( }; const LinkWrapper = ({ children }: PropsWithChildren<{}>) => { - const to = configApi.getString('app.baseUrl').concat(result.location); + let to = result.location; + // Is relative url + if (!to.match(/^([a-z]*:)?\/\//i)) { + to = configApi.getString('app.baseUrl').concat(to); + } return asLink ? ( {children}