2019-08-27 12:25:38 +00:00
|
|
|
import React from 'react';
|
2022-03-29 16:09:44 +00:00
|
|
|
import { Text } from 'react-native';
|
|
|
|
import Clipboard from '@react-native-clipboard/clipboard';
|
2019-08-27 12:25:38 +00:00
|
|
|
|
|
|
|
import styles from './styles';
|
2020-02-14 13:39:42 +00:00
|
|
|
import { LISTENER } from '../Toast';
|
2022-06-06 14:17:51 +00:00
|
|
|
import EventEmitter from '../../lib/methods/helpers/events';
|
2020-02-14 13:39:42 +00:00
|
|
|
import I18n from '../../i18n';
|
2022-06-06 14:17:51 +00:00
|
|
|
import openLink from '../../lib/methods/helpers/openLink';
|
2022-02-17 15:27:01 +00:00
|
|
|
import { TOnLinkPress } from './interfaces';
|
2022-06-27 21:27:22 +00:00
|
|
|
import { useTheme } from '../../theme';
|
2019-08-27 12:25:38 +00:00
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
interface ILink {
|
2022-03-25 17:20:09 +00:00
|
|
|
children: React.ReactElement | null;
|
2021-09-13 20:41:05 +00:00
|
|
|
link: string;
|
2022-02-17 15:27:01 +00:00
|
|
|
onLinkPress?: TOnLinkPress;
|
2022-06-27 21:27:22 +00:00
|
|
|
testID: string;
|
2021-09-13 20:41:05 +00:00
|
|
|
}
|
|
|
|
|
2022-06-27 21:27:22 +00:00
|
|
|
const Link = React.memo(({ children, link, onLinkPress, testID }: ILink) => {
|
|
|
|
const { colors, theme } = useTheme();
|
|
|
|
|
2019-08-27 12:25:38 +00:00
|
|
|
const handlePress = () => {
|
2021-07-15 14:39:44 +00:00
|
|
|
if (!link) {
|
2019-08-27 12:25:38 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-07-15 14:39:44 +00:00
|
|
|
if (onLinkPress) {
|
|
|
|
return onLinkPress(link);
|
|
|
|
}
|
|
|
|
openLink(link, theme);
|
2019-08-27 12:25:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const childLength = React.Children.toArray(children).filter(o => o).length;
|
2020-02-14 13:39:42 +00:00
|
|
|
const onLongPress = () => {
|
|
|
|
Clipboard.setString(link);
|
|
|
|
EventEmitter.emit(LISTENER, { message: I18n.t('Copied_to_clipboard') });
|
|
|
|
};
|
2019-08-27 12:25:38 +00:00
|
|
|
|
|
|
|
// if you have a [](https://rocket.chat) render https://rocket.chat
|
|
|
|
return (
|
2022-06-27 21:27:22 +00:00
|
|
|
<Text
|
|
|
|
onPress={handlePress}
|
|
|
|
onLongPress={onLongPress}
|
|
|
|
style={{ ...styles.link, color: colors.actionTintColor }}
|
|
|
|
testID={`${testID}-link`}>
|
2021-09-13 20:41:05 +00:00
|
|
|
{childLength !== 0 ? children : link}
|
2019-08-27 12:25:38 +00:00
|
|
|
</Text>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
export default Link;
|