import type { FC } from 'react' import { useEffect, useState, } from 'react' import { useAsyncEffect } from 'ahooks' import { ChatWithHistoryContext, useChatWithHistoryContext, } from './context' import { useChatWithHistory } from './hooks' import Sidebar from './sidebar' import HeaderInMobile from './header-in-mobile' import ConfigPanel from './config-panel' 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' type ChatWithHistoryProps = { className?: string } const ChatWithHistory: FC = ({ className, }) => { const { appData, appInfoLoading, appPrevChatList, showConfigPanelBeforeChat, appChatListDataLoading, chatShouldReloadKey, isMobile, } = useChatWithHistoryContext() const chatReady = (!showConfigPanelBeforeChat || !!appPrevChatList.length) const customConfig = appData?.custom_config const site = appData?.site useEffect(() => { if (site) { if (customConfig) document.title = `${site.title}` else document.title = `${site.title} - Powered by Dify` } }, [site, customConfig]) if (appInfoLoading) { return ( ) } return (
{ !isMobile && ( ) } { isMobile && ( ) }
{ showConfigPanelBeforeChat && !appChatListDataLoading && !appPrevChatList.length && (
) } { appChatListDataLoading && chatReady && ( ) } { chatReady && !appChatListDataLoading && ( ) }
) } export type ChatWithHistoryWrapProps = { installedAppInfo?: InstalledApp className?: string } const ChatWithHistoryWrap: FC = ({ installedAppInfo, className, }) => { const media = useBreakpoints() const isMobile = media === MediaType.mobile const { appInfoLoading, appData, appParams, appMeta, appChatListDataLoading, currentConversationId, currentConversationItem, appPrevChatList, pinnedConversationList, conversationList, showConfigPanelBeforeChat, newConversationInputs, handleNewConversationInputsChange, inputsForms, handleNewConversation, handleStartChat, handleChangeConversation, handlePinConversation, handleUnpinConversation, handleDeleteConversation, conversationRenaming, handleRenameConversation, handleNewConversationCompleted, chatShouldReloadKey, isInstalledApp, appId, handleFeedback, currentChatInstanceRef, } = useChatWithHistory(installedAppInfo) return ( ) } const ChatWithHistoryWrapWithCheckToken: FC = ({ installedAppInfo, className, }) => { const [inited, setInited] = useState(false) useAsyncEffect(async () => { if (!inited) { if (!installedAppInfo) await checkOrSetAccessToken() setInited(true) } }, []) if (!inited) return null return ( ) } export default ChatWithHistoryWrapWithCheckToken