Skip to content

Commit

Permalink
Sp hide triggers (#599)
Browse files Browse the repository at this point in the history
* fix(triggers): hide triggers

* fix(webchat): unread count

* init the chat

* doesn't fail on sound play failure

* classNames

* consple

* gfdg

* added custom events

* cleanup
  • Loading branch information
slvnperron committed Jun 9, 2023
1 parent 6699923 commit d28cdf0
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
10 changes: 8 additions & 2 deletions packages/webchat/src/components/messages/Message.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class Message extends Component<MessageProps> {
if (this.props.noBubble || this.props.payload?.wrapped?.noBubble) {
return (
<div
className={classnames(this.props.className, wrappedClass, messageSelectedClass)}
className={classnames(this.props.className, wrappedClass, messageSelectedClass, this.props.payload.className)}
style={additionalStyle}
onClick={this.onMessageClick}
>
Expand All @@ -99,7 +99,13 @@ class Message extends Component<MessageProps> {

return (
<div
className={classnames(this.props.className, wrappedClass, 'bpw-chat-bubble', `bpw-bubble-${type}`)}
className={classnames(
this.props.className,
wrappedClass,
'bpw-chat-bubble',
`bpw-bubble-${type}`,
this.props.payload.className
)}
data-from={this.props.fromLabel}
tabIndex={-1}
style={additionalStyle}
Expand Down
35 changes: 33 additions & 2 deletions packages/webchat/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import constants from './core/constants'
import BpSocket from './core/socket'
import ChatIcon from './icons/Chat'
import { RootStore, StoreDef } from './store'
import { Config, Message } from './typings'
import { Config, Message, Trigger } from './typings'
import { isIE } from './utils'
import { initializeAnalytics, trackMessage, trackWebchatState } from './utils/analytics'
import { postMessageToParent } from './utils/webchatEvents'
Expand Down Expand Up @@ -229,9 +229,18 @@ class Web extends React.Component<MainProps> {
return
}

if (event.payload?.type === 'trigger') {
//
const { trigger } = event.payload
if (typeof trigger?.type === 'string') {
await this.handleTrigger(trigger)
return
}
}

if (this.props.currentConversation?.userId !== event.authorId) {
trackMessage('received')
postMessageToParent('MESSAGE.RECEIVED', event, this.props.config!.chatId)
postMessageToParent('MESSAGE.RECEIVED', event, this.config.chatId)

// This is to handle a special case for the emulator, setting the selected css class to the last message group
// This needs a rethinking
Expand Down Expand Up @@ -259,6 +268,28 @@ class Web extends React.Component<MainProps> {
await this.props.updateTyping!(event)
}

handleTrigger = async (trigger: Trigger.CustomEvent | Trigger.WebchatVisibility | Trigger.WebchatConfig) => {
if (trigger.type === 'webchat-visibility') {
if (trigger.visibility === 'hide') {
this.props.hideChat!()
} else if (trigger.visibility === 'show') {
this.props.showChat!()
} else if (trigger.visibility === 'toggle') {
this.props.displayWidgetView ? this.props.showChat!() : this.props.hideChat!()
}
}

if (trigger.type === 'webchat-config') {
await this.handleIframeApi({
data: { action: 'mergeConfig', payload: { ...this.config, ...(trigger.config ?? {}) } }
})
}

if (trigger.type === 'custom-event') {
postMessageToParent('TRIGGER', trigger.event, this.props.config!.chatId)
}
}

playSound = debounce(async () => {
const disableNotificationSound = this.config.disableNotificationSound || this.props.config?.disableNotificationSound
if (disableNotificationSound || this.audio.readyState < 2) {
Expand Down
18 changes: 18 additions & 0 deletions packages/webchat/src/typings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,23 @@ declare global {
}
}

export namespace Trigger {
export interface WebchatVisibility {
type: 'webchat-visibility'
visibility: 'show' | 'hide' | 'toggle'
}

export interface WebchatConfig {
type: 'webchat-config'
config: Partial<Config>
}

export interface CustomEvent {
type: 'custom-event'
event: any
}
}

export namespace Renderer {
export interface Message {
type?: string
Expand Down Expand Up @@ -168,6 +185,7 @@ export type WebchatEventType =
| 'MESSAGE.RECEIVED'
| 'MESSAGE.SELECTED'
| 'USER.CONNECTED'
| 'TRIGGER'

export interface WebchatEvent {
type: WebchatEventType
Expand Down

0 comments on commit d28cdf0

Please sign in to comment.