Skip to content

Commit e9baf44

Browse files
committed
fix: indexing
1 parent e1ba91d commit e9baf44

File tree

3 files changed

+44
-50
lines changed

3 files changed

+44
-50
lines changed

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

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,45 +4,49 @@ 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 { useImageGalleryContext } from '../../../contexts/imageGalleryContext/ImageGalleryContext';
8+
import { useStateStore } from '../../../hooks/useStateStore';
9+
import {
10+
ImageGalleryAsset,
11+
ImageGalleryState,
12+
} from '../../../state-store/image-gallery-state-store';
713
import { getResizedImageUrl } from '../../../utils/getResizedImageUrl';
814
import { useAnimatedGalleryStyle } from '../hooks/useAnimatedGalleryStyle';
915

1016
const oneEighth = 1 / 8;
1117

1218
type Props = {
1319
accessibilityLabel: string;
14-
index: number;
1520
offsetScale: SharedValue<number>;
16-
photo: { uri: string };
17-
previous: boolean;
21+
photo: ImageGalleryAsset;
1822
scale: SharedValue<number>;
1923
screenHeight: number;
2024
screenWidth: number;
21-
selected: boolean;
22-
shouldRender: boolean;
2325
translateX: SharedValue<number>;
2426
translateY: SharedValue<number>;
2527
style?: StyleProp<ImageStyle>;
2628
};
2729

