diff --git a/apps/admin-x-settings/src/api/oembed.ts b/apps/admin-x-settings/src/api/oembed.ts index 471c0fab85..8a3e879d80 100644 --- a/apps/admin-x-settings/src/api/oembed.ts +++ b/apps/admin-x-settings/src/api/oembed.ts @@ -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; + } } }; }; diff --git a/apps/admin-x-settings/src/components/settings/site/recommendations/AddRecommendationModal.tsx b/apps/admin-x-settings/src/components/settings/site/recommendations/AddRecommendationModal.tsx index 45f0c75ae3..29a460ce39 100644 --- a/apps/admin-x-settings/src/components/settings/site/recommendations/AddRecommendationModal.tsx +++ b/apps/admin-x-settings/src/components/settings/site/recommendations/AddRecommendationModal.tsx @@ -38,12 +38,14 @@ const AddRecommendationModal: React.FC = ({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 = ({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 } }); },