UNPKG

stream-chat-react

Version:

React components to create chat conversations or livestream style chat

38 lines (37 loc) 2.02 kB
import { useCallback, useEffect, useState } from 'react'; /** * Controls the logic when an `UnreadMessagesNotification` component should be shown. * In virtualized message list there is no notion of being scrolled below or above `UnreadMessagesSeparator`. * Therefore, the `UnreadMessagesNotification` component is rendered based on timestamps. * If the there are unread messages in the channel and the `VirtualizedMessageList` renders * messages created later than the last read message in the channel, then the * `UnreadMessagesNotification` component is rendered. This is an approximate equivalent to being * scrolled below the `UnreadMessagesNotification` component. * @param lastRead * @param showAlways * @param unreadCount */ export const useUnreadMessagesNotificationVirtualized = ({ lastRead, showAlways, unreadCount, }) => { const [show, setShow] = useState(false); const toggleShowUnreadMessagesNotification = useCallback((renderedMessages) => { if (!unreadCount) return; const firstRenderedMessage = renderedMessages[0]; const lastRenderedMessage = renderedMessages.slice(-1)[0]; if (!(firstRenderedMessage && lastRenderedMessage)) return; const firstRenderedMessageTime = new Date(firstRenderedMessage.created_at ?? 0).getTime(); const lastRenderedMessageTime = new Date(lastRenderedMessage.created_at ?? 0).getTime(); const lastReadTime = new Date(lastRead ?? 0).getTime(); const scrolledBelowSeparator = !!lastReadTime && firstRenderedMessageTime > lastReadTime; const scrolledAboveSeparator = !!lastReadTime && lastRenderedMessageTime < lastReadTime; setShow(showAlways ? scrolledBelowSeparator || scrolledAboveSeparator : scrolledBelowSeparator); }, [lastRead, showAlways, unreadCount]); useEffect(() => { if (!unreadCount) setShow(false); }, [unreadCount]); return { show, toggleShowUnreadMessagesNotification }; };