-
Notifications
You must be signed in to change notification settings - Fork 374
Expand file tree
/
Copy pathReply.tsx
More file actions
343 lines (306 loc) · 9.86 KB
/
Reply.tsx
File metadata and controls
343 lines (306 loc) · 9.86 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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import React, { useMemo } from 'react';
import {
I18nManager,
Image,
ImageProps,
Platform,
StyleSheet,
Text,
TextStyle,
View,
ViewStyle,
} from 'react-native';
import {
isFileAttachment,
isImageAttachment,
isVideoAttachment,
MessageComposerState,
} from 'stream-chat';
import { ReplyMessageView } from './ReplyMessageView';
import { useChatContext } from '../../contexts/chatContext/ChatContext';
import { useComponentsContext } from '../../contexts/componentsContext/ComponentsContext';
import {
MessageContextValue,
useMessageContext,
} from '../../contexts/messageContext/MessageContext';
import { useMessageComposer } from '../../contexts/messageInputContext/hooks/useMessageComposer';
import { MessagesContextValue } from '../../contexts/messagesContext/MessagesContext';
import { useTheme } from '../../contexts/themeContext/ThemeContext';
import { useTranslationContext } from '../../contexts/translationContext/TranslationContext';
import { useStateStore } from '../../hooks';
import { primitives } from '../../theme';
import { FileTypes } from '../../types/types';
import { checkQuotedMessageEquality } from '../../utils/utils';
import { FileIcon } from '../Attachment/FileIcon';
import { AttachmentRemoveControl } from '../MessageInput/components/AttachmentPreview/AttachmentRemoveControl';
import { VideoPlayIndicator } from '../ui/VideoPlayIndicator';
const messageComposerStateStoreSelector = (state: MessageComposerState) => ({
quotedMessage: state.quotedMessage,
});
const RightContent = React.memo(
(props: Pick<ReplyPropsWithContext, 'ImageComponent' | 'message'>) => {
const { ImageComponent, message } = props;
const attachments = message?.attachments;
const styles = useStyles();
if (!attachments || attachments.length > 1) {
return null;
}
const attachment = attachments?.[0];
const uri = attachment?.image_url || attachment?.thumb_url;
if (
attachment &&
(isImageAttachment(attachment) ||
attachment.type === FileTypes.Giphy ||
attachment.type === FileTypes.Imgur)
) {
return (
<View style={[styles.contentWrapper, styles.contentBorder]}>
<ImageComponent source={{ uri }} style={StyleSheet.absoluteFill} />
</View>
);
}
if (attachment && isVideoAttachment(attachment)) {
return (
<View style={[styles.contentWrapper, styles.contentBorder]}>
<View style={styles.attachmentContainer}>
<Image source={{ uri: attachment.thumb_url }} style={StyleSheet.absoluteFill} />
<VideoPlayIndicator size='sm' />
</View>
</View>
);
}
if (attachment?.type === FileTypes.VoiceRecording) {
return null;
}
if (attachment && isFileAttachment(attachment)) {
return <FileIcon mimeType={attachment.mime_type} />;
}
return null;
},
);
export type ReplyPropsWithContext = { ImageComponent: React.ComponentType<ImageProps> } & Pick<
MessageContextValue,
'message'
> &
Pick<MessagesContextValue, 'quotedMessage'> & {
isMyMessage: boolean;
onDismiss?: () => void;
mode: 'reply' | 'edit';
// This is temporary for the MessageContent Component to style the Reply component
styles?: {
container?: ViewStyle;
leftContainer?: ViewStyle;
rightContainer?: ViewStyle;
title?: TextStyle;
subtitleContainer?: ViewStyle;
dismissWrapper?: ViewStyle;
};
};
export const ReplyWithContext = (props: ReplyPropsWithContext) => {
const { t } = useTranslationContext();
const {
isMyMessage,
ImageComponent,
message: messageFromContext,
mode,
onDismiss,
quotedMessage,
styles: stylesProp,
} = props;
const {
theme: {
reply: {
wrapper,
container,
leftContainer,
rightContainer,
title: titleStyle,
dismissWrapper,
},
},
} = useTheme();
const styles = useStyles();
const title = useMemo(
() =>
mode === 'edit'
? t('Edit Message')
: isMyMessage
? t('You')
: quotedMessage?.user?.name
? t('Reply to {{name}}', { name: quotedMessage?.user?.name })
: t('Reply'),
[mode, isMyMessage, quotedMessage?.user?.name, t],
);
if (!quotedMessage) {
return null;
}
return (
<View style={[!messageFromContext?.quoted_message ? styles.wrapper : null, wrapper]}>
<View style={[styles.container, container, stylesProp?.container]}>
<View style={[styles.leftContainer, leftContainer, stylesProp?.leftContainer]}>
<Text numberOfLines={1} style={[styles.title, titleStyle, stylesProp?.title]}>
{title}
</Text>
<ReplyMessageView message={quotedMessage} isMyMessage={isMyMessage} />
</View>
<View style={[styles.rightContainer, rightContainer, stylesProp?.rightContainer]}>
<RightContent ImageComponent={ImageComponent} message={quotedMessage} />
</View>
</View>
{onDismiss ? (
<View style={[styles.dismissWrapper, dismissWrapper, stylesProp?.dismissWrapper]}>
<AttachmentRemoveControl onPress={onDismiss} />
</View>
) : null}
</View>
);
};
const areEqual = (prevProps: ReplyPropsWithContext, nextProps: ReplyPropsWithContext) => {
const {
styles: prevStyles,
isMyMessage: prevIsMyMessage,
mode: prevMode,
quotedMessage: prevQuotedMessage,
onDismiss: prevOnDismiss,
} = prevProps;
const {
styles: nextStyles,
isMyMessage: nextIsMyMessage,
mode: nextMode,
quotedMessage: nextQuotedMessage,
onDismiss: nextOnDismiss,
} = nextProps;
if (prevStyles !== nextStyles) {
return false;
}
const isMyMessageEqual = prevIsMyMessage === nextIsMyMessage;
if (!isMyMessageEqual) {
return false;
}
const modeEqual = prevMode === nextMode;
if (!modeEqual) {
return false;
}
const onDismissEqual = prevOnDismiss === nextOnDismiss;
if (!onDismissEqual) {
return false;
}
const messageEqual =
prevQuotedMessage &&
nextQuotedMessage &&
checkQuotedMessageEquality(prevQuotedMessage, nextQuotedMessage);
if (!messageEqual) {
return false;
}
return true;
};
export const MemoizedReply = React.memo(ReplyWithContext, areEqual) as typeof ReplyWithContext;
export type ReplyProps = Partial<ReplyPropsWithContext> &
Pick<ReplyPropsWithContext, 'mode' | 'onDismiss'>;
export const Reply = (props: ReplyProps) => {
const { message: messageFromContext } = useMessageContext();
const { client } = useChatContext();
const { ImageComponent } = useComponentsContext();
const messageComposer = useMessageComposer();
const { quotedMessage: quotedMessageFromComposer } = useStateStore(
messageComposer.state,
messageComposerStateStoreSelector,
);
const quotedMessage = messageFromContext
? (messageFromContext.quoted_message as MessagesContextValue['quotedMessage'])
: quotedMessageFromComposer;
const isMyMessage = client.user?.id === quotedMessage?.user?.id;
return (
<MemoizedReply
ImageComponent={ImageComponent}
isMyMessage={isMyMessage}
message={messageFromContext}
quotedMessage={quotedMessage}
{...props}
/>
);
};
const useStyles = () => {
const {
theme: { semantics },
} = useTheme();
const messageComposer = useMessageComposer();
const { quotedMessage: quotedMessageFromComposer } = useStateStore(
messageComposer.state,
messageComposerStateStoreSelector,
);
const { client } = useChatContext();
const isMyMessage = client.user?.id === quotedMessageFromComposer?.user?.id;
const isRTL = I18nManager.isRTL;
return useMemo(
() =>
StyleSheet.create({
attachmentContainer: {
alignItems: 'center',
flex: 1,
justifyContent: 'center',
},
container: {
borderRadius: primitives.radiusLg,
flexDirection: isRTL ? 'row-reverse' : 'row',
padding: primitives.spacingXs,
backgroundColor: isMyMessage ? semantics.chatBgOutgoing : semantics.chatBgIncoming,
},
contentWrapper: {
borderRadius: primitives.radiusMd,
borderWidth: 1,
height: 40,
overflow: 'hidden',
width: 40,
},
contentBorder: {
borderColor: semantics.borderCoreOpacitySubtle,
},
dismissWrapper: {
position: 'absolute',
right: 0,
top: 0,
},
leftContainer: {
flex: 1,
justifyContent: 'center',
paddingHorizontal: primitives.spacingXs,
gap: primitives.spacingXxxs,
alignItems: 'flex-start',
...(Platform.OS === 'android'
? {
borderLeftColor: isMyMessage
? semantics.chatReplyIndicatorOutgoing
: semantics.chatReplyIndicatorIncoming,
borderLeftWidth: 2,
}
: isRTL
? {
borderRightColor: isMyMessage
? semantics.chatReplyIndicatorOutgoing
: semantics.chatReplyIndicatorIncoming,
borderRightWidth: 2,
}
: {
borderLeftColor: isMyMessage
? semantics.chatReplyIndicatorOutgoing
: semantics.chatReplyIndicatorIncoming,
borderLeftWidth: 2,
}),
},
rightContainer: {},
title: {
color: isMyMessage ? semantics.chatTextOutgoing : semantics.chatTextIncoming,
fontSize: primitives.typographyFontSizeXs,
fontWeight: primitives.typographyFontWeightSemiBold,
includeFontPadding: false,
lineHeight: primitives.typographyLineHeightTight,
textAlign: isRTL ? 'right' : 'left',
},
wrapper: {
padding: primitives.spacingXxs,
},
}),
[isMyMessage, isRTL, semantics],
);
};