@sendbird/uikit-react-native
Version:
Sendbird UIKit for React Native: A feature-rich and customizable chat UI kit with messaging, channel management, and user authentication.
117 lines • 4.15 kB
JavaScript
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
import React, { useContext, useEffect, useLayoutEffect, useRef } from 'react';
import { useChannelHandler } from '@sendbird/uikit-chat-hooks';
import { isDifferentChannel, useFreshCallback, useUniqHandlerId } from '@sendbird/uikit-utils';
import ChannelThreadMessageList from '../../../components/ChannelThreadMessageList';
import { useSendbirdChat } from '../../../hooks/useContext';
import { GroupChannelThreadContexts } from '../module/moduleContext';
const GroupChannelThreadMessageList = props => {
const {
sdk
} = useSendbirdChat();
const {
setMessageToEdit
} = useContext(GroupChannelThreadContexts.Fragment);
const {
subscribe
} = useContext(GroupChannelThreadContexts.PubSub);
const {
flatListRef,
lazyScrollToBottom,
lazyScrollToIndex
} = useContext(GroupChannelThreadContexts.MessageList);
const id = useUniqHandlerId('GroupChannelThreadMessageList');
const ignorePropReached = useRef(false);
const _onTopReached = () => {
if (!ignorePropReached.current) {
props.onTopReached();
}
};
const _onBottomReached = () => {
if (!ignorePropReached.current) {
props.onBottomReached();
}
};
const scrollToBottom = useFreshCallback(async function () {
let animated = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
if (props.hasNext()) {
props.onScrolledAwayFromBottom(false);
await props.onResetMessageList();
props.onScrolledAwayFromBottom(false);
lazyScrollToBottom({
animated
});
} else {
lazyScrollToBottom({
animated
});
}
});
useLayoutEffect(() => {
if (props.startingPoint) {
const foundMessageIndex = props.messages.findIndex(it => it.createdAt === props.startingPoint);
const isIncludedInList = foundMessageIndex > -1;
if (isIncludedInList) {
ignorePropReached.current = true;
const timeout = 300;
lazyScrollToIndex({
index: foundMessageIndex,
animated: true,
timeout: timeout
});
setTimeout(() => {
ignorePropReached.current = false;
}, timeout + 50);
}
}
}, [props.startingPoint]);
useChannelHandler(sdk, id, {
onReactionUpdated(channel, event) {
if (isDifferentChannel(channel, props.channel)) return;
const recentMessage = props.messages[0];
const isRecentMessage = recentMessage && recentMessage.messageId === event.messageId;
const scrollReachedBottomAndCanScroll = !props.scrolledAwayFromBottom && !props.hasNext();
if (isRecentMessage && scrollReachedBottomAndCanScroll) {
lazyScrollToBottom({
animated: true,
timeout: 250
});
}
}
});
useEffect(() => {
return subscribe(_ref => {
let {
type
} = _ref;
switch (type) {
case 'TYPING_BUBBLE_RENDERED':
case 'MESSAGES_RECEIVED':
{
if (!props.scrolledAwayFromBottom) {
scrollToBottom(true);
}
break;
}
case 'MESSAGE_SENT_SUCCESS':
case 'MESSAGE_SENT_PENDING':
{
scrollToBottom(false);
break;
}
}
});
}, [props.scrolledAwayFromBottom]);
return /*#__PURE__*/React.createElement(ChannelThreadMessageList, _extends({}, props, {
ref: flatListRef,
onTopReached: _onTopReached,
onBottomReached: _onBottomReached,
onEditMessage: setMessageToEdit,
onPressNewMessagesButton: scrollToBottom,
onPressScrollToBottomButton: scrollToBottom,
renderNewMessagesButton: null,
renderScrollToBottomButton: null
}));
};
export default /*#__PURE__*/React.memo(GroupChannelThreadMessageList);
//# sourceMappingURL=GroupChannelThreadMessageList.js.map