cometchat-react-ui-kit2
Version:
CometChat UI Kit for React App
821 lines (787 loc) • 45.2 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.CometChatConversationList = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _react = _interopRequireWildcard(require("react"));
var _chat = require("@cometchat-pro/chat");
var _propTypes = _interopRequireDefault(require("prop-types"));
var _controller = require("./controller");
var _hooks = require("./hooks");
var styles = _interopRequireWildcard(require("./style"));
var _spinner = _interopRequireDefault(require("./resources/spinner.svg"));
var _Shared = require("../../Shared");
var _ = require("../");
var _Chats = require("../../Chats");
var _ConversationListCustomView = require("../ConversationListCustomView");
var _delete = _interopRequireDefault(require("./resources/delete.svg"));
var _this = void 0;
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(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 ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
/**
*
* @version 1.0.0
* @author CometChatTeam
* @description CometChatConversationList component retrieves the latest conversations that a CometChat logged-in user has been a part of.
* The state of the component is communicated via 3 states i.e empty, loading and error.
*
*/
var ConversationList = /*#__PURE__*/_react["default"].forwardRef(function (props, ref) {
var _theme$palette, _theme$palette2;
/**
* Props destructuring
*/
var activeConversation = props.activeConversation,
emptyText = props.emptyText,
errorText = props.errorText,
conversationType = props.conversationType,
limit = props.limit,
hideError = props.hideError,
tags = props.tags,
userAndGroupTags = props.userAndGroupTags,
loadingIconURL = props.loadingIconURL,
style = props.style,
enableSoundForMessages = props.enableSoundForMessages,
customIncomingMessageSound = props.customIncomingMessageSound,
customView = props.customView,
conversationListItemConfiguration = props.conversationListItemConfiguration,
theme = props.theme;
/**
* Component internal state
*/
var loggedInUser = (0, _react.useRef)(null);
var conversationListManager = (0, _react.useRef)(new _controller.ConversationListManager({
conversationType: conversationType,
limit: limit,
tags: tags,
userAndGroupTags: userAndGroupTags
}));
var _useState = (0, _react.useState)([]),
_useState2 = (0, _slicedToArray2["default"])(_useState, 2),
conversationList = _useState2[0],
setConversationList = _useState2[1];
var _useState3 = (0, _react.useState)(null),
_useState4 = (0, _slicedToArray2["default"])(_useState3, 2),
callbackData = _useState4[0],
setCallbackData = _useState4[1];
var _useState5 = (0, _react.useState)((0, _Shared.localize)("LOADING")),
_useState6 = (0, _slicedToArray2["default"])(_useState5, 2),
message = _useState6[0],
setMessage = _useState6[1];
var _useState7 = (0, _react.useState)({
show: false,
message: (0, _Shared.localize)("CONFIRM_DELETE_CONVERSATION"),
confirmButtonText: (0, _Shared.localize)("DELETE"),
conversation: null,
onCancel: function onCancel() {}
}),
_useState8 = (0, _slicedToArray2["default"])(_useState7, 2),
showConfirm = _useState8[0],
setShowConfirm = _useState8[1];
/**
* Component private scoping
*/
var _conversationListItemConfiguration = new _Shared.ConversationListItemConfiguration(conversationListItemConfiguration !== null && conversationListItemConfiguration !== void 0 ? conversationListItemConfiguration : {});
var _theme = new _Shared.CometChatTheme(theme !== null && theme !== void 0 ? theme : {});
/**
* Component internal handlers/methods
*/
var conversationCallback = function conversationCallback(listenerName) {
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
args[_key - 1] = arguments[_key];
}
setCallbackData({
name: listenerName,
args: [].concat(args)
});
};
/**
* Mark the incoming message as delivered
*/
var markMessageAsDelivered = function markMessageAsDelivered(message) {
if (message.hasOwnProperty("deliveredAt") === false) {
_chat.CometChat.markAsDelivered(message);
}
};
/**
*
* If the incoming message is 1-1 conversation, and the conversation type filter is set to groups return false
* If the incoming message is group conversation, and the conversation type filter is set to users return false
* else return true
*
*/
_react["default"].useImperativeHandle(ref, function () {
return {
resetUnreadCount: resetUnreadCount,
updateLastMessage: updateLastMessage,
removeConversation: removeConversation,
deleteConversation: deleteConversation,
updateConversation: updateConversation
};
});
var filterByConversationType = function filterByConversationType(message) {
if (conversationType !== _Shared.ConversationTypeConstants.both) {
if (conversationType === _Shared.ConversationTypeConstants.users && (message === null || message === void 0 ? void 0 : message.receiverType) === _Shared.ReceiverTypeConstants.group || conversationType === _Shared.ConversationTypeConstants.groups && (message === null || message === void 0 ? void 0 : message.receiverType) === _Shared.ReceiverTypeConstants.user) {
return false;
}
}
return true;
};
/**
*
* Converting message object received in the listener callback to conversation object
*/
var getConversationFromMessage = function getConversationFromMessage(message) {
return new Promise(function (resolve, reject) {
_chat.CometChat.CometChatHelper.getConversationFromMessage(message).then(function (conversation) {
var conversationKey = conversationList.findIndex(function (c) {
return c.conversationId === conversation.conversationId;
});
if (conversationKey > -1) {
resolve({
conversationKey: conversationKey,
conversationId: conversation.conversationId,
conversationType: conversation.conversationType,
conversationWith: conversation.conversationWith,
conversation: conversationList[conversationKey],
conversations: (0, _toConsumableArray2["default"])(conversationList)
});
}
resolve({
conversationKey: conversationKey,
conversationId: conversation.conversationId,
conversationType: conversation.conversationType,
conversationWith: conversation.conversationWith,
conversation: conversation,
conversations: (0, _toConsumableArray2["default"])(conversationList)
});
})["catch"](function (error) {
_.CometChatConversationEvents.emit(_.CometChatConversationEvents.onError, error);
reject(error);
});
});
};
var getUnreadMessageCount = function getUnreadMessageCount(message) {
var conversation = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var unreadMessageCount = conversation !== null && conversation !== void 0 && conversation.unreadMessageCount ? Number(conversation === null || conversation === void 0 ? void 0 : conversation.unreadMessageCount) : 0;
unreadMessageCount = shouldIncrementCount(message) ? ++unreadMessageCount : unreadMessageCount;
return unreadMessageCount;
};
/**
*
* If the message is sent by the logged in user, return false
* If the message has category message or has incrementUnreadCount key in the metadata with value set to true, return true else return false
*
*/
var shouldIncrementCount = function shouldIncrementCount(message) {
var _message$sender, _loggedInUser$current, _message$metadata;
if ((message === null || message === void 0 ? void 0 : (_message$sender = message.sender) === null || _message$sender === void 0 ? void 0 : _message$sender.uid) === (loggedInUser === null || loggedInUser === void 0 ? void 0 : (_loggedInUser$current = loggedInUser.current) === null || _loggedInUser$current === void 0 ? void 0 : _loggedInUser$current.uid)) {
return false;
}
if ((message === null || message === void 0 ? void 0 : message.category) === _Shared.MessageCategoryConstants.message || (message === null || message === void 0 ? void 0 : (_message$metadata = message.metadata) === null || _message$metadata === void 0 ? void 0 : _message$metadata.incrementUnreadCount) === true) {
return true;
}
return false;
};
/**
* play notification sound for incoming messages
*/
var playNotificationSound = function playNotificationSound(message) {
/**
* If unreadcount is not incremented, don't play notification sound
*/
if (!shouldIncrementCount(message)) {
return false;
}
/**
* If customIncomingMessageSound url is present then CometChatSoundManager will play this sound
else play the default sound
*/
if (enableSoundForMessages && activeConversation !== null && activeConversation !== void 0 && activeConversation.conversationType && activeConversation !== null && activeConversation !== void 0 && activeConversation.conversationWith) {
var _message$getSender, _activeConversation$c, _activeConversation$c2;
var receiverType = message.getReceiverType();
var receiverId = receiverType === _Shared.ReceiverTypeConstants.user ? message === null || message === void 0 ? void 0 : (_message$getSender = message.getSender()) === null || _message$getSender === void 0 ? void 0 : _message$getSender.getUid() : message === null || message === void 0 ? void 0 : message.getReceiverId();
if (receiverId !== (activeConversation === null || activeConversation === void 0 ? void 0 : (_activeConversation$c = activeConversation.conversationWith) === null || _activeConversation$c === void 0 ? void 0 : _activeConversation$c.uid) && receiverId !== (activeConversation === null || activeConversation === void 0 ? void 0 : (_activeConversation$c2 = activeConversation.conversationWith) === null || _activeConversation$c2 === void 0 ? void 0 : _activeConversation$c2.guid)) {
if (customIncomingMessageSound) {
_Shared.CometChatSoundManager.play(customIncomingMessageSound);
} else {
_Shared.CometChatSoundManager.play(_Shared.CometChatSoundManager.Sound.incomingMessageFromOther);
}
}
}
};
/**
*
* When a user goes online/ offline
*/
var handleUsers = function handleUsers(user) {
var conversationKey = conversationList.findIndex(function (eachConversation) {
return eachConversation.conversationType && eachConversation.conversationType === _Shared.ReceiverTypeConstants.user && eachConversation.conversationWith && eachConversation.conversationWith.uid && eachConversation.conversationWith.uid === user.uid;
});
if (conversationKey > -1) {
var conversations = (0, _toConsumableArray2["default"])(conversationList);
var conversation = conversations[conversationKey];
var conversationWith = _objectSpread(_objectSpread({}, conversation.conversationWith), {}, {
status: user.getStatus()
});
var newConversation = _objectSpread(_objectSpread({}, conversation), {}, {
conversationWith: conversationWith
});
conversations.splice(conversationKey, 1, newConversation);
setConversationList(conversations);
}
};
/**
*
* When a text message / media message / custom message is received
*/
var handleMessages = function handleMessages() {
var message = arguments.length <= 0 ? undefined : arguments[0];
/**
* marking the incoming messages as read
*/
markMessageAsDelivered(message);
/**
* If the incoming message is 1-1 and the conversation type filter is set to group, return false
* OR
* If the incoming message is group and the conversation type filter is set to "users", return false
* ELSE
* return true
*/
if (filterByConversationType() === false) {
return false;
}
getConversationFromMessage(message).then(function (response) {
var conversationKey = response.conversationKey,
conversationId = response.conversationId,
conversationType = response.conversationType,
conversationWith = response.conversationWith,
conversation = response.conversation,
conversations = response.conversations;
var lastMessage = _objectSpread(_objectSpread({}, conversation === null || conversation === void 0 ? void 0 : conversation.lastMessage), message);
if (conversationKey > -1) {
var unreadMessageCount = getUnreadMessageCount(message, conversation);
var newConversation = new _chat.CometChat.Conversation(conversationId, conversationType, lastMessage, conversationWith, unreadMessageCount);
conversations.splice(conversationKey, 1);
conversations.unshift(newConversation);
setConversationList(conversations);
//play notification sound
playNotificationSound(message);
} else {
var _unreadMessageCount = getUnreadMessageCount(message);
var _newConversation = new _chat.CometChat.Conversation(conversationId, conversationType, lastMessage, conversationWith, _unreadMessageCount);
conversations.unshift(_newConversation);
setConversationList(conversations);
//play notification sound
playNotificationSound(message);
}
});
};
/**
* Listener callback when a message is edited, deleted or updated
*/
var handleMessageActions = function handleMessageActions() {
var message = arguments.length <= 0 ? undefined : arguments[0];
getConversationFromMessage(message).then(function (response) {
var conversationKey = response.conversationKey,
conversationId = response.conversationId,
conversationType = response.conversationType,
conversationWith = response.conversationWith,
conversation = response.conversation,
conversations = response.conversations;
if (conversationKey > -1 && conversation.lastMessage.id === message.id) {
var lastMessage = _objectSpread(_objectSpread({}, conversation.lastMessage), message);
var unreadMessageCount = getUnreadMessageCount(message, conversation);
var newConversation = new _chat.CometChat.Conversation(conversationId, conversationType, lastMessage, conversationWith, unreadMessageCount);
conversations.splice(conversationKey, 1, newConversation);
setConversationList(conversations);
}
});
};
/**
*
* Listener callback when a message is read
*/
var handleMessageReadActions = function handleMessageReadActions() {
var messageReceipt = arguments.length <= 0 ? undefined : arguments[0];
var conversationKey = conversationList.findIndex(function (conversation) {
var _conversation$convers, _conversation$convers2;
return (messageReceipt === null || messageReceipt === void 0 ? void 0 : messageReceipt.receiverType) === (conversation === null || conversation === void 0 ? void 0 : conversation.conversationType) && ((messageReceipt === null || messageReceipt === void 0 ? void 0 : messageReceipt.receiver) === (conversation === null || conversation === void 0 ? void 0 : (_conversation$convers = conversation.conversationWith) === null || _conversation$convers === void 0 ? void 0 : _conversation$convers.uid) || (messageReceipt === null || messageReceipt === void 0 ? void 0 : messageReceipt.receiver) === (conversation === null || conversation === void 0 ? void 0 : (_conversation$convers2 = conversation.conversationWith) === null || _conversation$convers2 === void 0 ? void 0 : _conversation$convers2.guid));
});
if (conversationKey > -1) {
var _conversation$lastMes;
var conversations = _objectSpread({}, conversationList);
var conversation = conversations[conversationKey];
var unreadMessageCount = getUnreadMessageCount(message, conversation);
/**
* If the message id in the read receipt is greater than or equal to the lastmessage id, set unreadmessagecount to 0
*/
if ((messageReceipt === null || messageReceipt === void 0 ? void 0 : messageReceipt.messageId) >= (conversation === null || conversation === void 0 ? void 0 : (_conversation$lastMes = conversation.lastMessage) === null || _conversation$lastMes === void 0 ? void 0 : _conversation$lastMes.id)) {
unreadMessageCount = 0;
}
var newConversation = new _chat.CometChat.Conversation(conversation.conversationId, conversation.conversationType, conversation.lastMessage, conversation.conversationWith, unreadMessageCount);
conversations.splice(conversationKey, 1, newConversation);
setConversationList(conversations);
}
};
/**
*
* Listener callback when a user joins/added to the group
*/
var handleGroupMemberAddition = function handleGroupMemberAddition() {
var message = arguments.length <= 0 ? undefined : arguments[0];
var newUser = arguments.length <= 1 ? undefined : arguments[1];
var group = arguments.length <= 3 ? undefined : arguments[3];
getConversationFromMessage(message).then(function (response) {
var _loggedInUser$current2;
var conversationKey = response.conversationKey,
conversationId = response.conversationId,
conversationType = response.conversationType,
conversationWith = response.conversationWith,
conversation = response.conversation,
conversations = response.conversations;
if (conversationKey > -1) {
var lastMessage = _objectSpread(_objectSpread({}, conversation.lastMessage), message);
var newConversationWith = _objectSpread(_objectSpread({}, conversationWith), group);
var unreadMessageCount = getUnreadMessageCount(message, conversation);
var newConversation = new _chat.CometChat.Conversation(conversationId, conversationType, lastMessage, newConversationWith, unreadMessageCount);
conversations.splice(conversationKey, 1, newConversation);
setConversationList(conversations);
} else if ((loggedInUser === null || loggedInUser === void 0 ? void 0 : (_loggedInUser$current2 = loggedInUser.current) === null || _loggedInUser$current2 === void 0 ? void 0 : _loggedInUser$current2.uid) === newUser.uid) {
/**
* If the loggedin user is added to the group, add the conversation to the chats list
*/
var _lastMessage = _objectSpread({}, message);
var _newConversationWith = _objectSpread(_objectSpread(_objectSpread({}, conversationWith), group), {}, {
hasJoined: true
});
var _unreadMessageCount2 = getUnreadMessageCount(message, conversation);
var _newConversation2 = new _chat.CometChat.Conversation(conversationId, conversationType, _lastMessage, _newConversationWith, _unreadMessageCount2);
conversations.unshift(_newConversation2);
setConversationList(conversations);
}
});
};
/**
*
* Listener callback when a member is kicked from / has left the group
*/
var handleGroupMemberRemoval = function handleGroupMemberRemoval() {
var message = arguments.length <= 0 ? undefined : arguments[0];
var removedUser = arguments.length <= 1 ? undefined : arguments[1];
var group = arguments.length <= 3 ? undefined : arguments[3];
getConversationFromMessage(message).then(function (response) {
var conversationKey = response.conversationKey,
conversationId = response.conversationId,
conversationType = response.conversationType,
conversationWith = response.conversationWith,
conversation = response.conversation,
conversations = response.conversations;
if (conversationKey > -1) {
var _loggedInUser$current3;
/**
* If the loggedin user is removed from the group, remove the conversation from the chats list
*/
if ((loggedInUser === null || loggedInUser === void 0 ? void 0 : (_loggedInUser$current3 = loggedInUser.current) === null || _loggedInUser$current3 === void 0 ? void 0 : _loggedInUser$current3.uid) === removedUser.uid) {
conversations.splice(conversationKey, 1);
setConversationList(conversations);
} else {
var lastMessage = _objectSpread(_objectSpread({}, conversation.lastMessage), message);
var newConversationWith = _objectSpread(_objectSpread({}, conversationWith), group);
var unreadMessageCount = getUnreadMessageCount(message, conversation);
var newConversation = new _chat.CometChat.Conversation(conversationId, conversationType, lastMessage, newConversationWith, unreadMessageCount);
conversations.splice(conversationKey, 1, newConversation);
setConversationList(conversations);
}
}
});
};
/**
*
* Listener callback when a member is banned from the group
*/
var handleGroupMemberBan = function handleGroupMemberBan() {
var message = arguments.length <= 0 ? undefined : arguments[0];
var removedUser = arguments.length <= 1 ? undefined : arguments[1];
var group = arguments.length <= 3 ? undefined : arguments[3];
getConversationFromMessage(message).then(function (response) {
var conversationKey = response.conversationKey,
conversationId = response.conversationId,
conversationType = response.conversationType,
conversationWith = response.conversationWith,
conversation = response.conversation,
conversations = response.conversations;
if (conversationKey > -1) {
var _loggedInUser$current4;
/**
* If the loggedin user is banned from the group, remove the conversation from the chats list
*/
if ((loggedInUser === null || loggedInUser === void 0 ? void 0 : (_loggedInUser$current4 = loggedInUser.current) === null || _loggedInUser$current4 === void 0 ? void 0 : _loggedInUser$current4.uid) === removedUser.uid) {
conversations.splice(conversationKey, 1);
setConversationList(conversations);
} else {
var lastMessage = _objectSpread(_objectSpread({}, conversation.lastMessage), message);
var newConversationWith = _objectSpread(_objectSpread({}, conversationWith), group);
var unreadMessageCount = getUnreadMessageCount(message, conversation);
var newConversation = new _chat.CometChat.Conversation(conversationId, conversationType, lastMessage, newConversationWith, unreadMessageCount);
conversations.splice(conversationKey, 1, newConversation);
setConversationList(conversations);
}
}
});
};
/**
*
* Listener callback when a group member scope is updated
*/
var handleGroupMemberScopeChange = function handleGroupMemberScopeChange() {
var message = arguments.length <= 0 ? undefined : arguments[0];
var user = arguments.length <= 1 ? undefined : arguments[1];
var newScope = arguments.length <= 2 ? undefined : arguments[2];
var group = arguments.length <= 4 ? undefined : arguments[4];
getConversationFromMessage(message).then(function (response) {
var conversationKey = response.conversationKey,
conversationId = response.conversationId,
conversationType = response.conversationType,
conversationWith = response.conversationWith,
conversation = response.conversation,
conversations = response.conversations;
if (conversationKey > -1) {
var _loggedInUser$current5;
var lastMessage = _objectSpread(_objectSpread({}, conversation.lastMessage), message);
var unreadMessageCount = getUnreadMessageCount(message, conversation);
if ((loggedInUser === null || loggedInUser === void 0 ? void 0 : (_loggedInUser$current5 = loggedInUser.current) === null || _loggedInUser$current5 === void 0 ? void 0 : _loggedInUser$current5.uid) === user.uid) {
var newConversationWith = _objectSpread(_objectSpread(_objectSpread({}, conversationWith), group), {}, {
scope: newScope
});
var newConversation = new _chat.CometChat.Conversation(conversationId, conversationType, lastMessage, newConversationWith, unreadMessageCount);
conversations.splice(conversationKey, 1);
conversations.unshift(newConversation);
setConversationList(conversations);
} else {
var _newConversationWith2 = _objectSpread(_objectSpread({}, conversationWith), group);
var _newConversation3 = new _chat.CometChat.Conversation(conversationId, conversationType, lastMessage, _newConversationWith2, unreadMessageCount);
conversations.splice(conversationKey, 1, _newConversation3);
setConversationList(conversations);
}
}
});
};
/**
*
* Listener callback for typing events
*/
var handleTyping = function handleTyping() {
var typingData = arguments.length <= 0 ? undefined : arguments[0];
var typingStarted = arguments.length <= 1 ? undefined : arguments[1];
var conversations = (0, _toConsumableArray2["default"])(conversationList);
var conversationKey = conversations.findIndex(function (conversation) {
var _conversation$convers3, _typingData$sender, _conversation$convers4;
return (conversation === null || conversation === void 0 ? void 0 : conversation.conversationType) === (typingData === null || typingData === void 0 ? void 0 : typingData.receiverType) && ((conversation === null || conversation === void 0 ? void 0 : conversation.conversationType) === _Shared.ReceiverTypeConstants.user && ((_conversation$convers3 = conversation.conversationWith) === null || _conversation$convers3 === void 0 ? void 0 : _conversation$convers3.uid) === (typingData === null || typingData === void 0 ? void 0 : (_typingData$sender = typingData.sender) === null || _typingData$sender === void 0 ? void 0 : _typingData$sender.uid) || conversation.conversationType === _Shared.ReceiverTypeConstants.group && ((_conversation$convers4 = conversation.conversationWith) === null || _conversation$convers4 === void 0 ? void 0 : _conversation$convers4.guid) === (typingData === null || typingData === void 0 ? void 0 : typingData.receiverId));
});
if (conversationKey > -1) {
var typingIndicatorText = "";
if (typingStarted) {
var _typingData$sender2;
typingIndicatorText = (typingData === null || typingData === void 0 ? void 0 : typingData.receiverType) === _Shared.ReceiverTypeConstants.group ? "".concat(typingData === null || typingData === void 0 ? void 0 : (_typingData$sender2 = typingData.sender) === null || _typingData$sender2 === void 0 ? void 0 : _typingData$sender2.name, " ").concat((0, _Shared.localize)("IS_TYPING")) : (0, _Shared.localize)("IS_TYPING");
}
var conversation = conversationList[conversationKey];
var newConversation = _objectSpread(_objectSpread({}, conversation), {}, {
showTypingIndicator: typingStarted,
typingIndicatorText: typingIndicatorText
});
conversations.splice(conversationKey, 1, newConversation);
setConversationList(conversations);
}
};
/**
*
* @param {Object} conversation
* Public method to update the conversation list programmatically
*/
var updateConversation = function updateConversation(conversation) {
var conversationKey = conversationList.findIndex(function (eachConversation) {
var _conversation$convers5;
return eachConversation.conversationWith && eachConversation.conversationWith.uid && eachConversation.conversationWith.uid === (conversation === null || conversation === void 0 ? void 0 : (_conversation$convers5 = conversation.conversationWith) === null || _conversation$convers5 === void 0 ? void 0 : _conversation$convers5.uid);
});
if (conversationKey > -1) {
var conversations = (0, _toConsumableArray2["default"])(conversationList);
var _conversation = conversations[conversationKey];
var conversationWith = _objectSpread({}, _conversation.conversationWith);
var newConversation = _objectSpread(_objectSpread({}, _conversation), {}, {
conversationWith: conversationWith
});
conversations.splice(conversationKey, 1, newConversation);
setConversationList(conversations);
}
};
var handlers = {
onUserOnline: handleUsers,
onUserOffline: handleUsers,
onTextMessageReceived: handleMessages,
onMediaMessageReceived: handleMessages,
onCustomMessageReceived: handleMessages,
onIncomingCallReceived: handleMessages,
onIncomingCallCancelled: handleMessages,
messageEdited: handleMessageActions,
onMessageDeleted: handleMessageActions,
messageRead: handleMessageReadActions,
onMemberAddedToGroup: handleGroupMemberAddition,
onGroupMemberJoined: handleGroupMemberAddition,
onGroupMemberKicked: handleGroupMemberRemoval,
onGroupMemberLeft: handleGroupMemberRemoval,
onGroupMemberBanned: handleGroupMemberBan,
onGroupMemberScopeChanged: handleGroupMemberScopeChange,
onTypingStarted: handleTyping,
onTypingEnded: handleTyping
};
var handleConversations = function handleConversations() {
getConversations().then(function (conversations) {
if (conversationList.length === 0 && conversations.length === 0) {
setMessage("NO_CHATS_FOUND");
} else {
setMessage("");
}
setConversationList(function (conversationList) {
return [].concat((0, _toConsumableArray2["default"])(conversationList), (0, _toConsumableArray2["default"])(conversations));
});
})["catch"](function (error) {
_.CometChatConversationEvents.emit(_.CometChatConversationEvents.onError, error);
setMessage("SOMETHING_WENT_WRONG");
});
};
var getConversations = function getConversations() {
return new Promise(function (resolve, reject) {
conversationListManager === null || conversationListManager === void 0 ? void 0 : conversationListManager.current.fetchNextConversation().then(function (conversations) {
return resolve(conversations);
})["catch"](function (error) {
_.CometChatConversationEvents.emit(_.CometChatConversationEvents.onError, error);
reject(error);
});
});
};
var handleScroll = function handleScroll(event) {
var bottom = Math.round(event.currentTarget.scrollHeight - event.currentTarget.scrollTop) === Math.round(event.currentTarget.clientHeight);
if (bottom) {
handleConversations();
}
};
/**
* reset un read count
*/
var resetUnreadCount = function resetUnreadCount(conversation) {
var conversations = (0, _toConsumableArray2["default"])(conversationList);
var conversationKey = conversations.findIndex(function (conversationObject) {
return conversationObject.conversationId === conversation.conversationId;
});
if (conversationKey > -1) {
var _conversation2 = conversations[conversationKey];
var newConversation = _objectSpread(_objectSpread({}, _conversation2), {}, {
unreadMessageCount: 0
});
conversations.splice(conversationKey, 1, newConversation);
setConversationList(conversations);
}
};
/**
* update last message
*/
var updateLastMessage = function updateLastMessage(message) {
getConversationFromMessage(message).then(function (response) {
var conversationKey = response.conversationKey,
conversations = response.conversations,
conversation = response.conversation;
var lastMessage = _objectSpread({}, conversation.lastMessage);
if (conversationKey > -1) {
var newConversation = _objectSpread(_objectSpread({}, conversation), {}, {
lastMessage: lastMessage
});
conversations.splice(conversationKey, 1, newConversation);
setConversationList(conversations);
}
});
};
/**
* Remove conversation from the conversationlist upon delete
*/
var removeConversation = function removeConversation(conversation) {
var conversationKey = conversationList.findIndex(function (c) {
return c.conversationId === conversation.conversationId;
});
if (conversationKey > -1) {
var newConversationList = (0, _toConsumableArray2["default"])(conversationList);
newConversationList.splice(conversationKey, 1);
setConversationList(newConversationList);
}
};
var deleteConversation = function deleteConversation(conversationToBeDeleted) {
return new Promise(function (resolve, reject) {
var _conversationToBeDele, _conversationToBeDele2;
var conversationWith = (conversationToBeDeleted === null || conversationToBeDeleted === void 0 ? void 0 : conversationToBeDeleted.conversationType) === _Shared.ReceiverTypeConstants.group ? conversationToBeDeleted === null || conversationToBeDeleted === void 0 ? void 0 : (_conversationToBeDele = conversationToBeDeleted.conversationWith) === null || _conversationToBeDele === void 0 ? void 0 : _conversationToBeDele.guid : conversationToBeDeleted === null || conversationToBeDeleted === void 0 ? void 0 : (_conversationToBeDele2 = conversationToBeDeleted.conversationWith) === null || _conversationToBeDele2 === void 0 ? void 0 : _conversationToBeDele2.uid;
_chat.CometChat.deleteConversation(conversationWith, conversationToBeDeleted === null || conversationToBeDeleted === void 0 ? void 0 : conversationToBeDeleted.conversationType).then(function (deletedConversation) {
setShowConfirm(_objectSpread(_objectSpread({}, showConfirm), {}, {
show: false
}));
removeConversation(conversationToBeDeleted);
resolve(deletedConversation);
_.CometChatConversationEvents.emit(_.CometChatConversationEvents.onDeleteConversation, conversationToBeDeleted);
})["catch"](function (error) {
_.CometChatConversationEvents.emit(_.CometChatConversationEvents.onError, conversationToBeDeleted);
reject(error);
});
});
};
var cancelDelete = function cancelDelete() {
return setShowConfirm(_objectSpread(_objectSpread({}, showConfirm), {}, {
show: false
}));
};
var confirmDelete = function confirmDelete(conversation) {
setShowConfirm(_objectSpread(_objectSpread({}, showConfirm), {}, {
show: true,
conversation: conversation,
onCancel: cancelDelete
}));
};
var itemClickhandler = function itemClickhandler(e) {
_.CometChatConversationEvents.emit(_.CometChatConversationEvents.onItemClick, e);
};
/**
* Component hooks
*/
(0, _hooks.Hooks)(conversationType, limit, tags, userAndGroupTags, setConversationList, conversationCallback, conversationListManager, handlers, callbackData, loggedInUser, handleConversations, confirmDelete);
/**
* Component template scoping
*/
var getCustomView = function getCustomView(customView, props) {
return /*#__PURE__*/_react["default"].createElement(customView, props);
};
var getMessageContainer = function getMessageContainer() {
var messageContainer = null;
if (conversationList.length === 0 && message.toLowerCase() === (0, _Shared.localize)("LOADING")) {
/**Loading custom view */
messageContainer = /*#__PURE__*/_react["default"].createElement("div", {
style: styles.messageContainerStyle(style),
className: "chats__message"
}, customView.loading ? getCustomView(customView.loading, props) : /*#__PURE__*/_react["default"].createElement("div", {
style: styles.messageImgStyle(style, _theme, loadingIconURL),
className: "message"
}));
} else if (conversationList.length === 0 && message.toLowerCase() === 'no chats found') {
/**Empty custom view */
messageContainer = /*#__PURE__*/_react["default"].createElement("div", {
style: styles.messageContainerStyle(style),
className: "chats__message"
}, customView.empty ? getCustomView(customView.empty, props) : /*#__PURE__*/_react["default"].createElement("div", {
style: styles.messageTextStyle(style, _theme, message),
className: "message"
}, emptyText));
} else if (!hideError && message.toLowerCase() === 'something went wrong') {
/**Error custom view */
messageContainer = /*#__PURE__*/_react["default"].createElement("div", {
style: styles.messageContainerStyle(style),
className: "chats__message"
}, customView.error ? getCustomView(customView.error, props) : /*#__PURE__*/_react["default"].createElement("div", {
style: styles.messageTextStyle(style, _theme, message),
className: "message"
}, errorText));
}
return messageContainer;
};
var renderItems = conversationList.map(function (conversation) {
var _conversationOptions;
var typingIndicatorText = conversation !== null && conversation !== void 0 && conversation.typingIndicatorText ? conversation === null || conversation === void 0 ? void 0 : conversation.typingIndicatorText : "";
var isActive = (conversation === null || conversation === void 0 ? void 0 : conversation.conversationId) === (activeConversation === null || activeConversation === void 0 ? void 0 : activeConversation.conversationId) ? true : false;
/**
* Calculate conversations options for child component
*/
var conversationOptions = _conversationListItemConfiguration === null || _conversationListItemConfiguration === void 0 ? void 0 : _conversationListItemConfiguration.conversationOptions;
if (((_conversationOptions = conversationOptions) === null || _conversationOptions === void 0 ? void 0 : _conversationOptions.length) == 0) {
conversationOptions = [new _Chats.CometChatConversationOptions({
id: _Shared.ConversationOptionConstants["delete"],
title: (0, _Shared.localize)("DELETE"),
iconURL: _delete["default"],
onClick: function onClick() {
return confirmDelete(conversation);
}
})];
}
return /*#__PURE__*/_react["default"].createElement(_Shared.CometChatConversationListItem, {
key: conversation.conversationId,
conversationObject: conversation,
isActive: isActive,
conversationInputData: _conversationListItemConfiguration === null || _conversationListItemConfiguration === void 0 ? void 0 : _conversationListItemConfiguration.conversationInputData,
conversationOptions: conversationOptions,
style: new _Shared.ConversationListItemStyles(_objectSpread({}, styles.listItemStyle(style, _theme))),
hideDeletedMessages: _conversationListItemConfiguration === null || _conversationListItemConfiguration === void 0 ? void 0 : _conversationListItemConfiguration.hideDeletedMessages,
hideGroupActions: _conversationListItemConfiguration === null || _conversationListItemConfiguration === void 0 ? void 0 : _conversationListItemConfiguration.hideGroupActions,
showTypingIndicator: _conversationListItemConfiguration === null || _conversationListItemConfiguration === void 0 ? void 0 : _conversationListItemConfiguration.showTypingIndicator,
typingIndicatorText: typingIndicatorText,
hideThreadIndicator: _conversationListItemConfiguration === null || _conversationListItemConfiguration === void 0 ? void 0 : _conversationListItemConfiguration.hideThreadIndicator,
threadIndicatorText: (0, _Shared.localize)("IN_A_THREAD"),
theme: _theme,
onClick: itemClickhandler.bind(_this),
avatarConfiguration: _conversationListItemConfiguration === null || _conversationListItemConfiguration === void 0 ? void 0 : _conversationListItemConfiguration.avatarConfiguration,
statusIndicatorConfiguration: _conversationListItemConfiguration === null || _conversationListItemConfiguration === void 0 ? void 0 : _conversationListItemConfiguration.statusIndicatorConfiguration,
badgeCountConfiguration: _conversationListItemConfiguration === null || _conversationListItemConfiguration === void 0 ? void 0 : _conversationListItemConfiguration.badgeCountConfiguration,
messageReceiptConfiguration: _conversationListItemConfiguration === null || _conversationListItemConfiguration === void 0 ? void 0 : _conversationListItemConfiguration.messageReceiptConfiguration,
loggedInUser: loggedInUser === null || loggedInUser === void 0 ? void 0 : loggedInUser.current
});
});
/**
* Component template
*/
return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, getMessageContainer(), /*#__PURE__*/_react["default"].createElement("div", {
style: styles.chatsListStyle(style, _theme),
className: "conversation__list",
onScroll: handleScroll
}, renderItems), /*#__PURE__*/_react["default"].createElement(_Shared.CometChatBackdrop, {
isOpen: showConfirm.show,
onClick: showConfirm.onCancel,
background: _theme === null || _theme === void 0 ? void 0 : (_theme$palette = _theme.palette) === null || _theme$palette === void 0 ? void 0 : _theme$palette.accent200[_theme === null || _theme === void 0 ? void 0 : (_theme$palette2 = _theme.palette) === null || _theme$palette2 === void 0 ? void 0 : _theme$palette2.mode]
}, /*#__PURE__*/_react["default"].createElement(_Shared.CometChatConfirmDialog, {
isOpen: showConfirm.show,
onConfirm: deleteConversation.bind(_this, showConfirm.conversation),
onCancel: showConfirm.onCancel,
style: styles.DialogStyle(_theme),
title: (0, _Shared.localize)("DELETE_CONVERSATION"),
messageText: (0, _Shared.localize)("CONFIRM_DELETE_CONVERSATION"),
confirmButtonText: (0, _Shared.localize)("CONFIRM_BUTTON_TEXT"),
cancelButtonText: (0, _Shared.localize)("CANCEL_BUTTON_TEXT")
})));
});
/**
* Component default props
*/
ConversationList.defaultProps = {
loadingIconURL: _spinner["default"]
};
/**
* Component default props types
*/
ConversationList.propTypes = {
conversationType: _propTypes["default"].oneOf(["users", "groups", "both"]),
limit: _propTypes["default"].number,
hideError: _propTypes["default"].bool,
tags: _propTypes["default"].array,
userAndGroupTags: _propTypes["default"].bool,
emptyText: _propTypes["default"].string,
errorText: _propTypes["default"].string,
loadingIconURL: _propTypes["default"].string,
style: _propTypes["default"].object,
activeConversation: _propTypes["default"].object,
enableSoundForMessages: _propTypes["default"].bool,
customIncomingMessageSound: _propTypes["default"].string,
customView: _propTypes["default"].object,
conversationListItemConfiguration: _propTypes["default"].object,
theme: _propTypes["default"].object
};
var CometChatConversationList = /*#__PURE__*/_react["default"].memo(ConversationList);
exports.CometChatConversationList = CometChatConversationList;