@ibm-watson/assistant-web-chat-react
Version:
A provider to inject web chat into your React application.
76 lines (75 loc) • 3.9 kB
TypeScript
/**
* (C) Copyright IBM Corp. 2022, 2024.
*
* Licensed under the MIT License (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* https://opensource.org/licenses/MIT
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
*/
import React, { ReactNode, MutableRefObject } from 'react';
import { WebChatConfig } from './types/WebChatConfig';
import { WebChatInstance } from './types/WebChatInstance';
import { UserDefinedResponseEvent } from './types/UserDefinedResponseEvent';
/**
* The type of the render function that is used to render user defined responses. This function should return a
* component that renders the display for the message contained in the given event.
*
* @param event The UserDefinedResponseEvent that was originally fired by web chat when the user defined response
* was first fired.
* @param instance The current instance of the web chat.
*/
type RenderUserDefinedResponse = (event: UserDefinedResponseEvent, instance: WebChatInstance) => ReactNode;
interface WebChatContainerProps {
/**
* The config to use to load web chat. Note that the "onLoad" property is overridden by this component. If you
* need to perform any actions after web chat been loaded, use the "onBeforeRender" or "onAfterRender" props.
*/
config: WebChatConfig;
/**
* This function is called before the render function of web chat is called. This function can return a Promise
* which will cause web chat to wait for it before rendering.
*/
onBeforeRender?: (instance: WebChatInstance) => Promise<void>;
/**
* This function is called after the render function of web chat is called. This function can return a Promise
* which will cause web chat to wait for it before rendering.
*/
onAfterRender?: (instance: WebChatInstance) => Promise<void>;
/**
* This is the function that this component will call when a user defined response should be rendered.
*/
renderUserDefinedResponse?: RenderUserDefinedResponse;
/**
* A convenience prop that is a reference to the web chat instance. This component will set the value of this ref
* when the instance has been created.
*/
instanceRef?: MutableRefObject<WebChatInstance>;
/**
* Set the url where web chat assets are hosted. Used for development purposes.
*/
hostURL?: string;
}
/**
* This is a component wrapper for web chat. This can be rendered anywhere in your application but you should make
* sure it doesn't get unmounted during in the middle of your App's life or it will lose any user defined responses
* that were previously received.
*
* Note that this container will override any config.onLoad property you have set. If you need access to the web
* chat instance or need to perform additional customizations of web chat when it loads, use the onBeforeRender
* callback prop to this component.
*/
declare function WebChatContainer({ onBeforeRender, onAfterRender, renderUserDefinedResponse, config, instanceRef, hostURL, }: WebChatContainerProps): React.JSX.Element;
/**
* A public function that can be used to turn logging off or on.
*/
declare function setEnableDebug(enableDebug: boolean): void;
/**
* Ensures that the javascript for web chat has been loaded.
*/
declare function ensureWebChatScript(webChatConfig: WebChatConfig, hostURL: string): Promise<any>;
export { setEnableDebug, WebChatContainer, WebChatContainerProps, ensureWebChatScript, RenderUserDefinedResponse };