Skip to content

Commit 66a9b7e

Browse files
committed
fix: indexing in the animated image gallery image and video
1 parent dde2e11 commit 66a9b7e

File tree

3 files changed

+33
-27
lines changed

3 files changed

+33
-27
lines changed

package/src/components/ImageGallery/ImageGallery.tsx

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -364,11 +364,8 @@ export const ImageGalleryWithContext = (props: ImageGalleryWithContextProps) =>
364364
key={photo.id}
365365
offsetScale={offsetScale}
366366
photo={photo}
367-
previous={currentIndex > i}
368367
scale={scale}
369368
screenHeight={fullWindowHeight}
370-
selected={currentIndex === i}
371-
shouldRender={Math.abs(currentIndex - i) < 4}
372369
style={[
373370
{
374371
height: fullWindowHeight * 8,
@@ -387,12 +384,9 @@ export const ImageGalleryWithContext = (props: ImageGalleryWithContextProps) =>
387384
key={photo.id}
388385
offsetScale={offsetScale}
389386
photo={photo}
390-
previous={currentIndex > i}
391387
scale={scale}
392388
screenHeight={fullWindowHeight}
393389
screenWidth={fullWindowWidth}
394-
selected={currentIndex === i}
395-
shouldRender={Math.abs(currentIndex - i) < 4}
396390
style={[
397391
{
398392
height: fullWindowHeight * 8,

package/src/components/ImageGallery/components/AnimatedGalleryImage.tsx

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ import type { ImageStyle, StyleProp } from 'react-native';
44
import Animated, { SharedValue } from 'react-native-reanimated';
55

66
import { useChatConfigContext } from '../../../contexts/chatConfigContext/ChatConfigContext';
7-
import { ImageGalleryAsset } from '../../../state-store/image-gallery-state-store';
7+
import { useImageGalleryContext } from '../../../contexts/imageGalleryContext/ImageGalleryContext';
8+
import { useStateStore } from '../../../hooks';
9+
import {
10+
ImageGalleryAsset,
11+
ImageGalleryState,
12+
} from '../../../state-store/image-gallery-state-store';
813
import { getResizedImageUrl } from '../../../utils/getResizedImageUrl';
914
import { useAnimatedGalleryStyle } from '../hooks/useAnimatedGalleryStyle';
1015

@@ -15,35 +20,35 @@ type Props = {
1520
index: number;
1621
offsetScale: SharedValue<number>;
1722
photo: ImageGalleryAsset;
18-
previous: boolean;
1923
scale: SharedValue<number>;
2024
screenHeight: number;
2125
screenWidth: number;
22-
selected: boolean;
23-
shouldRender: boolean;
2426
translateX: SharedValue<number>;
2527
translateY: SharedValue<number>;
2628
style?: StyleProp<ImageStyle>;
2729
};
2830

31+
const imageGallerySelector = (state: ImageGalleryState) => ({
32+
currentIndex: state.currentIndex,
33+
});
34+
2935
export const AnimatedGalleryImage = React.memo(
3036
(props: Props) => {
3137
const {
3238
accessibilityLabel,
3339
index,
3440
offsetScale,
3541
photo,
36-
previous,
3742
scale,
38-
selected,
3943
screenHeight,
4044
screenWidth,
4145
style,
42-
shouldRender,
4346
translateX,
4447
translateY,
4548
} = props;
49+
const { imageGalleryStateStore } = useImageGalleryContext();
4650
const { resizableCDNHosts } = useChatConfigContext();
51+
const { currentIndex } = useStateStore(imageGalleryStateStore.state, imageGallerySelector);
4752

4853
const uri = useMemo(() => {
4954
return getResizedImageUrl({
@@ -54,6 +59,10 @@ export const AnimatedGalleryImage = React.memo(
5459
});
5560
}, [photo.uri, resizableCDNHosts, screenHeight, screenWidth]);
5661

62+
const selected = currentIndex === index;
63+
const previous = currentIndex > index;
64+
const shouldRender = Math.abs(currentIndex - index) < 4;
65+
5766
const animatedStyles = useAnimatedGalleryStyle({
5867
index,
5968
offsetScale,
@@ -85,10 +94,7 @@ export const AnimatedGalleryImage = React.memo(
8594
},
8695
(prevProps, nextProps) => {
8796
if (
88-
prevProps.selected === nextProps.selected &&
89-
prevProps.shouldRender === nextProps.shouldRender &&
9097
prevProps.photo.uri === nextProps.photo.uri &&
91-
prevProps.previous === nextProps.previous &&
9298
prevProps.index === nextProps.index &&
9399
prevProps.screenHeight === nextProps.screenHeight &&
94100
prevProps.screenWidth === nextProps.screenWidth

package/src/components/ImageGallery/components/AnimatedGalleryVideo.tsx

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import { StyleSheet, View, ViewStyle } from 'react-native';
33
import type { StyleProp } from 'react-native';
44
import Animated, { SharedValue } from 'react-native-reanimated';
55

6-
import { useStateStore } from '../../../hooks/useStateStore';
6+
import { useImageGalleryContext } from '../../../contexts/imageGalleryContext/ImageGalleryContext';
7+
import { useStateStore } from '../../../hooks';
8+
79
import {
810
isVideoPlayerAvailable,
911
NativeHandlers,
@@ -13,7 +15,10 @@ import {
1315
VideoType,
1416
} from '../../../native';
1517

16-
import { ImageGalleryAsset } from '../../../state-store/image-gallery-state-store';
18+
import {
19+
ImageGalleryAsset,
20+
ImageGalleryState,
21+
} from '../../../state-store/image-gallery-state-store';
1722
import { VideoPlayerState } from '../../../state-store/video-player';
1823
import { ONE_SECOND_IN_MILLISECONDS } from '../../../utils/constants';
1924
import { Spinner } from '../../UIComponents/Spinner';
@@ -26,9 +31,6 @@ export type AnimatedGalleryVideoType = {
2631
attachmentId: string;
2732
index: number;
2833
offsetScale: SharedValue<number>;
29-
previous: boolean;
30-
selected: boolean;
31-
shouldRender: boolean;
3234
scale: SharedValue<number>;
3335
screenHeight: number;
3436
photo: ImageGalleryAsset;
@@ -47,23 +49,26 @@ const styles = StyleSheet.create({
4749
},
4850
});
4951

52+
const imageGallerySelector = (state: ImageGalleryState) => ({
53+
currentIndex: state.currentIndex,
54+
});
55+
5056
const videoPlayerSelector = (state: VideoPlayerState) => ({
5157
isPlaying: state.isPlaying,
5258
});
5359

5460
export const AnimatedGalleryVideo = React.memo(
5561
(props: AnimatedGalleryVideoType) => {
5662
const [opacity, setOpacity] = useState<number>(1);
63+
const { imageGalleryStateStore } = useImageGalleryContext();
64+
const { currentIndex } = useStateStore(imageGalleryStateStore.state, imageGallerySelector);
5765

5866
const {
5967
attachmentId,
6068
index,
6169
offsetScale,
62-
previous,
6370
scale,
6471
screenHeight,
65-
selected,
66-
shouldRender,
6772
style,
6873
photo,
6974
translateX,
@@ -138,6 +143,10 @@ export const AnimatedGalleryVideo = React.memo(
138143
}
139144
};
140145

146+
const selected = currentIndex === index;
147+
const previous = currentIndex > index;
148+
const shouldRender = Math.abs(currentIndex - index) < 4;
149+
141150
const animatedStyles = useAnimatedGalleryStyle({
142151
index,
143152
offsetScale,
@@ -204,10 +213,7 @@ export const AnimatedGalleryVideo = React.memo(
204213

205214
(prevProps, nextProps) => {
206215
if (
207-
prevProps.shouldRender === nextProps.shouldRender &&
208216
prevProps.screenHeight === nextProps.screenHeight &&
209-
prevProps.selected === nextProps.selected &&
210-
prevProps.previous === nextProps.previous &&
211217
prevProps.index === nextProps.index &&
212218
prevProps.photo === nextProps.photo
213219
) {

0 commit comments

Comments
 (0)