-
Notifications
You must be signed in to change notification settings - Fork 375
Expand file tree
/
Copy pathGalleryImage.tsx
More file actions
60 lines (50 loc) · 1.64 KB
/
GalleryImage.tsx
File metadata and controls
60 lines (50 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import React from 'react';
import { Image, ImageProps, StyleSheet } from 'react-native';
import { ChatContextValue, useChatContext } from '../../contexts/chatContext/ChatContext';
import { getUrlWithoutParams, isLocalUrl, makeImageCompatibleUrl } from '../../utils/utils';
export type GalleryImageWithContextProps = GalleryImageProps &
Pick<ChatContextValue, 'ImageComponent'>;
export const GalleryImageWithContext = (props: GalleryImageWithContextProps) => {
const { ImageComponent = Image, uri, style, ...rest } = props;
// Caching image components such as FastImage will not work with local images.
// This for the case of local uris, we use the default Image component.
if (!isLocalUrl(uri)) {
return (
<ImageComponent
{...rest}
accessibilityLabel='Gallery Image'
style={[styles.image, style]}
source={{
uri: makeImageCompatibleUrl(uri),
}}
/>
);
}
return (
<Image
{...rest}
accessibilityLabel='Gallery Image'
style={[styles.image, style]}
source={{
uri: makeImageCompatibleUrl(uri),
}}
/>
);
};
export const MemoizedGalleryImage = React.memo(
GalleryImageWithContext,
(prevProps, nextProps) =>
getUrlWithoutParams(prevProps.uri) === getUrlWithoutParams(nextProps.uri),
);
export type GalleryImageProps = Omit<ImageProps, 'height' | 'source'> & {
uri: string;
};
export const GalleryImage = (props: GalleryImageProps) => {
const { ImageComponent } = useChatContext();
return <MemoizedGalleryImage ImageComponent={ImageComponent} {...props} />;
};
const styles = StyleSheet.create({
image: {
flex: 1,
},
});