Profile: Show links without preview in the list (#6450)

This commit is contained in:
zubiden 2025-11-11 14:37:45 +01:00 committed by Alexander Zinchuk
parent 1d457f7ae7
commit 9192eb10fe

View File

@ -1,4 +1,4 @@
import { memo } from '../../lib/teact/teact'; import { memo, useMemo } from '../../lib/teact/teact';
import { withGlobal } from '../../global'; import { withGlobal } from '../../global';
import type { ApiMessage, ApiWebPage } from '../../api/types'; import type { ApiMessage, ApiWebPage } from '../../api/types';
@ -50,41 +50,64 @@ const WebLink = ({
const lang = useLang(); const lang = useLang();
const oldLang = useOldLang(); const oldLang = useOldLang();
const handleMessageClick = useLastCallback(() => {
onMessageClick(message);
});
let linkData: ApiWebPageWithFormatted | undefined = webPage; let linkData: ApiWebPageWithFormatted | undefined = webPage;
if (!linkData) { if (!linkData) {
const link = getFirstLinkInMessage(message); const link = getFirstLinkInMessage(message);
if (link) { if (link) {
const { url, domain } = link; const { url: linkUrl, domain } = link;
linkData = { linkData = {
mediaType: 'webpage',
webpageType: 'full',
id: '',
displayUrl: linkUrl,
siteName: domain.replace(/^www./, ''), siteName: domain.replace(/^www./, ''),
url: url.includes('://') ? url : url.includes('@') ? `mailto:${url}` : `http://${url}`, url: linkUrl.includes('://') ? linkUrl : linkUrl.includes('@') ? `mailto:${linkUrl}` : `http://${linkUrl}`,
formattedDescription: getMessageTextWithFallback(lang, message)?.text !== url formattedDescription: getMessageTextWithFallback(lang, message)?.text !== linkUrl
? renderMessageSummary(lang, message, undefined, undefined, MAX_TEXT_LENGTH) ? renderMessageSummary(lang, message, undefined, undefined, MAX_TEXT_LENGTH)
: undefined, : undefined,
} as ApiWebPageWithFormatted; } satisfies ApiWebPageWithFormatted;
} }
} }
const handleMessageClick = useLastCallback(() => { const fullLinkData = linkData?.webpageType === 'full' ? linkData : undefined;
onMessageClick(message);
});
if (linkData?.webpageType !== 'full') { const { url } = linkData || {};
return undefined;
} const fallbackDescription = useMemo(() => {
return getMessageTextWithFallback(lang, message)?.text !== url
? renderMessageSummary(lang, message, undefined, undefined, MAX_TEXT_LENGTH)
: undefined;
}, [lang, message, url]);
const { const {
siteName, siteName,
url,
displayUrl, displayUrl,
title, title,
description, description,
formattedDescription, formattedDescription = fallbackDescription,
photo, photo,
video, video,
} = linkData; } = fullLinkData || {};
const siteTitle = useMemo(() => {
const text = title || siteName || displayUrl;
if (text) return text;
if (!url) return '';
try {
const parsedUrl = new URL(url);
return parsedUrl.hostname;
} catch (e) {
return '';
}
}, [title, siteName, displayUrl, url]);
if (!url) return undefined;
const truncatedDescription = !senderTitle && description && trimText(description, MAX_TEXT_LENGTH); const truncatedDescription = !senderTitle && description && trimText(description, MAX_TEXT_LENGTH);
@ -93,12 +116,12 @@ const WebLink = ({
(!photo && !video) && 'without-media', (!photo && !video) && 'without-media',
); );
const safeLinkContent = url.replace('mailto:', '') || displayUrl; const safeLinkContent = displayUrl || url.replace('mailto:', '');
return ( return (
<div <div
className={className} className={className}
data-initial={(siteName || displayUrl)[0]} data-initial={siteTitle[0]}
dir={lang.isRtl ? 'rtl' : undefined} dir={lang.isRtl ? 'rtl' : undefined}
> >
{photo && ( {photo && (
@ -106,7 +129,7 @@ const WebLink = ({
)} )}
<div className="content"> <div className="content">
<Link isRtl={lang.isRtl} className="site-title" onClick={handleMessageClick}> <Link isRtl={lang.isRtl} className="site-title" onClick={handleMessageClick}>
{renderText(title || siteName || displayUrl)} {renderText(siteTitle)}
</Link> </Link>
{(truncatedDescription || formattedDescription) && ( {(truncatedDescription || formattedDescription) && (
<Link isRtl={lang.isRtl} className="site-description" onClick={handleMessageClick}> <Link isRtl={lang.isRtl} className="site-description" onClick={handleMessageClick}>