UNPKG

@cometchat/chat-uikit-react-native

Version:

Ready-to-use Chat UI Components for React Native

179 lines 7.67 kB
import { DataSourceDecorator } from "../../shared/framework"; import { MessageCategoryConstants, MetadataConstants } from "../../shared/constants/UIKitConstants"; import { ChatConfigurator } from "../../shared/framework"; import { CometChatMessageTemplate } from "../../shared/modals"; import { localize } from "../../shared/resources/CometChatLocalize"; import { ExtensionTypeConstants } from "../ExtensionConstants"; import { getExtensionData } from "../ExtensionModerator"; import React from "react"; import { View } from "react-native"; import { CometChatUIKit } from "../../shared"; import { Icon } from "../../shared/icons/Icon"; import { getMessagePreviewInternal } from "../../shared/utils/MessageUtils"; import { CometChatCreatePoll } from "./Polls"; import { PollsBubble } from "./PollsBubble"; /** * * * @type {PollsConfigurationInterface} * @description Optional configuration for polls. */ export class PollsExtensionDecorator extends DataSourceDecorator { pollsConfiguration; /** * * * @param {DataSource} dataSource - The data source to decorate. * @param {PollsConfigurationInterface} [pollsConfiguration] - Optional polls configuration. */ constructor(dataSource, pollsConfiguration) { super(dataSource); if (pollsConfiguration != undefined) { this.pollsConfiguration = pollsConfiguration; } } /** * * * @returns {boolean} * @description Returns true if the message is deleted. */ isDeletedMessage(message) { return message.getDeletedBy() != null; } /** * * * @returns {string} * @description Returns the unique ID for this extension. */ getId() { return "Polls"; } /** * * * @param {CometChat.Conversation} conversation - The conversation object. * @param {CometChatTheme} [theme] - The current theme. * @returns {string | JSX.Element} * @description Returns the preview text or element for the last conversation message. */ getLastConversationMessage(conversation, theme) { if (conversation.getLastMessage() == undefined) { return ""; } if (conversation.getLastMessage().getType() == ExtensionTypeConstants.extensionPoll && conversation.getLastMessage().getCategory() == MessageCategoryConstants.custom && conversation.getLastMessage().getDeletedAt() === undefined) { return getMessagePreviewInternal("bar-chart-fill", localize("CUSTOM_MESSAGE_POLL"), { theme, }); } else { return super.getLastConversationMessage(conversation, theme); } } /** * * * @returns {string[]} * @description Returns all message categories including custom types. */ getAllMessageCategories() { var categoryList = super.getAllMessageCategories(); if (!categoryList.includes(MessageCategoryConstants.custom)) { categoryList.push(MessageCategoryConstants.custom); } return categoryList; } /** * * * @returns {string[]} * @description Returns all message types including the poll extension type. */ getAllMessageTypes() { var messagesTypes = super.getAllMessageTypes(); messagesTypes.push(ExtensionTypeConstants.extensionPoll); return messagesTypes; } /** * * * @param {CometChatTheme} theme - The current theme. * @param {any} [user] - The current user. * @param {any} [group] - The current group. * @param {any} [composerId] - The composer identifier. * @returns {CometChatMessageComposerAction[]} * @description Returns the attachment options for the message composer including polls. */ getAttachmentOptions(theme, user, group, composerId, additionalAttachmentOptionsParams) { let attachmentOptions = super.getAttachmentOptions(theme, user, group, composerId, additionalAttachmentOptionsParams); if (additionalAttachmentOptionsParams?.hidePollsAttachmentOption) return attachmentOptions; if (composerId == undefined || composerId.get("parentMessageId") == undefined) attachmentOptions.push({ id: "polls", title: localize("CUSTOM_MESSAGE_POLL"), icon: <Icon name='poll_icon' color={theme.color.primary}/>, CustomView: (user, group, _id, pollsProps) => { return (<CometChatCreatePoll user={user} group={group} {...pollsProps} {...this.pollsConfiguration}/>); }, }); return attachmentOptions; } /** * * * @param {CometChatTheme} theme - The current theme. * @param {AdditionalParams} [additionalParams] - Additional parameters. * @returns {CometChatMessageTemplate[]} * @description Returns all message templates including the poll template. */ getAllMessageTemplates(theme, additionalParams) { let templateList = super.getAllMessageTemplates(theme, additionalParams); templateList.push(new CometChatMessageTemplate({ type: ExtensionTypeConstants.extensionPoll, category: MessageCategoryConstants.custom, ContentView: (message, _alignment) => { if (this.isDeletedMessage(message)) { return ChatConfigurator.dataSource.getDeleteMessageBubble(message, theme); } else { return this.getPollBubble(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; } /** * * * @param {CometChat.BaseMessage} message - The poll message. * @param {MessageBubbleAlignmentType} _alignment - The alignment for the message bubble. * @param {CometChatTheme} theme - The current theme. * @returns {JSX.Element} * @description Returns the poll bubble element for a given poll message. */ getPollBubble(message, _alignment, theme) { let _loggedInUser = CometChatUIKit.loggedInUser; if (message && _loggedInUser) { const metaData = getExtensionData(message, MetadataConstants.extensions?.polls); const _style = message.getSender().getUid() === _loggedInUser.getUid() ? theme.messageListStyles.outgoingMessageBubbleStyles?.pollBubbleStyles : theme.messageListStyles.incomingMessageBubbleStyles?.pollBubbleStyles; return (<PollsBubble pollQuestion={message["customData"]?.["question"]} options={message["customData"]?.["options"]} pollId={message["customData"]?.["id"]} loggedInUser={_loggedInUser} // choosePoll senderUid={message["sender"]?.["uid"]} metadata={metaData} titleStyle={_style?.titleStyle} optionTextStyle={_style?.optionTextStyle} voteCountTextStyle={_style?.voteCountTextStyle} selectedIconStyle={_style?.selectedIconStyle} radioButtonStyle={_style?.radioButtonStyle} voteravatarStyle={_style?.voteravatarStyle} progressBarStyle={_style?.progressBarStyle} activeProgressBarTint={_style?.activeProgressBarTint}/>); } return <View></View>; } } //# sourceMappingURL=PollsDecorator.js.map