index.tsx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. import type { FC } from 'react'
  2. import {
  3. useEffect,
  4. useState,
  5. } from 'react'
  6. import { useAsyncEffect } from 'ahooks'
  7. import { useThemeContext } from '../embedded-chatbot/theme/theme-context'
  8. import {
  9. ChatWithHistoryContext,
  10. useChatWithHistoryContext,
  11. } from './context'
  12. import { useChatWithHistory } from './hooks'
  13. import Sidebar from './sidebar'
  14. import Header from './header'
  15. import HeaderInMobile from './header-in-mobile'
  16. import ChatWrapper from './chat-wrapper'
  17. import type { InstalledApp } from '@/models/explore'
  18. import Loading from '@/app/components/base/loading'
  19. import useBreakpoints, { MediaType } from '@/hooks/use-breakpoints'
  20. import { checkOrSetAccessToken } from '@/app/components/share/utils'
  21. import AppUnavailable from '@/app/components/base/app-unavailable'
  22. import cn from '@/utils/classnames'
  23. type ChatWithHistoryProps = {
  24. className?: string
  25. }
  26. const ChatWithHistory: FC<ChatWithHistoryProps> = ({
  27. className,
  28. }) => {
  29. const {
  30. appInfoError,
  31. appData,
  32. appInfoLoading,
  33. appChatListDataLoading,
  34. chatShouldReloadKey,
  35. isMobile,
  36. themeBuilder,
  37. sidebarCollapseState,
  38. } = useChatWithHistoryContext()
  39. const isSidebarCollapsed = sidebarCollapseState
  40. const customConfig = appData?.custom_config
  41. const site = appData?.site
  42. const [showSidePanel, setShowSidePanel] = useState(false)
  43. useEffect(() => {
  44. themeBuilder?.buildTheme(site?.chat_color_theme, site?.chat_color_theme_inverted)
  45. if (site) {
  46. if (customConfig)
  47. document.title = `${site.title}`
  48. else
  49. document.title = `${site.title} - Powered by Dify`
  50. }
  51. }, [site, customConfig, themeBuilder])
  52. if (appInfoLoading) {
  53. return (
  54. <Loading type='app' />
  55. )
  56. }
  57. if (appInfoError) {
  58. return (
  59. <AppUnavailable />
  60. )
  61. }
  62. return (
  63. <div className={cn(
  64. 'flex h-full bg-background-default-burn',
  65. isMobile && 'flex-col',
  66. className,
  67. )}>
  68. {!isMobile && (
  69. <div className={cn(
  70. 'flex w-[236px] flex-col p-1 pr-0 transition-all duration-200 ease-in-out',
  71. isSidebarCollapsed && 'w-0 overflow-hidden !p-0',
  72. )}>
  73. <Sidebar />
  74. </div>
  75. )}
  76. {isMobile && (
  77. <HeaderInMobile />
  78. )}
  79. <div className={cn('relative grow p-2', isMobile && 'h-[calc(100%_-_56px)] p-0')}>
  80. {isSidebarCollapsed && (
  81. <div
  82. className={cn(
  83. 'absolute top-0 z-20 flex h-full w-[256px] flex-col p-2 transition-all duration-500 ease-in-out',
  84. showSidePanel ? 'left-0' : 'left-[-248px]',
  85. )}
  86. onMouseEnter={() => setShowSidePanel(true)}
  87. onMouseLeave={() => setShowSidePanel(false)}
  88. >
  89. <Sidebar isPanel />
  90. </div>
  91. )}
  92. <div className={cn('flex h-full flex-col overflow-hidden border-[0,5px] border-components-panel-border-subtle bg-chatbot-bg', isMobile ? 'rounded-t-2xl' : 'rounded-2xl')}>
  93. {!isMobile && <Header />}
  94. {appChatListDataLoading && (
  95. <Loading type='app' />
  96. )}
  97. {!appChatListDataLoading && (
  98. <ChatWrapper key={chatShouldReloadKey} />
  99. )}
  100. </div>
  101. </div>
  102. </div>
  103. )
  104. }
  105. export type ChatWithHistoryWrapProps = {
  106. installedAppInfo?: InstalledApp
  107. className?: string
  108. }
  109. const ChatWithHistoryWrap: FC<ChatWithHistoryWrapProps> = ({
  110. installedAppInfo,
  111. className,
  112. }) => {
  113. const media = useBreakpoints()
  114. const isMobile = media === MediaType.mobile
  115. const themeBuilder = useThemeContext()
  116. const {
  117. appInfoError,
  118. appInfoLoading,
  119. appData,
  120. appParams,
  121. appMeta,
  122. appChatListDataLoading,
  123. currentConversationId,
  124. currentConversationItem,
  125. appPrevChatTree,
  126. pinnedConversationList,
  127. conversationList,
  128. newConversationInputs,
  129. newConversationInputsRef,
  130. handleNewConversationInputsChange,
  131. inputsForms,
  132. handleNewConversation,
  133. handleStartChat,
  134. handleChangeConversation,
  135. handlePinConversation,
  136. handleUnpinConversation,
  137. handleDeleteConversation,
  138. conversationRenaming,
  139. handleRenameConversation,
  140. handleNewConversationCompleted,
  141. chatShouldReloadKey,
  142. isInstalledApp,
  143. appId,
  144. handleFeedback,
  145. currentChatInstanceRef,
  146. sidebarCollapseState,
  147. handleSidebarCollapse,
  148. clearChatList,
  149. setClearChatList,
  150. isResponding,
  151. setIsResponding,
  152. } = useChatWithHistory(installedAppInfo)
  153. return (
  154. <ChatWithHistoryContext.Provider value={{
  155. appInfoError,
  156. appInfoLoading,
  157. appData,
  158. appParams,
  159. appMeta,
  160. appChatListDataLoading,
  161. currentConversationId,
  162. currentConversationItem,
  163. appPrevChatTree,
  164. pinnedConversationList,
  165. conversationList,
  166. newConversationInputs,
  167. newConversationInputsRef,
  168. handleNewConversationInputsChange,
  169. inputsForms,
  170. handleNewConversation,
  171. handleStartChat,
  172. handleChangeConversation,
  173. handlePinConversation,
  174. handleUnpinConversation,
  175. handleDeleteConversation,
  176. conversationRenaming,
  177. handleRenameConversation,
  178. handleNewConversationCompleted,
  179. chatShouldReloadKey,
  180. isMobile,
  181. isInstalledApp,
  182. appId,
  183. handleFeedback,
  184. currentChatInstanceRef,
  185. themeBuilder,
  186. sidebarCollapseState,
  187. handleSidebarCollapse,
  188. clearChatList,
  189. setClearChatList,
  190. isResponding,
  191. setIsResponding,
  192. }}>
  193. <ChatWithHistory className={className} />
  194. </ChatWithHistoryContext.Provider>
  195. )
  196. }
  197. const ChatWithHistoryWrapWithCheckToken: FC<ChatWithHistoryWrapProps> = ({
  198. installedAppInfo,
  199. className,
  200. }) => {
  201. const [initialized, setInitialized] = useState(false)
  202. const [appUnavailable, setAppUnavailable] = useState<boolean>(false)
  203. const [isUnknownReason, setIsUnknownReason] = useState<boolean>(false)
  204. useAsyncEffect(async () => {
  205. if (!initialized) {
  206. if (!installedAppInfo) {
  207. try {
  208. await checkOrSetAccessToken()
  209. }
  210. catch (e: any) {
  211. if (e.status === 404) {
  212. setAppUnavailable(true)
  213. }
  214. else {
  215. setIsUnknownReason(true)
  216. setAppUnavailable(true)
  217. }
  218. }
  219. }
  220. setInitialized(true)
  221. }
  222. }, [])
  223. if (!initialized)
  224. return null
  225. if (appUnavailable)
  226. return <AppUnavailable isUnknownReason={isUnknownReason} />
  227. return (
  228. <ChatWithHistoryWrap
  229. installedAppInfo={installedAppInfo}
  230. className={className}
  231. />
  232. )
  233. }
  234. export default ChatWithHistoryWrapWithCheckToken