-
Notifications
You must be signed in to change notification settings - Fork 376
Expand file tree
/
Copy pathFileAttachmentGroup.tsx
More file actions
188 lines (163 loc) · 5.7 KB
/
FileAttachmentGroup.tsx
File metadata and controls
188 lines (163 loc) · 5.7 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
import React, { useState } from 'react';
import { StyleProp, StyleSheet, View, ViewStyle } from 'react-native';
import type { Attachment } from 'stream-chat';
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 { isSoundPackageAvailable } from '../../native';
import { FileTypes } from '../../types/types';
export type FileAttachmentGroupPropsWithContext = Pick<MessageContextValue, 'files'> &
Pick<MessagesContextValue, 'Attachment' | 'AudioAttachment'> & {
/**
* The unique id for the message with file attachments
*/
messageId: string;
styles?: Partial<{
attachmentContainer: StyleProp<ViewStyle>;
container: StyleProp<ViewStyle>;
}>;
};
type FilesToDisplayType = Attachment & {
duration: number;
paused: boolean;
progress: number;
};
const FileAttachmentGroupWithContext = (props: FileAttachmentGroupPropsWithContext) => {
const { Attachment, AudioAttachment, files, messageId, styles: stylesProp = {} } = props;
const [filesToDisplay, setFilesToDisplay] = useState<FilesToDisplayType[]>(() =>
files.map((file) => ({ ...file, duration: file.duration || 0, paused: true, progress: 0 })),
);
// Handler triggered when an audio is loaded in the message input. The initial state is defined for the audio here and the duration is set.
const onLoad = (index: string, duration: number) => {
setFilesToDisplay((prevFilesToDisplay) =>
prevFilesToDisplay.map((fileToDisplay, id) => ({
...fileToDisplay,
duration: id.toString() === index ? duration : fileToDisplay.duration,
})),
);
};
// The handler which is triggered when the audio progresses/ the thumb is dragged in the progress control. The progressed duration is set here.
const onProgress = (index: string, progress: number) => {
setFilesToDisplay((prevFilesToDisplay) =>
prevFilesToDisplay.map((filesToDisplay, id) => ({
...filesToDisplay,
progress: id.toString() === index ? progress : filesToDisplay.progress,
})),
);
};
// The handler which controls or sets the paused/played state of the audio.
const onPlayPause = (index: string, pausedStatus?: boolean) => {
if (pausedStatus === false) {
// If the status is false we set the audio with the index as playing and the others as paused.
setFilesToDisplay((prevFilesToDisplay) =>
prevFilesToDisplay.map((fileToDisplay, id) => ({
...fileToDisplay,
paused: id.toString() !== index,
})),
);
} else {
// If the status is true we simply set all the audio's paused state as true.
setFilesToDisplay((prevFilesToDisplay) =>
prevFilesToDisplay.map((fileToDisplay) => ({
...fileToDisplay,
paused: true,
})),
);
}
};
const {
theme: {
messageSimple: {
fileAttachmentGroup: { attachmentContainer, container },
},
},
} = useTheme();
return (
<View style={[styles.container, container, stylesProp.container]}>
{filesToDisplay.map((file, index) => (
<View
key={`file-by-attachment-group-${messageId}-${index}`}
style={[
{ paddingBottom: index !== files.length - 1 ? 4 : 0 },
stylesProp.attachmentContainer,
attachmentContainer,
]}
>
{(file.type === FileTypes.Audio || file.type === FileTypes.VoiceRecording) &&
isSoundPackageAvailable() ? (
<AudioAttachment
item={{
duration: file.duration,
file: {
name: file.title as string,
size: file.file_size || 0,
type: file.mime_type || '',
uri: file.asset_url || '',
waveform_data: file.waveform_data,
},
id: index.toString(),
paused: file.paused,
progress: file.progress,
type: file.type,
}}
onLoad={onLoad}
onPlayPause={onPlayPause}
onProgress={onProgress}
showSpeedSettings={true}
/>
) : (
<Attachment attachment={file} />
)}
</View>
))}
</View>
);
};
const areEqual = (
prevProps: FileAttachmentGroupPropsWithContext,
nextProps: FileAttachmentGroupPropsWithContext,
) => {
const { files: prevFiles } = prevProps;
const { files: nextFiles } = nextProps;
return prevFiles.length === nextFiles.length;
};
const MemoizedFileAttachmentGroup = React.memo(
FileAttachmentGroupWithContext,
areEqual,
) as typeof FileAttachmentGroupWithContext;
export type FileAttachmentGroupProps = Partial<
Omit<FileAttachmentGroupPropsWithContext, 'messageId'>
> &
Pick<FileAttachmentGroupPropsWithContext, 'messageId'>;
export const FileAttachmentGroup = (props: FileAttachmentGroupProps) => {
const { files: propFiles, messageId } = props;
const { files: contextFiles } = useMessageContext();
const { Attachment = AttachmentDefault, AudioAttachment } = useMessagesContext();
const files = propFiles || contextFiles;
if (!files.length) {
return null;
}
return (
<MemoizedFileAttachmentGroup
{...{
Attachment,
AudioAttachment,
files,
messageId,
}}
/>
);
};
const styles = StyleSheet.create({
container: {
padding: 4,
},
});
FileAttachmentGroup.displayName = 'FileAttachmentGroup{messageSimple{fileAttachmentGroup}}';