You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.tsx 6.3KB

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