-
Notifications
You must be signed in to change notification settings - Fork 373
Expand file tree
/
Copy pathFileAttachmentGroup.tsx
More file actions
109 lines (89 loc) · 2.84 KB
/
FileAttachmentGroup.tsx
File metadata and controls
109 lines (89 loc) · 2.84 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
import React from 'react';
import { StyleProp, StyleSheet, View, ViewStyle } from 'react-native';
import { Attachment as AttachmentDefault } from './Attachment';
import {
MessageContextValue,
useMessageContext,
} from '../../contexts/messageContext/MessageContext';
import {
MessagesContextValue,
useMessagesContext,
} from '../../contexts/messagesContext/MessagesContext';
import { useTheme } from '../../contexts/themeContext/ThemeContext';
import { primitives } from '../../theme';
export type FileAttachmentGroupPropsWithContext = Pick<MessageContextValue, 'files' | 'message'> &
Pick<MessagesContextValue, 'Attachment'> & {
styles?: Partial<{
attachmentContainer: StyleProp<ViewStyle>;
container: StyleProp<ViewStyle>;
}>;
};
const FileAttachmentGroupWithContext = (props: FileAttachmentGroupPropsWithContext) => {
const { Attachment, files, message, styles: stylesProp = {} } = props;
const {
theme: {
messageItemView: {
fileAttachmentGroup: { attachmentContainer, container },
},
},
} = useTheme();
return (
<View style={[styles.container, {}, container, stylesProp.container]}>
{files.map((file, index) => (
<View
key={`file-by-attachment-group-${message.id}-${index}`}
style={[styles.item, stylesProp.attachmentContainer, attachmentContainer]}
>
<Attachment attachment={file} />
</View>
))}
</View>
);
};
const areEqual = (
prevProps: FileAttachmentGroupPropsWithContext,
nextProps: FileAttachmentGroupPropsWithContext,
) => {
const { files: prevFiles, message: prevMessage } = prevProps;
const { files: nextFiles, message: nextMessage } = nextProps;
const messageEqual = prevMessage?.id === nextMessage?.id;
if (!messageEqual) {
return false;
}
return prevFiles.length === nextFiles.length;
};
const MemoizedFileAttachmentGroup = React.memo(
FileAttachmentGroupWithContext,
areEqual,
) as typeof FileAttachmentGroupWithContext;
export type FileAttachmentGroupProps = Partial<FileAttachmentGroupPropsWithContext>;
export const FileAttachmentGroup = (props: FileAttachmentGroupProps) => {
const { files: propFiles } = props;
const { files: contextFiles, message } = useMessageContext();
const { Attachment = AttachmentDefault, AudioAttachment } = useMessagesContext();
const files = propFiles || contextFiles;
if (!files.length) {
return null;
}
return (
<MemoizedFileAttachmentGroup
{...{
Attachment,
AudioAttachment,
files,
message,
}}
/>
);
};
const styles = StyleSheet.create({
container: {
alignItems: 'center',
gap: primitives.spacingXs,
},
item: {
borderRadius: primitives.radiusLg,
overflow: 'hidden',
},
});
FileAttachmentGroup.displayName = 'FileAttachmentGroup{messageItemView{fileAttachmentGroup}}';