2019-08-27 12:25:38 +00:00
|
|
|
import React from 'react';
|
2020-02-14 13:39:42 +00:00
|
|
|
import { Text, Clipboard } from 'react-native';
|
2019-08-27 12:25:38 +00:00
|
|
|
|
|
|
|
import styles from './styles';
|
2019-12-04 16:39:53 +00:00
|
|
|
import { themes } from '../../constants/colors';
|
2020-02-14 13:39:42 +00:00
|
|
|
import { LISTENER } from '../Toast';
|
|
|
|
import EventEmitter from '../../utils/events';
|
|
|
|
import I18n from '../../i18n';
|
2021-07-15 14:39:44 +00:00
|
|
|
import openLink from '../../utils/openLink';
|
2019-08-27 12:25:38 +00:00
|
|
|
|
2021-07-21 15:15:13 +00:00
|
|
|
interface ILink {
|
|
|
|
children: JSX.Element;
|
|
|
|
link: string;
|
|
|
|
theme: string;
|
|
|
|
onLinkPress: Function;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Link = React.memo(({ children, link, theme, onLinkPress }: ILink) => {
|
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 (
|
|
|
|
<Text
|
2020-02-28 16:18:03 +00:00
|
|
|
onPress={handlePress}
|
|
|
|
onLongPress={onLongPress}
|
|
|
|
style={{ ...styles.link, color: themes[theme].actionTintColor }}
|
2019-08-27 12:25:38 +00:00
|
|
|
>
|
|
|
|
{ childLength !== 0 ? children : link }
|
|
|
|
</Text>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
export default Link;
|