mattermost-mobile-upstream/app/components/markdown/markdown_link/markdown_link.tsx
Daniel Espino García 84ea188366
Mattermost Blocks (#9764)
* Mattermost Blocks

* Move block renderer to its own components folder

* Sync translation layer

* UX fixes

* Refactor button to use the button component

* Code review

* Fix CI

* Add animations to collapse blocks

* Add unit tests

* Add e2e tests and related fixes

* Address coderabbit comments

* Address UX comments

* Address feedback

* Address coderabbit comments

* Fix snapshots

* Sync with webapp translation

* Add node limit to button markdown node

* Address feedback

* i18n-extract

* Fix test

* Move image utils to the utils folder

* Remove unneeded style

* Revert build script change

---------

Co-authored-by: Mattermost Build <build@mattermost.com>
2026-06-29 18:42:16 +02:00

146 lines
4.8 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {useManagedConfig} from '@mattermost/react-native-emm';
import Clipboard from '@react-native-clipboard/clipboard';
import React, {Children, type ReactElement, useCallback, useMemo} from 'react';
import {useIntl, defineMessages} from 'react-intl';
import {StyleSheet, Text, View} from 'react-native';
import urlParse from 'url-parse';
import SlideUpPanelItem, {ITEM_HEIGHT} from '@components/slide_up_panel_item';
import {useServerUrl} from '@context/server';
import {usePreventDoubleTap} from '@hooks/utils';
import {bottomSheet, dismissBottomSheet} from '@screens/navigation';
import {bottomSheetSnapPoint, isEmail} from '@utils/helpers';
import {openLink} from '@utils/url/links';
type MarkdownLinkProps = {
children: ReactElement;
experimentalNormalizeMarkdownLinks: string;
href: string;
siteURL: string;
onLinkLongPress?: (url?: string) => void;
}
const messages = defineMessages({
copyEmail: {
id: 'mobile.markdown.link.copy_email',
defaultMessage: 'Copy Email Address',
},
copyURL: {
id: 'mobile.markdown.link.copy_url',
defaultMessage: 'Copy URL',
},
});
const style = StyleSheet.create({
bottomSheet: {
flex: 1,
},
});
const parseLinkLiteral = (literal: string) => {
let nextLiteral = literal;
const WWW_REGEX = /\b^(?:www.)/i;
if (nextLiteral.match(WWW_REGEX)) {
nextLiteral = literal.replace(WWW_REGEX, 'www.');
}
const parsed = urlParse(nextLiteral, {});
return parsed.href;
};
const MarkdownLink = ({children, experimentalNormalizeMarkdownLinks, href, siteURL, onLinkLongPress}: MarkdownLinkProps) => {
const intl = useIntl();
const managedConfig = useManagedConfig<ManagedConfig>();
const serverUrl = useServerUrl();
const handlePress = usePreventDoubleTap(useCallback(() => {
openLink(href, serverUrl, siteURL ?? '', intl);
}, [href, intl, serverUrl, siteURL]));
const handleLongPress = useCallback(() => {
if (managedConfig?.copyAndPasteProtection !== 'true') {
if (onLinkLongPress) {
onLinkLongPress(href);
return;
}
const cleanHref = href.replace(/^mailto:/, '');
const isEmailLink = isEmail(cleanHref);
const renderContent = () => {
return (
<View
testID='at_mention.bottom_sheet'
style={style.bottomSheet}
>
<SlideUpPanelItem
leftIcon='content-copy'
onPress={() => {
dismissBottomSheet();
Clipboard.setString(cleanHref);
}}
testID='at_mention.bottom_sheet.copy_url'
text={intl.formatMessage(isEmailLink ? messages.copyEmail : messages.copyURL)}
/>
<SlideUpPanelItem
destructive={true}
leftIcon='cancel'
onPress={() => {
dismissBottomSheet();
}}
testID='at_mention.bottom_sheet.cancel'
text={intl.formatMessage({id: 'mobile.post.cancel', defaultMessage: 'Cancel'})}
/>
</View>
);
};
bottomSheet(renderContent, [1, bottomSheetSnapPoint(2, ITEM_HEIGHT)]);
}
}, [managedConfig?.copyAndPasteProtection, onLinkLongPress, href, intl]);
const renderedChildren = useMemo(() => {
if (!experimentalNormalizeMarkdownLinks) {
return children;
}
return Children.map(children, (c) => {
const child = c as ReactElement<any>;
if (!child.props.literal || typeof child.props.literal !== 'string' || (child.props.context && child.props.context.length && !child.props.context.includes('link'))) {
return child;
}
const {props, ...otherChildProps} = child;
const {literal, ...otherProps} = props;
const nextProps = {
literal: parseLinkLiteral(literal),
...otherProps,
};
return {
props: nextProps,
...otherChildProps,
};
});
}, [children, experimentalNormalizeMarkdownLinks]);
return (
<Text
onPress={handlePress}
onLongPress={handleLongPress}
testID='markdown_link'
>
{renderedChildren}
</Text>
);
};
export default MarkdownLink;