| 
									
										
										
										
											2024-06-14 08:42:41 +08:00
										 |  |  | import { | 
					
						
							|  |  |  |   useEffect, | 
					
						
							|  |  |  |   useState, | 
					
						
							|  |  |  | } from 'react' | 
					
						
							|  |  |  | import { useAsyncEffect } from 'ahooks' | 
					
						
							| 
									
										
										
										
											2024-07-24 14:46:52 +08:00
										 |  |  | import { useTranslation } from 'react-i18next' | 
					
						
							| 
									
										
										
										
											2024-06-14 08:42:41 +08:00
										 |  |  | import { | 
					
						
							|  |  |  |   EmbeddedChatbotContext, | 
					
						
							|  |  |  |   useEmbeddedChatbotContext, | 
					
						
							|  |  |  | } from './context' | 
					
						
							|  |  |  | import { useEmbeddedChatbot } from './hooks' | 
					
						
							|  |  |  | import { isDify } from './utils' | 
					
						
							| 
									
										
										
										
											2024-06-26 04:51:00 -05:00
										 |  |  | import { useThemeContext } from './theme/theme-context' | 
					
						
							| 
									
										
										
										
											2025-03-03 14:44:51 +08:00
										 |  |  | import { CssTransform } from './theme/utils' | 
					
						
							| 
									
										
										
										
											2024-06-14 08:42:41 +08:00
										 |  |  | import { checkOrSetAccessToken } from '@/app/components/share/utils' | 
					
						
							|  |  |  | import AppUnavailable from '@/app/components/base/app-unavailable' | 
					
						
							|  |  |  | import useBreakpoints, { MediaType } from '@/hooks/use-breakpoints' | 
					
						
							|  |  |  | import Loading from '@/app/components/base/loading' | 
					
						
							| 
									
										
										
										
											2024-09-08 12:14:11 +07:00
										 |  |  | import LogoHeader from '@/app/components/base/logo/logo-embedded-chat-header' | 
					
						
							| 
									
										
										
										
											2024-06-14 08:42:41 +08:00
										 |  |  | import Header from '@/app/components/base/chat/embedded-chatbot/header' | 
					
						
							|  |  |  | import ChatWrapper from '@/app/components/base/chat/embedded-chatbot/chat-wrapper' | 
					
						
							| 
									
										
										
										
											2025-03-03 14:44:51 +08:00
										 |  |  | import LogoSite from '@/app/components/base/logo/logo-site' | 
					
						
							|  |  |  | import cn from '@/utils/classnames' | 
					
						
							| 
									
										
										
										
											2024-06-14 08:42:41 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | const Chatbot = () => { | 
					
						
							|  |  |  |   const { | 
					
						
							|  |  |  |     isMobile, | 
					
						
							|  |  |  |     appInfoError, | 
					
						
							|  |  |  |     appInfoLoading, | 
					
						
							|  |  |  |     appData, | 
					
						
							|  |  |  |     appChatListDataLoading, | 
					
						
							| 
									
										
										
										
											2025-03-03 14:44:51 +08:00
										 |  |  |     chatShouldReloadKey, | 
					
						
							| 
									
										
										
										
											2024-06-14 08:42:41 +08:00
										 |  |  |     handleNewConversation, | 
					
						
							| 
									
										
										
										
											2024-06-26 04:51:00 -05:00
										 |  |  |     themeBuilder, | 
					
						
							| 
									
										
										
										
											2024-06-14 08:42:41 +08:00
										 |  |  |   } = useEmbeddedChatbotContext() | 
					
						
							| 
									
										
										
										
											2025-03-03 14:44:51 +08:00
										 |  |  |   const { t } = useTranslation() | 
					
						
							| 
									
										
										
										
											2024-06-14 08:42:41 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   const customConfig = appData?.custom_config | 
					
						
							|  |  |  |   const site = appData?.site | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const difyIcon = <LogoHeader /> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   useEffect(() => { | 
					
						
							| 
									
										
										
										
											2024-06-26 04:51:00 -05:00
										 |  |  |     themeBuilder?.buildTheme(site?.chat_color_theme, site?.chat_color_theme_inverted) | 
					
						
							| 
									
										
										
										
											2024-06-14 08:42:41 +08:00
										 |  |  |     if (site) { | 
					
						
							|  |  |  |       if (customConfig) | 
					
						
							|  |  |  |         document.title = `${site.title}` | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  |         document.title = `${site.title} - Powered by Dify` | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-08-02 15:08:14 +08:00
										 |  |  |   }, [site, customConfig, themeBuilder]) | 
					
						
							| 
									
										
										
										
											2024-06-14 08:42:41 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if (appInfoLoading) { | 
					
						
							|  |  |  |     return ( | 
					
						
							| 
									
										
										
										
											2025-03-03 14:44:51 +08:00
										 |  |  |       <> | 
					
						
							|  |  |  |         {!isMobile && <Loading type='app' />} | 
					
						
							|  |  |  |         {isMobile && ( | 
					
						
							|  |  |  |           <div className={cn('relative')}> | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |             <div className={cn('flex h-[calc(100vh_-_60px)] flex-col rounded-2xl border-[0.5px] border-components-panel-border shadow-xs')}> | 
					
						
							| 
									
										
										
										
											2025-03-03 14:44:51 +08:00
										 |  |  |               <Loading type='app' /> | 
					
						
							|  |  |  |             </div> | 
					
						
							|  |  |  |           </div> | 
					
						
							|  |  |  |         )} | 
					
						
							|  |  |  |       </> | 
					
						
							| 
									
										
										
										
											2024-06-14 08:42:41 +08:00
										 |  |  |     ) | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (appInfoError) { | 
					
						
							|  |  |  |     return ( | 
					
						
							| 
									
										
										
										
											2025-03-03 14:44:51 +08:00
										 |  |  |       <> | 
					
						
							|  |  |  |         {!isMobile && <AppUnavailable />} | 
					
						
							|  |  |  |         {isMobile && ( | 
					
						
							|  |  |  |           <div className={cn('relative')}> | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |             <div className={cn('flex h-[calc(100vh_-_60px)] flex-col rounded-2xl border-[0.5px] border-components-panel-border shadow-xs')}> | 
					
						
							| 
									
										
										
										
											2025-03-03 14:44:51 +08:00
										 |  |  |               <AppUnavailable /> | 
					
						
							|  |  |  |             </div> | 
					
						
							|  |  |  |           </div> | 
					
						
							|  |  |  |         )} | 
					
						
							|  |  |  |       </> | 
					
						
							| 
									
										
										
										
											2024-06-14 08:42:41 +08:00
										 |  |  |     ) | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return ( | 
					
						
							| 
									
										
										
										
											2025-03-03 14:44:51 +08:00
										 |  |  |     <div className='relative'> | 
					
						
							|  |  |  |       <div | 
					
						
							|  |  |  |         className={cn( | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |           'flex flex-col rounded-2xl border border-components-panel-border-subtle', | 
					
						
							| 
									
										
										
										
											2025-03-03 14:44:51 +08:00
										 |  |  |           isMobile ? 'h-[calc(100vh_-_60px)] border-[0.5px] border-components-panel-border shadow-xs' : 'h-[100vh] bg-chatbot-bg', | 
					
						
							|  |  |  |         )} | 
					
						
							|  |  |  |         style={isMobile ? Object.assign({}, CssTransform(themeBuilder?.theme?.backgroundHeaderColorStyle ?? '')) : {}} | 
					
						
							|  |  |  |       > | 
					
						
							|  |  |  |         <Header | 
					
						
							|  |  |  |           isMobile={isMobile} | 
					
						
							|  |  |  |           title={site?.title || ''} | 
					
						
							|  |  |  |           customerIcon={isDify() ? difyIcon : ''} | 
					
						
							|  |  |  |           theme={themeBuilder?.theme} | 
					
						
							|  |  |  |           onCreateNewChat={handleNewConversation} | 
					
						
							|  |  |  |         /> | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |         <div className={cn('flex grow flex-col overflow-y-auto', isMobile && '!h-[calc(100vh_-_3rem)] rounded-2xl bg-chatbot-bg')}> | 
					
						
							| 
									
										
										
										
											2025-03-03 14:44:51 +08:00
										 |  |  |           {appChatListDataLoading && ( | 
					
						
							| 
									
										
										
										
											2024-06-14 08:42:41 +08:00
										 |  |  |             <Loading type='app' /> | 
					
						
							|  |  |  |           )} | 
					
						
							| 
									
										
										
										
											2025-03-03 14:44:51 +08:00
										 |  |  |           {!appChatListDataLoading && ( | 
					
						
							|  |  |  |             <ChatWrapper key={chatShouldReloadKey} /> | 
					
						
							|  |  |  |           )} | 
					
						
							|  |  |  |         </div> | 
					
						
							|  |  |  |       </div> | 
					
						
							|  |  |  |       {/* powered by */} | 
					
						
							|  |  |  |       {isMobile && ( | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |         <div className='flex h-[60px] shrink-0 items-center pl-2'> | 
					
						
							| 
									
										
										
										
											2025-03-03 14:44:51 +08:00
										 |  |  |           {!appData?.custom_config?.remove_webapp_brand && ( | 
					
						
							|  |  |  |             <div className={cn( | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |               'flex shrink-0 items-center gap-1.5 px-2', | 
					
						
							| 
									
										
										
										
											2025-03-03 14:44:51 +08:00
										 |  |  |             )}> | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |               <div className='system-2xs-medium-uppercase text-text-tertiary'>{t('share.chat.poweredBy')}</div> | 
					
						
							| 
									
										
										
										
											2025-03-03 14:44:51 +08:00
										 |  |  |               {appData?.custom_config?.replace_webapp_logo && ( | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |                 <img src={appData?.custom_config?.replace_webapp_logo} alt='logo' className='block h-5 w-auto' /> | 
					
						
							| 
									
										
										
										
											2025-03-03 14:44:51 +08:00
										 |  |  |               )} | 
					
						
							|  |  |  |               {!appData?.custom_config?.replace_webapp_logo && ( | 
					
						
							|  |  |  |                 <LogoSite className='!h-5' /> | 
					
						
							| 
									
										
										
										
											2024-07-24 14:46:52 +08:00
										 |  |  |               )} | 
					
						
							|  |  |  |             </div> | 
					
						
							| 
									
										
										
										
											2024-06-14 08:42:41 +08:00
										 |  |  |           )} | 
					
						
							|  |  |  |         </div> | 
					
						
							| 
									
										
										
										
											2025-03-03 14:44:51 +08:00
										 |  |  |       )} | 
					
						
							| 
									
										
										
										
											2024-06-14 08:42:41 +08:00
										 |  |  |     </div> | 
					
						
							|  |  |  |   ) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const EmbeddedChatbotWrapper = () => { | 
					
						
							|  |  |  |   const media = useBreakpoints() | 
					
						
							|  |  |  |   const isMobile = media === MediaType.mobile | 
					
						
							| 
									
										
										
										
											2024-06-26 04:51:00 -05:00
										 |  |  |   const themeBuilder = useThemeContext() | 
					
						
							| 
									
										
										
										
											2024-06-14 08:42:41 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   const { | 
					
						
							|  |  |  |     appInfoError, | 
					
						
							|  |  |  |     appInfoLoading, | 
					
						
							|  |  |  |     appData, | 
					
						
							|  |  |  |     appParams, | 
					
						
							|  |  |  |     appMeta, | 
					
						
							|  |  |  |     appChatListDataLoading, | 
					
						
							|  |  |  |     currentConversationId, | 
					
						
							|  |  |  |     currentConversationItem, | 
					
						
							|  |  |  |     appPrevChatList, | 
					
						
							|  |  |  |     pinnedConversationList, | 
					
						
							|  |  |  |     conversationList, | 
					
						
							|  |  |  |     newConversationInputs, | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  |     newConversationInputsRef, | 
					
						
							| 
									
										
										
										
											2024-06-14 08:42:41 +08:00
										 |  |  |     handleNewConversationInputsChange, | 
					
						
							|  |  |  |     inputsForms, | 
					
						
							|  |  |  |     handleNewConversation, | 
					
						
							|  |  |  |     handleStartChat, | 
					
						
							|  |  |  |     handleChangeConversation, | 
					
						
							|  |  |  |     handleNewConversationCompleted, | 
					
						
							|  |  |  |     chatShouldReloadKey, | 
					
						
							|  |  |  |     isInstalledApp, | 
					
						
							|  |  |  |     appId, | 
					
						
							|  |  |  |     handleFeedback, | 
					
						
							|  |  |  |     currentChatInstanceRef, | 
					
						
							| 
									
										
										
										
											2025-03-13 14:23:41 +08:00
										 |  |  |     clearChatList, | 
					
						
							|  |  |  |     setClearChatList, | 
					
						
							|  |  |  |     isResponding, | 
					
						
							|  |  |  |     setIsResponding, | 
					
						
							| 
									
										
										
										
											2025-03-27 11:58:16 +08:00
										 |  |  |     currentConversationInputs, | 
					
						
							|  |  |  |     setCurrentConversationInputs, | 
					
						
							| 
									
										
										
										
											2024-06-14 08:42:41 +08:00
										 |  |  |   } = useEmbeddedChatbot() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return <EmbeddedChatbotContext.Provider value={{ | 
					
						
							|  |  |  |     appInfoError, | 
					
						
							|  |  |  |     appInfoLoading, | 
					
						
							|  |  |  |     appData, | 
					
						
							|  |  |  |     appParams, | 
					
						
							|  |  |  |     appMeta, | 
					
						
							|  |  |  |     appChatListDataLoading, | 
					
						
							|  |  |  |     currentConversationId, | 
					
						
							|  |  |  |     currentConversationItem, | 
					
						
							|  |  |  |     appPrevChatList, | 
					
						
							|  |  |  |     pinnedConversationList, | 
					
						
							|  |  |  |     conversationList, | 
					
						
							|  |  |  |     newConversationInputs, | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  |     newConversationInputsRef, | 
					
						
							| 
									
										
										
										
											2024-06-14 08:42:41 +08:00
										 |  |  |     handleNewConversationInputsChange, | 
					
						
							|  |  |  |     inputsForms, | 
					
						
							|  |  |  |     handleNewConversation, | 
					
						
							|  |  |  |     handleStartChat, | 
					
						
							|  |  |  |     handleChangeConversation, | 
					
						
							|  |  |  |     handleNewConversationCompleted, | 
					
						
							|  |  |  |     chatShouldReloadKey, | 
					
						
							|  |  |  |     isMobile, | 
					
						
							|  |  |  |     isInstalledApp, | 
					
						
							|  |  |  |     appId, | 
					
						
							|  |  |  |     handleFeedback, | 
					
						
							|  |  |  |     currentChatInstanceRef, | 
					
						
							| 
									
										
										
										
											2024-06-26 04:51:00 -05:00
										 |  |  |     themeBuilder, | 
					
						
							| 
									
										
										
										
											2025-03-13 14:23:41 +08:00
										 |  |  |     clearChatList, | 
					
						
							|  |  |  |     setClearChatList, | 
					
						
							|  |  |  |     isResponding, | 
					
						
							|  |  |  |     setIsResponding, | 
					
						
							| 
									
										
										
										
											2025-03-27 11:58:16 +08:00
										 |  |  |     currentConversationInputs, | 
					
						
							|  |  |  |     setCurrentConversationInputs, | 
					
						
							| 
									
										
										
										
											2024-06-14 08:42:41 +08:00
										 |  |  |   }}> | 
					
						
							|  |  |  |     <Chatbot /> | 
					
						
							|  |  |  |   </EmbeddedChatbotContext.Provider> | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const EmbeddedChatbot = () => { | 
					
						
							|  |  |  |   const [initialized, setInitialized] = useState(false) | 
					
						
							|  |  |  |   const [appUnavailable, setAppUnavailable] = useState<boolean>(false) | 
					
						
							|  |  |  |   const [isUnknownReason, setIsUnknownReason] = useState<boolean>(false) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   useAsyncEffect(async () => { | 
					
						
							|  |  |  |     if (!initialized) { | 
					
						
							|  |  |  |       try { | 
					
						
							|  |  |  |         await checkOrSetAccessToken() | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       catch (e: any) { | 
					
						
							|  |  |  |         if (e.status === 404) { | 
					
						
							|  |  |  |           setAppUnavailable(true) | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |           setIsUnknownReason(true) | 
					
						
							|  |  |  |           setAppUnavailable(true) | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       setInitialized(true) | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }, []) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (!initialized) | 
					
						
							|  |  |  |     return null | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (appUnavailable) | 
					
						
							|  |  |  |     return <AppUnavailable isUnknownReason={isUnknownReason} /> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return <EmbeddedChatbotWrapper /> | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default EmbeddedChatbot |