2019-08-27 12:25:38 +00:00
|
|
|
import React from 'react';
|
2021-09-13 20:41:05 +00:00
|
|
|
import { Clipboard, Text } 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';
|
2022-02-17 15:27:01 +00:00
|
|
|
import { TOnLinkPress } from './interfaces';
|
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;
|
|
|
|
theme: string;
|
2022-02-17 15:27:01 +00:00
|
|
|
onLinkPress?: TOnLinkPress;
|
2021-09-13 20:41:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 (
|
2021-09-13 20:41:05 +00:00
|
|
|
<Text onPress={handlePress} onLongPress={onLongPress} style={{ ...styles.link, color: themes[theme].actionTintColor }}>
|
|
|
|
{childLength !== 0 ? children : link}
|
2019-08-27 12:25:38 +00:00
|
|
|
</Text>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
export default Link;
|