From 319890d138b9ffae980c9456e85adb7bf8e0c708 Mon Sep 17 00:00:00 2001 From: Fedor Indutny <79877362+indutny-signal@users.noreply.github.com> Date: Mon, 9 Sep 2024 14:00:21 -0700 Subject: [PATCH] Better exception handling for getDomain --- ts/components/StoryLinkPreview.tsx | 6 +++--- ts/components/TextAttachment.tsx | 4 ++-- ts/state/selectors/linkPreviews.ts | 4 ++-- ts/state/selectors/message.ts | 8 ++++++-- ts/types/LinkPreview.ts | 8 ++++++++ 5 files changed, 21 insertions(+), 9 deletions(-) diff --git a/ts/components/StoryLinkPreview.tsx b/ts/components/StoryLinkPreview.tsx index 35a6184640..6a4a7b1f05 100644 --- a/ts/components/StoryLinkPreview.tsx +++ b/ts/components/StoryLinkPreview.tsx @@ -9,7 +9,7 @@ import type { LinkPreviewType } from '../types/message/LinkPreviews'; import type { LocalizerType } from '../types/Util'; import { CurveType, Image } from './conversation/Image'; import { isImageAttachment } from '../types/Attachment'; -import { getDomain } from '../types/LinkPreview'; +import { getSafeDomain } from '../types/LinkPreview'; export type Props = LinkPreviewType & { forceCompactMode?: boolean; @@ -26,7 +26,7 @@ export function StoryLinkPreview({ url, }: Props): JSX.Element { const isImage = isImageAttachment(image); - const location = domain || getDomain(String(url)); + const location = domain || getSafeDomain(String(url)); const isCompact = forceCompactMode || !image; let content: JSX.Element | undefined; @@ -74,7 +74,7 @@ export function StoryLinkPreview({
{i18n('icu:stagedPreviewThumbnail',( )} { ); } +export function getSafeDomain(href: string): string | undefined { + try { + return getDomain(href); + } catch { + return undefined; + } +} + export function getDomain(href: string): string { const url = maybeParseUrl(href); if (!url || !url.hostname) {