import type { FC } from 'react' import { useEffect, useState, } from 'react' import { useAsyncEffect } from 'ahooks' import { useThemeContext } from '../embedded-chatbot/theme/theme-context' import { ChatWithHistoryContext, useChatWithHistoryContext, } from './context' import { useChatWithHistory } from './hooks' import Sidebar from './sidebar' import Header from './header' import HeaderInMobile from './header-in-mobile' import ChatWrapper from './chat-wrapper' import type { InstalledApp } from '@/models/explore' import Loading from '@/app/components/base/loading' import useBreakpoints, { MediaType } from '@/hooks/use-breakpoints' import { checkOrSetAccessToken } from '@/app/components/share/utils' import AppUnavailable from '@/app/components/base/app-unavailable' import cn from '@/utils/classnames' type ChatWithHistoryProps = { className?: string } const ChatWithHistory: FC = ({ className, }) => { const { appInfoError, appData, appInfoLoading, appChatListDataLoading, chatShouldReloadKey, isMobile, themeBuilder, sidebarCollapseState, } = useChatWithHistoryContext() const isSidebarCollapsed = sidebarCollapseState const customConfig = appData?.custom_config const site = appData?.site const [showSidePanel, setShowSidePanel] = useState(false) 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]) if (appInfoLoading) { return ( ) } if (appInfoError) { return ( ) } return (
{!isMobile && (
)} {isMobile && ( )}
{isSidebarCollapsed && (
setShowSidePanel(true)} onMouseLeave={() => setShowSidePanel(false)} >
)}
{!isMobile &&
} {appChatListDataLoading && ( )} {!appChatListDataLoading && ( )}
) } export type ChatWithHistoryWrapProps = { installedAppInfo?: InstalledApp className?: string } const ChatWithHistoryWrap: FC = ({ installedAppInfo, className, }) => { const media = useBreakpoints() const isMobile = media === MediaType.mobile const themeBuilder = useThemeContext() const { appInfoError, appInfoLoading, appData, appParams, appMeta, appChatListDataLoading, currentConversationId, currentConversationItem, appPrevChatTree, pinnedConversationList, conversationList, newConversationInputs, newConversationInputsRef, handleNewConversationInputsChange, inputsForms, handleNewConversation, handleStartChat, handleChangeConversation, handlePinConversation, handleUnpinConversation, handleDeleteConversation, conversationRenaming, handleRenameConversation, handleNewConversationCompleted, chatShouldReloadKey, isInstalledApp, appId, handleFeedback, currentChatInstanceRef, sidebarCollapseState, handleSidebarCollapse, clearChatList, setClearChatList, isResponding, setIsResponding, } = useChatWithHistory(installedAppInfo) return ( ) } const ChatWithHistoryWrapWithCheckToken: FC = ({ installedAppInfo, className, }) => { const [initialized, setInitialized] = useState(false) const [appUnavailable, setAppUnavailable] = useState(false) const [isUnknownReason, setIsUnknownReason] = useState(false) useAsyncEffect(async () => { if (!initialized) { if (!installedAppInfo) { 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 return ( ) } export default ChatWithHistoryWrapWithCheckToken