diff --git a/src/components/feed-post/feed-post.tsx b/src/components/feed-post/feed-post.tsx index 245f6214..6c5670ff 100644 --- a/src/components/feed-post/feed-post.tsx +++ b/src/components/feed-post/feed-post.tsx @@ -19,7 +19,7 @@ const FeedPost: FC = ({ post, index }) => { const { t } = useTranslation(); const [expandoVisible, setExpandoVisible] = useState(false); const subplebbit = useSubplebbit({ subplebbitAddress }); - const commentMediaInfo = utils.getCommentMediaInfo(post); + const commentMediaInfo = utils.getCommentMediaInfoMemo(post); const iframeThumbnail = commentMediaInfo?.patternThumbnailUrl || commentMediaInfo?.thumbnail; const hasThumbnail = link && @@ -66,13 +66,7 @@ const FeedPost: FC = ({ post, index }) => { ( - {(() => { - try { - return new URL(link).hostname.replace(/^www\./, ''); - } catch (e) { - return 'Invalid URL'; - } - })()} + {utils.getHostname(link)} ) diff --git a/src/components/thumbnail/thumbnail.tsx b/src/components/thumbnail/thumbnail.tsx index 5a499996..8e156325 100644 --- a/src/components/thumbnail/thumbnail.tsx +++ b/src/components/thumbnail/thumbnail.tsx @@ -12,7 +12,7 @@ const Thumbnail: FC = ({ commentCid }) => { const comment = useComment({ commentCid }); const subplebbitAddress = comment.subplebbitAddress; const { cid, linkHeight, linkWidth } = comment; - const commentMediaInfo = utils.getCommentMediaInfo(comment); + const commentMediaInfo = utils.getCommentMediaInfoMemo(comment); const iframeThumbnail = commentMediaInfo?.patternThumbnailUrl || commentMediaInfo?.thumbnail; let displayWidth, displayHeight, hasLinkDimensions; diff --git a/src/lib/utils.ts b/src/lib/utils.ts index 01f6b296..3046c55a 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -58,9 +58,9 @@ const getCommentMediaInfo = (comment: Comment) => { } }; -export const getCommentLinkMediaType = memoize(getCommentMediaInfo, { max: 1000 }); +const getCommentMediaInfoMemo = memoize(getCommentMediaInfo, { max: 1000 }); -export const getFormattedTime = (unixTimestamp: number): string => { +const getFormattedTime = (unixTimestamp: number): string => { const currentTime = Date.now() / 1000; const timeDifference = currentTime - unixTimestamp; const t = i18next.t; @@ -95,9 +95,19 @@ export const getFormattedTime = (unixTimestamp: number): string => { return t('time_x_years_ago', { count: Math.floor(timeDifference / 31104000) }); }; +const getHostname = (url: string) => { + try { + return new URL(url).hostname.replace(/^www\./, '') + } catch (e) { + console.log(e); + return ''; + } +} + const utils = { + getCommentMediaInfoMemo, getFormattedTime, - getCommentMediaInfo, + getHostname, }; export default utils;