-
Notifications
You must be signed in to change notification settings - Fork 356
fix: multiple image upload issue using native image picker and generic improvements for upload #2638
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: multiple image upload issue using native image picker and generic improvements for upload #2638
Changes from 2 commits
ef559fa
4c058e2
3263159
96e00d4
7fe5302
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -49,7 +49,7 @@ import { | |
| useTranslationContext, | ||
| } from '../../contexts/translationContext/TranslationContext'; | ||
|
|
||
| import { triggerHaptic } from '../../native'; | ||
| import { isImageMediaLibraryAvailable, triggerHaptic } from '../../native'; | ||
| import type { Asset, DefaultStreamChatGenerics } from '../../types/types'; | ||
| import { AutoCompleteInput } from '../AutoCompleteInput/AutoCompleteInput'; | ||
|
|
||
|
|
@@ -118,7 +118,6 @@ type MessageInputPropsWithContext< | |
| | 'FileUploadPreview' | ||
| | 'fileUploads' | ||
| | 'giphyActive' | ||
| | 'hasImagePicker' | ||
| | 'ImageUploadPreview' | ||
| | 'imageUploads' | ||
| | 'Input' | ||
|
|
@@ -185,7 +184,6 @@ const MessageInputWithContext = < | |
| FileUploadPreview, | ||
| fileUploads, | ||
| giphyActive, | ||
| hasImagePicker, | ||
| ImageUploadPreview, | ||
| imageUploads, | ||
| Input, | ||
|
|
@@ -349,46 +347,71 @@ const MessageInputWithContext = < | |
| imagesToRemove.forEach((image) => removeImage(image.id)); | ||
| }; | ||
|
|
||
| const uploadFilesHandler = async () => { | ||
| const fileToUpload = selectedFiles.find((selectedFile) => { | ||
| const uploadedFile = fileUploads.find( | ||
| (fileUpload) => | ||
| fileUpload.file.uri === selectedFile.uri || fileUpload.url === selectedFile.uri, | ||
| ); | ||
| return !uploadedFile; | ||
| }); | ||
| if (fileToUpload) await uploadNewFile(fileToUpload); | ||
| }; | ||
|
|
||
| const removeFilesHandler = () => { | ||
| const filesToRemove = fileUploads.filter( | ||
| (fileUpload) => | ||
| !selectedFiles.find( | ||
| (selectedFile) => | ||
| selectedFile.uri === fileUpload.file.uri || selectedFile.uri === fileUpload.url, | ||
| ), | ||
| ); | ||
| filesToRemove.forEach((file) => removeFile(file.id)); | ||
| }; | ||
|
|
||
| /** | ||
| * When a user selects or deselects an image in the image picker using media library. | ||
| */ | ||
| useEffect(() => { | ||
| if (imagesForInput) { | ||
| if (selectedImagesLength > imageUploadsLength) { | ||
| /** User selected an image in bottom sheet attachment picker */ | ||
| uploadImagesHandler(); | ||
| } else { | ||
| /** User de-selected an image in bottom sheet attachment picker */ | ||
| removeImagesHandler(); | ||
| const uploadOrRemoveImage = async () => { | ||
| if (imagesForInput) { | ||
| if (selectedImagesLength > imageUploadsLength) { | ||
| /** User selected an image in bottom sheet attachment picker */ | ||
| await uploadImagesHandler(); | ||
| } else { | ||
| /** User de-selected an image in bottom sheet attachment picker */ | ||
| removeImagesHandler(); | ||
| } | ||
| } | ||
| } | ||
| }; | ||
| // If image picker is not available, don't do anything | ||
| if (!isImageMediaLibraryAvailable()) return; | ||
| uploadOrRemoveImage(); | ||
| // eslint-disable-next-line react-hooks/exhaustive-deps | ||
| }, [selectedImagesLength]); | ||
khushal87 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * When a user selects or deselects a video in the image picker using media library. | ||
| */ | ||
| useEffect(() => { | ||
| if (selectedFilesLength > fileUploadsLength) { | ||
| /** User selected a video in bottom sheet attachment picker */ | ||
| const fileToUpload = selectedFiles.find((selectedFile) => { | ||
| const uploadedFile = fileUploads.find( | ||
| (fileUpload) => | ||
| fileUpload.file.uri === selectedFile.uri || fileUpload.url === selectedFile.uri, | ||
| ); | ||
| return !uploadedFile; | ||
| }); | ||
| if (fileToUpload) uploadNewFile(fileToUpload); | ||
| } else { | ||
| /** User de-selected a video in bottom sheet attachment picker */ | ||
| const filesToRemove = fileUploads.filter( | ||
| (fileUpload) => | ||
| !selectedFiles.find( | ||
| (selectedFile) => | ||
| selectedFile.uri === fileUpload.file.uri || selectedFile.uri === fileUpload.url, | ||
| ), | ||
| ); | ||
| filesToRemove.forEach((file) => removeFile(file.id)); | ||
| } | ||
| const uploadOrRemoveFile = async () => { | ||
| if (selectedFilesLength > fileUploadsLength) { | ||
| /** User selected a video in bottom sheet attachment picker */ | ||
| await uploadFilesHandler(); | ||
| } else { | ||
| /** User de-selected a video in bottom sheet attachment picker */ | ||
| removeFilesHandler(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This also covers the case where |
||
| } | ||
| }; | ||
| uploadOrRemoveFile(); | ||
| // eslint-disable-next-line react-hooks/exhaustive-deps | ||
| }, [selectedFilesLength]); | ||
|
Comment on lines
407
to
408
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here |
||
|
|
||
| /** | ||
| * This is for image attachments selected from attachment picker. | ||
| */ | ||
| useEffect(() => { | ||
| if (imagesForInput && hasImagePicker) { | ||
| if (imagesForInput && isImageMediaLibraryAvailable()) { | ||
| if (imageUploadsLength < selectedImagesLength) { | ||
| // /** User removed some image from seleted images within ImageUploadPreview. */ | ||
| const updatedSelectedImages = selectedImages.filter((selectedImage) => { | ||
|
|
@@ -401,9 +424,7 @@ const MessageInputWithContext = < | |
| setSelectedImages(updatedSelectedImages); | ||
| } else if (imageUploadsLength > selectedImagesLength) { | ||
| /** | ||
| * User is editing some message which contains image attachments OR | ||
| * image attachment is added from custom image picker (other than the default bottomsheet image picker) | ||
| * using `uploadNewImage` function from `MessageInputContext`. | ||
| * User is editing some message which contains image attachments. | ||
| **/ | ||
| setSelectedImages( | ||
| imageUploads | ||
|
|
@@ -418,10 +439,13 @@ const MessageInputWithContext = < | |
| } | ||
| } | ||
| // eslint-disable-next-line react-hooks/exhaustive-deps | ||
| }, [imageUploadsLength, hasImagePicker]); | ||
| }, [imageUploadsLength]); | ||
khushal87 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * This is for video attachments selected from attachment picker. | ||
| */ | ||
| useEffect(() => { | ||
| if (hasImagePicker) { | ||
| if (isImageMediaLibraryAvailable()) { | ||
| if (fileUploadsLength < selectedFilesLength) { | ||
| /** User removed some video from seleted files within ImageUploadPreview. */ | ||
| const updatedSelectedFiles = selectedFiles.filter((selectedFile) => { | ||
|
|
@@ -434,9 +458,7 @@ const MessageInputWithContext = < | |
| setSelectedFiles(updatedSelectedFiles); | ||
| } else if (fileUploadsLength > selectedFilesLength) { | ||
| /** | ||
| * User is editing some message which contains video attachments OR | ||
| * video attachment is added from custom image picker (other than the default bottom-sheet image picker) | ||
| * using `uploadNewFile` function from `MessageInputContext`. | ||
| * User is editing some message which contains video attachments. | ||
| **/ | ||
| setSelectedFiles( | ||
| fileUploads.map((fileUpload) => ({ | ||
|
|
@@ -450,9 +472,10 @@ const MessageInputWithContext = < | |
| } | ||
| } | ||
| // eslint-disable-next-line react-hooks/exhaustive-deps | ||
| }, [fileUploadsLength, hasImagePicker]); | ||
| }, [fileUploadsLength]); | ||
|
|
||
| const editingExists = !!editing; | ||
|
|
||
| useEffect(() => { | ||
| if (editing && inputBoxRef.current) { | ||
| inputBoxRef.current.focus(); | ||
|
|
@@ -1036,7 +1059,6 @@ export const MessageInput = < | |
| FileUploadPreview, | ||
| fileUploads, | ||
| giphyActive, | ||
| hasImagePicker, | ||
| ImageUploadPreview, | ||
| imageUploads, | ||
| Input, | ||
|
|
@@ -1117,7 +1139,6 @@ export const MessageInput = < | |
| FileUploadPreview, | ||
| fileUploads, | ||
| giphyActive, | ||
| hasImagePicker, | ||
| ImageUploadPreview, | ||
| imageUploads, | ||
| Input, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.