Repository navigation
Expand file tree
/
Copy pathVirtualizedMessageListComponents.tsx
More file actions
209 lines (189 loc) Β· 6.41 KB
/
Copy pathVirtualizedMessageListComponents.tsx
File metadata and controls
209 lines (189 loc) Β· 6.41 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import clsx from 'clsx';
import throttle from 'lodash.throttle';
import React from 'react';
import type { ItemProps, ListItem } from 'react-virtuoso';
import { EmptyStateIndicator as DefaultEmptyStateIndicator } from '../EmptyStateIndicator';
import { LoadingIndicator as DefaultLoadingIndicator } from '../Loading';
import { Message } from '../Message';
import { useComponentContext } from '../../context';
import type { GroupStyle, RenderedMessage } from './utils';
import { getIsFirstUnreadMessage, isDateSeparatorMessage, isIntroMessage } from './utils';
import { omitUndefinedProps } from './omitUndefinedProps';
import type { VirtuosoContext } from './VirtualizedMessageList';
import type { UnknownType } from '../../types/types';
import { useThreadContext } from '../Threads';
const PREPEND_OFFSET = 10 ** 7;
export function calculateItemIndex(virtuosoIndex: number, numItemsPrepended: number) {
return virtuosoIndex + numItemsPrepended - PREPEND_OFFSET;
}
export function calculateFirstItemIndex(numItemsPrepended: number) {
return PREPEND_OFFSET - numItemsPrepended;
}
export const makeItemsRenderedHandler = (
renderedItemsActions: Array<(msg: RenderedMessage[]) => void>,
processedMessages: RenderedMessage[],
) =>
throttle((items: ListItem<UnknownType>[]) => {
const renderedMessages = items
.map((item) => {
if (!item.originalIndex) return undefined;
return processedMessages[calculateItemIndex(item.originalIndex, PREPEND_OFFSET)];
})
.filter((msg) => !!msg);
renderedItemsActions.forEach((action) =>
action(renderedMessages as RenderedMessage[]),
);
}, 200);
type CommonVirtuosoComponentProps = {
context?: VirtuosoContext;
};
// `display: inline-block` traps the items' CSS margins, preventing incorrect item measurements.
export const Item = ({ context, ...props }: ItemProps & CommonVirtuosoComponentProps) => {
if (!context) return <></>;
const message =
context.processedMessages[
calculateItemIndex(props['data-item-index'], context.numItemsPrepended)
];
const groupStyles: GroupStyle = context.messageGroupStyles[message.id];
return (
<div
{...props}
className={
context?.customClasses?.virtualMessage ||
clsx('str-chat__virtual-list-message-wrapper str-chat__li', {
[`str-chat__li--${groupStyles}`]: groupStyles,
})
}
/>
);
};
export const Header = ({ context }: CommonVirtuosoComponentProps) => {
const { LoadingIndicator = DefaultLoadingIndicator } = useComponentContext();
return (
<>
{context?.head}
{context?.loadingMore && LoadingIndicator && (
<div className='str-chat__virtual-list__loading'>
<LoadingIndicator />
</div>
)}
</>
);
};
export const EmptyPlaceholder = ({ context }: CommonVirtuosoComponentProps) => {
const thread = useThreadContext();
const isThreadList = !!thread;
const { EmptyStateIndicator = DefaultEmptyStateIndicator } = useComponentContext();
// Virtuoso can render the empty placeholder first even when `totalCount` > 0, so hide it while
// there are messages.
if (
typeof context?.processedMessages !== 'undefined' &&
context.processedMessages.length > 0
)
return null;
return (
<>
{EmptyStateIndicator && (
<EmptyStateIndicator listType={isThreadList ? 'thread' : 'message'} />
)}
</>
);
};
export const messageRenderer = (
virtuosoIndex: number,
_data: UnknownType,
virtuosoContext: VirtuosoContext,
) => {
const {
additionalMessageComposerProps,
channel,
closeReactionSelectorOnClick,
customMessageRenderer,
DateSeparator,
firstUnreadMessageId,
focusedMessageId,
formatDate,
lastOwnMessage,
lastReadDate,
lastReadMessageId,
lastReceivedMessageId,
messageGroupStyles,
MessageSystem,
numItemsPrepended,
onMentionsClick,
onMentionsHover,
onUserClick,
onUserHover,
ownMessagesDeliveredToOthers,
ownMessagesReadByOthers,
processedMessages: messageList,
reactionDetailsSort,
renderText,
returnAllReadData,
showAvatar,
sortReactions,
unreadMessageCount = 0,
UnreadMessagesSeparator,
unsafeHTML,
virtuosoRef,
} = virtuosoContext;
const streamMessageIndex = calculateItemIndex(virtuosoIndex, numItemsPrepended);
if (customMessageRenderer) {
return customMessageRenderer(messageList, streamMessageIndex);
}
const message = messageList[streamMessageIndex];
// Virtuoso breaks on null or zero height.
if (!message || isIntroMessage(message)) return <div style={{ height: '1px' }}></div>;
if (isDateSeparatorMessage(message)) {
return DateSeparator ? (
<DateSeparator date={message.date} unread={message.unread} />
) : null;
}
if (message.type === 'system') {
return MessageSystem ? (
<MessageSystem {...omitUndefinedProps({ unsafeHTML })} message={message} />
) : null;
}
const isFirstUnreadMessage = getIsFirstUnreadMessage({
channel,
firstUnreadMessageId,
isFirstMessage: streamMessageIndex === 0,
lastReadAt: lastReadDate,
lastReadMessageId,
message,
previousMessage: streamMessageIndex ? messageList[streamMessageIndex - 1] : undefined,
unreadCount: unreadMessageCount,
});
return (
<>
{isFirstUnreadMessage && (
<div className='str-chat__unread-messages-separator-wrapper'>
<UnreadMessagesSeparator unreadCount={unreadMessageCount} />
</div>
)}
<Message
additionalMessageComposerProps={additionalMessageComposerProps}
autoscrollToBottom={virtuosoRef.current?.autoscrollToBottom}
closeReactionSelectorOnClick={closeReactionSelectorOnClick}
deliveredTo={ownMessagesDeliveredToOthers[message.id] || []}
formatDate={formatDate}
groupStyles={[messageGroupStyles[message.id] ?? '']}
highlighted={focusedMessageId === message.id}
lastOwnMessage={lastOwnMessage}
lastReceivedId={lastReceivedMessageId}
message={message}
onMentionsClick={onMentionsClick}
onMentionsHover={onMentionsHover}
onUserClick={onUserClick}
onUserHover={onUserHover}
reactionDetailsSort={reactionDetailsSort}
readBy={ownMessagesReadByOthers[message.id] || []}
renderText={renderText}
returnAllReadData={returnAllReadData}
showAvatar={showAvatar}
sortReactions={sortReactions}
unsafeHTML={unsafeHTML}
/>
</>
);
};