index.tsx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import React, { useEffect, useState } from 'react'
  2. import type { FC } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import {
  5. PencilSquareIcon,
  6. } from '@heroicons/react/24/outline'
  7. import cn from 'classnames'
  8. import Button from '../../../base/button'
  9. import List from './list'
  10. import AppInfo from '@/app/components/share/chat/sidebar/app-info'
  11. // import Card from './card'
  12. import type { ConversationItem, SiteInfo } from '@/models/share'
  13. import { fetchConversations } from '@/service/share'
  14. import { fetchConversations as fetchUniversalConversations } from '@/service/universal-chat'
  15. export type ISidebarProps = {
  16. copyRight: string
  17. currentId: string
  18. onCurrentIdChange: (id: string) => void
  19. list: ConversationItem[]
  20. onListChanged: (newList: ConversationItem[]) => void
  21. isClearConversationList: boolean
  22. pinnedList: ConversationItem[]
  23. onPinnedListChanged: (newList: ConversationItem[]) => void
  24. isClearPinnedConversationList: boolean
  25. isInstalledApp: boolean
  26. installedAppId?: string
  27. isUniversalChat?: boolean
  28. siteInfo: SiteInfo
  29. onMoreLoaded: (res: { data: ConversationItem[]; has_more: boolean }) => void
  30. onPinnedMoreLoaded: (res: { data: ConversationItem[]; has_more: boolean }) => void
  31. isNoMore: boolean
  32. isPinnedNoMore: boolean
  33. onPin: (id: string) => void
  34. onUnpin: (id: string) => void
  35. controlUpdateList: number
  36. onDelete: (id: string) => void
  37. onStartChat: (inputs: Record<string, any>) => void
  38. }
  39. const Sidebar: FC<ISidebarProps> = ({
  40. copyRight,
  41. currentId,
  42. onCurrentIdChange,
  43. list,
  44. onListChanged,
  45. isClearConversationList,
  46. pinnedList,
  47. onPinnedListChanged,
  48. isClearPinnedConversationList,
  49. isInstalledApp,
  50. installedAppId,
  51. isUniversalChat,
  52. siteInfo,
  53. onMoreLoaded,
  54. onPinnedMoreLoaded,
  55. isNoMore,
  56. isPinnedNoMore,
  57. onPin,
  58. onUnpin,
  59. controlUpdateList,
  60. onDelete,
  61. onStartChat,
  62. }) => {
  63. const { t } = useTranslation()
  64. const [hasPinned, setHasPinned] = useState(false)
  65. const checkHasPinned = async () => {
  66. let res: any
  67. if (isUniversalChat)
  68. res = await fetchUniversalConversations(undefined, true)
  69. else
  70. res = await fetchConversations(isInstalledApp, installedAppId, undefined, true)
  71. setHasPinned(res.data.length > 0)
  72. }
  73. useEffect(() => {
  74. checkHasPinned()
  75. }, [])
  76. useEffect(() => {
  77. if (controlUpdateList !== 0)
  78. checkHasPinned()
  79. }, [controlUpdateList])
  80. const maxListHeight = (isInstalledApp || isUniversalChat) ? 'max-h-[30vh]' : 'max-h-[40vh]'
  81. return (
  82. <div
  83. className={
  84. cn(
  85. (isInstalledApp || isUniversalChat) ? 'tablet:h-[calc(100vh_-_74px)]' : '',
  86. 'shrink-0 flex flex-col bg-white pc:w-[244px] tablet:w-[192px] mobile:w-[240px] border-r border-gray-200 mobile:h-screen',
  87. )
  88. }
  89. >
  90. {isInstalledApp && (
  91. <AppInfo
  92. className='my-4 px-4'
  93. name={siteInfo.title || ''}
  94. icon={siteInfo.icon || ''}
  95. icon_background={siteInfo.icon_background}
  96. />
  97. )}
  98. <div className="flex flex-shrink-0 p-4 !pb-0">
  99. <Button
  100. onClick={() => onStartChat({})}
  101. className="group block w-full flex-shrink-0 !justify-start !h-9 text-primary-600 items-center text-sm">
  102. <PencilSquareIcon className="mr-2 h-4 w-4" /> {t('share.chat.newChat')}
  103. </Button>
  104. </div>
  105. <div className={'flex-grow flex flex-col h-0 overflow-y-auto overflow-x-hidden'}>
  106. {/* pinned list */}
  107. {hasPinned && (
  108. <div className={cn('mt-4 px-4', list.length === 0 && 'flex flex-col flex-grow')}>
  109. <div className='mb-1.5 leading-[18px] text-xs text-gray-500 font-medium uppercase'>{t('share.chat.pinnedTitle')}</div>
  110. <List
  111. className={cn(list.length > 0 ? maxListHeight : 'flex-grow')}
  112. currentId={currentId}
  113. onCurrentIdChange={onCurrentIdChange}
  114. list={pinnedList}
  115. onListChanged={onPinnedListChanged}
  116. isClearConversationList={isClearPinnedConversationList}
  117. isInstalledApp={isInstalledApp}
  118. installedAppId={installedAppId}
  119. isUniversalChat={isUniversalChat}
  120. onMoreLoaded={onPinnedMoreLoaded}
  121. isNoMore={isPinnedNoMore}
  122. isPinned={true}
  123. onPinChanged={id => onUnpin(id)}
  124. controlUpdate={controlUpdateList + 1}
  125. onDelete={onDelete}
  126. />
  127. </div>
  128. )}
  129. {/* unpinned list */}
  130. <div className={cn('grow flex flex-col mt-4 px-4', !hasPinned && 'flex flex-col flex-grow')}>
  131. {(hasPinned && list.length > 0) && (
  132. <div className='mb-1.5 leading-[18px] text-xs text-gray-500 font-medium uppercase'>{t('share.chat.unpinnedTitle')}</div>
  133. )}
  134. <List
  135. className={cn('flex-grow h-0')}
  136. currentId={currentId}
  137. onCurrentIdChange={onCurrentIdChange}
  138. list={list}
  139. onListChanged={onListChanged}
  140. isClearConversationList={isClearConversationList}
  141. isInstalledApp={isInstalledApp}
  142. installedAppId={installedAppId}
  143. isUniversalChat={isUniversalChat}
  144. onMoreLoaded={onMoreLoaded}
  145. isNoMore={isNoMore}
  146. isPinned={false}
  147. onPinChanged={id => onPin(id)}
  148. controlUpdate={controlUpdateList + 1}
  149. onDelete={onDelete}
  150. />
  151. </div>
  152. </div>
  153. {!isUniversalChat && (
  154. <div className="flex flex-shrink-0 pr-4 pb-4 pl-4">
  155. <div className="text-gray-400 font-normal text-xs">© {copyRight} {(new Date()).getFullYear()}</div>
  156. </div>
  157. )}
  158. </div>
  159. )
  160. }
  161. export default React.memo(Sidebar)