mattermost-mobile/app/components/files/file.tsx
Lucas Reis 8e854a8bdd
Implementing the Audio Playback/Download on the chat (#7900)
* feat: added new check for isAudio and added the supported mime types

* feat: adding the progress and audio on the audio file message

* feat: finishing the layout of the audio_file

* feat: play and pause audio

* feat: update the progress bar when audio is playing

* feat: update the timeframe of the audio

* feat: update with the new design

* feat: adding download and preview

* feat: creates a hook for the file download and preview

* feat: adding useCallback to make the return stable

* fix: iOS issue when playing in loop

* feat: adding localization for the error

* fix: removing code tha was inserted for debug

* feat: add a new line on the en.json

* fix: fixing types

* feat: adding the onSeek method inside the progress bar

* feat: changing progress value to animated value

* feat: changing to GestureDetector and making the seek method work

* feat: adding a touchable without feedback to prevent the audio to triggering other page

* feat: adding the drag on the seek method

* feat: making the download button more gray

* fix: fix tests

* fix: prevent onProgress from clearing the seconds when the audio is paused

* feat: clamping the position of the cursor to never be dragged offscreen

* feat: enhancing the experience of dragging the cursor on the progress bar

* feat: differentiate between the throttles

* feat: remvoing the aac audio files support

* feat: pausing if the focus has changed

* feat: render differently the audio file when in the files list

* refactor: optimize audio file component by using useCallback for event handlers

* refactor: extract stopPropagation function for better readability in AudioFile component

* feat: implement custom useThrottled hook and replace lodash throttle in AudioFile component

* refactor: update useThrottled hook to accept a generic callback type

* fix: loading of uri of audio files after merge

* feat: add audioFile style to enhance layout for audio files

* fix: tests
2025-02-19 11:17:30 +01:00

254 lines
7.7 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import React, {useCallback, useRef} from 'react';
import {View, TouchableWithoutFeedback} from 'react-native';
import Animated from 'react-native-reanimated';
import TouchableWithFeedback from '@components/touchable_with_feedback';
import {useTheme} from '@context/theme';
import {useGalleryItem} from '@hooks/gallery';
import {isAudio, isDocument, isImage, isVideo} from '@utils/file';
import {changeOpacity, makeStyleSheetFromTheme} from '@utils/theme';
import AudioFile from './audio_file';
import DocumentFile from './document_file';
import FileIcon from './file_icon';
import FileInfo from './file_info';
import FileOptionsIcon from './file_options_icon';
import ImageFile from './image_file';
import ImageFileOverlay from './image_file_overlay';
import VideoFile from './video_file';
import type {DocumentRef} from '@components/document';
type FileProps = {
canDownloadFiles: boolean;
file: FileInfo;
galleryIdentifier: string;
index: number;
inViewPort: boolean;
isSingleImage?: boolean;
nonVisibleImagesCount: number;
onPress: (index: number) => void;
publicLinkEnabled: boolean;
channelName?: string;
onOptionsPress?: (fileInfo: FileInfo) => void;
optionSelected?: boolean;
wrapperWidth?: number;
showDate?: boolean;
updateFileForGallery: (idx: number, file: FileInfo) => void;
asCard?: boolean;
isPressDisabled?: boolean;
};
const getStyleSheet = makeStyleSheetFromTheme((theme: Theme) => {
return {
fileWrapper: {
flex: 1,
flexDirection: 'row',
alignItems: 'center',
borderWidth: 1,
borderColor: changeOpacity(theme.centerChannelColor, 0.24),
borderRadius: 4,
},
iconWrapper: {
marginTop: 8,
marginRight: 7,
marginBottom: 8,
marginLeft: 6,
},
imageVideo: {
height: 40,
width: 40,
margin: 4,
},
audioFile: {
flex: 1,
flexDirection: 'row',
alignItems: 'center',
},
};
});
const File = ({
asCard = false,
canDownloadFiles,
channelName,
file,
galleryIdentifier,
inViewPort = false,
index,
isSingleImage = false,
nonVisibleImagesCount = 0,
onOptionsPress,
onPress,
optionSelected,
publicLinkEnabled,
showDate = false,
updateFileForGallery,
wrapperWidth = 300,
isPressDisabled = false,
}: FileProps) => {
const document = useRef<DocumentRef>(null);
const theme = useTheme();
const style = getStyleSheet(theme);
const handlePreviewPress = useCallback(() => {
if (document.current) {
document.current.handlePreviewPress();
} else {
onPress(index);
}
}, [index, onPress]);
const {styles, onGestureEvent, ref} = useGalleryItem(galleryIdentifier, index, handlePreviewPress);
const handleOnOptionsPress = useCallback(() => {
onOptionsPress?.(file);
}, [file, onOptionsPress]);
const renderCardWithImage = (fileIcon: JSX.Element) => {
const fileInfo = (
<FileInfo
disabled={isPressDisabled}
file={file}
showDate={showDate}
channelName={channelName}
onPress={handlePreviewPress}
/>
);
return (
<View style={[style.fileWrapper]}>
<View style={style.iconWrapper}>
{fileIcon}
</View>
{fileInfo}
{onOptionsPress &&
<FileOptionsIcon
onPress={handleOnOptionsPress}
selected={optionSelected}
/>
}
</View>
);
};
const touchableWithPreview = (
<TouchableWithFeedback
onPress={handlePreviewPress}
disabled={isPressDisabled}
type={'opacity'}
>
<FileIcon
file={file}
/>
</TouchableWithFeedback>
);
let fileComponent;
if (isVideo(file) && publicLinkEnabled) {
const renderVideoFile = (
<TouchableWithoutFeedback
disabled={isPressDisabled}
onPress={onGestureEvent}
>
<Animated.View style={[styles, asCard ? style.imageVideo : null]}>
<VideoFile
file={file}
forwardRef={ref}
inViewPort={inViewPort}
isSingleImage={isSingleImage}
contentFit={'cover'}
wrapperWidth={wrapperWidth}
updateFileForGallery={updateFileForGallery}
index={index}
/>
{Boolean(nonVisibleImagesCount) &&
<ImageFileOverlay
value={nonVisibleImagesCount}
/>
}
</Animated.View>
</TouchableWithoutFeedback>
);
fileComponent = asCard ? renderCardWithImage(renderVideoFile) : renderVideoFile;
} else if (isImage(file)) {
const renderImageFile = (
<TouchableWithoutFeedback
onPress={onGestureEvent}
disabled={isPressDisabled}
>
<Animated.View style={[styles, asCard ? style.imageVideo : null]}>
<ImageFile
file={file}
forwardRef={ref}
inViewPort={inViewPort}
isSingleImage={isSingleImage}
contentFit={'cover'}
wrapperWidth={wrapperWidth}
/>
{Boolean(nonVisibleImagesCount) &&
<ImageFileOverlay
value={nonVisibleImagesCount}
/>
}
</Animated.View>
</TouchableWithoutFeedback>
);
fileComponent = asCard ? renderCardWithImage(renderImageFile) : renderImageFile;
} else if (isDocument(file)) {
const renderDocumentFile = (
<View style={style.iconWrapper}>
<DocumentFile
ref={document}
canDownloadFiles={canDownloadFiles}
disabled={isPressDisabled}
file={file}
/>
</View>
);
const fileInfo = (
<FileInfo
disabled={isPressDisabled}
file={file}
showDate={showDate}
channelName={channelName}
onPress={handlePreviewPress}
/>
);
fileComponent = (
<View style={[style.fileWrapper]}>
{renderDocumentFile}
{fileInfo}
{onOptionsPress &&
<FileOptionsIcon
onPress={handleOnOptionsPress}
selected={optionSelected}
/>
}
</View>
);
} else if (isAudio(file)) {
const renderAudioFile = (
<Animated.View style={[styles, asCard ? style.imageVideo : style.audioFile]}>
<AudioFile
file={file}
canDownloadFiles={canDownloadFiles}
/>
</Animated.View>
);
fileComponent = asCard ? renderCardWithImage(touchableWithPreview) : renderAudioFile;
} else {
fileComponent = renderCardWithImage(touchableWithPreview);
}
return fileComponent;
};
export default File;