Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: crashes due to negative bottom sheet snap points #2473

Merged
merged 2 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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],
);

Expand Down
27 changes: 25 additions & 2 deletions package/src/components/ImageGallery/ImageGallery.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -132,6 +132,29 @@ type Props<StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamC
| 'autoPlayVideo'
>;

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(0, 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,
>(
Expand Down Expand Up @@ -685,7 +708,7 @@ export const ImageGallery = <
index={0}
onChange={(index: number) => setCurrentBottomSheetIndex(index)}
ref={bottomSheetModalRef}
snapPoints={imageGalleryGridSnapPoints || snapPoints}
snapPoints={normalizeSnapPoints(imageGalleryGridSnapPoints || snapPoints)}
>
<ImageGrid
closeGridView={closeGridView}
Expand Down