Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IMPROVEMENT] Message Touchable #2082

Merged
merged 9 commits into from
Apr 30, 2020
7,675 changes: 3,603 additions & 4,072 deletions __tests__/__snapshots__/Storyshots.test.js.snap

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions app/containers/Avatar.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import React from 'react';
import PropTypes from 'prop-types';
import { View } from 'react-native';
import FastImage from 'react-native-fast-image';
import Touchable from 'react-native-platform-touchable';
import { settings as RocketChatSettings } from '@rocket.chat/sdk';
import Touch from '../utils/touch';

import { avatarURL } from '../utils/avatar';

const Avatar = React.memo(({
text, size, baseUrl, borderRadius, style, avatar, type, children, userId, token, onPress, theme
text, size, baseUrl, borderRadius, style, avatar, type, children, userId, token, onPress
}) => {
const avatarStyle = {
width: size,
Expand Down Expand Up @@ -36,9 +37,9 @@ const Avatar = React.memo(({

if (onPress) {
image = (
<Touch onPress={onPress} theme={theme}>
<Touchable onPress={onPress}>
{image}
</Touch>
</Touchable>
);
}

Expand All @@ -61,7 +62,6 @@ Avatar.propTypes = {
children: PropTypes.object,
userId: PropTypes.string,
token: PropTypes.string,
theme: PropTypes.string,
onPress: PropTypes.func
};

Expand Down
12 changes: 5 additions & 7 deletions app/containers/message/Attachments.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,31 @@ import Video from './Video';
import Reply from './Reply';

const Attachments = React.memo(({
attachments, timeFormat, user, baseUrl, showAttachment, getCustomEmoji, theme
attachments, timeFormat, showAttachment, getCustomEmoji, theme
}) => {
if (!attachments || attachments.length === 0) {
return null;
}

return attachments.map((file, index) => {
if (file.image_url) {
return <Image key={file.image_url} file={file} user={user} baseUrl={baseUrl} showAttachment={showAttachment} getCustomEmoji={getCustomEmoji} theme={theme} />;
return <Image key={file.image_url} file={file} showAttachment={showAttachment} getCustomEmoji={getCustomEmoji} theme={theme} />;
}
if (file.audio_url) {
return <Audio key={file.audio_url} file={file} user={user} baseUrl={baseUrl} getCustomEmoji={getCustomEmoji} theme={theme} />;
return <Audio key={file.audio_url} file={file} getCustomEmoji={getCustomEmoji} theme={theme} />;
}
if (file.video_url) {
return <Video key={file.video_url} file={file} user={user} baseUrl={baseUrl} showAttachment={showAttachment} getCustomEmoji={getCustomEmoji} theme={theme} />;
return <Video key={file.video_url} file={file} showAttachment={showAttachment} getCustomEmoji={getCustomEmoji} theme={theme} />;
}

// eslint-disable-next-line react/no-array-index-key
return <Reply key={index} index={index} attachment={file} timeFormat={timeFormat} user={user} baseUrl={baseUrl} getCustomEmoji={getCustomEmoji} theme={theme} />;
return <Reply key={index} index={index} attachment={file} timeFormat={timeFormat} getCustomEmoji={getCustomEmoji} theme={theme} />;
});
}, (prevProps, nextProps) => isEqual(prevProps.attachments, nextProps.attachments) && prevProps.theme === nextProps.theme);

Attachments.propTypes = {
attachments: PropTypes.array,
timeFormat: PropTypes.string,
user: PropTypes.object,
baseUrl: PropTypes.string,
showAttachment: PropTypes.func,
getCustomEmoji: PropTypes.func,
theme: PropTypes.string
Expand Down
24 changes: 11 additions & 13 deletions app/containers/message/Audio.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import { Audio } from 'expo-av';
import Slider from '@react-native-community/slider';
import moment from 'moment';
import equal from 'deep-equal';
import Touchable from 'react-native-platform-touchable';

import Touchable from './Touchable';
import Markdown from '../markdown';
import { CustomIcon } from '../../lib/Icons';
import sharedStyles from '../../views/Styles';
import { themes } from '../../constants/colors';
import { isAndroid, isIOS } from '../../utils/deviceInfo';
import { withSplit } from '../../split';
import MessageContext from './Context';
import ActivityIndicator from '../ActivityIndicator';

const mode = {
Expand Down Expand Up @@ -91,37 +92,36 @@ Button.propTypes = {
Button.displayName = 'MessageAudioButton';

class MessageAudio extends React.Component {
static contextType = MessageContext;

static propTypes = {
file: PropTypes.object.isRequired,
baseUrl: PropTypes.string.isRequired,
user: PropTypes.object.isRequired,
theme: PropTypes.string,
split: PropTypes.bool,
getCustomEmoji: PropTypes.func
}

constructor(props) {
super(props);
const { baseUrl, file, user } = props;
this.state = {
loading: false,
currentTime: 0,
duration: 0,
paused: true,
uri: `${ baseUrl }${ file.audio_url }?rc_uid=${ user.id }&rc_token=${ user.token }`
paused: true
};

this.sound = new Audio.Sound();
this.sound.setOnPlaybackStatusUpdate(this.onPlaybackStatusUpdate);
}

async componentDidMount() {
const { uri } = this.state;
const { file } = this.props;
const { baseUrl, user } = this.context;

this.setState({ loading: true });
try {
await Audio.setAudioModeAsync(mode);
await this.sound.loadAsync({ uri });
await this.sound.loadAsync({ uri: `${ baseUrl }${ file.audio_url }?rc_uid=${ user.id }&rc_token=${ user.token }` });
} catch {
// Do nothing
}
Expand All @@ -130,7 +130,7 @@ class MessageAudio extends React.Component {

shouldComponentUpdate(nextProps, nextState) {
const {
currentTime, duration, paused, uri, loading
currentTime, duration, paused, loading
} = this.state;
const { file, split, theme } = this.props;
if (nextProps.theme !== theme) {
Expand All @@ -145,9 +145,6 @@ class MessageAudio extends React.Component {
if (nextState.paused !== paused) {
return true;
}
if (nextState.uri !== uri) {
return true;
}
if (!equal(nextProps.file, file)) {
return true;
}
Expand Down Expand Up @@ -237,9 +234,10 @@ class MessageAudio extends React.Component {
loading, paused, currentTime, duration
} = this.state;
const {
user, baseUrl, file, getCustomEmoji, split, theme
file, getCustomEmoji, split, theme
} = this.props;
const { description } = file;
const { baseUrl, user } = this.context;

if (!baseUrl) {
return null;
Expand Down
12 changes: 6 additions & 6 deletions app/containers/message/Broadcast.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import React from 'react';
import React, { useContext } from 'react';
import { View, Text } from 'react-native';
import Touchable from 'react-native-platform-touchable';
import PropTypes from 'prop-types';

import Touchable from './Touchable';
import { CustomIcon } from '../../lib/Icons';
import styles from './styles';
import { BUTTON_HIT_SLOP } from './utils';
import I18n from '../../i18n';
import { themes } from '../../constants/colors';
import MessageContext from './Context';

const Broadcast = React.memo(({
author, user, broadcast, replyBroadcast, theme
author, broadcast, theme
}) => {
const { user, replyBroadcast } = useContext(MessageContext);
const isOwn = author._id === user.id;
if (broadcast && !isOwn) {
return (
Expand All @@ -36,10 +38,8 @@ const Broadcast = React.memo(({

Broadcast.propTypes = {
author: PropTypes.object,
user: PropTypes.object,
broadcast: PropTypes.bool,
theme: PropTypes.string,
replyBroadcast: PropTypes.func
theme: PropTypes.string
};
Broadcast.displayName = 'MessageBroadcast';

Expand Down
2 changes: 1 addition & 1 deletion app/containers/message/CallButton.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React from 'react';
import { View, Text } from 'react-native';
import Touchable from 'react-native-platform-touchable';
import PropTypes from 'prop-types';

import Touchable from './Touchable';
import { formatLastMessage, BUTTON_HIT_SLOP } from './utils';
import styles from './styles';
import I18n from '../../i18n';
Expand Down
10 changes: 5 additions & 5 deletions app/containers/message/Content.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useContext } from 'react';
import { Text, View } from 'react-native';
import PropTypes from 'prop-types';
import equal from 'deep-equal';
Expand All @@ -8,6 +8,7 @@ import styles from './styles';
import Markdown from '../markdown';
import { getInfoMessage } from './utils';
import { themes } from '../../constants/colors';
import MessageContext from './Context';

const Content = React.memo((props) => {
if (props.isInfo) {
Expand All @@ -26,12 +27,13 @@ const Content = React.memo((props) => {
if (props.tmid && !props.msg) {
content = <Text style={[styles.text, { color: themes[props.theme].bodyText }]}>{I18n.t('Sent_an_attachment')}</Text>;
} else {
const { baseUrl, user } = useContext(MessageContext);
content = (
<Markdown
msg={props.msg}
baseUrl={props.baseUrl}
baseUrl={baseUrl}
getCustomEmoji={props.getCustomEmoji}
username={props.user.username}
username={user.username}
isEdited={props.isEdited}
numberOfLines={(props.tmid && !props.isThreadRoom) ? 1 : 0}
preview={props.tmid && !props.isThreadRoom}
Expand Down Expand Up @@ -77,8 +79,6 @@ Content.propTypes = {
msg: PropTypes.string,
theme: PropTypes.string,
isEdited: PropTypes.bool,
baseUrl: PropTypes.string,
user: PropTypes.object,
getCustomEmoji: PropTypes.func,
channels: PropTypes.oneOfType([PropTypes.array, PropTypes.object]),
mentions: PropTypes.oneOfType([PropTypes.array, PropTypes.object]),
Expand Down
4 changes: 4 additions & 0 deletions app/containers/message/Context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import React from 'react';

const MessageContext = React.createContext();
export default MessageContext;
11 changes: 6 additions & 5 deletions app/containers/message/Discussion.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import React from 'react';
import React, { useContext } from 'react';
import { View, Text } from 'react-native';
import Touchable from 'react-native-platform-touchable';
import PropTypes from 'prop-types';

import Touchable from './Touchable';
import { formatLastMessage, formatMessageCount, BUTTON_HIT_SLOP } from './utils';
import styles from './styles';
import I18n from '../../i18n';
import { CustomIcon } from '../../lib/Icons';
import { DISCUSSION } from './constants';
import { themes } from '../../constants/colors';
import MessageContext from './Context';

const Discussion = React.memo(({
msg, dcount, dlm, onDiscussionPress, theme
msg, dcount, dlm, theme
}) => {
const time = formatLastMessage(dlm);
const buttonText = formatMessageCount(dcount, DISCUSSION);
const { onDiscussionPress } = useContext(MessageContext);
return (
<>
<Text style={[styles.startedDiscussion, { color: themes[theme].auxiliaryText }]}>{I18n.t('Started_discussion')}</Text>
Expand Down Expand Up @@ -55,8 +57,7 @@ Discussion.propTypes = {
msg: PropTypes.string,
dcount: PropTypes.number,
dlm: PropTypes.string,
theme: PropTypes.string,
onDiscussionPress: PropTypes.func
theme: PropTypes.string
};
Discussion.displayName = 'MessageDiscussion';

Expand Down
7 changes: 4 additions & 3 deletions app/containers/message/Emoji.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import React from 'react';
import React, { useContext } from 'react';
import { Text } from 'react-native';
import PropTypes from 'prop-types';

import shortnameToUnicode from '../../utils/shortnameToUnicode';
import CustomEmoji from '../EmojiPicker/CustomEmoji';
import MessageContext from './Context';

const Emoji = React.memo(({
content, standardEmojiStyle, customEmojiStyle, baseUrl, getCustomEmoji
content, standardEmojiStyle, customEmojiStyle, getCustomEmoji
}) => {
const { baseUrl } = useContext(MessageContext);
const parsedContent = content.replace(/^:|:$/g, '');
const emoji = getCustomEmoji(parsedContent);
if (emoji) {
Expand All @@ -20,7 +22,6 @@ Emoji.propTypes = {
content: PropTypes.string,
standardEmojiStyle: PropTypes.object,
customEmojiStyle: PropTypes.object,
baseUrl: PropTypes.string,
getCustomEmoji: PropTypes.func
};
Emoji.displayName = 'MessageEmoji';
Expand Down
10 changes: 5 additions & 5 deletions app/containers/message/Image.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import React from 'react';
import React, { useContext } from 'react';
import { View } from 'react-native';
import PropTypes from 'prop-types';
import FastImage from 'react-native-fast-image';
import equal from 'deep-equal';
import Touchable from 'react-native-platform-touchable';
import { createImageProgress } from 'react-native-image-progress';
import * as Progress from 'react-native-progress';

import Touchable from './Touchable';
import Markdown from '../markdown';
import styles from './styles';
import { formatAttachmentUrl } from '../../lib/utils';
import { withSplit } from '../../split';
import { themes } from '../../constants/colors';
import sharedStyles from '../../views/Styles';
import MessageContext from './Context';

const ImageProgress = createImageProgress(FastImage);

Expand Down Expand Up @@ -41,8 +42,9 @@ export const MessageImage = React.memo(({ img, theme }) => (
));

const ImageContainer = React.memo(({
file, imageUrl, baseUrl, user, showAttachment, getCustomEmoji, split, theme
file, imageUrl, showAttachment, getCustomEmoji, split, theme
}) => {
const { baseUrl, user } = useContext(MessageContext);
const img = imageUrl || formatAttachmentUrl(file.image_url, user.id, user.token, baseUrl);
if (!img) {
return null;
Expand Down Expand Up @@ -71,8 +73,6 @@ const ImageContainer = React.memo(({
ImageContainer.propTypes = {
file: PropTypes.object,
imageUrl: PropTypes.string,
baseUrl: PropTypes.string,
user: PropTypes.object,
showAttachment: PropTypes.func,
theme: PropTypes.string,
getCustomEmoji: PropTypes.func,
Expand Down
Loading