stream-chat-react
Version:
React components to create chat conversations or livestream style chat
104 lines (103 loc) • 4.19 kB
JavaScript
import { useMemo } from 'react';
import { isDate, isDayOrMoment } from '../../../i18n';
export const useCreateChannelStateContext = (value) => {
const { acceptedFiles, channel, channelCapabilitiesArray = [], channelConfig, channelUnreadUiState, debounceURLEnrichmentMs, dragAndDropWindow, enrichURLForPreview, error, findURLFn, giphyVersion, hasMore, hasMoreNewer, highlightedMessageId, imageAttachmentSizeHandler, loading, loadingMore, maxNumberOfFiles, members, messages = [], multipleUploads, mutes, notifications, onLinkPreviewDismissed, pinnedMessages, quotedMessage, read = {}, shouldGenerateVideoThumbnail, skipMessageDataMemoization, suppressAutoscroll, thread, threadHasMore, threadLoadingMore, threadMessages = [], videoAttachmentSizeHandler, watcher_count, watcherCount, watchers, } = value;
const channelId = channel.cid;
const lastRead = channel.initialized && channel.lastRead()?.getTime();
const membersLength = Object.keys(members || []).length;
const notificationsLength = notifications.length;
const readUsers = Object.values(read);
const readUsersLength = readUsers.length;
const readUsersLastReads = readUsers
.map(({ last_read }) => last_read.toISOString())
.join();
const threadMessagesLength = threadMessages?.length;
const channelCapabilities = {};
channelCapabilitiesArray.forEach((capability) => {
channelCapabilities[capability] = true;
});
const memoizedMessageData = skipMessageDataMemoization
? messages
: messages
.map(({ deleted_at, latest_reactions, pinned, reply_count, status, updated_at, user, }) => `${deleted_at}${latest_reactions ? latest_reactions.map(({ type }) => type).join() : ''}${pinned}${reply_count}${status}${updated_at && (isDayOrMoment(updated_at) || isDate(updated_at))
? updated_at.toISOString()
: updated_at || ''}${user?.updated_at}`)
.join();
const memoizedThreadMessageData = threadMessages
.map(({ deleted_at, latest_reactions, pinned, status, updated_at, user }) => `${deleted_at}${latest_reactions ? latest_reactions.map(({ type }) => type).join() : ''}${pinned}${status}${updated_at && (isDayOrMoment(updated_at) || isDate(updated_at))
? updated_at.toISOString()
: updated_at || ''}${user?.updated_at}`)
.join();
const channelStateContext = useMemo(() => ({
acceptedFiles,
channel,
channelCapabilities,
channelConfig,
channelUnreadUiState,
debounceURLEnrichmentMs,
dragAndDropWindow,
enrichURLForPreview,
error,
findURLFn,
giphyVersion,
hasMore,
hasMoreNewer,
highlightedMessageId,
imageAttachmentSizeHandler,
loading,
loadingMore,
maxNumberOfFiles,
members,
messages,
multipleUploads,
mutes,
notifications,
onLinkPreviewDismissed,
pinnedMessages,
quotedMessage,
read,
shouldGenerateVideoThumbnail,
suppressAutoscroll,
thread,
threadHasMore,
threadLoadingMore,
threadMessages,
videoAttachmentSizeHandler,
watcher_count,
watcherCount,
watchers,
}),
// eslint-disable-next-line react-hooks/exhaustive-deps
[
channel.data?.name, // otherwise ChannelHeader will not be updated
channelId,
channelUnreadUiState,
debounceURLEnrichmentMs,
enrichURLForPreview,
error,
findURLFn,
hasMore,
hasMoreNewer,
highlightedMessageId,
lastRead,
loading,
loadingMore,
membersLength,
memoizedMessageData,
memoizedThreadMessageData,
notificationsLength,
onLinkPreviewDismissed,
quotedMessage,
readUsersLength,
readUsersLastReads,
shouldGenerateVideoThumbnail,
skipMessageDataMemoization,
suppressAutoscroll,
thread,
threadHasMore,
threadLoadingMore,
threadMessagesLength,
watcherCount,
]);
return channelStateContext;
};