You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
240 lines
6.9 KiB
TypeScript
240 lines
6.9 KiB
TypeScript
'use client'
|
|
import {
|
|
useCallback,
|
|
useEffect,
|
|
useState,
|
|
} from 'react'
|
|
import { useAsyncEffect } from 'ahooks'
|
|
import { useTranslation } from 'react-i18next'
|
|
import { RiLoopLeftLine } from '@remixicon/react'
|
|
import { usePathname, useRouter, useSearchParams } from 'next/navigation'
|
|
import Tooltip from '../../tooltip'
|
|
import {
|
|
EmbeddedChatbotContext,
|
|
useEmbeddedChatbotContext,
|
|
} from './context'
|
|
import { useEmbeddedChatbot } from './hooks'
|
|
import { isDify } from './utils'
|
|
import { useThemeContext } from './theme/theme-context'
|
|
import { checkOrSetAccessToken, removeAccessToken } 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'
|
|
import LogoHeader from '@/app/components/base/logo/logo-embedded-chat-header'
|
|
import Header from '@/app/components/base/chat/embedded-chatbot/header'
|
|
import ConfigPanel from '@/app/components/base/chat/embedded-chatbot/config-panel'
|
|
import ChatWrapper from '@/app/components/base/chat/embedded-chatbot/chat-wrapper'
|
|
import cn from '@/utils/classnames'
|
|
import useDocumentTitle from '@/hooks/use-document-title'
|
|
|
|
const Chatbot = () => {
|
|
const { t } = useTranslation()
|
|
const {
|
|
userCanAccess,
|
|
isMobile,
|
|
appInfoError,
|
|
appInfoLoading,
|
|
appData,
|
|
appPrevChatList,
|
|
showConfigPanelBeforeChat,
|
|
appChatListDataLoading,
|
|
handleNewConversation,
|
|
themeBuilder,
|
|
isInstalledApp,
|
|
} = useEmbeddedChatbotContext()
|
|
|
|
const chatReady = (!showConfigPanelBeforeChat || !!appPrevChatList.length)
|
|
const customConfig = appData?.custom_config
|
|
const site = appData?.site
|
|
|
|
const difyIcon = <LogoHeader />
|
|
|
|
useEffect(() => {
|
|
themeBuilder?.buildTheme(site?.chat_color_theme, site?.chat_color_theme_inverted)
|
|
if (site) {
|
|
if (customConfig)
|
|
document.title = `${site.title}`
|
|
else
|
|
document.title = `${site.title} - Powered by Dify`
|
|
}
|
|
}, [site, customConfig, themeBuilder])
|
|
|
|
useDocumentTitle(site?.title || 'Chat')
|
|
|
|
const searchParams = useSearchParams()
|
|
const router = useRouter()
|
|
const pathname = usePathname()
|
|
const getSigninUrl = useCallback(() => {
|
|
const params = new URLSearchParams(searchParams)
|
|
params.delete('message')
|
|
params.set('redirect_url', pathname)
|
|
return `/webapp-signin?${params.toString()}`
|
|
}, [searchParams, pathname])
|
|
|
|
const backToHome = useCallback(() => {
|
|
removeAccessToken()
|
|
const url = getSigninUrl()
|
|
router.replace(url)
|
|
}, [getSigninUrl, router])
|
|
|
|
if (appInfoLoading) {
|
|
return (
|
|
<Loading type='app' />
|
|
)
|
|
}
|
|
|
|
if (!userCanAccess) {
|
|
return <div className='flex h-full flex-col items-center justify-center gap-y-2'>
|
|
<AppUnavailable className='h-auto w-auto' code={403} unknownReason='no permission.' />
|
|
{!isInstalledApp && <span className='system-sm-regular cursor-pointer text-text-tertiary' onClick={backToHome}>{t('common.userProfile.logout')}</span>}
|
|
</div>
|
|
}
|
|
|
|
if (appInfoError) {
|
|
return (
|
|
<AppUnavailable />
|
|
)
|
|
}
|
|
return (
|
|
<div>
|
|
<Header
|
|
isMobile={isMobile}
|
|
title={site?.title || ''}
|
|
customerIcon={isDify() ? difyIcon : ''}
|
|
theme={themeBuilder?.theme}
|
|
onCreateNewChat={handleNewConversation}
|
|
/>
|
|
<div className='flex bg-white overflow-hidden'>
|
|
<div className={cn('h-[100vh] grow flex flex-col overflow-y-auto', isMobile && '!h-[calc(100vh_-_3rem)]')}>
|
|
{showConfigPanelBeforeChat && !appChatListDataLoading && !appPrevChatList.length && (
|
|
<div className={cn('flex w-full items-center justify-center h-full tablet:px-4', isMobile && 'px-4')}>
|
|
<ConfigPanel />
|
|
</div>
|
|
)}
|
|
{appChatListDataLoading && chatReady && (
|
|
<Loading type='app' />
|
|
)}
|
|
{chatReady && !appChatListDataLoading && (
|
|
<div className='relative h-full pt-8 mx-auto w-full max-w-[720px]'>
|
|
{!isMobile && (
|
|
<div className='absolute top-2.5 right-3 z-20'>
|
|
<Tooltip
|
|
popupContent={t('share.chat.resetChat')}
|
|
>
|
|
<div className='p-1.5 bg-white border-[0.5px] border-gray-100 rounded-lg shadow-md cursor-pointer' onClick={handleNewConversation}>
|
|
<RiLoopLeftLine className="h-4 w-4 text-gray-500" />
|
|
</div>
|
|
</Tooltip>
|
|
</div>
|
|
)}
|
|
<ChatWrapper />
|
|
</div>
|
|
)}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
const EmbeddedChatbotWrapper = () => {
|
|
const media = useBreakpoints()
|
|
const isMobile = media === MediaType.mobile
|
|
const themeBuilder = useThemeContext()
|
|
|
|
const {
|
|
appInfoError,
|
|
appInfoLoading,
|
|
appData,
|
|
userCanAccess,
|
|
appParams,
|
|
appMeta,
|
|
appChatListDataLoading,
|
|
currentConversationId,
|
|
currentConversationItem,
|
|
appPrevChatList,
|
|
pinnedConversationList,
|
|
conversationList,
|
|
showConfigPanelBeforeChat,
|
|
newConversationInputs,
|
|
newConversationInputsRef,
|
|
handleNewConversationInputsChange,
|
|
inputsForms,
|
|
handleNewConversation,
|
|
handleStartChat,
|
|
handleChangeConversation,
|
|
handleNewConversationCompleted,
|
|
chatShouldReloadKey,
|
|
isInstalledApp,
|
|
appId,
|
|
handleFeedback,
|
|
currentChatInstanceRef,
|
|
} = useEmbeddedChatbot()
|
|
|
|
return <EmbeddedChatbotContext.Provider value={{
|
|
userCanAccess,
|
|
appInfoError,
|
|
appInfoLoading,
|
|
appData,
|
|
appParams,
|
|
appMeta,
|
|
appChatListDataLoading,
|
|
currentConversationId,
|
|
currentConversationItem,
|
|
appPrevChatList,
|
|
pinnedConversationList,
|
|
conversationList,
|
|
showConfigPanelBeforeChat,
|
|
newConversationInputs,
|
|
newConversationInputsRef,
|
|
handleNewConversationInputsChange,
|
|
inputsForms,
|
|
handleNewConversation,
|
|
handleStartChat,
|
|
handleChangeConversation,
|
|
handleNewConversationCompleted,
|
|
chatShouldReloadKey,
|
|
isMobile,
|
|
isInstalledApp,
|
|
appId,
|
|
handleFeedback,
|
|
currentChatInstanceRef,
|
|
themeBuilder,
|
|
}}>
|
|
<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
|