@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.
127 lines (126 loc) • 5.67 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _uikitChatHooks = require("@sendbird/uikit-chat-hooks");
var _uikitUtils = require("@sendbird/uikit-utils");
var _ChannelThreadMessageList = _interopRequireDefault(require("../../../components/ChannelThreadMessageList"));
var _useContext = require("../../../hooks/useContext");
var _moduleContext = require("../module/moduleContext");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
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); }
const GroupChannelThreadMessageList = props => {
const {
sdk
} = (0, _useContext.useSendbirdChat)();
const {
setMessageToEdit
} = (0, _react.useContext)(_moduleContext.GroupChannelThreadContexts.Fragment);
const {
subscribe
} = (0, _react.useContext)(_moduleContext.GroupChannelThreadContexts.PubSub);
const {
flatListRef,
lazyScrollToBottom,
lazyScrollToIndex
} = (0, _react.useContext)(_moduleContext.GroupChannelThreadContexts.MessageList);
const id = (0, _uikitUtils.useUniqHandlerId)('GroupChannelThreadMessageList');
const ignorePropReached = (0, _react.useRef)(false);
const _onTopReached = () => {
if (!ignorePropReached.current) {
props.onTopReached();
}
};
const _onBottomReached = () => {
if (!ignorePropReached.current) {
props.onBottomReached();
}
};
const scrollToBottom = (0, _uikitUtils.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
});
}
});
(0, _react.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]);
(0, _uikitChatHooks.useChannelHandler)(sdk, id, {
onReactionUpdated(channel, event) {
if ((0, _uikitUtils.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
});
}
}
});
(0, _react.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.default.createElement(_ChannelThreadMessageList.default, _extends({}, props, {
ref: flatListRef,
onTopReached: _onTopReached,
onBottomReached: _onBottomReached,
onEditMessage: setMessageToEdit,
onPressNewMessagesButton: scrollToBottom,
onPressScrollToBottomButton: scrollToBottom,
renderNewMessagesButton: null,
renderScrollToBottomButton: null
}));
};
var _default = /*#__PURE__*/_react.default.memo(GroupChannelThreadMessageList);
exports.default = _default;
//# sourceMappingURL=GroupChannelThreadMessageList.js.map
;