-
Notifications
You must be signed in to change notification settings - Fork 375
Expand file tree
/
Copy pathFileAttachment.tsx
More file actions
164 lines (147 loc) · 4.66 KB
/
FileAttachment.tsx
File metadata and controls
164 lines (147 loc) · 4.66 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
import React, { useMemo } from 'react';
import { Pressable, StyleProp, StyleSheet, TextStyle, View, ViewStyle } from 'react-native';
import type { Attachment } from 'stream-chat';
import { openUrlSafely } from './utils/openUrlSafely';
import { FileIconProps } from '../../components/Attachment/FileIcon';
import {
MessageContextValue,
useMessageContext,
} from '../../contexts/messageContext/MessageContext';
import {
MessagesContextValue,
useMessagesContext,
} from '../../contexts/messagesContext/MessagesContext';
import { useTheme } from '../../contexts/themeContext/ThemeContext';
import { useIsPendingAttachmentUploading } from '../../hooks/useIsPendingAttachmentUploading';
import type { DefaultAttachmentData } from '../../types/types';
export type FileAttachmentPropsWithContext = Pick<
MessageContextValue,
'onLongPress' | 'onPress' | 'onPressIn' | 'preventPress'
> &
Pick<
MessagesContextValue,
'additionalPressableProps' | 'FilePreview' | 'ImageUploadingIndicator'
> & {
/** The attachment to render */
attachment: Attachment;
attachmentIconSize?: FileIconProps['size'];
// TODO: Think we really need a way to style the file preview using props if we have theme.
styles?: Partial<{
container: StyleProp<ViewStyle>;
details: StyleProp<ViewStyle>;
size: StyleProp<TextStyle>;
title: StyleProp<TextStyle>;
}>;
};
const FileAttachmentWithContext = (props: FileAttachmentPropsWithContext) => {
const styles = useStyles();
const {
additionalPressableProps,
attachment,
attachmentIconSize,
FilePreview,
ImageUploadingIndicator,
onLongPress,
onPress,
onPressIn,
preventPress,
styles: stylesProp = styles,
} = props;
const localId = (attachment as DefaultAttachmentData).localId;
const isPendingAttachmentUploading = useIsPendingAttachmentUploading(localId);
const defaultOnPress = () => openUrlSafely(attachment.asset_url);
return (
<Pressable
disabled={preventPress}
onLongPress={(event) => {
if (onLongPress) {
onLongPress({
additionalInfo: { attachment },
emitter: 'fileAttachment',
event,
});
}
}}
onPress={(event) => {
if (onPress) {
onPress({
additionalInfo: { attachment },
defaultHandler: defaultOnPress,
emitter: 'fileAttachment',
event,
});
}
}}
onPressIn={(event) => {
if (onPressIn) {
onPressIn({
additionalInfo: { attachment },
defaultHandler: defaultOnPress,
emitter: 'fileAttachment',
event,
});
}
}}
testID='file-attachment'
{...additionalPressableProps}
>
<View style={styles.previewWrap}>
<FilePreview
attachment={attachment}
attachmentIconSize={attachmentIconSize}
styles={stylesProp}
/>
{isPendingAttachmentUploading ? <ImageUploadingIndicator /> : null}
</View>
</Pressable>
);
};
export type FileAttachmentProps = Partial<Omit<FileAttachmentPropsWithContext, 'attachment'>> &
Pick<FileAttachmentPropsWithContext, 'attachment'>;
export const FileAttachment = (props: FileAttachmentProps) => {
const { FilePreview: PropFilePreview } = props;
const { onLongPress, onPress, onPressIn, preventPress } = useMessageContext();
const {
additionalPressableProps,
FilePreview: ContextFilePreview,
ImageUploadingIndicator: ContextImageUploadingIndicator,
} = useMessagesContext();
const FilePreview = PropFilePreview || ContextFilePreview;
const ImageUploadingIndicator = props.ImageUploadingIndicator || ContextImageUploadingIndicator;
return (
<FileAttachmentWithContext
{...{
additionalPressableProps,
FilePreview,
ImageUploadingIndicator,
onLongPress,
onPress,
onPressIn,
preventPress,
}}
{...props}
/>
);
};
FileAttachment.displayName = 'FileAttachment{messageItemView{file}}';
const useStyles = () => {
const {
theme: { semantics },
} = useTheme();
const { isMyMessage, messageHasOnlySingleAttachment } = useMessageContext();
const showBackgroundTransparent = messageHasOnlySingleAttachment;
return useMemo(() => {
return StyleSheet.create({
container: {
backgroundColor: showBackgroundTransparent
? 'transparent'
: isMyMessage
? semantics.chatBgAttachmentOutgoing
: semantics.chatBgAttachmentIncoming,
},
previewWrap: {
position: 'relative',
},
});
}, [showBackgroundTransparent, isMyMessage, semantics]);
};