UNPKG

@cometchat/chat-uikit-react-native

Version:

Ready-to-use Chat UI Components for React Native

137 lines 7.63 kB
import { CometChat } from "@cometchat/chat-sdk-react-native"; import React from "react"; import { Text, View } from "react-native"; import { CometChatUIKit } from "../../shared"; import { MessageCategoryConstants, MetadataConstants, ReceiverTypeConstants, } from "../../shared/constants/UIKitConstants"; import { CometChatUIEvents, MessageEvents } from "../../shared/events"; import { CometChatUIEventHandler } from "../../shared/events/CometChatUIEventHandler/CometChatUIEventHandler"; import { ChatConfigurator, DataSourceDecorator } from "../../shared/framework"; import { Icon } from "../../shared/icons/Icon"; import { CometChatMessageTemplate } from "../../shared/modals"; import { localize } from "../../shared/resources/CometChatLocalize"; import { getMessagePreviewInternal } from "../../shared/utils/MessageUtils"; import { DEFAULT_ICONS } from "../../theme/default/resources/icons"; import { CometChatCollaborativeBubble } from "../CollaborativeBubble/CometChatCollaborativeBubble"; import { ExtensionConstants, ExtensionTypeConstants } from "../ExtensionConstants"; import { getExtensionData } from "../ExtensionModerator"; export class CollaborativeDocumentExtensionDecorator extends DataSourceDecorator { documentUrl = "v1/create"; constructor(dataSource) { super(dataSource); } isDeletedMessage(message) { return message.getDeletedBy() != null; } getId() { return "CollaborativeDocument"; } getLastConversationMessage(conversation, theme) { if (conversation.getLastMessage() == undefined) { return ""; } if (conversation.getLastMessage().getType() == ExtensionTypeConstants.document && conversation.getLastMessage().getCategory() == MessageCategoryConstants.custom && conversation.getLastMessage().getDeletedAt() === undefined) { return getMessagePreviewInternal("collaborative-document-fill", localize("CUSTOM_MESSAGE_DOCUMENT"), { theme }); } else { return super.getLastConversationMessage(conversation, theme); } } getAllMessageCategories() { var categoryList = super.getAllMessageCategories(); if (!categoryList.includes(MessageCategoryConstants.custom)) { categoryList.push(MessageCategoryConstants.custom); } return categoryList; } getAllMessageTypes() { var messagesTypes = super.getAllMessageTypes(); messagesTypes.push(ExtensionTypeConstants.document); return messagesTypes; } getAttachmentOptions(theme, user, group, composerId, additionalAttachmentOptionsParams) { let attachmentOptions = super.getAttachmentOptions(theme, user, group, composerId, additionalAttachmentOptionsParams); if (additionalAttachmentOptionsParams?.hideCollaborativeDocumentOption) return attachmentOptions; if (composerId == undefined || composerId.get("parentMessageId") == undefined) attachmentOptions.push({ id: "document", title: localize("COLLABORATIVE_DOCUMENT"), icon: (<Icon name='collaborative-document-icon' color={theme.color.primary} height={theme.messageComposerStyles?.attachmentOptionsStyles?.optionsItemStyle?.iconStyle ?.height} width={theme.messageComposerStyles?.attachmentOptionsStyles?.optionsItemStyle?.iconStyle ?.width} containerStyle={theme.messageComposerStyles?.attachmentOptionsStyles?.optionsItemStyle ?.iconContainerStyle}/>), onPress: (user, group) => { this.shareCollaborativedocument(user, group); }, }); return attachmentOptions; } shareCollaborativedocument(user, group) { CometChatUIEventHandler.emitUIEvent(CometChatUIEvents.ccToggleBottomSheet, { isBottomSheetVisible: false, }); let receiverId; let receiverType; if (user != undefined) { receiverId = user.getUid(); receiverType = ReceiverTypeConstants.user; } else if (group != undefined) { receiverId = group.getGuid(); receiverType = ReceiverTypeConstants.group; } else { } CometChat.callExtension(ExtensionConstants.document, ExtensionConstants.post, this.documentUrl, { receiver: receiverId, receiverType: receiverType, }) .then((response) => { console.log("extension sent ", response); }) .catch((error) => { console.log("error", error); CometChatUIEventHandler.emitMessageEvent(MessageEvents.ccMessageError, error); }); } getAllMessageTemplates(theme, additionalParams) { let templateList = super.getAllMessageTemplates(theme, additionalParams); templateList.push(new CometChatMessageTemplate({ type: ExtensionTypeConstants.document, category: MessageCategoryConstants.custom, ContentView: (message, _alignment) => { if (this.isDeletedMessage(message)) { return ChatConfigurator.dataSource.getDeleteMessageBubble(message, theme); } else { return this.getCollaborativeBubble(message, _alignment, theme); } }, options: (loggedInUser, messageObject, theme, group) => ChatConfigurator.dataSource.getMessageOptions(loggedInUser, messageObject, theme, group, additionalParams), BottomView: (message, alignment) => { return ChatConfigurator.dataSource.getBottomView(message, alignment); }, })); return templateList; } getCollaborativeBubble(message, _alignment, theme) { let loggedInUser = CometChatUIKit.loggedInUser; if (message) { const documentData = getExtensionData(message, MetadataConstants.extensions?.document); if (documentData && documentData.document_url && documentData.document_url.trim().length) { let url = documentData.document_url; const _style = message.getSender().getUid() === loggedInUser.getUid() ? theme.messageListStyles.outgoingMessageBubbleStyles?.collaborativeBubbleStyles : theme.messageListStyles.incomingMessageBubbleStyles?.collaborativeBubbleStyles; return (<CometChatCollaborativeBubble title={localize("COLLABORATIVE_DOCUMENT")} titleStyle={_style?.titleStyle} subtitle={localize("OPEN_DOCUMENT_TO_DRAW")} subtitleStyle={_style?.subtitleStyle} buttonText={localize("OPEN_DOCUMENT")} icon={<Icon name='collaborative-whiteboard-fill' height={_style?.iconStyle?.height} width={_style?.iconStyle?.width} color={_style?.iconStyle?.tintColor} icon={_style?.iconCollaborativeDocument} imageStyle={_style?.iconStyle} containerStyle={_style?.iconContainerStyle}></Icon>} url={url} image={<Icon icon={_style?.imageCollaborativeDocument ?? DEFAULT_ICONS.COLLAB_DOCUMENT_3X} height={_style?.imageStyle?.height} width={_style?.imageStyle?.width} color={_style?.imageStyle?.tintColor} imageStyle={_style?.imageStyle} containerStyle={_style?.imageContainerStyle}></Icon>} buttonViewStyle={_style?.buttonViewStyle} buttonTextStyle={_style?.buttonTextStyle} dividerStyle={_style?.dividerStyle}/>); } } return (<View> <Text>{"no match"}</Text> </View>); } } //# sourceMappingURL=CollaborativeDocumentExtensionDecorator.js.map