Skip to content

Commit e1ba91d

Browse files
committed
feat: change message var to messages
1 parent bb14051 commit e1ba91d

File tree

9 files changed

+147
-139
lines changed

9 files changed

+147
-139
lines changed

examples/SampleApp/src/screens/ChannelImagesScreen.tsx

Lines changed: 16 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,18 @@ import Dayjs from 'dayjs';
1313
import { SafeAreaView } from 'react-native-safe-area-context';
1414
import {
1515
DateHeader,
16-
Photo,
1716
useImageGalleryContext,
1817
useOverlayContext,
1918
useTheme,
19+
ImageGalleryState,
20+
useStateStore,
2021
} from 'stream-chat-react-native';
2122

2223
import { ScreenHeader } from '../components/ScreenHeader';
2324
import { usePaginatedAttachments } from '../hooks/usePaginatedAttachments';
2425
import { Picture } from '../icons/Picture';
2526

2627
import type { RouteProp } from '@react-navigation/native';
27-
import type { Attachment } from 'stream-chat';
2828

2929
import type { StackNavigatorParamList } from '../types';
3030

@@ -61,16 +61,17 @@ export type ChannelImagesScreenProps = {
6161
route: ChannelImagesScreenRouteProp;
6262
};
6363

64+
const selector = (state: ImageGalleryState) => ({
65+
assets: state.assets,
66+
});
67+
6468
export const ChannelImagesScreen: React.FC<ChannelImagesScreenProps> = ({
6569
route: {
6670
params: { channel },
6771
},
6872
}) => {
69-
const {
70-
messages: images,
71-
setMessages: setImages,
72-
setSelectedMessage: setImage,
73-
} = useImageGalleryContext();
73+
const { imageGalleryStateStore } = useImageGalleryContext();
74+
const { assets } = useStateStore(imageGalleryStateStore.state, selector);
7475
const { setOverlay } = useOverlayContext();
7576
const { loading, loadMore, messages } = usePaginatedAttachments(channel, 'image');
7677
const {
@@ -79,8 +80,6 @@ export const ChannelImagesScreen: React.FC<ChannelImagesScreenProps> = ({
7980
},
8081
} = useTheme();
8182

82-
const channelImages = useRef(images);
83-
8483
const [stickyHeaderDate, setStickyHeaderDate] = useState(
8584
Dayjs(messages?.[0]?.created_at).format('MMM YYYY'),
8685
);
@@ -106,30 +105,6 @@ export const ChannelImagesScreen: React.FC<ChannelImagesScreenProps> = ({
106105
}
107106
});
108107

109-
/**
110-
* Photos array created from all currently available
111-
* photo attachments
112-
*/
113-
const photos = messages.reduce((acc: Photo[], cur) => {
114-
const attachmentImages =
115-
(cur.attachments as Attachment[])?.filter(
116-
(attachment) =>
117-
attachment.type === 'image' &&
118-
!attachment.title_link &&
119-
!attachment.og_scrape_url &&
120-
(attachment.image_url || attachment.thumb_url),
121-
) || [];
122-
123-
const attachmentPhotos = attachmentImages.map((attachmentImage) => ({
124-
created_at: cur.created_at,
125-
id: `photoId-${cur.id}-${attachmentImage.image_url || attachmentImage.thumb_url}`,
126-
messageId: cur.id,
127-
uri: attachmentImage.image_url || (attachmentImage.thumb_url as string),
128-
}));
129-
130-
return [...acc, ...attachmentPhotos];
131-
}, []);
132-
133108
const messagesWithImages = messages
134109
.map((message) => ({ ...message, groupStyles: [], readBy: false }))
135110
.filter((message) => {
@@ -145,32 +120,19 @@ export const ChannelImagesScreen: React.FC<ChannelImagesScreenProps> = ({
145120
return false;
146121
});
147122

148-
/**
149-
* This is for the useEffect to run again in the case that a message
150-
* gets edited with more or the same number of images
151-
*/
152-
const imageString = messagesWithImages
153-
.map((message) =>
154-
(message.attachments as Attachment[])
155-
.map((attachment) => attachment.image_url || attachment.thumb_url || '')
156-
.join(),
157-
)
158-
.join();
159-
160123
useEffect(() => {
161-
setImages(messagesWithImages);
162-
const channelImagesCurrent = channelImages.current;
163-
return () => setImages(channelImagesCurrent);
124+
imageGalleryStateStore.openImageGallery({ messages: messagesWithImages });
125+
return () => imageGalleryStateStore.clear();
164126
// eslint-disable-next-line react-hooks/exhaustive-deps
165-
}, [imageString, setImages]);
127+
}, [imageGalleryStateStore, messagesWithImages.length]);
166128

167129
return (
168130
<SafeAreaView style={[styles.flex, { backgroundColor: white }]}>
169131
<ScreenHeader inSafeArea titleText='Photos and Videos' />
170132
<View style={styles.flex}>
171133
<FlatList
172134
contentContainerStyle={styles.contentContainer}
173-
data={photos}
135+
data={assets}
174136
keyExtractor={(item, index) => `${item.id}-${index}`}
175137
ListEmptyComponent={EmptyListComponent}
176138
numColumns={3}
@@ -180,9 +142,9 @@ export const ChannelImagesScreen: React.FC<ChannelImagesScreenProps> = ({
180142
renderItem={({ item }) => (
181143
<TouchableOpacity
182144
onPress={() => {
183-
setImage({
184-
messageId: item.messageId,
185-
url: item.uri,
145+
imageGalleryStateStore.openImageGallery({
146+
messages: messagesWithImages,
147+
selectedAttachmentUrl: item.uri,
186148
});
187149
setOverlay('gallery');
188150
}}
@@ -202,7 +164,7 @@ export const ChannelImagesScreen: React.FC<ChannelImagesScreenProps> = ({
202164
viewAreaCoveragePercentThreshold: 50,
203165
}}
204166
/>
205-
{photos && photos.length ? (
167+
{assets.length > 0 ? (
206168
<View style={styles.stickyHeader}>
207169
<DateHeader dateString={stickyHeaderDate} />
208170
</View>

package/src/components/Attachment/Gallery.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,10 @@ const GalleryThumbnail = ({
284284
if (!message) {
285285
return;
286286
}
287-
imageGalleryStateStore.openImageGallery({ message, selectedAttachmentUrl: thumbnail.url });
287+
imageGalleryStateStore.openImageGallery({
288+
messages: [message],
289+
selectedAttachmentUrl: thumbnail.url,
290+
});
288291
setOverlay('gallery');
289292
};
290293

package/src/components/Attachment/Giphy.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ const GiphyWithContext = (props: GiphyPropsWithContext) => {
208208
if (!uri) {
209209
return;
210210
}
211-
imageGalleryStateStore.openImageGallery({ message, selectedAttachmentUrl: uri });
211+
imageGalleryStateStore.openImageGallery({ messages: [message], selectedAttachmentUrl: uri });
212212
setOverlay('gallery');
213213
};
214214

package/src/components/ImageGallery/ImageGallery.tsx

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import Animated, {
1313
} from 'react-native-reanimated';
1414

1515
import BottomSheet from '@gorhom/bottom-sheet';
16-
import type { UserResponse } from 'stream-chat';
1716

1817
import { AnimatedGalleryImage } from './components/AnimatedGalleryImage';
1918
import { AnimatedGalleryVideo } from './components/AnimatedGalleryVideo';
@@ -102,6 +101,7 @@ export type ImageGalleryCustomComponents = {
102101
};
103102

104103
const imageGallerySelector = (state: ImageGalleryState) => ({
104+
assets: state.assets,
105105
currentIndex: state.currentIndex,
106106
});
107107

@@ -129,8 +129,11 @@ export const ImageGalleryWithContext = (props: ImageGalleryWithContextProps) =>
129129
},
130130
} = useTheme();
131131
const { imageGalleryStateStore } = useImageGalleryContext();
132-
const { currentIndex } = useStateStore(imageGalleryStateStore.state, imageGallerySelector);
133-
const { assets, videoPlayerPool } = imageGalleryStateStore;
132+
const { assets, currentIndex } = useStateStore(
133+
imageGalleryStateStore.state,
134+
imageGallerySelector,
135+
);
136+
const { videoPlayerPool } = imageGalleryStateStore;
134137

135138
const { vh, vw } = useViewport();
136139

@@ -484,19 +487,4 @@ const styles = StyleSheet.create({
484487
},
485488
});
486489

487-
export type Photo = {
488-
id: string;
489-
uri: string;
490-
channelId?: string;
491-
created_at?: string | Date;
492-
messageId?: string;
493-
mime_type?: string;
494-
original_height?: number;
495-
original_width?: number;
496-
thumb_url?: string;
497-
type?: string;
498-
user?: UserResponse | null;
499-
user_id?: string;
500-
};
501-
502490
ImageGallery.displayName = 'ImageGallery{imageGallery}';

package/src/components/ImageGallery/components/ImageGalleryFooter.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ type ImageGalleryFooterPropsWithContext = ImageGalleryFooterCustomComponentProps
6666
};
6767

6868
const imageGallerySelector = (state: ImageGalleryState) => ({
69+
asset: state.assets[state.currentIndex],
6970
currentIndex: state.currentIndex,
7071
});
7172

@@ -96,8 +97,7 @@ export const ImageGalleryFooterWithContext = (props: ImageGalleryFooterPropsWith
9697
} = useTheme();
9798
const { t } = useTranslationContext();
9899
const { imageGalleryStateStore } = useImageGalleryContext();
99-
const { currentIndex } = useStateStore(imageGalleryStateStore.state, imageGallerySelector);
100-
const asset = imageGalleryStateStore.assets[currentIndex];
100+
const { asset, currentIndex } = useStateStore(imageGalleryStateStore.state, imageGallerySelector);
101101

102102
const footerStyle = useAnimatedStyle<ViewStyle>(
103103
() => ({

package/src/components/ImageGallery/components/ImageGalleryHeader.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ type Props = ImageGalleryHeaderCustomComponentProps & {
4343
};
4444

4545
const imageGallerySelector = (state: ImageGalleryState) => ({
46-
currentIndex: state.currentIndex,
46+
asset: state.assets[state.currentIndex],
4747
});
4848

4949
export const ImageGalleryHeader = (props: Props) => {
@@ -67,9 +67,8 @@ export const ImageGalleryHeader = (props: Props) => {
6767
} = useTheme();
6868
const { t, tDateTimeParser } = useTranslationContext();
6969
const { imageGalleryStateStore } = useImageGalleryContext();
70-
const { currentIndex } = useStateStore(imageGalleryStateStore.state, imageGallerySelector);
70+
const { asset } = useStateStore(imageGalleryStateStore.state, imageGallerySelector);
7171
const { setOverlay } = useOverlayContext();
72-
const asset = imageGalleryStateStore.assets[currentIndex];
7372

7473
const date = useMemo(
7574
() =>

package/src/components/ImageGallery/components/ImageGrid.tsx

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@ import { Image, StyleSheet, View } from 'react-native';
44
import { VideoThumbnail } from '../../../components/Attachment/VideoThumbnail';
55
import { useImageGalleryContext } from '../../../contexts/imageGalleryContext/ImageGalleryContext';
66
import { useTheme } from '../../../contexts/themeContext/ThemeContext';
7+
import { useStateStore } from '../../../hooks/useStateStore';
78
import { useViewport } from '../../../hooks/useViewport';
9+
import type {
10+
ImageGalleryAsset,
11+
ImageGalleryState,
12+
} from '../../../state-store/image-gallery-state-store';
813
import { FileTypes } from '../../../types/types';
914
import { BottomSheetFlatList } from '../../BottomSheetCompatibility/BottomSheetFlatList';
1015
import { BottomSheetTouchableOpacity } from '../../BottomSheetCompatibility/BottomSheetTouchableOpacity';
1116

12-
import type { Photo } from '../ImageGallery';
13-
1417
const styles = StyleSheet.create({
1518
avatarImage: {
1619
borderRadius: 22,
@@ -35,7 +38,7 @@ const styles = StyleSheet.create({
3538
export type ImageGalleryGridImageComponent = ({
3639
item,
3740
}: {
38-
item: Photo & {
41+
item: ImageGalleryAsset & {
3942
selectAndClose: () => void;
4043
numberOfImageGalleryGridColumns?: number;
4144
};
@@ -46,7 +49,7 @@ export type ImageGalleryGridImageComponents = {
4649
imageComponent?: ImageGalleryGridImageComponent;
4750
};
4851

49-
export type GridImageItem = Photo &
52+
export type GridImageItem = ImageGalleryAsset &
5053
ImageGalleryGridImageComponents & {
5154
selectAndClose: () => void;
5255
numberOfImageGalleryGridColumns?: number;
@@ -91,9 +94,14 @@ export type ImageGridType = ImageGalleryGridImageComponents & {
9194
numberOfImageGalleryGridColumns?: number;
9295
};
9396

97+
const imageGallerySelector = (state: ImageGalleryState) => ({
98+
assets: state.assets,
99+
});
100+
94101
export const ImageGrid = (props: ImageGridType) => {
95102
const { avatarComponent, closeGridView, imageComponent, numberOfImageGalleryGridColumns } = props;
96103
const { imageGalleryStateStore } = useImageGalleryContext();
104+
const { assets } = useStateStore(imageGalleryStateStore.state, imageGallerySelector);
97105

98106
const {
99107
theme: {
@@ -104,7 +112,7 @@ export const ImageGrid = (props: ImageGridType) => {
104112
},
105113
} = useTheme();
106114

107-
const imageGridItems = imageGalleryStateStore.assets.map((photo, index) => ({
115+
const imageGridItems = assets.map((photo, index) => ({
108116
...photo,
109117
avatarComponent,
110118
imageComponent,

package/src/components/ImageGallery/hooks/useImageGalleryGestures.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ const MARGIN = 32;
3333

3434
const imageGallerySelector = (state: ImageGalleryState) => ({
3535
currentIndex: state.currentIndex,
36-
message: state.message,
37-
selectedAttachmentUrl: state.selectedAttachmentUrl,
3836
});
3937

4038
export const useImageGalleryGestures = ({
@@ -158,7 +156,7 @@ export const useImageGalleryGestures = ({
158156
offsetScale.value = 1;
159157
};
160158

161-
const assetsLength = imageGalleryStateStore.attachments.length;
159+
const assetsLength = imageGalleryStateStore.assets.length;
162160

163161
const clearImageGallery = () => {
164162
runOnJS(imageGalleryStateStore.clear)();

0 commit comments

Comments
 (0)