import { Component, ElementRef, HostListener, OnInit, TemplateRef, ViewChild } from '@angular/core';
import { CometChat } from '@cometchat/chat-sdk-javascript';
import '@cometchat/uikit-elements';
import { CometChatMessageComposerAction, CometChatMessageEvents, CometChatMessageOption, CometChatMessageTemplate, CometChatTheme, CometChatUIKitConstants, fontHelper } from '@cometchat/uikit-resources';
import { MessagesConfiguration,ComposerId, CometChatUIKitHelper, CometChatUIKitUtility, MessageStatus } from '@cometchat/uikit-shared';
import { CometChatUIKit, CometChatThemeService } from '@cometchat/chat-uikit-angular';
@Component({
selector: 'app-auxiliary-button',
templateUrl: './auxiliary-button.component.html',
styleUrls: ['./auxiliary-button.component.scss']
})
export class AuxiliaryButtonComponent implements OnInit {
//template ref for image bubble
@ViewChild('auxButtonRef') auxButtonRef!:TemplateRef<any>;
@ViewChild('imageBubbleRef') imageBubbleRef!:TemplateRef<any>;
//properties
iconStyle = { iconTint: this.themeService.theme.palette.getAccent600(), width: "24px", height: "24px"};
messagesConfiguration:MessagesConfiguration = new MessagesConfiguration({});
loggedInUser:CometChat.User | null = null;
public user!:CometChat.User;
public group!:CometChat.Group;
//accessing global theme object.
constructor(private themeService:CometChatThemeService){}
ngAfterViewInit(){
//default templates
let templates = CometChatUIKit.getDataSource().getAllMessageTemplates(this.themeService.theme)
const messageTemplate = new CometChatMessageTemplate({
options: this.getMessageOptions,
contentView: () => this.imageBubbleRef,
type: "nudge",
category: "custom"
});
//adding new template
templates.push(messageTemplate)
let defaultTypes:string[] = CometChatUIKit.getDataSource().getAllMessageTypes()
let defaultCategory:string[] = CometChatUIKit.getDataSource().getAllMessageCategories()
if(!defaultCategory.includes("custom")){
defaultCategory.push("custom")
}
if(!defaultTypes.includes("nudge")){
defaultTypes.push("nudge")
}
//creating new configuration
this.messagesConfiguration.messageListConfiguration.templates = templates
this.messagesConfiguration.messageListConfiguration.messagesRequestBuilder = new CometChat.MessagesRequestBuilder()
.setLimit(30)
.setTypes(defaultTypes)
.setCategories(defaultCategory)
.hideReplies(true);
this.messagesConfiguration.messageComposerConfiguration.auxilaryButtonView = this.auxButtonRef;
this.messagesConfiguration = {...this.messagesConfiguration};
}
sendMessage(item:CometChat.User | CometChat.Group){
if(item instanceof CometChat.User){
this.user = item
} else {
this.group = item
}
let { receiverId, receiverType } = this.getReceiverDetails();
const customData = {nudge_url: "assets/rocket.gif"};
const customType = "nudge";
const customMessage: CometChat.CustomMessage = new CometChat.CustomMessage(receiverId, receiverType, customType, customData);
customMessage.setMetadata({ incrementUnreadCount: true });
(customMessage as any).setSentAt(CometChatUIKitUtility.getUnixTimestamp());
customMessage.setMuid(CometChatUIKitUtility.ID());
CometChatUIKit.sendCustomMessage(customMessage)
}
getReceiverDetails() {
let receiverId!: string;
let receiverType!: string;
if (this.user && this.user.getUid()) {
receiverId = this.user.getUid();
receiverType = CometChatUIKitConstants.MessageReceiverType.user;
} else if (this.group && this.group.getGuid()) {
receiverId = this.group.getGuid();
receiverType = CometChatUIKitConstants.MessageReceiverType.group;
}
return { receiverId: receiverId, receiverType: receiverType };
}
getMessageOptions(loggedInUser: CometChat.User, message: CometChat.BaseMessage, theme: CometChatTheme, group?: CometChat.Group | undefined) {
return CometChatUIKit.getDataSource().getCommonOptions(loggedInUser,message,theme,group)}
}
}