Fixed recommending site without available metadata (#17989)

fixes https://github.com/TryGhost/Product/issues/3813
This commit is contained in:
Simon Backx 2023-09-06 09:21:27 +02:00 committed by GitHub
parent 8f272e730b
commit 83399222ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 14 deletions

View File

@ -23,10 +23,16 @@ export const useGetOembed = () => {
return {
async query(searchParams: OembedRequest) {
const url = apiUrl(path, searchParams);
const result = await fetchApi(url, {
method: 'GET'
});
return result as OembedResponse;
try {
const result = await fetchApi(url, {
method: 'GET'
});
return result as OembedResponse;
} catch (e) {
// eslint-disable-next-line no-console
console.error(e);
return null;
}
}
};
};

View File

@ -38,12 +38,14 @@ const AddRecommendationModal: React.FC<AddRecommendationModalProps> = ({recommen
type: 'mention'
});
if (!oembed) {
showToast({
type: 'pageError',
message: 'Could not fetch metadata for this URL, please try again later'
});
return;
let defaultTitle = formState.title;
if (!defaultTitle) {
try {
defaultTitle = new URL(formState.url).hostname.replace('www.', '');
} catch (e) {
// Ignore
defaultTitle = formState.url;
}
}
// Switch modal without changing the route (the second modal is not reachable by URL)
@ -52,10 +54,10 @@ const AddRecommendationModal: React.FC<AddRecommendationModalProps> = ({recommen
animate: false,
recommendation: {
...formState,
title: oembed.metadata.title ?? formState.title,
excerpt: oembed.metadata.description ?? formState.excerpt,
featured_image: oembed.metadata.thumbnail ?? formState.featured_image,
favicon: oembed.metadata.icon ?? formState.favicon
title: oembed?.metadata?.title ?? defaultTitle,
excerpt: oembed?.metadata?.description ?? formState.excerpt ?? null,
featured_image: oembed?.metadata?.thumbnail ?? formState.featured_image ?? null,
favicon: oembed?.metadata?.icon ?? formState.favicon ?? null
}
});
},