2021-11-16 15:59:58 +00:00
|
|
|
import React, { useContext, useState } from 'react';
|
2022-03-21 20:44:06 +00:00
|
|
|
import { StyleProp, StyleSheet, TextStyle } from 'react-native';
|
2021-09-13 20:41:05 +00:00
|
|
|
import { dequal } from 'dequal';
|
|
|
|
|
|
|
|
import Touchable from './Touchable';
|
|
|
|
import Markdown from '../markdown';
|
|
|
|
import { isIOS } from '../../utils/deviceInfo';
|
2022-05-02 19:21:15 +00:00
|
|
|
import { CustomIcon } from '../CustomIcon';
|
2022-04-07 14:10:03 +00:00
|
|
|
import { themes } from '../../lib/constants';
|
2021-09-13 20:41:05 +00:00
|
|
|
import MessageContext from './Context';
|
2021-11-16 15:59:58 +00:00
|
|
|
import { fileDownload } from '../../utils/fileDownload';
|
|
|
|
import EventEmitter from '../../utils/events';
|
|
|
|
import { LISTENER } from '../Toast';
|
|
|
|
import I18n from '../../i18n';
|
2022-01-11 13:51:48 +00:00
|
|
|
import { IAttachment } from '../../definitions/IAttachment';
|
2021-11-16 15:59:58 +00:00
|
|
|
import RCActivityIndicator from '../ActivityIndicator';
|
2022-02-17 15:27:01 +00:00
|
|
|
import { TGetCustomEmoji } from '../../definitions/IEmoji';
|
2022-04-01 21:52:38 +00:00
|
|
|
import { useTheme } from '../../theme';
|
2022-04-07 13:13:19 +00:00
|
|
|
import { formatAttachmentUrl } from '../../lib/methods/helpers/formatAttachmentUrl';
|
2021-09-13 20:41:05 +00:00
|
|
|
|
|
|
|
const SUPPORTED_TYPES = ['video/quicktime', 'video/mp4', ...(isIOS ? [] : ['video/3gp', 'video/mkv'])];
|
2022-04-01 21:52:38 +00:00
|
|
|
const isTypeSupported = (type: string) => SUPPORTED_TYPES.indexOf(type) !== -1;
|
2021-09-13 20:41:05 +00:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
button: {
|
|
|
|
flex: 1,
|
|
|
|
borderRadius: 4,
|
|
|
|
height: 150,
|
|
|
|
marginBottom: 6,
|
|
|
|
alignItems: 'center',
|
|
|
|
justifyContent: 'center'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
interface IMessageVideo {
|
2022-01-11 13:51:48 +00:00
|
|
|
file: IAttachment;
|
2022-04-01 21:52:38 +00:00
|
|
|
showAttachment?: (file: IAttachment) => void;
|
2022-02-17 15:27:01 +00:00
|
|
|
getCustomEmoji: TGetCustomEmoji;
|
2022-03-21 20:44:06 +00:00
|
|
|
style?: StyleProp<TextStyle>[];
|
|
|
|
isReply?: boolean;
|
2021-09-13 20:41:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const Video = React.memo(
|
2022-04-01 21:52:38 +00:00
|
|
|
({ file, showAttachment, getCustomEmoji, style, isReply }: IMessageVideo) => {
|
2021-09-13 20:41:05 +00:00
|
|
|
const { baseUrl, user } = useContext(MessageContext);
|
2021-11-16 15:59:58 +00:00
|
|
|
const [loading, setLoading] = useState(false);
|
2022-04-01 21:52:38 +00:00
|
|
|
const { theme } = useTheme();
|
2021-11-16 15:59:58 +00:00
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
if (!baseUrl) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-04-01 21:52:38 +00:00
|
|
|
|
2021-11-16 15:59:58 +00:00
|
|
|
const onPress = async () => {
|
2022-04-01 21:52:38 +00:00
|
|
|
if (file.video_type && isTypeSupported(file.video_type) && showAttachment) {
|
2021-09-13 20:41:05 +00:00
|
|
|
return showAttachment(file);
|
|
|
|
}
|
2021-11-16 15:59:58 +00:00
|
|
|
|
2022-02-07 18:44:04 +00:00
|
|
|
if (!isIOS && file.video_url) {
|
2021-11-16 15:59:58 +00:00
|
|
|
const uri = formatAttachmentUrl(file.video_url, user.id, user.token, baseUrl);
|
|
|
|
await downloadVideo(uri);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
EventEmitter.emit(LISTENER, { message: I18n.t('Unsupported_format') });
|
|
|
|
};
|
|
|
|
|
|
|
|
const downloadVideo = async (uri: string) => {
|
|
|
|
setLoading(true);
|
|
|
|
const fileDownloaded = await fileDownload(uri, file);
|
|
|
|
setLoading(false);
|
|
|
|
|
|
|
|
if (fileDownloaded) {
|
|
|
|
EventEmitter.emit(LISTENER, { message: I18n.t('saved_to_gallery') });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
EventEmitter.emit(LISTENER, { message: I18n.t('error-save-video') });
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Markdown
|
|
|
|
msg={file.description}
|
|
|
|
baseUrl={baseUrl}
|
|
|
|
username={user.username}
|
|
|
|
getCustomEmoji={getCustomEmoji}
|
2022-03-21 20:44:06 +00:00
|
|
|
style={[isReply && style]}
|
2021-09-13 20:41:05 +00:00
|
|
|
theme={theme}
|
|
|
|
/>
|
2022-03-21 20:44:06 +00:00
|
|
|
<Touchable
|
|
|
|
disabled={isReply}
|
|
|
|
onPress={onPress}
|
|
|
|
style={[styles.button, { backgroundColor: themes[theme].videoBackground }]}
|
|
|
|
background={Touchable.Ripple(themes[theme].bannerBackground)}>
|
|
|
|
{loading ? <RCActivityIndicator /> : <CustomIcon name='play-filled' size={54} color={themes[theme].buttonText} />}
|
|
|
|
</Touchable>
|
2021-09-13 20:41:05 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
},
|
2022-04-01 21:52:38 +00:00
|
|
|
(prevProps, nextProps) => dequal(prevProps.file, nextProps.file)
|
2021-09-13 20:41:05 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
export default Video;
|