From e6e884f1cb80ecdc95051d792c8d81d6352f383f Mon Sep 17 00:00:00 2001 From: Lim Chee Aun Date: Fri, 28 Jun 2024 07:49:30 +0800 Subject: [PATCH] Refactor + make card post work for no-image cards --- src/components/status.jsx | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/src/components/status.jsx b/src/components/status.jsx index daf4ba64..327f3ae9 100644 --- a/src/components/status.jsx +++ b/src/components/status.jsx @@ -2456,6 +2456,22 @@ function MediaFirstContainer(props) { ); } +function getDomain(url) { + return punycode.toUnicode( + URL.parse(url) + .hostname.replace(/^www\./, '') + .replace(/\/$/, ''), + ); +} + +// "Post": Quote post + card link preview combo +// Assume all links from these domains are "posts" +// Mastodon links are "posts" too but they are converted to real quote posts and there's too many domains to check +// This is just "Progressive Enhancement" +function isCardPost(domain) { + return ['x.com', 'twitter.com', 'threads.net', 'bsky.app'].includes(domain); +} + function Card({ card, selfReferential, instance }) { const snapStates = useSnapshot(states); const { @@ -2534,11 +2550,7 @@ function Card({ card, selfReferential, instance }) { ); if (hasText && (image || (type === 'photo' && blurhash))) { - const domain = punycode.toUnicode( - URL.parse(url) - .hostname.replace(/^www\./, '') - .replace(/\/$/, ''), - ); + const domain = getDomain(url); let blurhashImage; const rgbAverageColor = image && blurhash ? getBlurHashAverageColor(blurhash) : null; @@ -2559,11 +2571,7 @@ function Card({ card, selfReferential, instance }) { blurhashImage = canvas.toDataURL(); } - // "Post": Quote post + card link preview combo - // Assume all links from these domains are "posts" - // Mastodon links are "posts" too but they are converted to real quote posts and there's too many domains to check - // This is just "Progressive Enhancement" - const isPost = ['x.com', 'twitter.com', 'threads.net'].includes(domain); + const isPost = isCardPost(domain); return (