|
| 1 | +/** |
| 2 | + * (C) Copyright IBM Corp. 2022, 2024. |
| 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, { ReactNode } from 'react'; |
| 16 | +import ReactDOM from 'react-dom'; |
| 17 | +import { UserDefinedResponseEvent } from './types/UserDefinedResponseEvent'; |
| 18 | +import { WebChatInstance } from './types/WebChatInstance'; |
| 19 | +import { RenderUserDefinedResponse } from './WebChatContainer'; |
| 20 | + |
| 21 | +interface UserDefinedResponsePortalsContainer { |
| 22 | + /** |
| 23 | + * The instance of a web chat that this component will register listeners on. |
| 24 | + */ |
| 25 | + webChatInstance: WebChatInstance; |
| 26 | + |
| 27 | + /** |
| 28 | + * The function that this component will use to request the actual React content to display for each user defined |
| 29 | + * response. |
| 30 | + */ |
| 31 | + renderResponse: RenderUserDefinedResponse; |
| 32 | + |
| 33 | + /** |
| 34 | + * The list of events that were fired that contain all the responses to render. |
| 35 | + */ |
| 36 | + userDefinedResponseEvents: UserDefinedResponseEvent[]; |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * This is a utility component that is used to manage all the user defined responses that are rendered by web chat. |
| 41 | + * When a user defined response message is received by web chat, it will fire a "userDefinedResponse" event that |
| 42 | + * provides an HTML element to which your application can attach user defined content. React portals are a mechanism |
| 43 | + * that allows you to render a component in your React application but attach that component to the HTML element |
| 44 | + * that was provided by web chat. |
| 45 | + * |
| 46 | + * This component will render a portal for each user defined response. The contents of that portal will be |
| 47 | + * determined by calling the provided "renderResponse" render prop. |
| 48 | + */ |
| 49 | +function UserDefinedResponsePortalsContainer({ |
| 50 | + webChatInstance, |
| 51 | + renderResponse, |
| 52 | + userDefinedResponseEvents, |
| 53 | +}: UserDefinedResponsePortalsContainer) { |
| 54 | + // All we need to do to enable the React portals is to render each portal somewhere in your application (it |
| 55 | + // doesn't really matter where). |
| 56 | + return ( |
| 57 | + <> |
| 58 | + {userDefinedResponseEvents.map(function mapEvent(event, index) { |
| 59 | + return ( |
| 60 | + // eslint-disable-next-line react/no-array-index-key |
| 61 | + <UserDefinedResponseComponentPortal key={index} hostElement={event.data.element}> |
| 62 | + {renderResponse(event, webChatInstance)} |
| 63 | + </UserDefinedResponseComponentPortal> |
| 64 | + ); |
| 65 | + })} |
| 66 | + </> |
| 67 | + ); |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * This is the component that will attach a React portal to the given host element. The host element is the element |
| 72 | + * provided by web chat where your user defined response will be displayed in the DOM. This portal will attach any React |
| 73 | + * children passed to it under this component so you can render the response using your own React application. Those |
| 74 | + * children will be rendered under the given element where it lives in the DOM. |
| 75 | + */ |
| 76 | +function UserDefinedResponseComponentPortal({ |
| 77 | + hostElement, |
| 78 | + children, |
| 79 | +}: { |
| 80 | + hostElement: HTMLElement; |
| 81 | + children: ReactNode; |
| 82 | +}) { |
| 83 | + return ReactDOM.createPortal(children, hostElement); |
| 84 | +} |
| 85 | + |
| 86 | +const UserDefinedResponsePortalsContainerExport = React.memo(UserDefinedResponsePortalsContainer); |
| 87 | +export { UserDefinedResponsePortalsContainerExport as UserDefinedResponsePortalsContainer }; |
0 commit comments