UNPKG

@gathertown/uikit-react-native

Version:

Sendbird UIKit for React Native: A feature-rich and customizable chat UI kit with messaging, channel management, and user authentication.

158 lines (154 loc) 7.63 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _react = _interopRequireWildcard(require("react")); var _uikitChatHooks = require("@gathertown/uikit-chat-hooks"); var _uikitReactNativeFoundation = require("@gathertown/uikit-react-native-foundation"); var _uikitUtils = require("@gathertown/uikit-utils"); var _ChannelMessageList = _interopRequireDefault(require("../../../components/ChannelMessageList")); var _constants = require("../../../constants"); 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 GroupChannelMessageList = props => { const toast = (0, _uikitReactNativeFoundation.useToast)(); const { STRINGS } = (0, _useContext.useLocalization)(); const { sdk } = (0, _useContext.useSendbirdChat)(); const { setMessageToEdit, setMessageToReply } = (0, _react.useContext)(_moduleContext.GroupChannelContexts.Fragment); const { subscribe } = (0, _react.useContext)(_moduleContext.GroupChannelContexts.PubSub); const id = (0, _uikitUtils.useUniqHandlerId)('GroupChannelMessageList'); const ref = (0, _react.useRef)(null); const isFirstMount = (0, _uikitUtils.useIsFirstMount)(); // FIXME: Workaround, should run after data has been applied to UI. const lazyScrollToBottom = function () { let animated = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false; let timeout = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0; setTimeout(() => { var _ref$current; (_ref$current = ref.current) === null || _ref$current === void 0 ? void 0 : _ref$current.scrollToOffset({ offset: 0, animated }); }, timeout); }; // FIXME: Workaround, should run after data has been applied to UI. const lazyScrollToIndex = function () { let index = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0; let animated = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; let timeout = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0; setTimeout(() => { var _ref$current2; (_ref$current2 = ref.current) === null || _ref$current2 === void 0 ? void 0 : _ref$current2.scrollToIndex({ index, animated, viewPosition: 0.5 }); }, timeout); }; const scrollToMessage = (0, _uikitUtils.useFreshCallback)(function (createdAt) { let focusAnimated = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; const foundMessageIndex = props.messages.findIndex(it => it.createdAt === createdAt); const isIncludedInList = foundMessageIndex > -1; if (isIncludedInList) { if (focusAnimated) { setTimeout(() => props.onUpdateSearchItem({ startingPoint: createdAt }), _constants.MESSAGE_FOCUS_ANIMATION_DELAY); } lazyScrollToIndex(foundMessageIndex, true, isFirstMount ? _constants.MESSAGE_SEARCH_SAFE_SCROLL_DELAY : 0); } else { if (props.channel.messageOffsetTimestamp <= createdAt) { if (focusAnimated) props.onUpdateSearchItem({ startingPoint: createdAt }); props.onResetMessageListWithStartingPoint(createdAt); } else { return false; } } return true; }); const scrollToBottom = (0, _uikitUtils.useFreshCallback)(function () { let animated = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false; if (props.hasNext()) { props.onUpdateSearchItem(undefined); props.onScrolledAwayFromBottom(false); props.onResetMessageList(() => { props.onScrolledAwayFromBottom(false); lazyScrollToBottom(animated); }); } else { lazyScrollToBottom(animated); } }); (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(true, 250); } } }); (0, _react.useEffect)(() => { return subscribe(_ref => { let { type } = _ref; switch (type) { case 'MESSAGES_RECEIVED': { if (!props.scrolledAwayFromBottom) { scrollToBottom(true); } break; } case 'MESSAGE_SENT_SUCCESS': case 'MESSAGE_SENT_PENDING': { scrollToBottom(false); break; } } }); }, [props.scrolledAwayFromBottom]); // Only trigger once when message list mount with initial props.searchItem // - Search screen + searchItem > mount message list // - Reset message list + searchItem > re-mount message list (0, _react.useEffect)(() => { if (isFirstMount && props.searchItem) { scrollToMessage(props.searchItem.startingPoint); } }, [isFirstMount]); const onPressParentMessage = (0, _uikitUtils.useFreshCallback)(message => { const canScrollToParent = scrollToMessage(message.createdAt, true); if (!canScrollToParent) toast.show(STRINGS.TOAST.FIND_PARENT_MSG_ERROR, 'error'); }); return /*#__PURE__*/_react.default.createElement(_uikitReactNativeFoundation.LocalImageProvider, null, /*#__PURE__*/_react.default.createElement(_ChannelMessageList.default, _extends({}, props, { ref: ref, onReplyMessage: setMessageToReply, onEditMessage: setMessageToEdit, onPressParentMessage: onPressParentMessage, onPressNewMessagesButton: scrollToBottom, onPressScrollToBottomButton: scrollToBottom }))); }; var _default = /*#__PURE__*/_react.default.memo(GroupChannelMessageList); exports.default = _default; //# sourceMappingURL=GroupChannelMessageList.js.map