-
Notifications
You must be signed in to change notification settings - Fork 299
Expand file tree
/
Copy pathAttachmentPreviewList.tsx
More file actions
132 lines (127 loc) Β· 5.13 KB
/
AttachmentPreviewList.tsx
File metadata and controls
132 lines (127 loc) Β· 5.13 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
import { type ComponentType, useMemo } from 'react';
import React from 'react';
import {
isLocalAttachment,
isLocalAudioAttachment,
isLocalFileAttachment,
isLocalImageAttachment,
isLocalVideoAttachment,
isLocalVoiceRecordingAttachment,
isScrapedContent,
isVoiceRecordingAttachment,
} from 'stream-chat';
import {
UnsupportedAttachmentPreview as DefaultUnknownAttachmentPreview,
type UnsupportedAttachmentPreviewProps,
} from './UnsupportedAttachmentPreview';
import {
FileAttachmentPreview as DefaultFileAttachmentPreview,
type FileAttachmentPreviewProps,
} from './FileAttachmentPreview';
import {
type AudioAttachmentPreviewProps,
AudioAttachmentPreview as DefaultAudioAttachmentPreview,
} from './AudioAttachmentPreview';
import { type ImageAttachmentPreviewProps } from './ImageAttachmentPreview';
import { useAttachmentsForPreview, useMessageComposer } from '../hooks';
import {
GeolocationPreview as DefaultGeolocationPreview,
type GeolocationPreviewProps,
} from './GeolocationPreview';
import {
MediaAttachmentPreview,
type MediaAttachmentPreviewProps,
} from './MediaAttachmentPreview';
export type AttachmentPreviewListProps = {
AudioAttachmentPreview?: ComponentType<AudioAttachmentPreviewProps>;
FileAttachmentPreview?: ComponentType<FileAttachmentPreviewProps>;
GeolocationPreview?: ComponentType<GeolocationPreviewProps>;
ImageAttachmentPreview?: ComponentType<ImageAttachmentPreviewProps>;
UnsupportedAttachmentPreview?: ComponentType<UnsupportedAttachmentPreviewProps>;
VideoAttachmentPreview?: ComponentType<MediaAttachmentPreviewProps>;
};
export const AttachmentPreviewList = ({
AudioAttachmentPreview = DefaultAudioAttachmentPreview,
FileAttachmentPreview = DefaultFileAttachmentPreview,
GeolocationPreview = DefaultGeolocationPreview,
ImageAttachmentPreview = MediaAttachmentPreview,
UnsupportedAttachmentPreview = DefaultUnknownAttachmentPreview,
VideoAttachmentPreview = MediaAttachmentPreview,
}: AttachmentPreviewListProps) => {
const messageComposer = useMessageComposer();
// todo: we could also allow to attach poll to a message composition
const { attachments, location } = useAttachmentsForPreview();
const filteredAttachments = useMemo(
() => attachments.filter((a) => !isVoiceRecordingAttachment(a)),
[attachments],
);
if (!filteredAttachments.length && !location) return null;
return (
<div className='str-chat__attachment-preview-list'>
{location && (
<GeolocationPreview
location={location}
// It is not possible to nullify shared_location field so we do not show a preview when editing
// to prevent a user from wanting to remove the location
remove={
messageComposer.editedMessage
? undefined
: messageComposer.locationComposer.initState
}
/>
)}
{attachments.map((attachment) => {
if (isScrapedContent(attachment)) return null;
// Voice recordings are rendered in the dedicated slot above (VoiceRecordingPreviewSlot)
if (isLocalVoiceRecordingAttachment(attachment)) return null;
if (isLocalAudioAttachment(attachment)) {
return (
<AudioAttachmentPreview
attachment={attachment}
handleRetry={messageComposer.attachmentManager.uploadAttachment}
key={attachment.localMetadata.id || attachment.asset_url}
removeAttachments={messageComposer.attachmentManager.removeAttachments}
/>
);
} else if (isLocalVideoAttachment(attachment)) {
return (
<VideoAttachmentPreview
attachment={attachment}
handleRetry={messageComposer.attachmentManager.uploadAttachment}
key={attachment.localMetadata.id || attachment.asset_url}
removeAttachments={messageComposer.attachmentManager.removeAttachments}
/>
);
} else if (isLocalImageAttachment(attachment)) {
return (
<ImageAttachmentPreview
attachment={attachment}
handleRetry={messageComposer.attachmentManager.uploadAttachment}
key={attachment.localMetadata.id || attachment.image_url}
removeAttachments={messageComposer.attachmentManager.removeAttachments}
/>
);
} else if (isLocalFileAttachment(attachment)) {
return (
<FileAttachmentPreview
attachment={attachment}
handleRetry={messageComposer.attachmentManager.uploadAttachment}
key={attachment.localMetadata.id || attachment.asset_url}
removeAttachments={messageComposer.attachmentManager.removeAttachments}
/>
);
} else if (isLocalAttachment(attachment)) {
return (
<UnsupportedAttachmentPreview
attachment={attachment}
handleRetry={messageComposer.attachmentManager.uploadAttachment}
key={attachment.localMetadata.id}
removeAttachments={messageComposer.attachmentManager.removeAttachments}
/>
);
}
return null;
})}
</div>
);
};