/* * Wire * Copyright (C) 2018 Wire Swiss GmbH * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see http://www.gnu.org/licenses/. * */ import React, {UIEvent} from 'react'; import cx from 'classnames'; import {Image} from 'Components/Image'; import {useKoSubscribableChildren} from 'Util/ComponentUtil'; import {handleKeyDown} from 'Util/KeyboardUtil'; import {t} from 'Util/LocalizerUtil'; import {safeWindowOpen} from 'Util/SanitizationUtil'; import {cleanURL, prependProtocol} from 'Util/UrlUtil'; import {isTweetUrl} from 'Util/ValidationUtil'; import {AssetHeader} from './AssetHeader'; import type {ContentMessage} from '../../../../../entity/message/ContentMessage'; import type {Text} from '../../../../../entity/message/Text'; import {useMessageFocusedTabIndex} from '../../util'; export interface LinkPreviewAssetProps { /** Does the asset have a visible header? */ header?: boolean; message: ContentMessage; isFocusable?: boolean; } const LinkPreviewAsset: React.FC = ({header = false, message, isFocusable = true}) => { const { previews: [preview], } = useKoSubscribableChildren(message.getFirstAsset() as Text, ['previews']); const isTypeTweet = !!preview?.tweet; const isTweet = isTypeTweet && isTweetUrl(preview?.url); const author = isTweet ? preview.tweet?.author?.substring(0, 20) : ''; const previewImage = preview?.image; const {isObfuscated} = useKoSubscribableChildren(message, ['isObfuscated']); const messageFocusedTabIndex = useMessageFocusedTabIndex(isFocusable); const linkRef = React.useRef(null); const onClick = ({target}: UIEvent) => { // Clicking on the link directly will already open the link, so we don't want to open it manually const wasLinkClicked = target === linkRef.current; if (!message.isExpired() && !wasLinkClicked) { safeWindowOpen(preview?.url); } }; return isObfuscated ? (

{preview?.title}

{preview?.url}

) : (
handleKeyDown(e, () => onClick(e))} >
{preview && previewImage ? ( ) : (
)}
{header && } {preview && ( <>

{preview.title}

{isTweet ? (

{author}

{t('conversationTweetAuthor')}

) : ( {cleanURL(preview.url)} )} )}
); }; export {LinkPreviewAsset};