/
ImageUploadPreview.tsx
240 lines (217 loc) · 6.76 KB
/
ImageUploadPreview.tsx
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import React from 'react';
import {
FlatList,
Image,
StyleSheet,
Text,
TouchableOpacity,
TouchableOpacityProps,
View,
} from 'react-native';
import { UploadProgressIndicator } from './UploadProgressIndicator';
import { ChatContextValue, useChatContext } from '../../contexts';
import {
MessageInputContextValue,
useMessageInputContext,
} from '../../contexts/messageInputContext/MessageInputContext';
import { useTheme } from '../../contexts/themeContext/ThemeContext';
import { useTranslationContext } from '../../contexts/translationContext/TranslationContext';
import { Close } from '../../icons/Close';
import { Warning } from '../../icons/Warning';
import type { DefaultStreamChatGenerics, ImageUpload } from '../../types/types';
import { getIndicatorTypeForFileState, ProgressIndicatorTypes } from '../../utils/utils';
const IMAGE_PREVIEW_SIZE = 100;
const WARNING_ICON_SIZE = 16;
const styles = StyleSheet.create({
dismiss: {
borderRadius: 24,
position: 'absolute',
right: 8,
top: 8,
},
fileSizeText: {
fontSize: 12,
paddingHorizontal: 10,
},
flatList: { paddingBottom: 12 },
iconContainer: {
alignItems: 'center',
flexDirection: 'row',
justifyContent: 'center',
},
itemContainer: {
flexDirection: 'row',
height: IMAGE_PREVIEW_SIZE,
marginLeft: 8,
},
unsupportedImage: {
borderRadius: 20,
bottom: 8,
flexDirection: 'row',
marginHorizontal: 3,
position: 'absolute',
},
upload: {
borderRadius: 10,
height: IMAGE_PREVIEW_SIZE,
width: IMAGE_PREVIEW_SIZE,
},
warningIconStyle: {
borderRadius: 24,
marginTop: 6,
},
warningText: {
alignItems: 'center',
color: 'black',
fontSize: 10,
justifyContent: 'center',
marginHorizontal: 4,
},
});
type ImageUploadPreviewPropsWithContext<
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
> = Pick<
MessageInputContextValue<StreamChatGenerics>,
'imageUploads' | 'removeImage' | 'uploadImage'
> &
Pick<ChatContextValue<StreamChatGenerics>, 'enableOfflineSupport'>;
export type ImageUploadPreviewProps<
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
> = Partial<ImageUploadPreviewPropsWithContext<StreamChatGenerics>>;
type ImageUploadPreviewItem = { index: number; item: ImageUpload };
const ImageUploadPreviewWithContext = <
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
>(
props: ImageUploadPreviewPropsWithContext<StreamChatGenerics>,
) => {
const { enableOfflineSupport, imageUploads, removeImage, uploadImage } = props;
const {
theme: {
messageInput: {
imageUploadPreview: { flatList, itemContainer, upload },
},
},
} = useTheme();
const UnsupportedImageTypeIndicator = ({
indicatorType,
}: {
indicatorType: (typeof ProgressIndicatorTypes)[keyof typeof ProgressIndicatorTypes] | null;
}) => {
const {
theme: {
colors: { accent_red, overlay, white },
},
} = useTheme();
const { t } = useTranslationContext();
return indicatorType === ProgressIndicatorTypes.NOT_SUPPORTED ? (
<View style={[styles.unsupportedImage, { backgroundColor: overlay }]}>
<View style={[styles.iconContainer]}>
<Warning
height={WARNING_ICON_SIZE}
pathFill={accent_red}
style={styles.warningIconStyle}
width={WARNING_ICON_SIZE}
/>
<Text style={[styles.warningText, { color: white }]}>{t<string>('Not supported')}</Text>
</View>
</View>
) : null;
};
const renderItem = ({ index, item }: ImageUploadPreviewItem) => {
const indicatorType = getIndicatorTypeForFileState(item.state, enableOfflineSupport);
const itemMarginForIndex = index === imageUploads.length - 1 ? { marginRight: 8 } : {};
return (
<View style={[styles.itemContainer, itemMarginForIndex, itemContainer]}>
<UploadProgressIndicator
action={() => {
uploadImage({ newImage: item });
}}
style={styles.upload}
type={indicatorType}
>
<Image
resizeMode='cover'
source={{ uri: item.file.uri || item.url }}
style={[styles.upload, upload]}
/>
</UploadProgressIndicator>
<DismissUpload
onPress={() => {
removeImage(item.id);
}}
/>
<UnsupportedImageTypeIndicator indicatorType={indicatorType} />
</View>
);
};
return imageUploads.length > 0 ? (
<FlatList
data={imageUploads}
getItemLayout={(_, index) => ({
index,
length: IMAGE_PREVIEW_SIZE + 8,
offset: (IMAGE_PREVIEW_SIZE + 8) * index,
})}
horizontal
keyExtractor={(item) => item.id}
renderItem={renderItem}
style={[styles.flatList, flatList]}
/>
) : null;
};
type DismissUploadProps = Pick<TouchableOpacityProps, 'onPress'>;
const DismissUpload = ({ onPress }: DismissUploadProps) => {
const {
theme: {
colors: { overlay, white },
messageInput: {
imageUploadPreview: { dismiss, dismissIconColor },
},
},
} = useTheme();
return (
<TouchableOpacity
onPress={onPress}
style={[styles.dismiss, { backgroundColor: overlay }, dismiss]}
testID='remove-image-upload-preview'
>
<Close pathFill={dismissIconColor || white} />
</TouchableOpacity>
);
};
const areEqual = <StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics>(
prevProps: ImageUploadPreviewPropsWithContext<StreamChatGenerics>,
nextProps: ImageUploadPreviewPropsWithContext<StreamChatGenerics>,
) => {
const { imageUploads: prevImageUploads } = prevProps;
const { imageUploads: nextImageUploads } = nextProps;
return (
prevImageUploads.length === nextImageUploads.length &&
prevImageUploads.every(
(prevImageUpload, index) => prevImageUpload.state === nextImageUploads[index].state,
)
);
};
const MemoizedImageUploadPreviewWithContext = React.memo(
ImageUploadPreviewWithContext,
areEqual,
) as typeof ImageUploadPreviewWithContext;
/**
* UI Component to preview the images set for upload
*/
export const ImageUploadPreview = <
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
>(
props: ImageUploadPreviewProps<StreamChatGenerics>,
) => {
const { enableOfflineSupport } = useChatContext<StreamChatGenerics>();
const { imageUploads, removeImage, uploadImage } = useMessageInputContext<StreamChatGenerics>();
return (
<MemoizedImageUploadPreviewWithContext
{...{ imageUploads, removeImage, uploadImage }}
{...{ enableOfflineSupport }}
{...props}
/>
);
};
ImageUploadPreview.displayName = 'ImageUploadPreview{messageInput{imageUploadPreview}}';