-
Notifications
You must be signed in to change notification settings - Fork 375
Expand file tree
/
Copy pathFileAttachment.tsx
More file actions
187 lines (171 loc) · 5.26 KB
/
FileAttachment.tsx
File metadata and controls
187 lines (171 loc) · 5.26 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
import React, { useCallback, useMemo } from 'react';
import {
ActivityIndicator,
Pressable,
StyleProp,
StyleSheet,
TextStyle,
ViewStyle,
} from 'react-native';
import type { Attachment } from 'stream-chat';
import { FilePreview } from './FilePreview';
import { openUrlSafely } from './utils/openUrlSafely';
import { FileIcon as FileIconDefault, 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 { useStateStore } from '../../hooks/useStateStore';
import type { PendingAttachmentsUploadingState } from '../../state-store/pending-attachments-uploading-state';
export type FileAttachmentPropsWithContext = Pick<
MessageContextValue,
'onLongPress' | 'onPress' | 'onPressIn' | 'preventPress'
> &
Pick<MessagesContextValue, 'additionalPressableProps'> & {
/** 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>;
}>;
/**
* Whether the attachment is currently being uploaded.
* This is used to show a loading indicator in the file attachment.
*/
isPendingAttachmentUploading: boolean;
};
const FileAttachmentWithContext = (props: FileAttachmentPropsWithContext) => {
const styles = useStyles();
const {
theme: { semantics },
} = useTheme();
const {
additionalPressableProps,
attachment,
attachmentIconSize,
onLongPress,
onPress,
onPressIn,
preventPress,
styles: stylesProp = styles,
isPendingAttachmentUploading,
} = props;
const defaultOnPress = () => openUrlSafely(attachment.asset_url);
const renderIndicator = useMemo(() => {
if (isPendingAttachmentUploading) {
return <ActivityIndicator color={semantics.accentPrimary} style={styles.activityIndicator} />;
}
return null;
}, [isPendingAttachmentUploading, semantics.accentPrimary, styles.activityIndicator]);
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}
>
<FilePreview
attachment={attachment}
attachmentIconSize={attachmentIconSize}
indicator={renderIndicator}
styles={stylesProp}
/>
</Pressable>
);
};
export type FileAttachmentProps = Partial<Omit<FileAttachmentPropsWithContext, 'attachment'>> &
Pick<FileAttachmentPropsWithContext, 'attachment'>;
export const FileAttachment = (props: FileAttachmentProps) => {
const { attachment } = props;
const { onLongPress, onPress, onPressIn, preventPress, message } = useMessageContext();
const {
additionalPressableProps,
FileAttachmentIcon = FileIconDefault,
pendingAttachmentsUploadingStore,
} = useMessagesContext();
const attachmentId = `${message.id}-${attachment.originalFile?.uri}`;
const selector = useCallback(
(state: PendingAttachmentsUploadingState) => ({
isPendingAttachmentUploading: state.pendingAttachmentsUploading[attachmentId] ?? false,
}),
[attachmentId],
);
const { isPendingAttachmentUploading } = useStateStore(
pendingAttachmentsUploadingStore.store,
selector,
);
return (
<FileAttachmentWithContext
{...{
additionalPressableProps,
FileAttachmentIcon,
onLongPress,
onPress,
onPressIn,
preventPress,
isPendingAttachmentUploading,
}}
{...props}
/>
);
};
FileAttachment.displayName = 'FileAttachment{messageSimple{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,
},
activityIndicator: {
alignItems: 'flex-start',
justifyContent: 'flex-start',
},
});
}, [showBackgroundTransparent, isMyMessage, semantics]);
};