Skip to content

Commit aa303b4

Browse files
committed
feat: introduce image gallery state store
1 parent 68bda2f commit aa303b4

File tree

11 files changed

+377
-321
lines changed

11 files changed

+377
-321
lines changed

package/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
]
6868
},
6969
"dependencies": {
70-
"@gorhom/bottom-sheet": "^5.1.8",
70+
"@gorhom/bottom-sheet": "^5.2.8",
7171
"@ungap/structured-clone": "^1.3.0",
7272
"dayjs": "1.11.13",
7373
"emoji-regex": "^10.4.0",

package/src/components/Attachment/Gallery.tsx

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,7 @@ import { isVideoPlayerAvailable } from '../../native';
3636
import { FileTypes } from '../../types/types';
3737
import { getUrlWithoutParams } from '../../utils/utils';
3838

39-
export type GalleryPropsWithContext = Pick<
40-
ImageGalleryContextValue,
41-
'setSelectedMessage' | 'setMessages'
42-
> &
39+
export type GalleryPropsWithContext = Pick<ImageGalleryContextValue, 'imageGalleryStateStore'> &
4340
Pick<
4441
MessageContextValue,
4542
| 'alignment'
@@ -73,6 +70,7 @@ const GalleryWithContext = (props: GalleryPropsWithContext) => {
7370
alignment,
7471
groupStyles,
7572
hasThreadReplies,
73+
imageGalleryStateStore,
7674
ImageLoadingFailedIndicator,
7775
ImageLoadingIndicator,
7876
ImageReloadIndicator,
@@ -82,9 +80,7 @@ const GalleryWithContext = (props: GalleryPropsWithContext) => {
8280
onPress,
8381
onPressIn,
8482
preventPress,
85-
setMessages,
8683
setOverlay,
87-
setSelectedMessage,
8884
threadList,
8985
videos,
9086
VideoThumbnail,
@@ -190,6 +186,7 @@ const GalleryWithContext = (props: GalleryPropsWithContext) => {
190186
additionalPressableProps={additionalPressableProps}
191187
borderRadius={borderRadius}
192188
colIndex={colIndex}
189+
imageGalleryStateStore={imageGalleryStateStore}
193190
ImageLoadingFailedIndicator={ImageLoadingFailedIndicator}
194191
ImageLoadingIndicator={ImageLoadingIndicator}
195192
ImageReloadIndicator={ImageReloadIndicator}
@@ -204,9 +201,7 @@ const GalleryWithContext = (props: GalleryPropsWithContext) => {
204201
onPressIn={onPressIn}
205202
preventPress={preventPress}
206203
rowIndex={rowIndex}
207-
setMessages={setMessages}
208204
setOverlay={setOverlay}
209-
setSelectedMessage={setSelectedMessage}
210205
thumbnail={thumbnail}
211206
VideoThumbnail={VideoThumbnail}
212207
/>
@@ -242,14 +237,15 @@ type GalleryThumbnailProps = {
242237
| 'ImageLoadingFailedIndicator'
243238
| 'ImageReloadIndicator'
244239
> &
245-
Pick<ImageGalleryContextValue, 'setSelectedMessage' | 'setMessages'> &
240+
Pick<ImageGalleryContextValue, 'imageGalleryStateStore'> &
246241
Pick<MessageContextValue, 'onLongPress' | 'onPress' | 'onPressIn' | 'preventPress'> &
247242
Pick<OverlayContextValue, 'setOverlay'>;
248243

249244
const GalleryThumbnail = ({
250245
additionalPressableProps,
251246
borderRadius,
252247
colIndex,
248+
imageGalleryStateStore,
253249
ImageLoadingFailedIndicator,
254250
ImageLoadingIndicator,
255251
ImageReloadIndicator,
@@ -263,9 +259,7 @@ const GalleryThumbnail = ({
263259
onPressIn,
264260
preventPress,
265261
rowIndex,
266-
setMessages,
267262
setOverlay,
268-
setSelectedMessage,
269263
thumbnail,
270264
VideoThumbnail,
271265
}: GalleryThumbnailProps) => {
@@ -288,8 +282,8 @@ const GalleryThumbnail = ({
288282

289283
const openImageViewer = () => {
290284
if (message) {
291-
setMessages([message]);
292-
setSelectedMessage({ messageId: message.id, url: thumbnail.url });
285+
imageGalleryStateStore.message = message;
286+
imageGalleryStateStore.selectedAttachmentUrl = thumbnail.url;
293287
setOverlay('gallery');
294288
}
295289
};
@@ -562,13 +556,12 @@ export const Gallery = (props: GalleryProps) => {
562556
onPressIn: propOnPressIn,
563557
preventPress: propPreventPress,
564558
setOverlay: propSetOverlay,
565-
setSelectedMessage: propSetSelectedMessage,
566559
threadList: propThreadList,
567560
videos: propVideos,
568561
VideoThumbnail: PropVideoThumbnail,
569562
} = props;
570563

571-
const { setMessages, setSelectedMessage: contextSetSelectedMessage } = useImageGalleryContext();
564+
const { imageGalleryStateStore } = useImageGalleryContext();
572565
const {
573566
alignment: contextAlignment,
574567
groupStyles: contextGroupStyles,
@@ -607,7 +600,6 @@ export const Gallery = (props: GalleryProps) => {
607600
const onPress = propOnPress || contextOnPress;
608601
const preventPress =
609602
typeof propPreventPress === 'boolean' ? propPreventPress : contextPreventPress;
610-
const setSelectedMessage = propSetSelectedMessage || contextSetSelectedMessage;
611603
const setOverlay = propSetOverlay || contextSetOverlay;
612604
const threadList = propThreadList || contextThreadList;
613605
const VideoThumbnail = PropVideoThumbnail || ContextVideoThumnbnail;
@@ -625,6 +617,7 @@ export const Gallery = (props: GalleryProps) => {
625617
channelId: message?.cid,
626618
groupStyles,
627619
hasThreadReplies: hasThreadReplies || !!message?.reply_count,
620+
imageGalleryStateStore,
628621
ImageLoadingFailedIndicator,
629622
ImageLoadingIndicator,
630623
ImageReloadIndicator,
@@ -635,9 +628,7 @@ export const Gallery = (props: GalleryProps) => {
635628
onPress,
636629
onPressIn,
637630
preventPress,
638-
setMessages,
639631
setOverlay,
640-
setSelectedMessage,
641632
threadList,
642633
videos,
643634
VideoThumbnail,

package/src/components/Attachment/Giphy.tsx

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,7 @@ const styles = StyleSheet.create({
132132
},
133133
});
134134

135-
export type GiphyPropsWithContext = Pick<
136-
ImageGalleryContextValue,
137-
'setSelectedMessage' | 'setMessages'
138-
> &
135+
export type GiphyPropsWithContext = Pick<ImageGalleryContextValue, 'imageGalleryStateStore'> &
139136
Pick<
140137
MessageContextValue,
141138
| 'handleAction'
@@ -164,6 +161,7 @@ const GiphyWithContext = (props: GiphyPropsWithContext) => {
164161
giphyVersion,
165162
handleAction,
166163
ImageComponent = Image,
164+
imageGalleryStateStore,
167165
ImageLoadingFailedIndicator,
168166
ImageLoadingIndicator,
169167
isMyMessage,
@@ -172,9 +170,7 @@ const GiphyWithContext = (props: GiphyPropsWithContext) => {
172170
onPress,
173171
onPressIn,
174172
preventPress,
175-
setMessages,
176173
setOverlay,
177-
setSelectedMessage,
178174
} = props;
179175

180176
const { actions, giphy: giphyData, image_url, thumb_url, title, type } = attachment;
@@ -209,8 +205,8 @@ const GiphyWithContext = (props: GiphyPropsWithContext) => {
209205
const giphyDimensions: { height?: number; width?: number } = {};
210206

211207
const defaultOnPress = () => {
212-
setMessages([message]);
213-
setSelectedMessage({ messageId: message.id, url: uri });
208+
imageGalleryStateStore.message = message;
209+
imageGalleryStateStore.selectedAttachmentUrl = uri;
214210
setOverlay('gallery');
215211
};
216212

@@ -452,7 +448,7 @@ export const Giphy = (props: GiphyProps) => {
452448
useMessageContext();
453449
const { ImageComponent } = useChatContext();
454450
const { additionalPressableProps, giphyVersion } = useMessagesContext();
455-
const { setMessages, setSelectedMessage } = useImageGalleryContext();
451+
const { imageGalleryStateStore } = useImageGalleryContext();
456452
const { setOverlay } = useOverlayContext();
457453

458454
const {
@@ -470,6 +466,7 @@ export const Giphy = (props: GiphyProps) => {
470466
giphyVersion,
471467
handleAction,
472468
ImageComponent,
469+
imageGalleryStateStore,
473470
ImageLoadingFailedIndicator,
474471
ImageLoadingIndicator,
475472
isMyMessage,
@@ -478,9 +475,7 @@ export const Giphy = (props: GiphyProps) => {
478475
onPress,
479476
onPressIn,
480477
preventPress,
481-
setMessages,
482478
setOverlay,
483-
setSelectedMessage,
484479
}}
485480
{...props}
486481
/>

0 commit comments

Comments
 (0)