30+
const imageGallerySelector = (state: ImageGalleryState) => ({
31+
currentIndex: state.currentIndex,
32+
});
33+
2834
export const AnimatedGalleryImage = React.memo(
2935
(props: Props) => {
3036
const {
3137
accessibilityLabel,
32-
index,
3338
offsetScale,
3439
photo,
35-
previous,
3640
scale,
3741
screenHeight,
3842
screenWidth,
39-
selected,
40-
shouldRender,
4143
style,
4244
translateX,
4345
translateY,
4446
} = props;
4547
const { resizableCDNHosts } = useChatConfigContext();
48+
const { imageGalleryStateStore } = useImageGalleryContext();
49+
const { currentIndex } = useStateStore(imageGalleryStateStore.state, imageGallerySelector);
4650

4751
const uri = useMemo(() => {
4852
return getResizedImageUrl({
@@ -53,17 +57,21 @@ export const AnimatedGalleryImage = React.memo(
5357
});
5458
}, [photo.uri, resizableCDNHosts, screenHeight, screenWidth]);
5559

60+
const index = photo.index;
61+
5662
const animatedStyles = useAnimatedGalleryStyle({
5763
index,
5864
offsetScale,
59-
previous,
65+
previous: currentIndex > index,
6066
scale,
6167
screenHeight,
62-
selected,
68+
selected: currentIndex === index,
6369
translateX,
6470
translateY,
6571
});
6672

73+
const shouldRender = Math.abs(currentIndex - index) < 4;
74+
6775
/**
6876
* An empty view is rendered for images not close to the currently
6977
* selected in order to maintain spacing while reducing the image
@@ -84,11 +92,7 @@ export const AnimatedGalleryImage = React.memo(
8492
},
8593
(prevProps, nextProps) => {
8694
if (
87-
prevProps.selected === nextProps.selected &&
88-
prevProps.shouldRender === nextProps.shouldRender &&
8995
prevProps.photo.uri === nextProps.photo.uri &&
90-
prevProps.previous === nextProps.previous &&
91-
prevProps.index === nextProps.index &&
9296
prevProps.screenHeight === nextProps.screenHeight &&
9397
prevProps.screenWidth === nextProps.screenWidth
9498
) {

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

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ 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 { useImageGalleryContext } from '../../../contexts/imageGalleryContext/ImageGalleryContext';
67
import { useStateStore } from '../../../hooks/useStateStore';
78
import {
89
isVideoPlayerAvailable,
@@ -13,28 +14,26 @@ import {
1314
VideoType,
1415
} from '../../../native';
1516

17+
import {
18+
ImageGalleryAsset,
19+
ImageGalleryState,
20+
} from '../../../state-store/image-gallery-state-store';
1621
import { VideoPlayerState } from '../../../state-store/video-player';
1722
import { ONE_SECOND_IN_MILLISECONDS } from '../../../utils/constants';
1823
import { Spinner } from '../../UIComponents/Spinner';
1924
import { useAnimatedGalleryStyle } from '../hooks/useAnimatedGalleryStyle';
2025
import { useImageGalleryVideoPlayer } from '../hooks/useImageGalleryVideoPlayer';
21-
import { Photo } from '../ImageGallery';
2226

2327
const oneEighth = 1 / 8;
2428

2529
export type AnimatedGalleryVideoType = {
2630
attachmentId: string;
27-
index: number;
2831
offsetScale: SharedValue<number>;
29-
previous: boolean;
3032
scale: SharedValue<number>;
3133
screenHeight: number;
32-
selected: boolean;
33-
shouldRender: boolean;
34-
photo: Photo;
34+
photo: ImageGalleryAsset;
3535
translateX: SharedValue<number>;
3636
translateY: SharedValue<number>;
37-
repeat?: boolean;
3837
style?: StyleProp<ViewStyle>;
3938
};
4039

@@ -52,25 +51,18 @@ const videoPlayerSelector = (state: VideoPlayerState) => ({
5251
isPlaying: state.isPlaying,
5352
});
5453

54+
const imageGallerySelector = (state: ImageGalleryState) => ({
55+
currentIndex: state.currentIndex,
56+
});
57+
5558
export const AnimatedGalleryVideo = React.memo(
5659
(props: AnimatedGalleryVideoType) => {
60+
const { imageGalleryStateStore } = useImageGalleryContext();
5761
const [opacity, setOpacity] = useState<number>(1);
62+
const { currentIndex } = useStateStore(imageGalleryStateStore.state, imageGallerySelector);
5863

59-
const {
60-
attachmentId,
61-
index,
62-
offsetScale,
63-
previous,
64-
repeat,
65-
scale,
66-
screenHeight,
67-
selected,
68-
shouldRender,
69-
style,
70-
photo,
71-
translateX,
72-
translateY,
73-
} = props;
64+
const { attachmentId, offsetScale, scale, screenHeight, style, photo, translateX, translateY } =
65+
props;
7466

7567
const videoRef = useRef<VideoType>(null);
7668

@@ -140,17 +132,21 @@ export const AnimatedGalleryVideo = React.memo(
140132
}
141133
};
142134

135+
const index = photo.index;
136+
143137
const animatedStyles = useAnimatedGalleryStyle({
144138
index,
145139
offsetScale,
146-
previous,
140+
previous: currentIndex > index,
147141
scale,
148142
screenHeight,
149-
selected,
143+
selected: currentIndex === index,
150144
translateX,
151145
translateY,
152146
});
153147

148+
const shouldRender = Math.abs(currentIndex - index) < 4;
149+
154150
/**
155151
* An empty view is rendered for images not close to the currently
156152
* selected in order to maintain spacing while reducing the image
@@ -176,7 +172,7 @@ export const AnimatedGalleryVideo = React.memo(
176172
onPlaybackStatusUpdate={onPlayBackStatusUpdate}
177173
onProgress={onProgress}
178174
paused={!isPlaying}
179-
repeat={repeat}
175+
repeat={true}
180176
resizeMode='contain'
181177
style={style}
182178
testID='video-player'
@@ -205,15 +201,7 @@ export const AnimatedGalleryVideo = React.memo(
205201
},
206202

207203
(prevProps, nextProps) => {
208-
if (
209-
prevProps.repeat === nextProps.repeat &&
210-
prevProps.shouldRender === nextProps.shouldRender &&
211-
prevProps.screenHeight === nextProps.screenHeight &&
212-
prevProps.selected === nextProps.selected &&
213-
prevProps.previous === nextProps.previous &&
214-
prevProps.index === nextProps.index &&
215-
prevProps.photo === nextProps.photo
216-
) {
204+
if (prevProps.screenHeight === nextProps.screenHeight && prevProps.photo === nextProps.photo) {
217205
return true;
218206
}
219207
return false;

package/src/state-store/image-gallery-state-store.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { getUrlOfImageAttachment } from '../utils/getUrlOfImageAttachment';
99

1010
export type ImageGalleryAsset = {
1111
id: string;
12+
index: number;
1213
uri: string;
1314
channelId?: string;
1415
created_at?: string | Date;
@@ -112,7 +113,7 @@ export class ImageGalleryStateStore {
112113
const attachmentsWithMessage = this.attachmentsWithMessage;
113114
const { giphyVersion = 'fixed_height' } = this.options;
114115

115-
return attachmentsWithMessage.flatMap(({ message, attachments }) => {
116+
return attachmentsWithMessage.flatMap(({ message, attachments }, index) => {
116117
return attachments.map((attachment) => {
117118
const assetUrl = getUrlOfImageAttachment(attachment, giphyVersion) as string;
118119
const assetId = this.getAssetId(message?.id ?? '', assetUrl);
@@ -124,6 +125,7 @@ export class ImageGalleryStateStore {
124125
channelId: message?.cid,
125126
created_at: message?.created_at,
126127
id: assetId,
128+
index,
127129
messageId: message?.id,
128130
mime_type: attachment.type === 'giphy' ? giphyMimeType : attachment.mime_type,
129131
original_height: attachment.original_height,

0 commit comments

Comments
 (0)