From aeabd52b4804deaed8c18078a195cf39a16bd8e7 Mon Sep 17 00:00:00 2001 From: Santhosh Vaiyapuri Date: Tue, 2 Apr 2024 17:00:08 +0200 Subject: [PATCH 1/2] fix: crashes due to negative bottom sheet snap points --- .../AttachmentPicker/AttachmentPicker.tsx | 2 +- .../components/ImageGallery/ImageGallery.tsx | 27 +++++++++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/package/src/components/AttachmentPicker/AttachmentPicker.tsx b/package/src/components/AttachmentPicker/AttachmentPicker.tsx index d3a549ab6c..de1fcfa126 100644 --- a/package/src/components/AttachmentPicker/AttachmentPicker.tsx +++ b/package/src/components/AttachmentPicker/AttachmentPicker.tsx @@ -292,7 +292,7 @@ export const AttachmentPicker = React.forwardRef( * this is an issue if you are calling close on the bottom sheet. */ const snapPoints = useMemo( - () => [initialSnapPoint, finalSnapPoint], + () => [Math.max(0, initialSnapPoint), Math.max(0, finalSnapPoint)], [initialSnapPoint, finalSnapPoint], ); diff --git a/package/src/components/ImageGallery/ImageGallery.tsx b/package/src/components/ImageGallery/ImageGallery.tsx index 959ea14c47..750f5d1954 100644 --- a/package/src/components/ImageGallery/ImageGallery.tsx +++ b/package/src/components/ImageGallery/ImageGallery.tsx @@ -26,7 +26,7 @@ import Animated, { withTiming, } from 'react-native-reanimated'; -import { BottomSheetModal, BottomSheetModalProvider } from '@gorhom/bottom-sheet'; +import { BottomSheetModal, BottomSheetModalProvider, BottomSheetProps } from '@gorhom/bottom-sheet'; import type { UserResponse } from 'stream-chat'; @@ -132,6 +132,29 @@ type Props; +type SnapPoints = BottomSheetProps['snapPoints']; + +const normalizeSnapPoints = (input: SnapPoints): SnapPoints => { + const snapPoints = input ? ('value' in input ? input.value : input) : []; + + return snapPoints.map((snapPoint) => { + if (typeof snapPoint === 'number') { + return Math.max(1, snapPoint); + } else { + const numericValue = Number(snapPoint.replace('%', '')); + const isPercentage = snapPoint.includes('%'); + + if (isNaN(numericValue)) { + return 0; + } else if (isPercentage) { + return `${Math.max(0, numericValue)}%`; + } else { + return Math.max(0, numericValue); + } + } + }); +}; + export const ImageGallery = < StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics, >( @@ -685,7 +708,7 @@ export const ImageGallery = < index={0} onChange={(index: number) => setCurrentBottomSheetIndex(index)} ref={bottomSheetModalRef} - snapPoints={imageGalleryGridSnapPoints || snapPoints} + snapPoints={normalizeSnapPoints(imageGalleryGridSnapPoints || snapPoints)} > Date: Tue, 2 Apr 2024 17:01:43 +0200 Subject: [PATCH 2/2] put 0 instead of 1 --- package/src/components/ImageGallery/ImageGallery.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package/src/components/ImageGallery/ImageGallery.tsx b/package/src/components/ImageGallery/ImageGallery.tsx index 750f5d1954..2db28a7276 100644 --- a/package/src/components/ImageGallery/ImageGallery.tsx +++ b/package/src/components/ImageGallery/ImageGallery.tsx @@ -139,7 +139,7 @@ const normalizeSnapPoints = (input: SnapPoints): SnapPoints => { return snapPoints.map((snapPoint) => { if (typeof snapPoint === 'number') { - return Math.max(1, snapPoint); + return Math.max(0, snapPoint); } else { const numericValue = Number(snapPoint.replace('%', '')); const isPercentage = snapPoint.includes('%');