1- import React , { useCallback , useEffect , useRef , useState } from 'react' ;
1+ import React , { useCallback , useEffect , useMemo , useRef , useState } from 'react' ;
22import { BackHandler , Keyboard , Platform , StyleSheet } from 'react-native' ;
33
44import BottomSheetOriginal from '@gorhom/bottom-sheet' ;
@@ -11,10 +11,8 @@ import type { AttachmentPickerErrorProps } from './components/AttachmentPickerEr
1111
1212import { renderAttachmentPickerItem } from './components/AttachmentPickerItem' ;
1313
14- import {
15- AttachmentPickerContextValue ,
16- useAttachmentPickerContext ,
17- } from '../../contexts/attachmentPickerContext/AttachmentPickerContext' ;
14+ import { useAttachmentPickerContext } from '../../contexts/attachmentPickerContext/AttachmentPickerContext' ;
15+ import { MessageInputContextValue } from '../../contexts/messageInputContext/MessageInputContext' ;
1816import { useTheme } from '../../contexts/themeContext/ThemeContext' ;
1917import { useScreenDimensions } from '../../hooks/useScreenDimensions' ;
2018import { NativeHandlers } from '../../native' ;
@@ -31,7 +29,7 @@ const styles = StyleSheet.create({
3129} ) ;
3230
3331export type AttachmentPickerProps = Pick <
34- AttachmentPickerContextValue ,
32+ MessageInputContextValue ,
3533 | 'AttachmentPickerBottomSheetHandle'
3634 | 'attachmentPickerBottomSheetHandleHeight'
3735 | 'attachmentSelectionBarHeight'
@@ -40,23 +38,27 @@ export type AttachmentPickerProps = Pick<
4038 /**
4139 * Custom UI component to render error component while opening attachment picker.
4240 *
43- * **Default** [AttachmentPickerError](https://github.com/GetStream/stream-chat-react-native/blob/main/package/src/components/AttachmentPicker/components/AttachmentPickerError.tsx)
41+ * **Default**
42+ * [AttachmentPickerError](https://github.com/GetStream/stream-chat-react-native/blob/main/package/src/components/AttachmentPicker/components/AttachmentPickerError.tsx)
4443 */
4544 AttachmentPickerError : React . ComponentType < AttachmentPickerErrorProps > ;
4645 /**
4746 * Custom UI component to render error image for attachment picker
4847 *
49- * **Default** [AttachmentPickerErrorImage](https://github.com/GetStream/stream-chat-react-native/blob/main/package/src/components/AttachmentPicker/components/AttachmentPickerErrorImage.tsx)
48+ * **Default**
49+ * [AttachmentPickerErrorImage](https://github.com/GetStream/stream-chat-react-native/blob/main/package/src/components/AttachmentPicker/components/AttachmentPickerErrorImage.tsx)
5050 */
5151 AttachmentPickerErrorImage : React . ComponentType ;
5252 /**
5353 * Custom UI Component to render select more photos for selected gallery access in iOS.
5454 */
5555 AttachmentPickerIOSSelectMorePhotos : React . ComponentType ;
5656 /**
57- * Custom UI component to render overlay component, that shows up on top of [selected image](https://github.com/GetStream/stream-chat-react-native/blob/main/screenshots/docs/1.png) (with tick mark)
57+ * Custom UI component to render overlay component, that shows up on top of [selected
58+ * image](https://github.com/GetStream/stream-chat-react-native/blob/main/screenshots/docs/1.png) (with tick mark)
5859 *
59- * **Default** [ImageOverlaySelectedComponent](https://github.com/GetStream/stream-chat-react-native/blob/main/package/src/components/AttachmentPicker/components/ImageOverlaySelectedComponent.tsx)
60+ * **Default**
61+ * [ImageOverlaySelectedComponent](https://github.com/GetStream/stream-chat-react-native/blob/main/package/src/components/AttachmentPicker/components/ImageOverlaySelectedComponent.tsx)
6062 */
6163 ImageOverlaySelectedComponent : React . ComponentType ;
6264 attachmentPickerErrorButtonText ?: string ;
@@ -87,17 +89,8 @@ export const AttachmentPicker = React.forwardRef(
8789 colors : { white } ,
8890 } ,
8991 } = useTheme ( ) ;
90- const {
91- closePicker,
92- maxNumberOfFiles,
93- selectedFiles,
94- selectedImages,
95- selectedPicker,
96- setSelectedFiles,
97- setSelectedImages,
98- setSelectedPicker,
99- topInset,
100- } = useAttachmentPickerContext ( ) ;
92+ const { closePicker, selectedPicker, setSelectedPicker, topInset } =
93+ useAttachmentPickerContext ( ) ;
10194 const { vh : screenVh } = useScreenDimensions ( ) ;
10295
10396 const fullScreenHeight = screenVh ( 100 ) ;
@@ -157,7 +150,8 @@ export const AttachmentPicker = React.forwardRef(
157150 if ( ! NativeHandlers . oniOS14GalleryLibrarySelectionChange ) {
158151 return ;
159152 }
160- // ios 14 library selection change event is fired when user reselects the images that are permitted to be readable by the app
153+ // ios 14 library selection change event is fired when user reselects the images that are permitted to be
154+ // readable by the app
161155 const { unsubscribe } = NativeHandlers . oniOS14GalleryLibrarySelectionChange ( ( ) => {
162156 // we reset the cursor and has next page to true to facilitate fetching of the first page of photos again
163157 hasNextPageRef . current = true ;
@@ -182,8 +176,7 @@ export const AttachmentPicker = React.forwardRef(
182176 const backHandler = BackHandler . addEventListener ( 'hardwareBackPress' , backAction ) ;
183177
184178 return ( ) => backHandler . remove ( ) ;
185- // eslint-disable-next-line react-hooks/exhaustive-deps
186- } , [ selectedPicker , closePicker ] ) ;
179+ } , [ selectedPicker , closePicker , setSelectedPicker ] ) ;
187180
188181 useEffect ( ( ) => {
189182 const onKeyboardOpenHandler = ( ) => {
@@ -207,8 +200,7 @@ export const AttachmentPicker = React.forwardRef(
207200 Keyboard . removeListener ( keyboardShowEvent , onKeyboardOpenHandler ) ;
208201 }
209202 } ;
210- // eslint-disable-next-line react-hooks/exhaustive-deps
211- } , [ closePicker , selectedPicker ] ) ;
203+ } , [ closePicker , selectedPicker , setSelectedPicker ] ) ;
212204
213205 useEffect ( ( ) => {
214206 if ( currentIndex < 0 ) {
@@ -232,25 +224,21 @@ export const AttachmentPicker = React.forwardRef(
232224 ! loadingPhotos
233225 ) {
234226 getMorePhotos ( ) ;
235- // we do this only once on open for avoiding to request permissions in rationale dialog again and again on Android
227+ // we do this only once on open for avoiding to request permissions in rationale dialog again and again on
228+ // Android
236229 attemptedToLoadPhotosOnOpenRef . current = true ;
237230 }
238231 } , [ currentIndex , selectedPicker , getMorePhotos , loadingPhotos ] ) ;
239232
240- const selectedPhotos = photos . map ( ( asset ) => ( {
241- asset,
242- ImageOverlaySelectedComponent,
243- maxNumberOfFiles,
244- numberOfAttachmentPickerImageColumns,
245- numberOfUploads : selectedFiles . length + selectedImages . length ,
246- selected :
247- selectedImages . some ( ( image ) => ( image ?. uri ? image . uri === asset . uri : false ) ) ||
248- selectedFiles . some ( ( file ) => ( file ?. uri ? file . uri === asset . uri : false ) ) ,
249- selectedFiles,
250- selectedImages,
251- setSelectedFiles,
252- setSelectedImages,
253- } ) ) ;
233+ const selectedPhotos = useMemo (
234+ ( ) =>
235+ photos . map ( ( asset ) => ( {
236+ asset,
237+ ImageOverlaySelectedComponent,
238+ numberOfAttachmentPickerImageColumns,
239+ } ) ) ,
240+ [ photos , ImageOverlaySelectedComponent , numberOfAttachmentPickerImageColumns ] ,
241+ ) ;
254242
255243 const handleHeight = attachmentPickerBottomSheetHandleHeight ;
256244
@@ -301,6 +289,7 @@ export const AttachmentPicker = React.forwardRef(
301289 numColumns = { numberOfAttachmentPickerImageColumns ?? 3 }
302290 onEndReached = { photoError ? undefined : getMorePhotos }
303291 renderItem = { renderAttachmentPickerItem }
292+ testID = { 'attachment-picker-list' }
304293 />
305294 </ BottomSheet >
306295 { selectedPicker === 'images' && photoError && (
0 commit comments