A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
fix: show hover preview for internal links (#3556)
Jiraliteca15bcfd095f
diff --git a/fluxer_app/src/features/channel/components/embeds/channel_embed/EmbedLink.tsx b/fluxer_app/src/features/channel/components/embeds/channel_embed/EmbedLink.tsxindex 99dc3df66..eb8ed3dce 100644--- a/fluxer_app/src/features/channel/components/embeds/channel_embed/EmbedLink.tsx+++ b/fluxer_app/src/features/channel/components/embeds/channel_embed/EmbedLink.tsx@@ -8,9 +8,10 @@ import {} from '@app/features/channel/components/embeds/channel_embed/ChannelEmbedShared';import {SUPPRESS_EMBEDS_DESCRIPTOR} from '@app/features/i18n/utils/CommonMessageDescriptors';import * as MessageCommands from '@app/features/messaging/commands/MessageCommands';+import {LinkDestinationTooltip} from '@app/features/messaging/components/LinkDestinationTooltip';import {useOpenInBrowserOnMiddleClick} from '@app/features/messaging/hooks/useOpenInBrowserOnMiddleClick';import type {Message} from '@app/features/messaging/models/MessagingMessage';-import {openExternalUrlWithWarning} from '@app/features/messaging/utils/ExternalLinkUtils';+import {isTrustedExternalUrl, openExternalUrlWithWarning} from '@app/features/messaging/utils/ExternalLinkUtils';import TrustedDomain from '@app/features/trusted_domain/state/TrustedDomain';import FocusRing from '@app/features/ui/focus_ring/FocusRing';import {Trans, useLingui} from '@lingui/react/macro';@@ -34,19 +35,25 @@ export const EmbedLink: FC<LinkComponentProps> = observer(({url, children, class};const openInBrowser = useOpenInBrowserOnMiddleClick(url);return (- <FocusRing data-flx="channel.embeds.embed.link-component.focus-ring">- <a- className={clsx(styles.embedLink, className)}- href={url}- rel="noopener noreferrer"- target="_blank"- onClick={handleClick}- onAuxClick={openInBrowser.onAuxClick}- data-flx="channel.embeds.embed.link-component.a"- >- {children}- </a>- </FocusRing>+ <LinkDestinationTooltip+ url={url}+ untrusted={!isTrustedExternalUrl(url)}+ data-flx="channel.embeds.channel-embed.embed-link.link-destination-tooltip"+ >+ <FocusRing data-flx="channel.embeds.embed.link-component.focus-ring">+ <a+ className={clsx(styles.embedLink, className)}+ href={url}+ rel="noopener noreferrer"+ target="_blank"+ onClick={handleClick}+ onAuxClick={openInBrowser.onAuxClick}+ data-flx="channel.embeds.embed.link-component.a"+ >+ {children}+ </a>+ </FocusRing>+ </LinkDestinationTooltip>);});export const SuppressEmbedsConfirmModal: FC<{message: Message}> = ({message}) => {diff --git a/fluxer_app/src/features/messaging/components/LinkDestinationTooltip.tsx b/fluxer_app/src/features/messaging/components/LinkDestinationTooltip.tsxnew file mode 100644index 000000000..32df3b18a--- /dev/null+++ b/fluxer_app/src/features/messaging/components/LinkDestinationTooltip.tsx@@ -0,0 +1,47 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++import linkRendererStyles from '@app/features/theme/styles/LinkRenderer.module.css';+import MobileLayout from '@app/features/ui/state/MobileLayout';+import {Tooltip} from '@app/features/ui/tooltip/Tooltip';+import {WarningIcon} from '@phosphor-icons/react';+import {observer} from 'mobx-react-lite';+import type React from 'react';++interface LinkDestinationTooltipProps {+ url: string;+ untrusted: boolean;+ children: React.ReactElement;+}++export const LinkDestinationTooltip = observer(function LinkDestinationTooltip({+ url,+ untrusted,+ children,+}: LinkDestinationTooltipProps) {+ const text = MobileLayout.enabled+ ? url+ : () => (+ <span className={linkRendererStyles.destination} data-flx="messaging.markdown.renderers.link-renderer.span">+ {untrusted && (+ <WarningIcon+ size={16}+ weight="fill"+ className={linkRendererStyles.destinationWarningIcon}+ data-flx="messaging.markdown.renderers.link-renderer.warning-icon"+ />+ )}+ <span data-flx="messaging.markdown.renderers.link-renderer.span--2">{url}</span>+ </span>+ );+ return (+ <Tooltip+ text={text}+ type="normal"+ position="bottom"+ maxWidth="xl"+ data-flx="messaging.markdown.renderers.link-renderer.tooltip.normal"+ >+ {children}+ </Tooltip>+ );+});diff --git a/fluxer_app/src/features/messaging/components/markdown/renderers/LinkRenderer.tsx b/fluxer_app/src/features/messaging/components/markdown/renderers/LinkRenderer.tsxindex 83f1c423b..1c225d294 100644--- a/fluxer_app/src/features/messaging/components/markdown/renderers/LinkRenderer.tsx+++ b/fluxer_app/src/features/messaging/components/markdown/renderers/LinkRenderer.tsx@@ -24,6 +24,7 @@ import {} from '@app/features/i18n/utils/CommonMessageDescriptors';import * as InviteCommands from '@app/features/invite/commands/InviteCommands';import * as InviteUtils from '@app/features/invite/utils/InviteUtils';+import {LinkDestinationTooltip} from '@app/features/messaging/components/LinkDestinationTooltip';import jumpLinkStyles from '@app/features/messaging/components/markdown/renderers/MessageJumpLink.module.css';import {isRestrictedInlineContext,@@ -58,7 +59,6 @@ import {currentInstanceTarget} from '@app/features/platform/transport/InstanceHTimport {Logger} from '@app/features/platform/utils/AppLogger';import StreamerMode from '@app/features/streamer_mode/state/StreamerMode';import * as ThemeCommands from '@app/features/theme/commands/ThemeCommands';-import linkRendererStyles from '@app/features/theme/styles/LinkRenderer.module.css';import markupStyles from '@app/features/theme/styles/Markup.module.css';import * as ThemeUtils from '@app/features/theme/utils/ThemeUtils';import TrustedDomain from '@app/features/trusted_domain/state/TrustedDomain';@@ -70,8 +70,6 @@ import * as ModalCommands from '@app/features/ui/commands/ModalCommands';import {modal} from '@app/features/ui/commands/ModalCommands';import * as TextCopyCommands from '@app/features/ui/commands/TextCopyCommands';import FocusRing from '@app/features/ui/focus_ring/FocusRing';-import MobileLayout from '@app/features/ui/state/MobileLayout';-import {Tooltip} from '@app/features/ui/tooltip/Tooltip';import {APP_PROTOCOL_PREFIX, APP_PROTOCOL_SCHEME, isAppProtocolUrl} from '@app/features/ui/utils/AppProtocol';import {openExternalUrl} from '@app/features/ui/utils/NativeUtils';import {@@ -86,7 +84,7 @@ import {isProbablyAValidSnowflake} from '@fluxer/snowflake/src/SnowflakeUtils';import type {I18n} from '@lingui/core';import {msg} from '@lingui/core/macro';import {useLingui} from '@lingui/react';-import {CaretRightIcon, ChatTeardropIcon, LockIcon, WarningIcon} from '@phosphor-icons/react';+import {CaretRightIcon, ChatTeardropIcon, LockIcon} from '@phosphor-icons/react';import {clsx} from 'clsx';import {observer} from 'mobx-react-lite';import type React from 'react';@@ -966,8 +964,7 @@ export const LinkRenderer = observer(function LinkRenderer({const shouldShowAccessDeniedModal = Boolean(jumpTarget && !jumpChannel);let isInternal = false;let handleClick: ((e: React.MouseEvent) => void) | undefined;- let isExternalDestination = false;- let isTrustedExternalDestination = false;+ let isUntrustedExternalDestination = false;if (userProfileId) {handleClick = (event) => {event.preventDefault();@@ -1123,8 +1120,7 @@ export const LinkRenderer = observer(function LinkRenderer({}if (!isInternal && !inviteCode) {const isTrusted = TrustedDomain.isTrustedDomain(parsed.hostname);- isExternalDestination = true;- isTrustedExternalDestination = isTrusted;+ isUntrustedExternalDestination = !isTrusted;if (!isTrusted) {handleClick = (e) => {e.preventDefault();@@ -1145,7 +1141,7 @@ export const LinkRenderer = observer(function LinkRenderer({}const href = AttachmentUrlRefresher.fresh(url);const warmAttachmentUrl = () => AttachmentUrlRefresher.warm(url);- const showDestinationTooltip = isExternalDestination && text != null;+ const showDestinationTooltip = text != null;const linkElement = (<FocusRing key={id} offset={-2} data-flx="messaging.markdown.renderers.link-renderer.focus-ring--2"><a@@ -1176,30 +1172,13 @@ export const LinkRenderer = observer(function LinkRenderer({if (!showDestinationTooltip) {return linkElement;}- const destinationTooltipText = MobileLayout.enabled- ? url- : () => (- <span className={linkRendererStyles.destination} data-flx="messaging.markdown.renderers.link-renderer.span">- {!isTrustedExternalDestination && (- <WarningIcon- size={16}- weight="fill"- className={linkRendererStyles.destinationWarningIcon}- data-flx="messaging.markdown.renderers.link-renderer.warning-icon"- />- )}- <span data-flx="messaging.markdown.renderers.link-renderer.span--2">{url}</span>- </span>- );return (- <Tooltip- text={destinationTooltipText}- type="normal"- position="bottom"- maxWidth="xl"- data-flx="messaging.markdown.renderers.link-renderer.tooltip.normal"+ <LinkDestinationTooltip+ url={url}+ untrusted={isUntrustedExternalDestination}+ data-flx="messaging.markdown.renderers.link-renderer.link-destination-tooltip">{linkElement}- </Tooltip>+ </LinkDestinationTooltip>);});diff --git a/fluxer_app/src/features/messaging/utils/ExternalLinkUtils.tsx b/fluxer_app/src/features/messaging/utils/ExternalLinkUtils.tsxindex c3d2e1507..a4f9323ac 100644--- a/fluxer_app/src/features/messaging/utils/ExternalLinkUtils.tsx+++ b/fluxer_app/src/features/messaging/utils/ExternalLinkUtils.tsx@@ -13,7 +13,7 @@ export const MIDDLE_MOUSE_BUTTON = 1;let externalLinkInterceptorAttached = false;-function isTrustedExternalUrl(url: string): boolean {+export function isTrustedExternalUrl(url: string): boolean {try {return TrustedDomain.isTrustedDomain(new URL(url).hostname);} catch {