diff --git a/app/components/files/audio_file.tsx b/app/components/files/audio_file.tsx index 413516f1239..6cf9c4b8cb9 100644 --- a/app/components/files/audio_file.tsx +++ b/app/components/files/audio_file.tsx @@ -9,28 +9,17 @@ import { TouchableOpacity, Text, } from 'react-native'; -import FileViewer from 'react-native-file-viewer'; -import FileSystem from 'react-native-fs'; import Video, {type OnLoadData, type OnProgressData} from 'react-native-video'; -import {DOWNLOAD_TIMEOUT} from '@constants/network'; -import {useServerUrl} from '@context/server'; import {useTheme} from '@context/theme'; -import NetworkManager from '@managers/network_manager'; -import {alertDownloadDocumentDisabled, alertDownloadFailed, alertFailedToOpenDocument} from '@utils/document'; -import {getFullErrorMessage, isErrorWithMessage} from '@utils/errors'; -import {fileExists, getLocalFilePathFromFile} from '@utils/file'; -import {emptyFunction} from '@utils/general'; -import {logDebug} from '@utils/log'; +import {useDownloadFileAndPreview} from '@hooks/files'; +import {alertDownloadDocumentDisabled} from '@utils/document'; import {changeOpacity, makeStyleSheetFromTheme} from '@utils/theme'; import {typography} from '@utils/typography'; import CompassIcon from '../compass_icon'; import ProgressBar from '../progress_bar'; -import type {Client} from '@client/rest'; -import type {ClientResponse, ProgressPromise} from '@mattermost/react-native-network-client'; - type Props = { file: FileInfo; canDownloadFiles: boolean; @@ -70,7 +59,6 @@ const getStyleSheet = makeStyleSheetFromTheme((theme: Theme) => ({ })); const AudioFile = ({file, canDownloadFiles}: Props) => { - const serverUrl = useServerUrl(); const intl = useIntl(); const theme = useTheme(); const style = getStyleSheet(theme); @@ -82,105 +70,19 @@ const AudioFile = ({file, canDownloadFiles}: Props) => { const source = useMemo(() => ({uri: file.uri}), [file.uri]); - const [preview, setPreview] = useState(false); - const [didCancel, setDidCancel] = useState(false); - const [downloading, setDownloading] = useState(false); - const [downloadProgress, setDownloadProgress] = useState(0); - let client: Client | undefined; - try { - client = NetworkManager.getClient(serverUrl); - } catch { - // do nothing - } - const downloadTask = useRef>(); + const {toggleDownloadAndPreview} = useDownloadFileAndPreview(); const onPlayPress = () => { setHasPaused(!hasPaused); }; - const openDocument = () => { - if (!didCancel && !preview) { - const path = getLocalFilePathFromFile(serverUrl, file); - setPreview(true); - FileViewer.open(path!, { - displayName: file.name, - onDismiss: onDonePreviewingFile, - showOpenWithDialog: true, - showAppsSuggestions: true, - }).then(() => { - setDownloading(false); - setDownloadProgress(0); - }).catch(() => { - alertFailedToOpenDocument(file, intl); - onDonePreviewingFile(); - - if (path) { - FileSystem.unlink(path).catch(emptyFunction); - } - }); - } - }; - - const onDonePreviewingFile = () => { - setDownloadProgress(0); - setDownloading(false); - setPreview(false); - }; - - const cancelDownload = () => { - setDidCancel(true); - if (downloadTask.current?.cancel) { - downloadTask.current.cancel(); - } - }; - - const downloadAndPreviewFile = async () => { - setDidCancel(false); - let path; - - try { - path = getLocalFilePathFromFile(serverUrl, file); - const exists = await fileExists(path); - if (exists) { - openDocument(); - } else { - setDownloading(true); - downloadTask.current = client?.apiClient.download(client?.getFileRoute(file.id!), path!.replace('file://', ''), {timeoutInterval: DOWNLOAD_TIMEOUT}); - downloadTask.current?.progress?.(setDownloadProgress); - - await downloadTask.current; - setDownloadProgress(1); - openDocument(); - } - } catch (error) { - if (path) { - FileSystem.unlink(path).catch(emptyFunction); - } - setDownloading(false); - setDownloadProgress(0); - - if (!isErrorWithMessage(error) || error.message !== 'cancelled') { - logDebug('error on downloadAndPreviewFile', getFullErrorMessage(error)); - alertDownloadFailed(intl); - } - } - }; - const onDownloadPress = async () => { if (!canDownloadFiles) { alertDownloadDocumentDisabled(intl); return; } - if (downloading && progress < 1) { - cancelDownload(); - } else if (downloading) { - setDownloadProgress(0); - setDidCancel(true); - setDownloading(false); - } else { - downloadAndPreviewFile(); - } + toggleDownloadAndPreview(file); }; const loadTimeInMinutes = throttle((timeInSeconds: number) => { @@ -226,11 +128,6 @@ const AudioFile = ({file, canDownloadFiles}: Props) => { size={24} style={style.playIcon} /> - -