forked from RocketChat/EmbeddedChat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessageAggregator.js
More file actions
155 lines (147 loc) · 4.77 KB
/
MessageAggregator.js
File metadata and controls
155 lines (147 loc) · 4.77 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
import React, { useState, useMemo } from 'react';
import { isSameDay, format } from 'date-fns';
import {
Box,
Sidebar,
Popup,
useTheme,
ActionButton,
Icon,
} from '@embeddedchat/ui-elements';
import { MessageDivider } from '../../Message/MessageDivider';
import Message from '../../Message/Message';
import getMessageAggregatorStyles from './MessageAggregator.styles';
import { useMessageStore, useSidebarStore } from '../../../store';
import { useSetMessageList } from '../../../hooks/useSetMessageList';
import LoadingIndicator from './LoadingIndicator';
import NoMessagesIndicator from './NoMessageIndicator';
import FileDisplay from '../../FileMessage/FileMessage';
import useSetExclusiveState from '../../../hooks/useSetExclusiveState';
export const MessageAggregator = ({
title,
iconName,
noMessageInfo,
shouldRender,
fetchedMessageList,
searchProps,
searchFiltered,
fetching,
type = 'message',
viewType = 'Sidebar',
}) => {
const { theme } = useTheme();
const styles = getMessageAggregatorStyles(theme);
const setExclusiveState = useSetExclusiveState();
const messages = useMessageStore((state) => state.messages);
const threadMessages = useMessageStore((state) => state.threadMessages) || [];
const allMessages = useMemo(
() => [...messages, ...threadMessages],
[messages, threadMessages]
);
const [messageRendered, setMessageRendered] = useState(false);
const { loading, messageList } = useSetMessageList(
fetchedMessageList || searchFiltered || allMessages,
shouldRender
);
const setShowSidebar = useSidebarStore((state) => state.setShowSidebar);
const setJumpToMessage = (msgId) => {
if (msgId) {
const element = document.getElementById(`ec-message-body-${msgId}`);
if (element) {
setShowSidebar(false);
element.scrollIntoView({ behavior: 'smooth', block: 'center' });
}
}
};
const isMessageNewDay = (current, previous) =>
!previous ||
shouldRender(previous) ||
!isSameDay(new Date(current.ts), new Date(previous.ts));
const noMessages = messageList?.length === 0 || !messageRendered;
const ViewComponent = viewType === 'Popup' ? Popup : Sidebar;
return (
<ViewComponent
title={title}
iconName={iconName}
searchProps={searchProps}
onClose={() => setExclusiveState(null)}
style={{ padding: 0, zIndex: window.innerWidth <= 780 ? 1 : null }}
{...(viewType === 'Popup'
? {
isPopupHeader: true,
}
: {})}
>
{fetching || loading ? (
<LoadingIndicator />
) : (
<Box
css={[
styles.listContainerStyles,
noMessages && styles.noMessageStyles,
]}
>
{noMessages && (
<NoMessagesIndicator iconName={iconName} message={noMessageInfo} />
)}
{messageList.map((msg, index, arr) => {
const newDay = isMessageNewDay(msg, arr[index - 1]);
if (!messageRendered && shouldRender(msg)) {
setMessageRendered(true);
}
return (
<React.Fragment key={msg._id}>
{type === 'message' && newDay && (
<MessageDivider>
{format(new Date(msg.ts), 'MMMM d, yyyy')}
</MessageDivider>
)}
{type === 'file' ? (
<FileDisplay
key={`${msg._id}-aggregated`}
fileMessage={msg}
/>
) : (
<Box
position="relative"
style={{
display: 'flex',
}}
>
<Message
key={`${msg._id}-aggregated`}
message={msg}
newDay={false}
type="default"
showAvatar
showToolbox={false}
showRoles={false}
isInSidebar
style={{
flex: 1,
paddingLeft: 3,
paddingRight: 2,
minWidth: 0,
}}
/>
<ActionButton
square
ghost
onClick={() => setJumpToMessage(msg._id)}
css={{
position: 'relative',
zIndex: 10,
}}
>
<Icon name="arrow-back" size="1.25rem" />
</ActionButton>
</Box>
)}
</React.Fragment>
);
})}
</Box>
)}
</ViewComponent>
);
};