UNPKG

@rohitninawe/chat-uikit-react-native

Version:

Ready-to-use Chat UI Components for React Native

134 lines 5.48 kB
import React, { useContext, useEffect, useState } from 'react'; import { View } from 'react-native'; import { CometChatContext, CometChatTheme, localize } from '../../shared'; import { CometChatPanel } from '../../shared/views/CometChatPanel/CometChatPanel'; import CometChatAICard from '../../shared/views/CometChatAICard/CometChatAICard'; ; const defaultProps = { getConversationSummaryCallback: undefined, editReplyCallback: undefined, configuration: undefined, theme: new CometChatTheme({}), onPanelClose: () => { } }; let inProgress = false; const AIConversationSummaryView = (props) => { const [messageListState, setMessageListState] = useState("loading" /* States.loading */); const [activeView, setActiveView] = useState(null); const { theme } = useContext(CometChatContext); const errorStateText = localize("SOMETHING_WRONG"); const emptyStateText = localize("NO_MESSAGES_FOUND"); const loadingStateText = localize("GENERATING_SUMMARY"); const titleText = localize("CONVERSATION_SUMMARY"); useEffect(() => { fetchButtonContent(); }, []); function fetchButtonContent() { setMessageListState("loading" /* States.loading */); if (props && props.getConversationSummaryCallback) { props.getConversationSummaryCallback(theme) .then((response) => { if (response) { inProgress = true; getLoadedView(response) .then(res => { setMessageListState("loaded" /* States.loaded */); setActiveView(res); }) .catch(err => { inProgress = false; setMessageListState("empty" /* States.empty */); }); } else { inProgress = false; setMessageListState("empty" /* States.empty */); } }) .catch((err) => { inProgress = false; setMessageListState("error" /* States.error */); }); } } /** * Create a view based on the value of the `state` prop. */ function getStateView() { let res = null; switch (messageListState) { case "loading" /* States.loading */: res = getLoadingView(); break; case "error" /* States.error */: res = getErrorView(); break; case "empty" /* States.empty */: res = getEmptyView(); break; case "loaded" /* States.loaded */: break; default: const x = messageListState; } return res; } /** * Creates the loading view */ function getLoadingView() { let LoadingView = props.configuration?.LoadingStateView; return (<CometChatAICard state={"loading" /* States.loading */} style={props.configuration?.conversationSummaryStyle} loadingIconURL={props.configuration?.loadingIconURL} loadingStateText={loadingStateText}> {LoadingView ? <LoadingView /> : null} </CometChatAICard>); } /** * Creates the error view */ function getErrorView() { let ErrorView = props.configuration?.ErrorStateView; return (<CometChatAICard state={"error" /* States.error */} style={props.configuration?.conversationSummaryStyle} errorIconURL={props.configuration?.errorIconURL} errorStateText={errorStateText}> {ErrorView ? <ErrorView /> : null} </CometChatAICard>); } /** * Creates the empty view */ function getEmptyView() { let EmptyView = props.configuration?.EmptyStateView; return (<CometChatAICard state={"empty" /* States.empty */} style={props.configuration?.conversationSummaryStyle} emptyIconURL={props.configuration?.emptyIconURL} emptyStateText={emptyStateText}> {EmptyView ? <EmptyView /> : null} </CometChatAICard>); } /** * Creates the loaded view */ async function getLoadedView(conversationSummary) { return new Promise((resolve, reject) => { try { let CustomView = props.configuration?.customView; if (CustomView) { props.configuration?.customView(conversationSummary, props.onPanelClose).then((res) => { return resolve(res); }) .catch((err) => { return reject(err); }); } else { let conversationSummaryView = <CometChatPanel title={titleText} textContent={conversationSummary} style={props.configuration?.conversationSummaryStyle} onClose={props.onPanelClose}/>; return resolve(conversationSummaryView); } } catch (e) { reject(e); } }); } return (<View style={{ marginTop: 10, overflow: "hidden" }}> {messageListState === "loaded" /* States.loaded */ ? activeView : getStateView()} </View>); }; AIConversationSummaryView.defaultProps = defaultProps; export default AIConversationSummaryView; //# sourceMappingURL=AIConversationSummaryView.js.map