-
Notifications
You must be signed in to change notification settings - Fork 298
Expand file tree
/
Copy pathMessageInputFlat.tsx
More file actions
150 lines (137 loc) Β· 4.93 KB
/
MessageInputFlat.tsx
File metadata and controls
150 lines (137 loc) Β· 4.93 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
import React from 'react';
import {
AttachmentSelector as DefaultAttachmentSelector,
SimpleAttachmentSelector,
} from './AttachmentSelector/AttachmentSelector';
import {
AttachmentPreviewList as DefaultAttachmentPreviewList,
VoiceRecordingPreviewSlot as DefaultVoiceRecordingPreviewSlot,
} from './AttachmentPreviewList';
import { AudioRecorder as DefaultAudioRecorder } from '../MediaRecorder';
import { EditedMessagePreview as DefaultEditedMessagePreview } from './EditedMessagePreview';
import { QuotedMessagePreview as DefaultQuotedMessagePreview } from './QuotedMessagePreview';
import { LinkPreviewList as DefaultLinkPreviewList } from './LinkPreviewList';
import { SendToChannelCheckbox as DefaultSendToChannelCheckbox } from './SendToChannelCheckbox';
import { TextareaComposer as DefaultTextareaComposer } from '../TextareaComposer';
import { useMessageInputContext } from '../../context/MessageInputContext';
import { useComponentContext } from '../../context/ComponentContext';
import { useMessageContext } from '../../context';
import { WithDragAndDropUpload } from './WithDragAndDropUpload';
import {
AdditionalMessageComposerActions as DefaultAdditionalMessageComposerActions,
MessageComposerActions,
} from './MessageComposerActions';
import { useMessageComposer } from './hooks';
import { useStateStore } from '../../store';
import {
type AttachmentManagerState,
LinkPreviewsManager,
type LinkPreviewsManagerState,
type MessageComposerState,
} from 'stream-chat';
import { CommandChip as DefaultCommandChip } from './CommandChip';
const messageComposerStateSelector = ({
editedMessage,
quotedMessage,
}: MessageComposerState) => ({
editedMessage,
quotedMessage,
});
const attachmentManagerStateSelector = (state: AttachmentManagerState) => ({
attachments: state.attachments,
});
const linkPreviewsManagerStateSelector = (state: LinkPreviewsManagerState) => ({
linkPreviews: Array.from(state.previews.values()).filter(
(preview) =>
LinkPreviewsManager.previewIsLoaded(preview) ||
LinkPreviewsManager.previewIsLoading(preview),
),
});
const MessageComposerPreviews = () => {
const {
AttachmentPreviewList = DefaultAttachmentPreviewList,
EditedMessagePreview = DefaultEditedMessagePreview,
LinkPreviewList = DefaultLinkPreviewList,
QuotedMessagePreview = DefaultQuotedMessagePreview,
VoiceRecordingPreviewSlot = DefaultVoiceRecordingPreviewSlot,
} = useComponentContext();
const messageComposer = useMessageComposer();
const { editedMessage, quotedMessage } = useStateStore(
messageComposer.state,
messageComposerStateSelector,
);
const { attachments } = useStateStore(
messageComposer.attachmentManager.state,
attachmentManagerStateSelector,
);
const { linkPreviewsManager } = messageComposer;
const { linkPreviews } = useStateStore(
linkPreviewsManager.state,
linkPreviewsManagerStateSelector,
);
if (
!quotedMessage &&
attachments.length === 0 &&
linkPreviews.length === 0 &&
!editedMessage
)
return null;
// todo: pass the entity arrays from here so that the preview lists do not have to subscribe to the composer state changes too?
return (
<div className='str-chat__message-composer-previews'>
{editedMessage ? (
<div className='str-chat__message-composer-previews'>
<EditedMessagePreview
message={editedMessage}
onCancel={() => {
messageComposer.clear();
}}
/>
</div>
) : (
<QuotedMessagePreview />
)}
<VoiceRecordingPreviewSlot />
<AttachmentPreviewList />
<LinkPreviewList />
</div>
);
};
export const MessageInputFlat = () => {
const { message } = useMessageContext();
const { recordingController } = useMessageInputContext();
const {
AdditionalMessageComposerActions = DefaultAdditionalMessageComposerActions,
AttachmentSelector = message ? SimpleAttachmentSelector : DefaultAttachmentSelector,
AudioRecorder = DefaultAudioRecorder,
CommandChip = DefaultCommandChip,
SendToChannelCheckbox = DefaultSendToChannelCheckbox,
TextareaComposer = DefaultTextareaComposer,
} = useComponentContext();
return (
<WithDragAndDropUpload
className='str-chat__message-composer-container'
component='div'
>
<div className='str-chat__message-composer'>
{recordingController.recordingState ? (
<AudioRecorder />
) : (
<>
<AttachmentSelector />
<div className='str-chat__message-composer-compose-area'>
<MessageComposerPreviews />
<div className='str-chat__message-composer-controls'>
<CommandChip />
<TextareaComposer />
<AdditionalMessageComposerActions />
<MessageComposerActions />
</div>
</div>
</>
)}
</div>
<SendToChannelCheckbox />
</WithDragAndDropUpload>
);
};