|
| 1 | +import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react'; |
| 2 | +import { FlatList, LayoutChangeEvent, StyleSheet, View } from 'react-native'; |
| 3 | + |
| 4 | +import { |
| 5 | + isLocalAudioAttachment, |
| 6 | + isLocalFileAttachment, |
| 7 | + isLocalImageAttachment, |
| 8 | + isLocalVoiceRecordingAttachment, |
| 9 | + isVideoAttachment, |
| 10 | + LocalAttachment, |
| 11 | + LocalImageAttachment, |
| 12 | +} from 'stream-chat'; |
| 13 | + |
| 14 | +import { useAudioPreviewManager } from './hooks/useAudioPreviewManager'; |
| 15 | + |
| 16 | +import { useMessageComposer } from '../../contexts'; |
| 17 | +import { useAttachmentManagerState } from '../../contexts/messageInputContext/hooks/useAttachmentManagerState'; |
| 18 | +import { |
| 19 | + MessageInputContextValue, |
| 20 | + useMessageInputContext, |
| 21 | +} from '../../contexts/messageInputContext/MessageInputContext'; |
| 22 | +import { useTheme } from '../../contexts/themeContext/ThemeContext'; |
| 23 | +import { isSoundPackageAvailable } from '../../native'; |
| 24 | + |
| 25 | +const IMAGE_PREVIEW_SIZE = 100; |
| 26 | +const FILE_PREVIEW_HEIGHT = 60; |
| 27 | + |
| 28 | +export type AttachmentUploadPreviewListPropsWithContext = Pick< |
| 29 | + MessageInputContextValue, |
| 30 | + | 'AudioAttachmentUploadPreview' |
| 31 | + | 'FileAttachmentUploadPreview' |
| 32 | + | 'ImageAttachmentUploadPreview' |
| 33 | + | 'VideoAttachmentUploadPreview' |
| 34 | +>; |
| 35 | + |
| 36 | +/** |
| 37 | + * AttachmentUploadPreviewList |
| 38 | + * UI Component to preview the files set for upload |
| 39 | + */ |
| 40 | +const UnMemoizedAttachmentUploadListPreview = ( |
| 41 | + props: AttachmentUploadPreviewListPropsWithContext, |
| 42 | +) => { |
| 43 | + const [flatListWidth, setFlatListWidth] = useState(0); |
| 44 | + const flatListRef = useRef<FlatList<LocalAttachment> | null>(null); |
| 45 | + const { |
| 46 | + AudioAttachmentUploadPreview, |
| 47 | + FileAttachmentUploadPreview, |
| 48 | + ImageAttachmentUploadPreview, |
| 49 | + VideoAttachmentUploadPreview, |
| 50 | + } = props; |
| 51 | + const { attachmentManager } = useMessageComposer(); |
| 52 | + const { attachments } = useAttachmentManagerState(); |
| 53 | + const { |
| 54 | + theme: { |
| 55 | + colors: { grey_whisper }, |
| 56 | + messageInput: { |
| 57 | + attachmentSeparator, |
| 58 | + attachmentUploadPreviewList: { filesFlatList, imagesFlatList, wrapper }, |
| 59 | + }, |
| 60 | + }, |
| 61 | + } = useTheme(); |
| 62 | + |
| 63 | + const imageUploads = attachments.filter((attachment) => isLocalImageAttachment(attachment)); |
| 64 | + const fileUploads = useMemo(() => { |
| 65 | + return attachments.filter((attachment) => !isLocalImageAttachment(attachment)); |
| 66 | + }, [attachments]); |
| 67 | + const audioUploads = useMemo(() => { |
| 68 | + return fileUploads.filter( |
| 69 | + (attachment) => |
| 70 | + isLocalAudioAttachment(attachment) || isLocalVoiceRecordingAttachment(attachment), |
| 71 | + ); |
| 72 | + }, [fileUploads]); |
| 73 | + |
| 74 | + const { audioAttachmentsStateMap, onLoad, onProgress, onPlayPause } = |
| 75 | + useAudioPreviewManager(audioUploads); |
| 76 | + |
| 77 | + const renderImageItem = useCallback( |
| 78 | + ({ item }: { item: LocalImageAttachment }) => { |
| 79 | + return ( |
| 80 | + <ImageAttachmentUploadPreview |
| 81 | + attachment={item} |
| 82 | + handleRetry={attachmentManager.uploadAttachment} |
| 83 | + removeAttachments={attachmentManager.removeAttachments} |
| 84 | + /> |
| 85 | + ); |
| 86 | + }, |
| 87 | + [ |
| 88 | + ImageAttachmentUploadPreview, |
| 89 | + attachmentManager.removeAttachments, |
| 90 | + attachmentManager.uploadAttachment, |
| 91 | + ], |
| 92 | + ); |
| 93 | + |
| 94 | + const renderFileItem = useCallback( |
| 95 | + ({ item }: { item: LocalAttachment }) => { |
| 96 | + if (isLocalImageAttachment(item)) { |
| 97 | + // This is already handled in the `renderImageItem` above, so we return null here to avoid duplication. |
| 98 | + return null; |
| 99 | + } else if (isLocalVoiceRecordingAttachment(item)) { |
| 100 | + return ( |
| 101 | + <AudioAttachmentUploadPreview |
| 102 | + attachment={item} |
| 103 | + audioAttachmentConfig={audioAttachmentsStateMap[item.localMetadata.id]} |
| 104 | + handleRetry={attachmentManager.uploadAttachment} |
| 105 | + onLoad={onLoad} |
| 106 | + onPlayPause={onPlayPause} |
| 107 | + onProgress={onProgress} |
| 108 | + removeAttachments={attachmentManager.removeAttachments} |
| 109 | + /> |
| 110 | + ); |
| 111 | + } else if (isLocalAudioAttachment(item)) { |
| 112 | + if (isSoundPackageAvailable()) { |
| 113 | + return ( |
| 114 | + <AudioAttachmentUploadPreview |
| 115 | + attachment={item} |
| 116 | + audioAttachmentConfig={audioAttachmentsStateMap[item.localMetadata.id]} |
| 117 | + handleRetry={attachmentManager.uploadAttachment} |
| 118 | + onLoad={onLoad} |
| 119 | + onPlayPause={onPlayPause} |
| 120 | + onProgress={onProgress} |
| 121 | + removeAttachments={attachmentManager.removeAttachments} |
| 122 | + /> |
| 123 | + ); |
| 124 | + } else { |
| 125 | + return ( |
| 126 | + <FileAttachmentUploadPreview |
| 127 | + attachment={item} |
| 128 | + flatListWidth={flatListWidth} |
| 129 | + handleRetry={attachmentManager.uploadAttachment} |
| 130 | + removeAttachments={attachmentManager.removeAttachments} |
| 131 | + /> |
| 132 | + ); |
| 133 | + } |
| 134 | + } else if (isVideoAttachment(item)) { |
| 135 | + return ( |
| 136 | + <VideoAttachmentUploadPreview |
| 137 | + attachment={item} |
| 138 | + flatListWidth={flatListWidth} |
| 139 | + handleRetry={attachmentManager.uploadAttachment} |
| 140 | + removeAttachments={attachmentManager.removeAttachments} |
| 141 | + /> |
| 142 | + ); |
| 143 | + } else if (isLocalFileAttachment(item)) { |
| 144 | + return ( |
| 145 | + <FileAttachmentUploadPreview |
| 146 | + attachment={item} |
| 147 | + flatListWidth={flatListWidth} |
| 148 | + handleRetry={attachmentManager.uploadAttachment} |
| 149 | + removeAttachments={attachmentManager.removeAttachments} |
| 150 | + /> |
| 151 | + ); |
| 152 | + } else return null; |
| 153 | + }, |
| 154 | + [ |
| 155 | + AudioAttachmentUploadPreview, |
| 156 | + FileAttachmentUploadPreview, |
| 157 | + VideoAttachmentUploadPreview, |
| 158 | + attachmentManager.removeAttachments, |
| 159 | + attachmentManager.uploadAttachment, |
| 160 | + audioAttachmentsStateMap, |
| 161 | + flatListWidth, |
| 162 | + onLoad, |
| 163 | + onPlayPause, |
| 164 | + onProgress, |
| 165 | + ], |
| 166 | + ); |
| 167 | + |
| 168 | + useEffect(() => { |
| 169 | + if (fileUploads.length && flatListRef.current) { |
| 170 | + setTimeout(() => flatListRef.current?.scrollToEnd(), 1); |
| 171 | + } |
| 172 | + }, [fileUploads.length]); |
| 173 | + |
| 174 | + const onLayout = useCallback( |
| 175 | + (event: LayoutChangeEvent) => { |
| 176 | + if (flatListRef.current) { |
| 177 | + setFlatListWidth(event.nativeEvent.layout.width); |
| 178 | + } |
| 179 | + }, |
| 180 | + [flatListRef], |
| 181 | + ); |
| 182 | + |
| 183 | + if (!attachments.length) { |
| 184 | + return null; |
| 185 | + } |
| 186 | + |
| 187 | + return ( |
| 188 | + <View style={[wrapper]}> |
| 189 | + {imageUploads.length ? ( |
| 190 | + <FlatList |
| 191 | + data={imageUploads} |
| 192 | + getItemLayout={(_, index) => ({ |
| 193 | + index, |
| 194 | + length: IMAGE_PREVIEW_SIZE + 8, |
| 195 | + offset: (IMAGE_PREVIEW_SIZE + 8) * index, |
| 196 | + })} |
| 197 | + horizontal |
| 198 | + keyExtractor={(item) => item.localMetadata.id} |
| 199 | + renderItem={renderImageItem} |
| 200 | + style={[styles.imagesFlatList, imagesFlatList]} |
| 201 | + /> |
| 202 | + ) : null} |
| 203 | + {imageUploads.length && fileUploads.length ? ( |
| 204 | + <View |
| 205 | + style={[ |
| 206 | + styles.attachmentSeparator, |
| 207 | + { |
| 208 | + borderBottomColor: grey_whisper, |
| 209 | + }, |
| 210 | + attachmentSeparator, |
| 211 | + ]} |
| 212 | + /> |
| 213 | + ) : null} |
| 214 | + {fileUploads.length ? ( |
| 215 | + <FlatList |
| 216 | + data={fileUploads} |
| 217 | + getItemLayout={(_, index) => ({ |
| 218 | + index, |
| 219 | + length: FILE_PREVIEW_HEIGHT + 8, |
| 220 | + offset: (FILE_PREVIEW_HEIGHT + 8) * index, |
| 221 | + })} |
| 222 | + keyExtractor={(item) => item.localMetadata.id} |
| 223 | + onLayout={onLayout} |
| 224 | + ref={flatListRef} |
| 225 | + renderItem={renderFileItem} |
| 226 | + style={[styles.filesFlatList, filesFlatList]} |
| 227 | + testID={'file-upload-preview'} |
| 228 | + /> |
| 229 | + ) : null} |
| 230 | + </View> |
| 231 | + ); |
| 232 | +}; |
| 233 | + |
| 234 | +export type AttachmentUploadPreviewListProps = Partial<AttachmentUploadPreviewListPropsWithContext>; |
| 235 | + |
| 236 | +const MemoizedAttachmentUploadPreviewListWithContext = React.memo( |
| 237 | + UnMemoizedAttachmentUploadListPreview, |
| 238 | +); |
| 239 | + |
| 240 | +/** |
| 241 | + * AttachmentUploadPreviewList |
| 242 | + * UI Component to preview the files set for upload |
| 243 | + */ |
| 244 | +export const AttachmentUploadPreviewList = (props: AttachmentUploadPreviewListProps) => { |
| 245 | + const { |
| 246 | + AudioAttachmentUploadPreview, |
| 247 | + FileAttachmentUploadPreview, |
| 248 | + ImageAttachmentUploadPreview, |
| 249 | + VideoAttachmentUploadPreview, |
| 250 | + } = useMessageInputContext(); |
| 251 | + return ( |
| 252 | + <MemoizedAttachmentUploadPreviewListWithContext |
| 253 | + {...{ |
| 254 | + AudioAttachmentUploadPreview, |
| 255 | + FileAttachmentUploadPreview, |
| 256 | + ImageAttachmentUploadPreview, |
| 257 | + VideoAttachmentUploadPreview, |
| 258 | + }} |
| 259 | + {...props} |
| 260 | + /> |
| 261 | + ); |
| 262 | +}; |
| 263 | + |
| 264 | +const styles = StyleSheet.create({ |
| 265 | + attachmentSeparator: { |
| 266 | + borderBottomWidth: 1, |
| 267 | + marginBottom: 10, |
| 268 | + }, |
| 269 | + filesFlatList: { marginBottom: 12, maxHeight: FILE_PREVIEW_HEIGHT * 2.5 + 16 }, |
| 270 | + imagesFlatList: { paddingBottom: 12 }, |
| 271 | +}); |
| 272 | + |
| 273 | +AttachmentUploadPreviewList.displayName = |
| 274 | + 'AttachmentUploadPreviewList{messageInput{attachmentUploadPreviewList}}'; |
0 commit comments