|
| 1 | +/** |
| 2 | + * (C) Copyright IBM Corp. 2022. |
| 3 | + * |
| 4 | + * Licensed under the MIT License (the "License"); you may not use this file except in compliance with |
| 5 | + * the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * https://opensource.org/licenses/MIT |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 10 | + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 11 | + * specific language governing permissions and limitations under the License. |
| 12 | + * |
| 13 | + */ |
| 14 | + |
| 15 | +import React, { useCallback, useMemo, useState } from 'react'; |
| 16 | +import { WebChatContainer, WebChatContainerProps } from './WebChatContainer'; |
| 17 | +import { WebChatInstance } from './types/WebChatInstance'; |
| 18 | + |
| 19 | +interface WebChatCustomElementProps extends WebChatContainerProps { |
| 20 | + /** |
| 21 | + * An optional classname that will be added to the custom element. |
| 22 | + */ |
| 23 | + className?: string; |
| 24 | + |
| 25 | + /** |
| 26 | + * An optional id that will be added to the custom element. |
| 27 | + */ |
| 28 | + id?: string; |
| 29 | + |
| 30 | + /** |
| 31 | + * An optional listener for "view:change" events. Such a listener is required when using a custom element in order |
| 32 | + * to control the visibility of the web chat main window. If no callback is provided here, a default one will be |
| 33 | + * used that just adds the classname "HideWebChat" when the main window is closed and removes it when the main |
| 34 | + * window is opened. If you use the default, you will also need to add a |
| 35 | + * "#WACContainer.WACContainer .HideWebChat { display: none }" rule to your CSS. |
| 36 | + * |
| 37 | + * You can provide a different callback here if you want custom behavior such as an animation when the main window |
| 38 | + * is opened or closed. |
| 39 | + * |
| 40 | + * Note that this function can only be provided before web chat is loaded. After web chat is loaded, the event |
| 41 | + * handler will not be updated. |
| 42 | + */ |
| 43 | + onViewChange?: (event: any, instance: WebChatInstance) => void; |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * This component can be used if you want to render web chat inside a custom element. It will perform two functions: |
| 48 | + * |
| 49 | + * 1. It will create the custom element as part of the React application. |
| 50 | + * 2. It will attach web chat to the custom element and use the WebChatContainer component to manage the life cycle |
| 51 | + * of the web chat instance. |
| 52 | + */ |
| 53 | +function WebChatCustomElement(props: WebChatCustomElementProps) { |
| 54 | + const { className, id, onViewChange, config, onBeforeRender, ...containerProps } = props; |
| 55 | + const [customElement, setCustomElement] = useState<HTMLDivElement>(); |
| 56 | + |
| 57 | + // Make sure to memoize the config object. If we pass a new object to WebChatContainer (even if all the properties |
| 58 | + // inside of it are the same), the container will just continually destroy and recreate the web chat instance |
| 59 | + // because it thinks the config keeps changing. |
| 60 | + const useConfig = useMemo(() => { |
| 61 | + return { |
| 62 | + ...config, |
| 63 | + element: customElement, |
| 64 | + }; |
| 65 | + }, [config, customElement]); |
| 66 | + |
| 67 | + const onBeforeRenderOverride = useCallback( |
| 68 | + async (instance: WebChatInstance) => { |
| 69 | + /** |
| 70 | + * A default handler for the "view:change" event. This will be used to show or hide the web chat main window |
| 71 | + * using a simple classname. |
| 72 | + */ |
| 73 | + function defaultViewChangeHandler(event: any, instance: WebChatInstance) { |
| 74 | + if (event.newViewState.mainWindow) { |
| 75 | + instance.elements.getMainWindow().removeClassName('HideWebChat'); |
| 76 | + } else { |
| 77 | + instance.elements.getMainWindow().addClassName('HideWebChat'); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + instance.on({ type: 'view:change', handler: onViewChange || defaultViewChangeHandler }); |
| 82 | + |
| 83 | + return onBeforeRender?.(instance); |
| 84 | + }, |
| 85 | + [onBeforeRender, onViewChange], |
| 86 | + ); |
| 87 | + |
| 88 | + return ( |
| 89 | + <> |
| 90 | + <div className={className} id={id} ref={setCustomElement} /> |
| 91 | + {customElement && ( |
| 92 | + <WebChatContainer config={useConfig} onBeforeRender={onBeforeRenderOverride} {...containerProps} /> |
| 93 | + )} |
| 94 | + </> |
| 95 | + ); |
| 96 | +} |
| 97 | + |
| 98 | +export { WebChatCustomElement }; |
0 commit comments