mattermost-mobile-upstream/app/screens/gallery/renderers/document/document.tsx
Elias Nahum b6fc85385e
Migrate from RNN to Expo router (#9402)
* Expo-Router partial migration

* Full migration to ExpoRouter

* i18n

* add tests

* Merge branch 'main' into expo-router

* align package-lock.json

* i18n

* feedback review

* feedback review

* android 16kb patch

* Refactored emoji picker keyboard using a state machine

* set android launch_screen in drawables instead of layout

* no conditional hooks

* remove duplicate call in test

* address feedback

* fix tests and address more feedback

* small corrections

* small corrections

* small corrections

* fix draft test

* fix draft test 2

* remove navigateToScreen in draff test

* fix nit in draft test

* remove doc file

* Feedback review

* fix snapshots

* improve test coverage

* fix i18n

* Upgrade t o RN 0.83, other dependencies and new architecture (#9737)

* library hardware-keyboard android new arch support fix

* library rnshare android new arch support fix

* library rnutils android new arch support fix

* library secure-pdf-viewer add new arch support

* Enable new arch on iOS and Android

* update emm and network client with new-arch

* update intune to use new-arch

* performance improvements

* fix tests

* fixes post input on new-arch

* fix android emoji picker

* remove doc

* run tests in parallel

* upgrade to React Native 0.83.9, Expo 55, React 19, and New Architecture

Bumps core dependencies to their latest versions:
- React Native 0.77.3 → 0.83.9 (New Architecture enabled)
- React 18.3.1 → 19.2.5
- Expo SDK 52 → 55
- TypeScript 5.8.3 → 6.0.3
- Node.js 22.16.0 → 24.15.0
- react-native-reanimated 3.17.3 → 4.3.0
- Various other dependency upgrades

Migrates iOS AppDelegate from Objective-C to Swift, updates Android
build tooling and Gradle wrapper, refreshes all patches and font
assets, and adapts app code, tests, and CI workflows throughout for
API changes introduced by these upgrades.

* remove unecessary dismissKeyboard in notifications

* feedback review

* update intune to use new-arch

* update @mattermost deps

* missing i18n string

* additional fixes

* update fastlane
2026-05-13 17:44:55 +08:00

171 lines
6.1 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import React, {useCallback, useMemo, useState} from 'react';
import {defineMessages, useIntl} from 'react-intl';
import {DeviceEventEmitter, StyleSheet, Text, View} from 'react-native';
import {RectButton, Pressable} from 'react-native-gesture-handler';
import Animated from 'react-native-reanimated';
import FileIcon from '@components/files/file_icon';
import {Events, Preferences} from '@constants';
import {useServerUrl} from '@context/server';
import SecurityManager from '@managers/security_manager';
import DownloadWithAction from '@screens/gallery/footer/download_with_action';
import {buttonBackgroundStyle, buttonTextStyle} from '@utils/buttonStyles';
import {isDocument, isPdf} from '@utils/file';
import {galleryItemToFileInfo} from '@utils/gallery';
import {changeOpacity} from '@utils/theme';
import {typography} from '@utils/typography';
import type {GalleryAction, GalleryItemType} from '@typings/screens/gallery';
type Props = {
canDownloadFiles: boolean;
enableSecureFilePreview: boolean;
item: GalleryItemType;
hideHeaderAndFooter: (hide?: boolean) => void;
width: number;
height: number;
}
const styles = StyleSheet.create({
container: {
alignItems: 'center',
justifyContent: 'center',
flex: 1,
maxWidth: 600,
},
filename: {
color: '#FFF',
...typography('Body', 200, 'SemiBold'),
marginVertical: 8,
paddingHorizontal: 25,
textAlign: 'center',
},
unsupported: {
color: '#FFF',
...typography('Body', 100, 'SemiBold'),
marginTop: 10,
paddingHorizontal: 25,
opacity: 0.64,
textAlign: 'center',
},
});
const messages = defineMessages({
unsupported: {
id: 'gallery.unsupported',
defaultMessage: "Preview isn't supported for this file type. Try downloading or sharing to open it in another app.",
},
unsupportedAndBlockedDownload: {
id: 'gallery.unsupported_and_blocked_download',
defaultMessage: "Preview isn't supported for this file type, and downloads are disabled by your administrator.",
},
openFile: {
id: 'gallery.open_file',
defaultMessage: 'Open file',
},
onlyPdf: {
id: 'gallery.only_pdf',
defaultMessage: 'Only PDF files are supported for secure file preview.',
},
});
const DocumentRenderer = ({canDownloadFiles, enableSecureFilePreview, item, hideHeaderAndFooter, width, height}: Props) => {
const {formatMessage} = useIntl();
const serverUrl = useServerUrl();
const file = useMemo(() => galleryItemToFileInfo(item), [item]);
const [enabled, setEnabled] = useState(true);
const isSupported = useMemo(() => isDocument(file), [file]);
const canOpenFile = useMemo(() => {
if (!isSupported) {
return false;
}
if (enableSecureFilePreview && isPdf(file)) {
return true;
}
return !enableSecureFilePreview && canDownloadFiles;
}, [canDownloadFiles, enableSecureFilePreview, file, isSupported]);
const optionText = useMemo(() => {
const allowSaveToLocation = SecurityManager.canSaveToLocation(serverUrl, 'FilesApp');
if (enableSecureFilePreview && !isPdf(file)) {
return formatMessage(messages.onlyPdf);
} else if (!isSupported && allowSaveToLocation) {
return formatMessage(messages.unsupported);
} else if (!isSupported && !allowSaveToLocation) {
return formatMessage(messages.unsupportedAndBlockedDownload);
}
return formatMessage(messages.openFile);
}, [enableSecureFilePreview, file, formatMessage, isSupported, serverUrl]);
const setGalleryAction = useCallback((action: GalleryAction) => {
DeviceEventEmitter.emit(Events.GALLERY_ACTIONS, action);
if (action === 'none') {
setEnabled(true);
}
}, []);
const handleOpenFile = useCallback(() => {
setEnabled(false);
}, []);
const handlePdfPreview = useCallback(() => {
if (enableSecureFilePreview && isPdf(file)) {
return;
}
hideHeaderAndFooter();
}, [file, enableSecureFilePreview, hideHeaderAndFooter]);
return (
<>
<Pressable onPress={handlePdfPreview}>
<Animated.View style={[styles.container, {width, height}]}>
<FileIcon
backgroundColor='transparent'
file={file}
iconSize={120}
/>
<Text
numberOfLines={2}
style={styles.filename}
>
{item.name}
</Text>
{!isSupported &&
<Text style={styles.unsupported}>{optionText}</Text>
}
{canOpenFile &&
<View style={{marginTop: 16}}>
<RectButton
enabled={enabled}
exclusive={true}
onPress={handleOpenFile}
rippleColor={changeOpacity('#fff', 0.16)}
>
<View style={buttonBackgroundStyle(Preferences.THEMES.onyx, 'lg', 'primary', enabled ? 'default' : 'disabled')}>
<Text style={buttonTextStyle(Preferences.THEMES.onyx, 'lg', 'primary', enabled ? 'default' : 'disabled')} >{optionText}</Text>
</View>
</RectButton>
</View>
}
</Animated.View>
</Pressable>
{!enabled &&
<DownloadWithAction
action='opening'
enableSecureFilePreview={enableSecureFilePreview}
setAction={setGalleryAction}
item={item}
onDownloadSuccess={handlePdfPreview}
/>
}
</>
);
};
export default DocumentRenderer;