Skip to content

Commit 699be5b

Browse files
committed
fix: indexing
1 parent 4d2ac4d commit 699be5b

File tree

4 files changed

+45
-42
lines changed

4 files changed

+45
-42
lines changed

package/src/components/ImageGallery/ImageGallery.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,6 @@ export const ImageGalleryWithContext = (props: ImageGalleryWithContextProps) =>
365365
offsetScale={offsetScale}
366366
photo={photo}
367367
previous={currentIndex > i}
368-
repeat={true}
369368
scale={scale}
370369
screenHeight={fullWindowHeight}
371370
selected={currentIndex === i}

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

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,49 +4,46 @@ 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';
7+
import { ImageGalleryAsset } from '../../../state-store/image-gallery-state-store';
138
import { getResizedImageUrl } from '../../../utils/getResizedImageUrl';
149
import { useAnimatedGalleryStyle } from '../hooks/useAnimatedGalleryStyle';
1510

1611
const oneEighth = 1 / 8;
1712

1813
type Props = {
1914
accessibilityLabel: string;
15+
index: number;
2016
offsetScale: SharedValue<number>;
2117
photo: ImageGalleryAsset;
18+
previous: boolean;
2219
scale: SharedValue<number>;
2320
screenHeight: number;
2421
screenWidth: number;
22+
selected: boolean;
23+
shouldRender: boolean;
2524
translateX: SharedValue<number>;
2625
translateY: SharedValue<number>;
2726
style?: StyleProp<ImageStyle>;
2827
};
2928

