|
| 1 | +import React, { useEffect, useState } from 'react'; |
| 2 | +import { StatusBar, StyleSheet, View } from 'react-native'; |
| 3 | +import { useSafeAreaInsets } from 'react-native-safe-area-context'; |
| 4 | + |
| 5 | +import { |
| 6 | + Icon, |
| 7 | + Image, |
| 8 | + LoadingSpinner, |
| 9 | + Text, |
| 10 | + createStyleSheet, |
| 11 | + useAlert, |
| 12 | + useHeaderStyle, |
| 13 | + useToast, |
| 14 | + useUIKitTheme, |
| 15 | +} from '@sendbird/uikit-react-native-foundation'; |
| 16 | +import type { SendbirdFileMessage } from '@sendbird/uikit-utils'; |
| 17 | +import { Logger, getFileExtension, getFileType, isMyMessage, toMegabyte, useIIFE } from '@sendbird/uikit-utils'; |
| 18 | + |
| 19 | +import { useLocalization, usePlatformService, useSendbirdChat } from '../hooks/useContext'; |
| 20 | +import SBUPressable from './SBUPressable'; |
| 21 | + |
| 22 | +type Props = { |
| 23 | + fileMessage: SendbirdFileMessage; |
| 24 | + deleteMessage: () => Promise<void>; |
| 25 | + |
| 26 | + onClose: () => void; |
| 27 | + onPressDownload?: (message: SendbirdFileMessage) => void; |
| 28 | + onPressDelete?: (message: SendbirdFileMessage) => void; |
| 29 | + |
| 30 | + headerShown?: boolean; |
| 31 | + headerTopInset?: number; |
| 32 | +}; |
| 33 | +const FileViewer = ({ |
| 34 | + headerShown = true, |
| 35 | + deleteMessage, |
| 36 | + headerTopInset, |
| 37 | + fileMessage, |
| 38 | + onPressDownload, |
| 39 | + onPressDelete, |
| 40 | + onClose, |
| 41 | +}: Props) => { |
| 42 | + const [loading, setLoading] = useState(true); |
| 43 | + |
| 44 | + const { bottom } = useSafeAreaInsets(); |
| 45 | + |
| 46 | + const { currentUser } = useSendbirdChat(); |
| 47 | + const { palette } = useUIKitTheme(); |
| 48 | + const { topInset, statusBarTranslucent, defaultHeight } = useHeaderStyle(); |
| 49 | + const { STRINGS } = useLocalization(); |
| 50 | + const { fileService, mediaService } = usePlatformService(); |
| 51 | + const toast = useToast(); |
| 52 | + const { alert } = useAlert(); |
| 53 | + |
| 54 | + const basicTopInset = statusBarTranslucent ? topInset : 0; |
| 55 | + const canDelete = isMyMessage(fileMessage, currentUser?.userId); |
| 56 | + const fileType = getFileType(fileMessage.type || getFileExtension(fileMessage.url)); |
| 57 | + |
| 58 | + useEffect(() => { |
| 59 | + if (!mediaService?.VideoComponent || fileType === 'file') { |
| 60 | + onClose(); |
| 61 | + } |
| 62 | + }, [mediaService]); |
| 63 | + |
| 64 | + const fileViewer = useIIFE(() => { |
| 65 | + switch (fileType) { |
| 66 | + case 'image': { |
| 67 | + return ( |
| 68 | + <Image |
| 69 | + source={{ uri: fileMessage.url }} |
| 70 | + style={StyleSheet.absoluteFill} |
| 71 | + resizeMode={'contain'} |
| 72 | + onLoadEnd={() => setLoading(false)} |
| 73 | + /> |
| 74 | + ); |
| 75 | + } |
| 76 | + |
| 77 | + case 'video': |
| 78 | + case 'audio': { |
| 79 | + if (!mediaService?.VideoComponent) return null; |
| 80 | + return ( |
| 81 | + <mediaService.VideoComponent |
| 82 | + source={{ uri: fileMessage.url }} |
| 83 | + style={[StyleSheet.absoluteFill, { top: basicTopInset + defaultHeight, bottom: defaultHeight + bottom }]} |
| 84 | + resizeMode={'contain'} |
| 85 | + onLoad={() => setLoading(false)} |
| 86 | + /> |
| 87 | + ); |
| 88 | + } |
| 89 | + |
| 90 | + default: { |
| 91 | + return null; |
| 92 | + } |
| 93 | + } |
| 94 | + }); |
| 95 | + |
| 96 | + const _onPressDelete = () => { |
| 97 | + if (!canDelete) return; |
| 98 | + |
| 99 | + if (onPressDelete) { |
| 100 | + onPressDelete(fileMessage); |
| 101 | + } else { |
| 102 | + alert({ |
| 103 | + title: STRINGS.GROUP_CHANNEL.DIALOG_MESSAGE_DELETE_CONFIRM_TITLE, |
| 104 | + buttons: [ |
| 105 | + { |
| 106 | + text: STRINGS.GROUP_CHANNEL.DIALOG_MESSAGE_DELETE_CONFIRM_CANCEL, |
| 107 | + }, |
| 108 | + { |
| 109 | + text: STRINGS.GROUP_CHANNEL.DIALOG_MESSAGE_DELETE_CONFIRM_OK, |
| 110 | + style: 'destructive', |
| 111 | + onPress: () => { |
| 112 | + deleteMessage() |
| 113 | + .then(() => { |
| 114 | + onClose(); |
| 115 | + }) |
| 116 | + .catch(() => { |
| 117 | + toast.show(STRINGS.TOAST.DELETE_MSG_ERROR, 'error'); |
| 118 | + }); |
| 119 | + }, |
| 120 | + }, |
| 121 | + ], |
| 122 | + }); |
| 123 | + } |
| 124 | + }; |
| 125 | + |
| 126 | + const _onPressDownload = () => { |
| 127 | + if (onPressDownload) { |
| 128 | + onPressDownload(fileMessage); |
| 129 | + } else { |
| 130 | + if (toMegabyte(fileMessage.size) > 4) { |
| 131 | + toast.show(STRINGS.TOAST.DOWNLOAD_START, 'success'); |
| 132 | + } |
| 133 | + |
| 134 | + fileService |
| 135 | + .save({ fileUrl: fileMessage.url, fileName: fileMessage.name, fileType: fileMessage.type }) |
| 136 | + .then((response) => { |
| 137 | + toast.show(STRINGS.TOAST.DOWNLOAD_OK, 'success'); |
| 138 | + Logger.log('File saved to', response); |
| 139 | + }) |
| 140 | + .catch((err) => { |
| 141 | + toast.show(STRINGS.TOAST.DOWNLOAD_ERROR, 'error'); |
| 142 | + Logger.log('File save failure', err); |
| 143 | + }); |
| 144 | + } |
| 145 | + }; |
| 146 | + |
| 147 | + return ( |
| 148 | + <View style={{ flex: 1, backgroundColor: palette.background700 }}> |
| 149 | + <StatusBar barStyle={'light-content'} animated /> |
| 150 | + <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}> |
| 151 | + {fileViewer} |
| 152 | + {loading && <LoadingSpinner style={{ position: 'absolute' }} size={40} color={palette.primary300} />} |
| 153 | + </View> |
| 154 | + {headerShown && ( |
| 155 | + <FileViewerHeader |
| 156 | + title={STRINGS.FILE_VIEWER.TITLE(fileMessage)} |
| 157 | + subtitle={STRINGS.FILE_VIEWER.SUBTITLE(fileMessage)} |
| 158 | + topInset={headerTopInset ?? basicTopInset} |
| 159 | + onClose={onClose} |
| 160 | + /> |
| 161 | + )} |
| 162 | + <FileViewerFooter |
| 163 | + bottomInset={bottom} |
| 164 | + deleteShown={canDelete} |
| 165 | + onPressDelete={_onPressDelete} |
| 166 | + onPressDownload={_onPressDownload} |
| 167 | + /> |
| 168 | + </View> |
| 169 | + ); |
| 170 | +}; |
| 171 | + |
| 172 | +type HeaderProps = { |
| 173 | + topInset: number; |
| 174 | + onClose: () => void; |
| 175 | + title: string; |
| 176 | + subtitle: string; |
| 177 | +}; |
| 178 | +const FileViewerHeader = ({ topInset, onClose, subtitle, title }: HeaderProps) => { |
| 179 | + const { palette } = useUIKitTheme(); |
| 180 | + const { defaultHeight } = useHeaderStyle(); |
| 181 | + const { left, right } = useSafeAreaInsets(); |
| 182 | + |
| 183 | + return ( |
| 184 | + <View |
| 185 | + style={[ |
| 186 | + styles.headerContainer, |
| 187 | + { |
| 188 | + paddingLeft: styles.headerContainer.paddingHorizontal + left, |
| 189 | + paddingRight: styles.headerContainer.paddingHorizontal + right, |
| 190 | + }, |
| 191 | + { paddingTop: topInset, height: defaultHeight + topInset, backgroundColor: palette.overlay01 }, |
| 192 | + ]} |
| 193 | + > |
| 194 | + <SBUPressable as={'TouchableOpacity'} onPress={onClose} style={styles.barButton}> |
| 195 | + <Icon icon={'close'} size={24} color={palette.onBackgroundDark01} /> |
| 196 | + </SBUPressable> |
| 197 | + <View style={styles.barTitleContainer}> |
| 198 | + <Text h2 color={palette.onBackgroundDark01} style={styles.headerTitle}> |
| 199 | + {title} |
| 200 | + </Text> |
| 201 | + <Text caption2 color={palette.onBackgroundDark01}> |
| 202 | + {subtitle} |
| 203 | + </Text> |
| 204 | + </View> |
| 205 | + <View style={styles.barButton} /> |
| 206 | + </View> |
| 207 | + ); |
| 208 | +}; |
| 209 | + |
| 210 | +type FooterProps = { |
| 211 | + bottomInset: number; |
| 212 | + deleteShown: boolean; |
| 213 | + onPressDelete: () => void; |
| 214 | + onPressDownload: () => void; |
| 215 | +}; |
| 216 | +const FileViewerFooter = ({ bottomInset, deleteShown, onPressDelete, onPressDownload }: FooterProps) => { |
| 217 | + const { palette } = useUIKitTheme(); |
| 218 | + const { defaultHeight } = useHeaderStyle(); |
| 219 | + const { left, right } = useSafeAreaInsets(); |
| 220 | + |
| 221 | + return ( |
| 222 | + <View |
| 223 | + style={[ |
| 224 | + styles.footerContainer, |
| 225 | + { |
| 226 | + paddingLeft: styles.headerContainer.paddingHorizontal + left, |
| 227 | + paddingRight: styles.headerContainer.paddingHorizontal + right, |
| 228 | + }, |
| 229 | + { |
| 230 | + paddingBottom: bottomInset, |
| 231 | + height: defaultHeight + bottomInset, |
| 232 | + backgroundColor: palette.overlay01, |
| 233 | + }, |
| 234 | + ]} |
| 235 | + > |
| 236 | + <SBUPressable as={'TouchableOpacity'} onPress={onPressDownload} style={styles.barButton}> |
| 237 | + <Icon icon={'download'} size={24} color={palette.onBackgroundDark01} /> |
| 238 | + </SBUPressable> |
| 239 | + <View style={styles.barTitleContainer} /> |
| 240 | + <SBUPressable as={'TouchableOpacity'} onPress={onPressDelete} style={styles.barButton} disabled={!deleteShown}> |
| 241 | + {deleteShown && <Icon icon={'delete'} size={24} color={palette.onBackgroundDark01} />} |
| 242 | + </SBUPressable> |
| 243 | + </View> |
| 244 | + ); |
| 245 | +}; |
| 246 | + |
| 247 | +const styles = createStyleSheet({ |
| 248 | + headerContainer: { |
| 249 | + top: 0, |
| 250 | + left: 0, |
| 251 | + right: 0, |
| 252 | + position: 'absolute', |
| 253 | + flexDirection: 'row', |
| 254 | + alignItems: 'center', |
| 255 | + justifyContent: 'center', |
| 256 | + paddingHorizontal: 12, |
| 257 | + }, |
| 258 | + barButton: { |
| 259 | + width: 32, |
| 260 | + height: 32, |
| 261 | + alignItems: 'center', |
| 262 | + justifyContent: 'center', |
| 263 | + }, |
| 264 | + barTitleContainer: { |
| 265 | + flex: 1, |
| 266 | + alignItems: 'center', |
| 267 | + justifyContent: 'center', |
| 268 | + }, |
| 269 | + headerTitle: { |
| 270 | + marginBottom: 2, |
| 271 | + }, |
| 272 | + footerContainer: { |
| 273 | + position: 'absolute', |
| 274 | + left: 0, |
| 275 | + right: 0, |
| 276 | + bottom: 0, |
| 277 | + flexDirection: 'row', |
| 278 | + alignItems: 'center', |
| 279 | + justifyContent: 'center', |
| 280 | + paddingHorizontal: 12, |
| 281 | + }, |
| 282 | +}); |
| 283 | + |
| 284 | +export default FileViewer; |
0 commit comments