-
Notifications
You must be signed in to change notification settings - Fork 373
Expand file tree
/
Copy pathFileAttachment.tsx
More file actions
143 lines (127 loc) · 3.89 KB
/
FileAttachment.tsx
File metadata and controls
143 lines (127 loc) · 3.89 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
import React, { useMemo } from 'react';
import { Pressable, StyleProp, StyleSheet, TextStyle, 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';
export type FileAttachmentPropsWithContext = Pick<
MessageContextValue,
'onLongPress' | 'onPress' | 'onPressIn' | 'preventPress'
> &
Pick<MessagesContextValue, 'additionalPressableProps' | 'FilePreview'> & {
/** 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,
onLongPress,
onPress,
onPressIn,
preventPress,
styles: stylesProp = styles,
} = props;
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}
>
<FilePreview
attachment={attachment}
attachmentIconSize={attachmentIconSize}
styles={stylesProp}
/>
</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 } = useMessagesContext();
const FilePreview = PropFilePreview || ContextFilePreview;
return (
<FileAttachmentWithContext
{...{
additionalPressableProps,
FilePreview,
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,
},
});
}, [showBackgroundTransparent, isMyMessage, semantics]);
};