import { v4 as uuidv4 } from 'uuid' import { MESSAGE_TYPES, NOTIFICATION_TYPES } from './constants' import { commsHandler } from './handlers' import { ConfirmNotificationOptions, Context, NotificationMessage, NotificationOptions, NotificationType, } from './types' export default class HelpScout { async getApplicationContext(): Promise { commsHandler.send(MESSAGE_TYPES.GET_APP_CONTEXT) return (await commsHandler.receive( MESSAGE_TYPES.SEND_APP_CONTEXT )) as Context } watchApplicationContext(callback: (context: Context) => void): void { commsHandler.listen(MESSAGE_TYPES.SEND_APP_CONTEXT, (data: unknown) => { callback(data as Context) }) } setClipboardText(text: string, successMessage = 'Text copied') { commsHandler.send(MESSAGE_TYPES.SET_CLIPBOARD_TEXT, { value: { text: text, successMessage: successMessage }, }) } setAppHeight(height: number) { commsHandler.send(MESSAGE_TYPES.SET_APP_HEIGHT, { value: height }) } showNotification( type: NotificationType, text: string, options: NotificationOptions | ConfirmNotificationOptions = {} ) { // Generate a unique ID for every notification if not already provided if (!options.id) { options.id = uuidv4() } if (type !== NOTIFICATION_TYPES.CONFIRM) { const notificationOptions = options as NotificationOptions const buttonOnClick = notificationOptions?.buttonOptions?.onClick if (buttonOnClick) { commsHandler.listen( MESSAGE_TYPES.CLICK_NOTIFICATION_BUTTON, (value: unknown) => { const message = value as NotificationMessage if (message.id === options.id) { buttonOnClick() } } ) } } commsHandler.send(MESSAGE_TYPES.SHOW_NOTIFICATION, { value: { type, message: text, options: type === NOTIFICATION_TYPES.CONFIRM ? (options as ConfirmNotificationOptions) : (options as NotificationOptions), }, }) if (type === NOTIFICATION_TYPES.CONFIRM) { const confirmNotificationOptions = options as ConfirmNotificationOptions if (confirmNotificationOptions.onConfirm) { commsHandler.listen( MESSAGE_TYPES.CONFIRM_NOTIFICATION_CONFIRMED, (value: unknown) => { const message = value as NotificationMessage if ( message.id === confirmNotificationOptions.id && confirmNotificationOptions.onConfirm ) { confirmNotificationOptions.onConfirm() } } ) } if (confirmNotificationOptions.onClose) { commsHandler.listen( MESSAGE_TYPES.CONFIRM_NOTIFICATION_CLOSED, (value: unknown) => { const message = value as NotificationMessage if ( message.id === confirmNotificationOptions.id && confirmNotificationOptions.onClose ) { confirmNotificationOptions.onClose() } } ) } } } async getAppStyles(): Promise { commsHandler.send(MESSAGE_TYPES.GET_APP_STYLES) const { styles }: { styles: string } = await commsHandler.receive( MESSAGE_TYPES.SEND_APP_STYLES ) return styles } openSidePanel(contentUrl: string) { commsHandler.send(MESSAGE_TYPES.OPEN_SIDE_PANEL, { value: contentUrl }) } closeSidePanel() { commsHandler.send(MESSAGE_TYPES.CLOSE_SIDE_PANEL) } getInstallationIds() { const name = window?.name return name && name.startsWith('app-') ? name .replace('app-', '') .split('-') .filter(item => !!item) : [] } }