30-
const imageGallerySelector = (state: ImageGalleryState) => ({
31-
currentIndex: state.currentIndex,
32-
});
33-
3429
export const AnimatedGalleryImage = React.memo(
3530
(props: Props) => {
3631
const {
3732
accessibilityLabel,
33+
index,
3834
offsetScale,
3935
photo,
36+
previous,
4037
scale,
38+
selected,
4139
screenHeight,
4240
screenWidth,
4341
style,
42+
shouldRender,
4443
translateX,
4544
translateY,
4645
} = props;
4746
const { resizableCDNHosts } = useChatConfigContext();
48-
const { imageGalleryStateStore } = useImageGalleryContext();
49-
const { currentIndex } = useStateStore(imageGalleryStateStore.state, imageGallerySelector);
5047

5148
const uri = useMemo(() => {
5249
return getResizedImageUrl({
@@ -57,21 +54,17 @@ export const AnimatedGalleryImage = React.memo(
5754
});
5855
}, [photo.uri, resizableCDNHosts, screenHeight, screenWidth]);
5956

60-
const index = photo.index;
61-
6257
const animatedStyles = useAnimatedGalleryStyle({
6358
index,
6459
offsetScale,
65-
previous: currentIndex > index,
60+
previous,
6661
scale,
6762
screenHeight,
68-
selected: currentIndex === index,
63+
selected,
6964
translateX,
7065
translateY,
7166
});
7267

73-
const shouldRender = Math.abs(currentIndex - index) < 4;
74-
7568
/**
7669
* An empty view is rendered for images not close to the currently
7770
* selected in order to maintain spacing while reducing the image
@@ -92,7 +85,11 @@ export const AnimatedGalleryImage = React.memo(
9285
},
9386
(prevProps, nextProps) => {
9487
if (
88+
prevProps.selected === nextProps.selected &&
89+
prevProps.shouldRender === nextProps.shouldRender &&
9590
prevProps.photo.uri === nextProps.photo.uri &&
91+
prevProps.previous === nextProps.previous &&
92+
prevProps.index === nextProps.index &&
9693
prevProps.screenHeight === nextProps.screenHeight &&
9794
prevProps.screenWidth === nextProps.screenWidth
9895
) {

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

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ 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';
76
import { useStateStore } from '../../../hooks/useStateStore';
87
import {
98
isVideoPlayerAvailable,
@@ -14,10 +13,7 @@ import {
1413
VideoType,
1514
} from '../../../native';
1615

17-
import {
18-
ImageGalleryAsset,
19-
ImageGalleryState,
20-
} from '../../../state-store/image-gallery-state-store';
16+
import { ImageGalleryAsset } from '../../../state-store/image-gallery-state-store';
2117
import { VideoPlayerState } from '../../../state-store/video-player';
2218
import { ONE_SECOND_IN_MILLISECONDS } from '../../../utils/constants';
2319
import { Spinner } from '../../UIComponents/Spinner';
@@ -28,7 +24,11 @@ const oneEighth = 1 / 8;
2824

2925
export type AnimatedGalleryVideoType = {
3026
attachmentId: string;
27+
index: number;
3128
offsetScale: SharedValue<number>;
29+
previous: boolean;
30+
selected: boolean;
31+
shouldRender: boolean;
3232
scale: SharedValue<number>;
3333
screenHeight: number;
3434
photo: ImageGalleryAsset;
@@ -51,18 +51,24 @@ const videoPlayerSelector = (state: VideoPlayerState) => ({
5151
isPlaying: state.isPlaying,
5252
});
5353

54-
const imageGallerySelector = (state: ImageGalleryState) => ({
55-
currentIndex: state.currentIndex,
56-
});
57-
5854
export const AnimatedGalleryVideo = React.memo(
5955
(props: AnimatedGalleryVideoType) => {
60-
const { imageGalleryStateStore } = useImageGalleryContext();
6156
const [opacity, setOpacity] = useState<number>(1);
62-
const { currentIndex } = useStateStore(imageGalleryStateStore.state, imageGallerySelector);
6357

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

6773
const videoRef = useRef<VideoType>(null);
6874

@@ -132,21 +138,17 @@ export const AnimatedGalleryVideo = React.memo(
132138
}
133139
};
134140

135-
const index = photo.index;
136-
137141
const animatedStyles = useAnimatedGalleryStyle({
138142
index,
139143
offsetScale,
140-
previous: currentIndex > index,
144+
previous,
141145
scale,
142146
screenHeight,
143-
selected: currentIndex === index,
147+
selected,
144148
translateX,
145149
translateY,
146150
});
147151

148-
const shouldRender = Math.abs(currentIndex - index) < 4;
149-
150152
/**
151153
* An empty view is rendered for images not close to the currently
152154
* selected in order to maintain spacing while reducing the image
@@ -201,7 +203,14 @@ export const AnimatedGalleryVideo = React.memo(
201203
},
202204

203205
(prevProps, nextProps) => {
204-
if (prevProps.screenHeight === nextProps.screenHeight && prevProps.photo === nextProps.photo) {
206+
if (
207+
prevProps.shouldRender === nextProps.shouldRender &&
208+
prevProps.screenHeight === nextProps.screenHeight &&
209+
prevProps.selected === nextProps.selected &&
210+
prevProps.previous === nextProps.previous &&
211+
prevProps.index === nextProps.index &&
212+
prevProps.photo === nextProps.photo
213+
) {
205214
return true;
206215
}
207216
return false;

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

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

1010
export type ImageGalleryAsset = {
1111
id: string;
12-
index: number;
1312
uri: string;
1413
channelId?: string;
1514
created_at?: string | Date;
@@ -113,7 +112,7 @@ export class ImageGalleryStateStore {
113112
const attachmentsWithMessage = this.attachmentsWithMessage;
114113
const { giphyVersion = 'fixed_height' } = this.options;
115114

116-
return attachmentsWithMessage.flatMap(({ message, attachments }, index) => {
115+
return attachmentsWithMessage.flatMap(({ message, attachments }) => {
117116
return attachments.map((attachment) => {
118117
const assetUrl = getUrlOfImageAttachment(attachment, giphyVersion) as string;
119118
const assetId = this.getAssetId(message?.id ?? '', assetUrl);
@@ -125,7 +124,6 @@ export class ImageGalleryStateStore {
125124
channelId: message?.cid,
126125
created_at: message?.created_at,
127126
id: assetId,
128-
index,
129127
messageId: message?.id,
130128
mime_type: attachment.type === 'giphy' ? giphyMimeType : attachment.mime_type,
131129
original_height: attachment.original_height,

0 commit comments

Comments
 (0)